Compare commits
14 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 0dfad07399 | |||
| e6b8b7d0fa | |||
| 0cdba0d764 | |||
| 9c0e0baf95 | |||
| 925d382f2f | |||
| 3ff97569f5 | |||
| 52a5ae9180 | |||
| 4647fbda16 | |||
| 647d26db85 | |||
| 40b8ca91ee | |||
| d92a58b2ad | |||
| 5a43ebe06f | |||
| 56259adaf7 | |||
| 470f272183 |
@ -1,2 +1,5 @@
|
|||||||
dist
|
dist
|
||||||
node_modules
|
node_modules
|
||||||
|
docs
|
||||||
|
.github
|
||||||
|
.vscode
|
||||||
|
|||||||
35
CHANGELOG.md
35
CHANGELOG.md
@ -1,6 +1,41 @@
|
|||||||
# Changelog
|
# Changelog
|
||||||
|
|
||||||
|
|
||||||
|
## v0.0.9
|
||||||
|
|
||||||
|
[compare changes](https://github.com/arashsheyda/nuxt-mongoose/compare/v0.0.8...v0.0.9)
|
||||||
|
|
||||||
|
### 🚀 Enhancements
|
||||||
|
|
||||||
|
- Add possibility to type mongoose model ([#8](https://github.com/arashsheyda/nuxt-mongoose/pull/8))
|
||||||
|
- Mongoose schema hooks ([d92a58b](https://github.com/arashsheyda/nuxt-mongoose/commit/d92a58b))
|
||||||
|
|
||||||
|
### 🩹 Fixes
|
||||||
|
|
||||||
|
- Update connection message in logger ([#10](https://github.com/arashsheyda/nuxt-mongoose/pull/10))
|
||||||
|
|
||||||
|
### 📖 Documentation
|
||||||
|
|
||||||
|
- **utils:** Add hooks and improve documentation ([647d26d](https://github.com/arashsheyda/nuxt-mongoose/commit/647d26d))
|
||||||
|
- **utils:** Fix model options type ([3ff9756](https://github.com/arashsheyda/nuxt-mongoose/commit/3ff9756))
|
||||||
|
- Add types link ([9c0e0ba](https://github.com/arashsheyda/nuxt-mongoose/commit/9c0e0ba))
|
||||||
|
|
||||||
|
### 🏡 Chore
|
||||||
|
|
||||||
|
- Fix defineNitroPlugin patch ([#9](https://github.com/arashsheyda/nuxt-mongoose/pull/9))
|
||||||
|
- Example env ([40b8ca9](https://github.com/arashsheyda/nuxt-mongoose/commit/40b8ca9))
|
||||||
|
- Fix defineNitroPlugin patch " ([#9](https://github.com/arashsheyda/nuxt-mongoose/pull/9))
|
||||||
|
- Update dependencies ([925d382](https://github.com/arashsheyda/nuxt-mongoose/commit/925d382))
|
||||||
|
- Lint ([0cdba0d](https://github.com/arashsheyda/nuxt-mongoose/commit/0cdba0d))
|
||||||
|
- Fix tsconfig for build time ([e6b8b7d](https://github.com/arashsheyda/nuxt-mongoose/commit/e6b8b7d))
|
||||||
|
|
||||||
|
### ❤️ Contributors
|
||||||
|
|
||||||
|
- Arash Sheyda <sheidaeearash1999@gmail.com>
|
||||||
|
- Arash
|
||||||
|
- Amir-al-mohamad111
|
||||||
|
- Oumar Barry ([@oumarbarry](http://github.com/oumarbarry))
|
||||||
|
|
||||||
## v0.0.8
|
## v0.0.8
|
||||||
|
|
||||||
[compare changes](https://github.com/arashsheyda/nuxt-mongoose/compare/v0.0.7...v0.0.8)
|
[compare changes](https://github.com/arashsheyda/nuxt-mongoose/compare/v0.0.7...v0.0.8)
|
||||||
|
|||||||
@ -9,6 +9,7 @@ export const wsError = ref<any>()
|
|||||||
export const wsConnectingDebounced = useDebounce(wsConnecting, 2000)
|
export const wsConnectingDebounced = useDebounce(wsConnecting, 2000)
|
||||||
|
|
||||||
const connectPromise = connectVite()
|
const connectPromise = connectVite()
|
||||||
|
// eslint-disable-next-line @typescript-eslint/ban-types
|
||||||
let onMessage: Function = () => {}
|
let onMessage: Function = () => {}
|
||||||
|
|
||||||
export const clientFunctions = {
|
export const clientFunctions = {
|
||||||
|
|||||||
@ -12,12 +12,16 @@ This function creates a new Mongoose model with schema. Example usage:
|
|||||||
export const User = defineMongooseModel({
|
export const User = defineMongooseModel({
|
||||||
name: 'User',
|
name: 'User',
|
||||||
schema: {
|
schema: {
|
||||||
name: {
|
email: {
|
||||||
type: String,
|
type: 'string',
|
||||||
required: true,
|
required: true,
|
||||||
|
unique: true,
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
options: {
|
options: {
|
||||||
|
|
||||||
|
},
|
||||||
|
hooks(schema) {
|
||||||
|
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
@ -27,11 +31,14 @@ This function creates a new Mongoose model with schema. Example usage:
|
|||||||
import { defineMongooseModel } from '#nuxt/mongoose'
|
import { defineMongooseModel } from '#nuxt/mongoose'
|
||||||
|
|
||||||
export const User = defineMongooseModel('User', {
|
export const User = defineMongooseModel('User', {
|
||||||
name: {
|
email: {
|
||||||
type: String,
|
type: 'string',
|
||||||
required: true,
|
required: true,
|
||||||
|
unique: true,
|
||||||
},
|
},
|
||||||
}, {
|
}, {
|
||||||
|
|
||||||
|
}, (schema) => {
|
||||||
|
|
||||||
})
|
})
|
||||||
```
|
```
|
||||||
@ -39,6 +46,16 @@ This function creates a new Mongoose model with schema. Example usage:
|
|||||||
::
|
::
|
||||||
|
|
||||||
|
|
||||||
|
| **Key** | **Type** | **Require** | **Description** |
|
||||||
|
| ---------------------------- | ----------- | ----------- | ----- |
|
||||||
|
| `name` | `string` | true | Name of Model |
|
||||||
|
| `schema` | [`SchemaDefinition`](https://mongoosejs.com/docs/schematypes.html) | true | Schema Definition of Model |
|
||||||
|
| `options` | [`SchemaOptions`](https://mongoosejs.com/docs/guide.html#options) | false | Schema Options for Model |
|
||||||
|
| `hooks` | [`(schema: Schema<T>) => void`](https://mongoosejs.com/docs/middleware.html) | false | Schema Hooks Function to customize Model |
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
## `defineMongooseConnection`
|
## `defineMongooseConnection`
|
||||||
This function creates a new Mongoose connection.
|
This function creates a new Mongoose connection.
|
||||||
- `nuxt-mongoose` provides a default connection for you, it auto-register a plugin in nitro, so you don't need to use this function unless you want to create a new connection. more info [here](https://github.com/arashsheyda/nuxt-mongoose/blob/main/src/runtime/server/plugins/mongoose.db.ts).
|
- `nuxt-mongoose` provides a default connection for you, it auto-register a plugin in nitro, so you don't need to use this function unless you want to create a new connection. more info [here](https://github.com/arashsheyda/nuxt-mongoose/blob/main/src/runtime/server/plugins/mongoose.db.ts).
|
||||||
@ -49,4 +66,4 @@ Example usage:
|
|||||||
import { defineMongooseConnection } from '#nuxt/mongoose'
|
import { defineMongooseConnection } from '#nuxt/mongoose'
|
||||||
|
|
||||||
export const connection = defineMongooseConnection('mongodb://127.0.0.1/nuxt-mongoose')
|
export const connection = defineMongooseConnection('mongodb://127.0.0.1/nuxt-mongoose')
|
||||||
```
|
```
|
||||||
|
|||||||
40
package.json
40
package.json
@ -1,7 +1,7 @@
|
|||||||
{
|
{
|
||||||
"name": "nuxt-mongoose",
|
"name": "nuxt-mongoose",
|
||||||
"type": "module",
|
"type": "module",
|
||||||
"version": "0.0.8",
|
"version": "0.0.9",
|
||||||
"description": "Nuxt 3 module for MongoDB with Mongoose",
|
"description": "Nuxt 3 module for MongoDB with Mongoose",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"repository": {
|
"repository": {
|
||||||
@ -34,36 +34,36 @@
|
|||||||
"test:watch": "vitest watch"
|
"test:watch": "vitest watch"
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@nuxt/devtools-kit": "^0.5.5",
|
"@nuxt/devtools-kit": "^0.6.7",
|
||||||
"@nuxt/kit": "^3.5.2",
|
"@nuxt/kit": "^3.6.2",
|
||||||
"@types/fs-extra": "^11.0.1",
|
"@types/fs-extra": "^11.0.1",
|
||||||
"birpc": "^0.2.11",
|
"birpc": "^0.2.12",
|
||||||
"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.2.2",
|
"mongoose": "^7.3.2",
|
||||||
"ofetch": "^1.1.0",
|
"ofetch": "^1.1.1",
|
||||||
"pathe": "^1.1.0",
|
"pathe": "^1.1.1",
|
||||||
"pluralize": "^8.0.0",
|
"pluralize": "^8.0.0",
|
||||||
"sirv": "^2.0.3",
|
"sirv": "^2.0.3",
|
||||||
"vite-hot-client": "^0.2.1",
|
"vite-hot-client": "^0.2.1",
|
||||||
"ws": "^8.13.0"
|
"ws": "^8.13.0"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@antfu/eslint-config": "^0.38.5",
|
"@antfu/eslint-config": "^0.39.7",
|
||||||
"@nuxt/devtools": "^0.5.5",
|
"@nuxt/devtools": "^0.6.7",
|
||||||
"@nuxt/devtools-ui-kit": "^0.5.5",
|
"@nuxt/devtools-ui-kit": "^0.6.7",
|
||||||
"@nuxt/module-builder": "^0.4.0",
|
"@nuxt/module-builder": "^0.4.0",
|
||||||
"@nuxt/schema": "^3.5.2",
|
"@nuxt/schema": "^3.6.2",
|
||||||
"@nuxt/test-utils": "^3.5.2",
|
"@nuxt/test-utils": "^3.6.2",
|
||||||
"@types/pluralize": "^0.0.29",
|
"@types/pluralize": "^0.0.30",
|
||||||
"@types/ws": "^8.5.4",
|
"@types/ws": "^8.5.5",
|
||||||
"changelogen": "^0.5.3",
|
"changelogen": "^0.5.4",
|
||||||
"eslint": "^8.39.0",
|
"eslint": "^8.44.0",
|
||||||
"nuxt": "^3.5.2",
|
"nuxt": "^3.6.2",
|
||||||
"sass": "^1.62.1",
|
"sass": "^1.63.6",
|
||||||
"sass-loader": "^13.3.1",
|
"sass-loader": "^13.3.2",
|
||||||
"splitpanes": "^3.1.5",
|
"splitpanes": "^3.1.5",
|
||||||
"vitest": "^0.31.2"
|
"vitest": "^0.33.0"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
1
playground/.env.example
Normal file
1
playground/.env.example
Normal file
@ -0,0 +1 @@
|
|||||||
|
MONGODB_URI="mongodb://127.0.0.1:27017/nuxt-mongoose"
|
||||||
@ -7,10 +7,20 @@ export const UserSchema = defineMongooseModel({
|
|||||||
type: 'string',
|
type: 'string',
|
||||||
required: true,
|
required: true,
|
||||||
},
|
},
|
||||||
slug: {
|
email: {
|
||||||
|
type: 'string',
|
||||||
|
required: true,
|
||||||
|
unique: false,
|
||||||
|
},
|
||||||
|
password: {
|
||||||
type: 'string',
|
type: 'string',
|
||||||
required: true,
|
required: true,
|
||||||
unique: true,
|
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
hooks(schema) {
|
||||||
|
schema.pre('save', function (this, next) {
|
||||||
|
this.password = `hash.${this.password}.${Math.random()}`
|
||||||
|
next()
|
||||||
|
})
|
||||||
|
},
|
||||||
})
|
})
|
||||||
|
|||||||
3712
pnpm-lock.yaml
generated
3712
pnpm-lock.yaml
generated
File diff suppressed because it is too large
Load Diff
@ -34,7 +34,7 @@ export default defineNuxtModule<ModuleOptions>({
|
|||||||
},
|
},
|
||||||
setup(options, nuxt) {
|
setup(options, nuxt) {
|
||||||
const { resolve } = createResolver(import.meta.url)
|
const { resolve } = createResolver(import.meta.url)
|
||||||
const runtimeConfig = nuxt.options.runtimeConfig
|
const runtimeConfig = nuxt.options.runtimeConfig as any
|
||||||
|
|
||||||
if (nuxt.options.dev) {
|
if (nuxt.options.dev) {
|
||||||
$fetch('https://registry.npmjs.org/nuxt-mongoose/latest').then((release) => {
|
$fetch('https://registry.npmjs.org/nuxt-mongoose/latest').then((release) => {
|
||||||
|
|||||||
@ -11,14 +11,24 @@ export async function defineMongooseConnection({ uri, options }: { uri?: string;
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
await mongoose.connect(mongooseUri, { ...mongooseOptions })
|
await mongoose.connect(mongooseUri, { ...mongooseOptions })
|
||||||
logger.info('Connected to MONGODB')
|
logger.success('Connected to MongoDB database')
|
||||||
}
|
}
|
||||||
catch (err) {
|
catch (err) {
|
||||||
logger.error('Error connecting to database', err)
|
logger.error('Error connecting to MongoDB database', err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export function defineMongooseModel(nameOrOptions: string | { name: string; schema: SchemaDefinition; options?: SchemaOptions }, schema?: SchemaDefinition, options?: SchemaOptions): Model<any> {
|
export function defineMongooseModel<T>(
|
||||||
|
nameOrOptions: string | {
|
||||||
|
name: string
|
||||||
|
schema: SchemaDefinition
|
||||||
|
options?: SchemaOptions
|
||||||
|
hooks?: (schema: mongoose.Schema<T>) => void
|
||||||
|
},
|
||||||
|
schema?: SchemaDefinition,
|
||||||
|
options?: SchemaOptions,
|
||||||
|
hooks?: (schema: mongoose.Schema<T>) => void,
|
||||||
|
): Model<T> {
|
||||||
let name: string
|
let name: string
|
||||||
if (typeof nameOrOptions === 'string') {
|
if (typeof nameOrOptions === 'string') {
|
||||||
name = nameOrOptions
|
name = nameOrOptions
|
||||||
@ -27,10 +37,13 @@ export function defineMongooseModel(nameOrOptions: string | { name: string; sche
|
|||||||
name = nameOrOptions.name
|
name = nameOrOptions.name
|
||||||
schema = nameOrOptions.schema
|
schema = nameOrOptions.schema
|
||||||
options = nameOrOptions.options
|
options = nameOrOptions.options
|
||||||
|
hooks = nameOrOptions.hooks
|
||||||
}
|
}
|
||||||
|
|
||||||
const newSchema = new mongoose.Schema({
|
const newSchema = new mongoose.Schema<T>(schema, options as any)
|
||||||
...schema,
|
|
||||||
}, { ...options })
|
if (hooks)
|
||||||
return mongoose.model(name, newSchema)
|
hooks(newSchema)
|
||||||
|
|
||||||
|
return mongoose.model<T>(name, newSchema)
|
||||||
}
|
}
|
||||||
|
|||||||
@ -5,7 +5,7 @@ import { generateApiRoute, generateSchemaFile } from '../utils/schematics'
|
|||||||
import { capitalize, pluralize, singularize } from '../utils/formatting'
|
import { capitalize, pluralize, singularize } from '../utils/formatting'
|
||||||
|
|
||||||
export function setupResourceRPC({ nuxt, rpc }: NuxtDevtoolsServerContext): any {
|
export function setupResourceRPC({ nuxt, rpc }: NuxtDevtoolsServerContext): any {
|
||||||
const runtimeConfig = nuxt.options.runtimeConfig
|
const runtimeConfig = nuxt.options.runtimeConfig as any
|
||||||
|
|
||||||
return {
|
return {
|
||||||
// TODO: maybe separate functions
|
// TODO: maybe separate functions
|
||||||
|
|||||||
@ -25,7 +25,7 @@ export interface ClientFunctions {
|
|||||||
|
|
||||||
export interface Collection {
|
export interface Collection {
|
||||||
name: string
|
name: string
|
||||||
fields?: {}[]
|
fields?: object[]
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface Resource {
|
export interface Resource {
|
||||||
|
|||||||
@ -11,5 +11,5 @@ export interface NuxtDevtoolsServerContext {
|
|||||||
|
|
||||||
refresh: (event: keyof ServerFunctions) => void
|
refresh: (event: keyof ServerFunctions) => void
|
||||||
|
|
||||||
extendServerRpc: <ClientFunctions = {}, ServerFunctions = {}>(name: string, functions: ServerFunctions) => BirpcGroup<ClientFunctions, ServerFunctions>
|
extendServerRpc: <ClientFunctions = object, ServerFunctions = object>(name: string, functions: ServerFunctions) => BirpcGroup<ClientFunctions, ServerFunctions>
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user