-
-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: split services into individual files
- Loading branch information
1 parent
8bb7665
commit d6b8e91
Showing
3 changed files
with
54 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,49 +1,2 @@ | ||
import { logger } from '@nuxt/kit' | ||
import mongoose from 'mongoose' | ||
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) | ||
} | ||
export { defineMongooseConnection } from './connection' | ||
export { defineMongooseModel } from './model' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
} |