Skip to content

Commit

Permalink
feat: auto-import schema files
Browse files Browse the repository at this point in the history
  • Loading branch information
arashsheyda committed May 31, 2023
1 parent cf48c2b commit 205435e
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 4 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,13 @@ export default defineNuxtConfig({
mongoose: {
uri: 'process.env.MONGODB_URI',
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). *

## API
Expand Down
33 changes: 29 additions & 4 deletions src/module.ts
Original file line number Diff line number Diff line change
@@ -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 { tinyws } from 'tinyws'
import { join } from 'pathe'
import { defu } from 'defu'
import sirv from 'sirv'

Expand All @@ -9,6 +17,8 @@ import type { ModuleOptions } from './types'

import { setupRPC } from './server-rpc'

export type { ModuleOptions }

export default defineNuxtModule<ModuleOptions>({
meta: {
name: 'nuxt-mongoose',
Expand All @@ -18,20 +28,25 @@ export default defineNuxtModule<ModuleOptions>({
uri: process.env.MONGODB_URI as string,
devtools: true,
options: {},
modelsDir: 'models',
},
setup(options, nuxt) {
const { resolve } = createResolver(import.meta.url)
const runtimeConfig = nuxt.options.runtimeConfig

addImportsDir(resolve('./runtime/composables'))

if (!options.uri)
console.warn('Missing `MONGODB_URI` in `.env`')
if (!options.uri) {
logger.warn('Missing `MONGODB_URI` in `.env`')
return
}

// Runtime Config
nuxt.options.runtimeConfig.mongoose = defu(nuxt.options.runtimeConfig.mongoose || {}, {
runtimeConfig.mongoose = defu(runtimeConfig.mongoose || {}, {
uri: options.uri,
options: options.options,
devtools: options.devtools,
modelsDir: options.modelsDir,
})

// Setup devtools UI
Expand Down Expand Up @@ -91,6 +106,16 @@ export default defineNuxtModule<ModuleOptions>({
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
addServerPlugin(resolve('./runtime/server/plugins/mongoose.db'))

Expand Down
1 change: 1 addition & 0 deletions src/types/module-options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ export interface ModuleOptions {
uri: string
devtools: boolean
options?: ConnectOptions
modelsDir?: string
}

0 comments on commit 205435e

Please sign in to comment.