refactor: split services into individual files
This commit is contained in:
20
src/runtime/server/services/connection.ts
Normal file
20
src/runtime/server/services/connection.ts
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
import type { ConnectOptions } from 'mongoose'
|
||||||
|
import { logger } from '@nuxt/kit'
|
||||||
|
import mongoose from 'mongoose'
|
||||||
|
|
||||||
|
// @ts-ignore
|
||||||
|
import { useRuntimeConfig } from '#imports'
|
||||||
|
|
||||||
|
export async function defineMongooseConnection({ uri, options }: { uri?: string; options?: ConnectOptions } = {}): Promise<void> {
|
||||||
|
const config = useRuntimeConfig().mongoose
|
||||||
|
const mongooseUri = uri || config.uri
|
||||||
|
const mongooseOptions = options || config.options
|
||||||
|
|
||||||
|
try {
|
||||||
|
await mongoose.connect(mongooseUri, { ...mongooseOptions })
|
||||||
|
logger.success('Connected to `MongoDB`')
|
||||||
|
}
|
||||||
|
catch (err) {
|
||||||
|
logger.error('Error connecting to `MongoDB`', err)
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -1,49 +1,2 @@
|
|||||||
import { logger } from '@nuxt/kit'
|
export { defineMongooseConnection } from './connection'
|
||||||
import mongoose from 'mongoose'
|
export { defineMongooseModel } from './model'
|
||||||
import type { ConnectOptions, Model, SchemaDefinition, SchemaOptions } from 'mongoose'
|
|
||||||
|
|
||||||
import { useRuntimeConfig } from '#imports'
|
|
||||||
|
|
||||||
export async function defineMongooseConnection({ uri, options }: { uri?: string; options?: ConnectOptions } = {}): Promise<void> {
|
|
||||||
const config = useRuntimeConfig().mongoose
|
|
||||||
const mongooseUri = uri || config.uri
|
|
||||||
const mongooseOptions = options || config.options
|
|
||||||
|
|
||||||
try {
|
|
||||||
await mongoose.connect(mongooseUri, { ...mongooseOptions })
|
|
||||||
logger.success('Connected to `MongoDB`')
|
|
||||||
}
|
|
||||||
catch (err) {
|
|
||||||
logger.error('Error connecting to `MongoDB`', err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
export function defineMongooseModel<T>(
|
|
||||||
nameOrOptions: string | {
|
|
||||||
name: string
|
|
||||||
schema: SchemaDefinition<T>
|
|
||||||
options?: SchemaOptions
|
|
||||||
hooks?: (schema: mongoose.Schema<T>) => void
|
|
||||||
},
|
|
||||||
schema?: SchemaDefinition<T>,
|
|
||||||
options?: SchemaOptions,
|
|
||||||
hooks?: (schema: mongoose.Schema<T>) => void,
|
|
||||||
): Model<T> {
|
|
||||||
let name: string
|
|
||||||
if (typeof nameOrOptions === 'string') {
|
|
||||||
name = nameOrOptions
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
name = nameOrOptions.name
|
|
||||||
schema = nameOrOptions.schema
|
|
||||||
options = nameOrOptions.options
|
|
||||||
hooks = nameOrOptions.hooks
|
|
||||||
}
|
|
||||||
|
|
||||||
const newSchema = new mongoose.Schema<T>(schema, options as any)
|
|
||||||
|
|
||||||
if (hooks)
|
|
||||||
hooks(newSchema)
|
|
||||||
|
|
||||||
return mongoose.model<T>(name, newSchema)
|
|
||||||
}
|
|
||||||
|
|||||||
32
src/runtime/server/services/model.ts
Normal file
32
src/runtime/server/services/model.ts
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
import type { Model, SchemaDefinition, SchemaOptions } from 'mongoose'
|
||||||
|
import mongoose from 'mongoose'
|
||||||
|
|
||||||
|
export function defineMongooseModel<T>(
|
||||||
|
nameOrOptions: string | {
|
||||||
|
name: string
|
||||||
|
schema: SchemaDefinition<T>
|
||||||
|
options?: SchemaOptions
|
||||||
|
hooks?: (schema: mongoose.Schema<T>) => void
|
||||||
|
},
|
||||||
|
schema?: SchemaDefinition<T>,
|
||||||
|
options?: SchemaOptions,
|
||||||
|
hooks?: (schema: mongoose.Schema<T>) => void,
|
||||||
|
): Model<T> {
|
||||||
|
let name: string
|
||||||
|
if (typeof nameOrOptions === 'string') {
|
||||||
|
name = nameOrOptions
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
name = nameOrOptions.name
|
||||||
|
schema = nameOrOptions.schema
|
||||||
|
options = nameOrOptions.options
|
||||||
|
hooks = nameOrOptions.hooks
|
||||||
|
}
|
||||||
|
|
||||||
|
const newSchema = new mongoose.Schema<T>(schema, options as any)
|
||||||
|
|
||||||
|
if (hooks)
|
||||||
|
hooks(newSchema)
|
||||||
|
|
||||||
|
return mongoose.model<T>(name, newSchema)
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user