Compare commits
7 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 66c08e5d52 | |||
| 774d4b6243 | |||
| 86c3c2954e | |||
| 205435e546 | |||
| cf48c2b6df | |||
| ac24680bd4 | |||
| d381058591 |
35
CHANGELOG.md
35
CHANGELOG.md
@ -1,6 +1,41 @@
|
||||
# Changelog
|
||||
|
||||
|
||||
## v0.0.7
|
||||
|
||||
[compare changes](https://github.com/arashsheyda/nuxt-mongoose/compare/v0.0.6...v0.0.7)
|
||||
|
||||
|
||||
### 🚀 Enhancements
|
||||
|
||||
- Auto-import schema files ([205435e](https://github.com/arashsheyda/nuxt-mongoose/commit/205435e))
|
||||
|
||||
### 🩹 Fixes
|
||||
|
||||
- Mongoose config ([cf48c2b](https://github.com/arashsheyda/nuxt-mongoose/commit/cf48c2b))
|
||||
|
||||
### 🏡 Chore
|
||||
|
||||
- Improve logger message ([86c3c29](https://github.com/arashsheyda/nuxt-mongoose/commit/86c3c29))
|
||||
- Update dependencies ([774d4b6](https://github.com/arashsheyda/nuxt-mongoose/commit/774d4b6))
|
||||
|
||||
### ❤️ Contributors
|
||||
|
||||
- Arashsheyda <sheidaeearash1999@gmail.com>
|
||||
|
||||
## v0.0.6
|
||||
|
||||
[compare changes](https://github.com/arashsheyda/nuxt-mongoose/compare/v0.0.5...v0.0.6)
|
||||
|
||||
|
||||
### 🩹 Fixes
|
||||
|
||||
- Remove useMongoose composable ([d381058](https://github.com/arashsheyda/nuxt-mongoose/commit/d381058))
|
||||
|
||||
### ❤️ Contributors
|
||||
|
||||
- Arashsheyda <sheidaeearash1999@gmail.com>
|
||||
|
||||
## v0.0.5
|
||||
|
||||
[compare changes](https://github.com/arashsheyda/nuxt-mongoose/compare/v0.0.4...v0.0.5)
|
||||
|
||||
14
README.md
14
README.md
@ -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
|
||||
@ -83,17 +86,6 @@ export const User = defineMongooseModel({
|
||||
})
|
||||
```
|
||||
|
||||
### useMongoose
|
||||
|
||||
This composable returns the Mongoose DB instance. Example usage:
|
||||
|
||||
```vue
|
||||
<script lang="ts" setup>
|
||||
const mongoose = useMongoose()
|
||||
const user = await mongoose.db.collection('users').findOne()
|
||||
</script>
|
||||
```
|
||||
|
||||
## License
|
||||
|
||||
[MIT License](./LICENSE)
|
||||
|
||||
25
package.json
25
package.json
@ -1,7 +1,7 @@
|
||||
{
|
||||
"name": "nuxt-mongoose",
|
||||
"type": "module",
|
||||
"version": "0.0.5",
|
||||
"version": "0.0.7",
|
||||
"description": "Nuxt 3 module for MongoDB with Mongoose",
|
||||
"license": "MIT",
|
||||
"repository": {
|
||||
@ -38,14 +38,15 @@
|
||||
"test:watch": "vitest watch"
|
||||
},
|
||||
"dependencies": {
|
||||
"@nuxt/devtools-kit": "^0.4.2",
|
||||
"@nuxt/kit": "^3.4.2",
|
||||
"@nuxt/devtools-kit": "^0.5.5",
|
||||
"@nuxt/kit": "^3.5.2",
|
||||
"@types/fs-extra": "^11.0.1",
|
||||
"birpc": "^0.2.11",
|
||||
"defu": "^6.1.2",
|
||||
"flatted": "^3.2.7",
|
||||
"fs-extra": "^11.1.1",
|
||||
"mongoose": "^7.0.5",
|
||||
"mongoose": "^7.2.2",
|
||||
"pathe": "^1.1.0",
|
||||
"pluralize": "^8.0.0",
|
||||
"sirv": "^2.0.3",
|
||||
"tinyws": "^0.1.0",
|
||||
@ -53,19 +54,19 @@
|
||||
},
|
||||
"devDependencies": {
|
||||
"@antfu/eslint-config": "^0.38.5",
|
||||
"@nuxt/devtools": "^0.4.2",
|
||||
"@nuxt/devtools-ui-kit": "^0.4.2",
|
||||
"@nuxt/module-builder": "^0.3.0",
|
||||
"@nuxt/schema": "^3.4.2",
|
||||
"@nuxt/test-utils": "^3.4.2",
|
||||
"@nuxt/devtools": "^0.5.5",
|
||||
"@nuxt/devtools-ui-kit": "^0.5.5",
|
||||
"@nuxt/module-builder": "^0.4.0",
|
||||
"@nuxt/schema": "^3.5.2",
|
||||
"@nuxt/test-utils": "^3.5.2",
|
||||
"@types/pluralize": "^0.0.29",
|
||||
"@types/ws": "^8.5.4",
|
||||
"changelogen": "^0.5.3",
|
||||
"eslint": "^8.39.0",
|
||||
"nuxt": "^3.4.2",
|
||||
"nuxt": "^3.5.2",
|
||||
"sass": "^1.62.1",
|
||||
"sass-loader": "^13.2.2",
|
||||
"sass-loader": "^13.3.1",
|
||||
"splitpanes": "^3.1.5",
|
||||
"vitest": "^0.30.1"
|
||||
"vitest": "^0.31.2"
|
||||
}
|
||||
}
|
||||
5145
pnpm-lock.yaml
generated
5145
pnpm-lock.yaml
generated
File diff suppressed because it is too large
Load Diff
@ -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'
|
||||
|
||||
@ -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',
|
||||
@ -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
|
||||
}
|
||||
|
||||
// Public runtimeConfig
|
||||
nuxt.options.runtimeConfig.public.mongoose = defu(nuxt.options.runtimeConfig.public.mongoose || {}, {
|
||||
// Runtime Config
|
||||
runtimeConfig.mongoose = defu(runtimeConfig.mongoose || {}, {
|
||||
uri: options.uri,
|
||||
options: options.options,
|
||||
devtools: options.devtools,
|
||||
modelsDir: options.modelsDir,
|
||||
})
|
||||
|
||||
// Setup devtools UI
|
||||
@ -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'))
|
||||
|
||||
|
||||
@ -1,8 +0,0 @@
|
||||
import type { mongo } from 'mongoose'
|
||||
import { connection } from 'mongoose'
|
||||
|
||||
export function useMongoose(): { db: mongo.Db } {
|
||||
return {
|
||||
db: connection?.db,
|
||||
}
|
||||
}
|
||||
@ -5,13 +5,13 @@ import { logger } from '@nuxt/kit'
|
||||
import { useRuntimeConfig } from '#imports'
|
||||
|
||||
export async function defineMongooseConnection({ uri, options }: { uri?: string; options?: ConnectOptions } = {}): Promise<void> {
|
||||
const config = useRuntimeConfig().public.mongoose
|
||||
const config = useRuntimeConfig().mongoose
|
||||
const mongooseUri = uri || config.uri
|
||||
const mongooseOptions = options || config.options
|
||||
|
||||
try {
|
||||
await mongoose.connect(mongooseUri, { ...mongooseOptions })
|
||||
logger.info('Connected to database')
|
||||
logger.info('Connected to mongoose database')
|
||||
}
|
||||
catch (err) {
|
||||
logger.error('Error connecting to database', err)
|
||||
|
||||
@ -4,4 +4,5 @@ export interface ModuleOptions {
|
||||
uri: string
|
||||
devtools: boolean
|
||||
options?: ConnectOptions
|
||||
modelsDir?: string
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user