initial commit
This commit is contained in:
12
.editorconfig
Normal file
12
.editorconfig
Normal file
@ -0,0 +1,12 @@
|
||||
root = true
|
||||
|
||||
[*]
|
||||
indent_size = 2
|
||||
indent_style = space
|
||||
end_of_line = lf
|
||||
charset = utf-8
|
||||
trim_trailing_whitespace = true
|
||||
insert_final_newline = true
|
||||
|
||||
[*.md]
|
||||
trim_trailing_whitespace = false
|
||||
2
.eslintignore
Normal file
2
.eslintignore
Normal file
@ -0,0 +1,2 @@
|
||||
dist
|
||||
node_modules
|
||||
4
.eslintrc
Normal file
4
.eslintrc
Normal file
@ -0,0 +1,4 @@
|
||||
{
|
||||
"root": true,
|
||||
"extends": ["@antfu/eslint-config"]
|
||||
}
|
||||
56
.gitignore
vendored
Normal file
56
.gitignore
vendored
Normal file
@ -0,0 +1,56 @@
|
||||
# Dependencies
|
||||
node_modules
|
||||
|
||||
# Logs
|
||||
*.log*
|
||||
|
||||
# Temp directories
|
||||
.temp
|
||||
.tmp
|
||||
.cache
|
||||
|
||||
# Yarn
|
||||
**/.yarn/cache
|
||||
**/.yarn/*state*
|
||||
|
||||
# Generated dirs
|
||||
dist
|
||||
|
||||
# Nuxt
|
||||
.nuxt
|
||||
.output
|
||||
.vercel_build_output
|
||||
.build-*
|
||||
.env
|
||||
.netlify
|
||||
|
||||
# Env
|
||||
.env
|
||||
|
||||
# Testing
|
||||
reports
|
||||
coverage
|
||||
*.lcov
|
||||
.nyc_output
|
||||
|
||||
# VSCode
|
||||
.vscode/*
|
||||
!.vscode/settings.json
|
||||
!.vscode/tasks.json
|
||||
!.vscode/launch.json
|
||||
!.vscode/extensions.json
|
||||
!.vscode/*.code-snippets
|
||||
|
||||
# Intellij idea
|
||||
*.iml
|
||||
.idea
|
||||
|
||||
# OSX
|
||||
.DS_Store
|
||||
.AppleDouble
|
||||
.LSOverride
|
||||
.AppleDB
|
||||
.AppleDesktop
|
||||
Network Trash Folder
|
||||
Temporary Items
|
||||
.apdisk
|
||||
2
.nuxtrc
Normal file
2
.nuxtrc
Normal file
@ -0,0 +1,2 @@
|
||||
imports.autoImport=false
|
||||
typescript.includeWorkspace=true
|
||||
16
.vscode/settings.json
vendored
Normal file
16
.vscode/settings.json
vendored
Normal file
@ -0,0 +1,16 @@
|
||||
{
|
||||
"editor.tabSize": 2,
|
||||
"files.exclude": {
|
||||
"**/.git": true,
|
||||
"**/.svn": true,
|
||||
"**/.hg": true,
|
||||
"**/CVS": true,
|
||||
"**/.DS_Store": true,
|
||||
"**/Thumbs.db": true,
|
||||
"**/node_modules": true,
|
||||
"**/.nuxt": true,
|
||||
"**/.output": true,
|
||||
"**/dist": true,
|
||||
"**/.nuxtrc": true
|
||||
}
|
||||
}
|
||||
88
README.md
Normal file
88
README.md
Normal file
@ -0,0 +1,88 @@
|
||||

|
||||
|
||||
<div align="center">
|
||||
<h1>Nuxt Mongoose</h1>
|
||||
|
||||
A Nuxt module for simplifying the use of Mongoose in your project.
|
||||
</div>
|
||||
|
||||
|
||||
## Installation
|
||||
|
||||
```bash
|
||||
pnpm add nuxt-mongoose mongoose
|
||||
```
|
||||
|
||||
## Usage
|
||||
|
||||
### Setup
|
||||
|
||||
Add `nuxt-mongoose` to the `modules` section of your `nuxt.config.ts` file.
|
||||
|
||||
```ts
|
||||
export default defineNuxtConfig({
|
||||
modules: [
|
||||
'nuxt-mongoose',
|
||||
],
|
||||
})
|
||||
```
|
||||
|
||||
### Configuration
|
||||
|
||||
You can configure the module by adding a `mongoose` section to your `nuxt.config` file.
|
||||
|
||||
```ts
|
||||
export default defineNuxtConfig({
|
||||
mongoose: {
|
||||
uri: 'process.env.MONGODB_URI',
|
||||
options: {},
|
||||
},
|
||||
})
|
||||
```
|
||||
|
||||
* for more information about the options, please refer to the [Mongoose documentation](https://mongoosejs.com/docs/connections.html#options). *
|
||||
|
||||
## API
|
||||
|
||||
### defineMongooseConnection
|
||||
|
||||
This function creates a new Mongoose connection. Example usage:
|
||||
|
||||
```ts
|
||||
import { defineMongooseConnection } from '#nuxt/mongoose'
|
||||
|
||||
export const connection = defineMongooseConnection('mongodb://127.0.0.1/nuxt-mongoose')
|
||||
```
|
||||
|
||||
### defineMongooseModel
|
||||
|
||||
This function creates a new Mongoose model with schema. Example usage:
|
||||
|
||||
```ts
|
||||
import { defineMongooseModel } from '#nuxt/mongoose'
|
||||
|
||||
export const User = defineMongooseModel('User', {
|
||||
name: {
|
||||
type: String,
|
||||
required: true,
|
||||
},
|
||||
})
|
||||
```
|
||||
|
||||
**or you could use it like:**
|
||||
|
||||
```ts
|
||||
export const User = defineMongooseModel({
|
||||
name: 'User',
|
||||
schema: {
|
||||
name: {
|
||||
type: String,
|
||||
required: true,
|
||||
},
|
||||
},
|
||||
})
|
||||
```
|
||||
|
||||
## License
|
||||
|
||||
[MIT License](./LICENSE)
|
||||
26
docs/public/nuxt-mongoose.svg
Normal file
26
docs/public/nuxt-mongoose.svg
Normal file
@ -0,0 +1,26 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<!-- Generator: Adobe Illustrator 25.0.0, SVG Export Plug-In . SVG Version: 6.00 Build 0) -->
|
||||
<svg version="1.1" id="Layer_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px"
|
||||
viewBox="0 0 914.6 296.6" style="enable-background:new 0 0 914.6 296.6;" xml:space="preserve">
|
||||
<style type="text/css">
|
||||
.st0{fill:#880000;}
|
||||
.st1{fill:#00DC82;}
|
||||
</style>
|
||||
<path class="st0" d="M620,225.4c-47.8-6.5-96.2,6.6-134.1,36.4c-9.7-24.4-21.2-48-34.1-70.8c-10.8-19.1-18.1-29.1-28.6-29.9
|
||||
c-10.5-0.8-21.3,9.9-25.3,15c-2.4-6.3-11.1-27-25.7-28.6c-14.6-1.6-26.7,16.2-41.7,38c-12.2,17.8-22,37.1-29.3,57.4
|
||||
c-4.9-14.1-18.2-46.7-51.4-69.3c-42.6-28.9-100.8-29.7-151.2-3.2c82.8-70,172.3-122.4,284.9-120c10.8,56.5,46.3,100.5,79.8,105.9
|
||||
c-14.4-13.7-33.3-29-48.6-47.3c-13.2-15.6-21.3-34.8-21-56.6c0.2-12.6,5.8-19.8,17.5-18.2c23.3,3.5,46.3,8.9,69.3,13.6
|
||||
c2.5,0.5,4.9,1.7,8.4,0.5l-23.2-11.5c0.4-1.4,0.7-2.8,1.1-4.2c114.6-5.8,221.8,26,328.4,65.5c-1.4,7.4-2.5,12.9-3.6,18.4l1.5,0.6
|
||||
l7.1-14c16.3,2.2,18.3,6.4,14.1,20.7c-7.4,25.7-24.1,39.2-50,44.1c-41.3,7.9-82.2,17.2-123.5,25.3
|
||||
C614.3,198.5,613.4,199.4,620,225.4z M481.1,94.6c-16.8-20-51.7-37.3-67.5-34.9c-2.3,22.4,34.4,68.2,57.8,71.7
|
||||
c-2.7-4.4-5.3-8.4-7.8-12.6l6.4-3.5c-4.6-6-8.8-11.4-15.2-19.8l18.7,7.9l-6.6-13.7L481.1,94.6z M672,98.1l0.2-4.8l-90.4-17.4
|
||||
l-1.3,4.5c14.2,7,27.8,16.7,42.8,20.2C639.4,104.2,656.2,103.4,672,98.1L672,98.1z M659.4,162.1l-1.6-4.3
|
||||
c-20.4,8.3-41.2,15.8-61,25.3c-11.1,5.3-10.9,14-3,22.5C601.6,170.5,636.4,174.7,659.4,162.1L659.4,162.1z"/>
|
||||
<path class="st1" d="M399.5,264.6H459c1.9,0,3.8-0.5,5.4-1.4c3.3-1.9,5.3-5.4,5.3-9.3c0-1.9-0.5-3.7-1.4-5.3l-40-68.7
|
||||
c-0.9-1.6-2.3-2.9-3.9-3.8c-3.4-2-7.5-2-10.8,0c-1.6,0.9-3,2.3-3.9,3.8l-10.2,17.6l-20-34.4c-1-1.6-2.4-2.9-4-3.8
|
||||
c-3.3-2-7.5-2-10.7,0c-1.6,0.9-3,2.3-3.9,3.8L311,248.6c-0.9,1.6-1.5,3.5-1.5,5.3c0.1,3.8,2.1,7.3,5.4,9.3c1.6,0.9,3.5,1.4,5.4,1.4
|
||||
h37.4c14.7,0,25.7-6.4,33.3-19l18.2-31.4l9.8-16.7l29.3,50.4h-39.1L399.5,264.6z M357.1,247.8H331l39.1-67.1l19.5,33.6l-13.1,22.4
|
||||
C371.6,244.9,365.9,247.8,357.1,247.8z"/>
|
||||
<path class="st1" d="M672,98.1l0.2-4.8l-90.4-17.4l-1.3,4.5c14.2,7,27.8,16.7,42.8,20.2C639.4,104.2,656.2,103.4,672,98.1L672,98.1z
|
||||
"/>
|
||||
</svg>
|
||||
|
After Width: | Height: | Size: 2.2 KiB |
52
package.json
Normal file
52
package.json
Normal file
@ -0,0 +1,52 @@
|
||||
{
|
||||
"name": "nuxt-mongoose",
|
||||
"type": "module",
|
||||
"version": "0.0.0",
|
||||
"description": "Nuxt 3 module for MongoDB with Mongoose",
|
||||
"license": "MIT",
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "https://github.com/arashsheyda/nuxt-mongoose"
|
||||
},
|
||||
"exports": {
|
||||
".": {
|
||||
"types": "./dist/types.d.ts",
|
||||
"require": "./dist/module.cjs",
|
||||
"import": "./dist/module.mjs"
|
||||
}
|
||||
},
|
||||
"main": "./dist/module.cjs",
|
||||
"types": "./dist/types.d.ts",
|
||||
"files": [
|
||||
"dist"
|
||||
],
|
||||
"scripts": {
|
||||
"prepack": "nuxt-module-build",
|
||||
"dev": "nuxi dev playground",
|
||||
"dev:build": "nuxi build playground",
|
||||
"dev:prepare": "nuxt-module-build --stub && nuxi prepare playground",
|
||||
"release": "npm run lint && npm run test && npm run prepack && changelogen --release && npm publish && git push --follow-tags",
|
||||
"lint": "eslint .",
|
||||
"test": "vitest run",
|
||||
"test:watch": "vitest watch"
|
||||
},
|
||||
"peerDependencies": {
|
||||
"mongoose": "^7.0.3",
|
||||
"nuxt": "^3.4.1"
|
||||
},
|
||||
"dependencies": {
|
||||
"@nuxt/kit": "^3.4.1",
|
||||
"defu": "^6.1.2",
|
||||
"mongoose": "^7.0.3"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@antfu/eslint-config": "^0.38.4",
|
||||
"@nuxt/module-builder": "^0.3.0",
|
||||
"@nuxt/schema": "^3.4.1",
|
||||
"@nuxt/test-utils": "^3.4.1",
|
||||
"changelogen": "^0.5.3",
|
||||
"eslint": "^8.38.0",
|
||||
"nuxt": "^3.4.1",
|
||||
"vitest": "^0.30.1"
|
||||
}
|
||||
}
|
||||
8
playground/app.vue
Normal file
8
playground/app.vue
Normal file
@ -0,0 +1,8 @@
|
||||
<script setup>
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<div>
|
||||
Nuxt module playground!
|
||||
</div>
|
||||
</template>
|
||||
7
playground/nuxt.config.ts
Normal file
7
playground/nuxt.config.ts
Normal file
@ -0,0 +1,7 @@
|
||||
export default defineNuxtConfig({
|
||||
modules: ['../src/module'],
|
||||
|
||||
mongoose: {
|
||||
uri: 'mongodb://127.0.0.1/nuxt-mongoose',
|
||||
},
|
||||
})
|
||||
4
playground/package.json
Normal file
4
playground/package.json
Normal file
@ -0,0 +1,4 @@
|
||||
{
|
||||
"name": "nuxt-mongoose-playground",
|
||||
"private": true
|
||||
}
|
||||
5
playground/server/api/users.get.ts
Normal file
5
playground/server/api/users.get.ts
Normal file
@ -0,0 +1,5 @@
|
||||
import { User } from '~/server/models/user.schema'
|
||||
|
||||
export default defineEventHandler(() => {
|
||||
return User.find()
|
||||
})
|
||||
18
playground/server/models/user.schema.ts
Normal file
18
playground/server/models/user.schema.ts
Normal file
@ -0,0 +1,18 @@
|
||||
import { defineMongooseModel } from '#nuxt/mongoose'
|
||||
|
||||
export const User = defineMongooseModel({
|
||||
name: 'User',
|
||||
schema: {
|
||||
name: {
|
||||
type: String,
|
||||
required: true,
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
// export const User = defineMongooseModel('User', {
|
||||
// name: {
|
||||
// type: String,
|
||||
// required: true,
|
||||
// },
|
||||
// })
|
||||
7026
pnpm-lock.yaml
generated
Normal file
7026
pnpm-lock.yaml
generated
Normal file
File diff suppressed because it is too large
Load Diff
59
src/module.ts
Normal file
59
src/module.ts
Normal file
@ -0,0 +1,59 @@
|
||||
import { addServerPlugin, addTemplate, createResolver, defineNuxtModule } from '@nuxt/kit'
|
||||
import { defu } from 'defu'
|
||||
import type { ConnectOptions } from 'mongoose'
|
||||
|
||||
export interface ModuleOptions {
|
||||
uri?: string
|
||||
options?: ConnectOptions
|
||||
}
|
||||
|
||||
export default defineNuxtModule<ModuleOptions>({
|
||||
meta: {
|
||||
name: 'nuxt-mongoose',
|
||||
configKey: 'mongoose',
|
||||
},
|
||||
defaults: {
|
||||
uri: process.env.MONGODB_URI as string,
|
||||
options: {},
|
||||
},
|
||||
setup(options, nuxt) {
|
||||
const { resolve } = createResolver(import.meta.url)
|
||||
|
||||
if (!options.uri)
|
||||
console.warn('Missing `MONGODB_URI` in `.env`')
|
||||
|
||||
// Public runtimeConfig
|
||||
nuxt.options.runtimeConfig.public.mongoose = defu(nuxt.options.runtimeConfig.public.mongoose || {}, {
|
||||
uri: options.uri,
|
||||
options: options.options,
|
||||
})
|
||||
|
||||
// virtual imports
|
||||
nuxt.hook('nitro:config', (nitroConfig) => {
|
||||
nitroConfig.alias = nitroConfig.alias || {}
|
||||
|
||||
// Inline module runtime in Nitro bundle
|
||||
nitroConfig.externals = defu(typeof nitroConfig.externals === 'object' ? nitroConfig.externals : {}, {
|
||||
inline: [resolve('./runtime')],
|
||||
})
|
||||
nitroConfig.alias['#nuxt/mongoose'] = resolve('./runtime/server/services')
|
||||
})
|
||||
|
||||
addTemplate({
|
||||
filename: 'types/nuxt-mongoose.d.ts',
|
||||
getContents: () => [
|
||||
'declare module \'#nuxt/mongoose\' {',
|
||||
` const defineMongooseConnection: typeof import('${resolve('./runtime/server/services')}').defineMongooseConnection`,
|
||||
` const defineMongooseModel: typeof import('${resolve('./runtime/server/services')}').defineMongooseModel`,
|
||||
'}',
|
||||
].join('\n'),
|
||||
})
|
||||
|
||||
nuxt.hook('prepare:types', (options) => {
|
||||
options.references.push({ path: resolve(nuxt.options.buildDir, 'types/nuxt-mongoose.d.ts') })
|
||||
})
|
||||
|
||||
// Add server-plugin for database connection
|
||||
addServerPlugin(resolve('./runtime/server/plugins/mongoose.db'))
|
||||
},
|
||||
})
|
||||
15
src/runtime/server/plugins/mongoose.db.ts
Normal file
15
src/runtime/server/plugins/mongoose.db.ts
Normal file
@ -0,0 +1,15 @@
|
||||
/**
|
||||
* Due to an upstream bug in Nuxt 3 we need to stub the plugin here, track:https://github.com/nuxt/nuxt/issues/18556
|
||||
* */
|
||||
import type { NitroApp } from 'nitropack'
|
||||
import { defineMongooseConnection } from '../services/mongoose'
|
||||
|
||||
type NitroAppPlugin = (nitro: NitroApp) => void
|
||||
|
||||
function defineNitroPlugin(def: NitroAppPlugin): NitroAppPlugin {
|
||||
return def
|
||||
}
|
||||
|
||||
export default defineNitroPlugin(() => {
|
||||
defineMongooseConnection()
|
||||
})
|
||||
1
src/runtime/server/services/index.ts
Normal file
1
src/runtime/server/services/index.ts
Normal file
@ -0,0 +1 @@
|
||||
export { defineMongooseConnection, defineMongooseModel } from './mongoose'
|
||||
36
src/runtime/server/services/mongoose.ts
Normal file
36
src/runtime/server/services/mongoose.ts
Normal file
@ -0,0 +1,36 @@
|
||||
import type { ConnectOptions, Model, SchemaDefinition, SchemaOptions } from 'mongoose'
|
||||
import { Schema, connect, model } from 'mongoose'
|
||||
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 mongooseUri = uri || config.uri
|
||||
const mongooseOptions = options || config.options
|
||||
|
||||
try {
|
||||
await connect(mongooseUri, { ...mongooseOptions })
|
||||
logger.info('Connected to database')
|
||||
}
|
||||
catch (err) {
|
||||
logger.error('Error connecting to database', err)
|
||||
}
|
||||
}
|
||||
|
||||
export function defineMongooseModel(nameOrOptions: string | { name: string; schema: SchemaDefinition; options?: SchemaOptions }, schema?: SchemaDefinition, options?: SchemaOptions): Model<any> {
|
||||
let name: string
|
||||
if (typeof nameOrOptions === 'string') {
|
||||
name = nameOrOptions
|
||||
}
|
||||
else {
|
||||
name = nameOrOptions.name
|
||||
schema = nameOrOptions.schema
|
||||
options = nameOrOptions.options
|
||||
}
|
||||
|
||||
const newSchema = new Schema({
|
||||
...schema,
|
||||
}, { ...options })
|
||||
return model(name, newSchema)
|
||||
}
|
||||
3
tsconfig.json
Normal file
3
tsconfig.json
Normal file
@ -0,0 +1,3 @@
|
||||
{
|
||||
"extends": "./playground/.nuxt/tsconfig.json"
|
||||
}
|
||||
Reference in New Issue
Block a user