feat: auto-import schema files
This commit is contained in:
@ -36,10 +36,13 @@ export default defineNuxtConfig({
|
|||||||
mongoose: {
|
mongoose: {
|
||||||
uri: 'process.env.MONGODB_URI',
|
uri: 'process.env.MONGODB_URI',
|
||||||
options: {},
|
options: {},
|
||||||
|
modelsDir: 'models',
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
```
|
```
|
||||||
|
|
||||||
|
by default, `nuxt-mongoose` will auto-import your schemas from the `models` directory from `server` directory. You can change this behavior by setting the `modelsDir` option.
|
||||||
|
|
||||||
* for more information about the options, please refer to the [Mongoose documentation](https://mongoosejs.com/docs/connections.html#options). *
|
* for more information about the options, please refer to the [Mongoose documentation](https://mongoosejs.com/docs/connections.html#options). *
|
||||||
|
|
||||||
## API
|
## API
|
||||||
|
|||||||
@ -1,6 +1,14 @@
|
|||||||
import { addImportsDir, addServerPlugin, addTemplate, createResolver, defineNuxtModule, logger } from '@nuxt/kit'
|
import {
|
||||||
|
addImportsDir,
|
||||||
|
addServerPlugin,
|
||||||
|
addTemplate,
|
||||||
|
createResolver,
|
||||||
|
defineNuxtModule,
|
||||||
|
logger,
|
||||||
|
} from '@nuxt/kit'
|
||||||
import { pathExists } from 'fs-extra'
|
import { pathExists } from 'fs-extra'
|
||||||
import { tinyws } from 'tinyws'
|
import { tinyws } from 'tinyws'
|
||||||
|
import { join } from 'pathe'
|
||||||
import { defu } from 'defu'
|
import { defu } from 'defu'
|
||||||
import sirv from 'sirv'
|
import sirv from 'sirv'
|
||||||
|
|
||||||
@ -9,6 +17,8 @@ import type { ModuleOptions } from './types'
|
|||||||
|
|
||||||
import { setupRPC } from './server-rpc'
|
import { setupRPC } from './server-rpc'
|
||||||
|
|
||||||
|
export type { ModuleOptions }
|
||||||
|
|
||||||
export default defineNuxtModule<ModuleOptions>({
|
export default defineNuxtModule<ModuleOptions>({
|
||||||
meta: {
|
meta: {
|
||||||
name: 'nuxt-mongoose',
|
name: 'nuxt-mongoose',
|
||||||
@ -18,20 +28,25 @@ export default defineNuxtModule<ModuleOptions>({
|
|||||||
uri: process.env.MONGODB_URI as string,
|
uri: process.env.MONGODB_URI as string,
|
||||||
devtools: true,
|
devtools: true,
|
||||||
options: {},
|
options: {},
|
||||||
|
modelsDir: 'models',
|
||||||
},
|
},
|
||||||
setup(options, nuxt) {
|
setup(options, nuxt) {
|
||||||
const { resolve } = createResolver(import.meta.url)
|
const { resolve } = createResolver(import.meta.url)
|
||||||
|
const runtimeConfig = nuxt.options.runtimeConfig
|
||||||
|
|
||||||
addImportsDir(resolve('./runtime/composables'))
|
addImportsDir(resolve('./runtime/composables'))
|
||||||
|
|
||||||
if (!options.uri)
|
if (!options.uri) {
|
||||||
console.warn('Missing `MONGODB_URI` in `.env`')
|
logger.warn('Missing `MONGODB_URI` in `.env`')
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
// Runtime Config
|
// Runtime Config
|
||||||
nuxt.options.runtimeConfig.mongoose = defu(nuxt.options.runtimeConfig.mongoose || {}, {
|
runtimeConfig.mongoose = defu(runtimeConfig.mongoose || {}, {
|
||||||
uri: options.uri,
|
uri: options.uri,
|
||||||
options: options.options,
|
options: options.options,
|
||||||
devtools: options.devtools,
|
devtools: options.devtools,
|
||||||
|
modelsDir: options.modelsDir,
|
||||||
})
|
})
|
||||||
|
|
||||||
// Setup devtools UI
|
// Setup devtools UI
|
||||||
@ -91,6 +106,16 @@ export default defineNuxtModule<ModuleOptions>({
|
|||||||
options.references.push({ path: resolve(nuxt.options.buildDir, 'types/nuxt-mongoose.d.ts') })
|
options.references.push({ path: resolve(nuxt.options.buildDir, 'types/nuxt-mongoose.d.ts') })
|
||||||
})
|
})
|
||||||
|
|
||||||
|
// Nitro auto imports
|
||||||
|
nuxt.hook('nitro:config', (_nitroConfig) => {
|
||||||
|
if (_nitroConfig.imports) {
|
||||||
|
_nitroConfig.imports.dirs = _nitroConfig.imports.dirs || []
|
||||||
|
_nitroConfig.imports.dirs?.push(
|
||||||
|
join(nuxt.options.serverDir, runtimeConfig.mongoose.modelsDir),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
// Add server-plugin for database connection
|
// Add server-plugin for database connection
|
||||||
addServerPlugin(resolve('./runtime/server/plugins/mongoose.db'))
|
addServerPlugin(resolve('./runtime/server/plugins/mongoose.db'))
|
||||||
|
|
||||||
|
|||||||
@ -4,4 +4,5 @@ export interface ModuleOptions {
|
|||||||
uri: string
|
uri: string
|
||||||
devtools: boolean
|
devtools: boolean
|
||||||
options?: ConnectOptions
|
options?: ConnectOptions
|
||||||
|
modelsDir?: string
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user