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
|
# 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
|
## v0.0.5
|
||||||
|
|
||||||
[compare changes](https://github.com/arashsheyda/nuxt-mongoose/compare/v0.0.4...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: {
|
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
|
||||||
@ -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
|
## License
|
||||||
|
|
||||||
[MIT License](./LICENSE)
|
[MIT License](./LICENSE)
|
||||||
|
|||||||
25
package.json
25
package.json
@ -1,7 +1,7 @@
|
|||||||
{
|
{
|
||||||
"name": "nuxt-mongoose",
|
"name": "nuxt-mongoose",
|
||||||
"type": "module",
|
"type": "module",
|
||||||
"version": "0.0.5",
|
"version": "0.0.7",
|
||||||
"description": "Nuxt 3 module for MongoDB with Mongoose",
|
"description": "Nuxt 3 module for MongoDB with Mongoose",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"repository": {
|
"repository": {
|
||||||
@ -38,14 +38,15 @@
|
|||||||
"test:watch": "vitest watch"
|
"test:watch": "vitest watch"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@nuxt/devtools-kit": "^0.4.2",
|
"@nuxt/devtools-kit": "^0.5.5",
|
||||||
"@nuxt/kit": "^3.4.2",
|
"@nuxt/kit": "^3.5.2",
|
||||||
"@types/fs-extra": "^11.0.1",
|
"@types/fs-extra": "^11.0.1",
|
||||||
"birpc": "^0.2.11",
|
"birpc": "^0.2.11",
|
||||||
"defu": "^6.1.2",
|
"defu": "^6.1.2",
|
||||||
"flatted": "^3.2.7",
|
"flatted": "^3.2.7",
|
||||||
"fs-extra": "^11.1.1",
|
"fs-extra": "^11.1.1",
|
||||||
"mongoose": "^7.0.5",
|
"mongoose": "^7.2.2",
|
||||||
|
"pathe": "^1.1.0",
|
||||||
"pluralize": "^8.0.0",
|
"pluralize": "^8.0.0",
|
||||||
"sirv": "^2.0.3",
|
"sirv": "^2.0.3",
|
||||||
"tinyws": "^0.1.0",
|
"tinyws": "^0.1.0",
|
||||||
@ -53,19 +54,19 @@
|
|||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@antfu/eslint-config": "^0.38.5",
|
"@antfu/eslint-config": "^0.38.5",
|
||||||
"@nuxt/devtools": "^0.4.2",
|
"@nuxt/devtools": "^0.5.5",
|
||||||
"@nuxt/devtools-ui-kit": "^0.4.2",
|
"@nuxt/devtools-ui-kit": "^0.5.5",
|
||||||
"@nuxt/module-builder": "^0.3.0",
|
"@nuxt/module-builder": "^0.4.0",
|
||||||
"@nuxt/schema": "^3.4.2",
|
"@nuxt/schema": "^3.5.2",
|
||||||
"@nuxt/test-utils": "^3.4.2",
|
"@nuxt/test-utils": "^3.5.2",
|
||||||
"@types/pluralize": "^0.0.29",
|
"@types/pluralize": "^0.0.29",
|
||||||
"@types/ws": "^8.5.4",
|
"@types/ws": "^8.5.4",
|
||||||
"changelogen": "^0.5.3",
|
"changelogen": "^0.5.3",
|
||||||
"eslint": "^8.39.0",
|
"eslint": "^8.39.0",
|
||||||
"nuxt": "^3.4.2",
|
"nuxt": "^3.5.2",
|
||||||
"sass": "^1.62.1",
|
"sass": "^1.62.1",
|
||||||
"sass-loader": "^13.2.2",
|
"sass-loader": "^13.3.1",
|
||||||
"splitpanes": "^3.1.5",
|
"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 { 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
|
||||||
|
}
|
||||||
|
|
||||||
// Public runtimeConfig
|
// Runtime Config
|
||||||
nuxt.options.runtimeConfig.public.mongoose = defu(nuxt.options.runtimeConfig.public.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'))
|
||||||
|
|
||||||
|
|||||||
@ -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'
|
import { useRuntimeConfig } from '#imports'
|
||||||
|
|
||||||
export async function defineMongooseConnection({ uri, options }: { uri?: string; options?: ConnectOptions } = {}): Promise<void> {
|
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 mongooseUri = uri || config.uri
|
||||||
const mongooseOptions = options || config.options
|
const mongooseOptions = options || config.options
|
||||||
|
|
||||||
try {
|
try {
|
||||||
await mongoose.connect(mongooseUri, { ...mongooseOptions })
|
await mongoose.connect(mongooseUri, { ...mongooseOptions })
|
||||||
logger.info('Connected to database')
|
logger.info('Connected to mongoose database')
|
||||||
}
|
}
|
||||||
catch (err) {
|
catch (err) {
|
||||||
logger.error('Error connecting to database', err)
|
logger.error('Error connecting to database', err)
|
||||||
|
|||||||
@ -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