14 Commits

13 changed files with 2060 additions and 1808 deletions

View File

@ -1,2 +1,5 @@
dist
node_modules
docs
.github
.vscode

View File

@ -1,6 +1,41 @@
# 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
[compare changes](https://github.com/arashsheyda/nuxt-mongoose/compare/v0.0.7...v0.0.8)

View File

@ -9,6 +9,7 @@ export const wsError = ref<any>()
export const wsConnectingDebounced = useDebounce(wsConnecting, 2000)
const connectPromise = connectVite()
// eslint-disable-next-line @typescript-eslint/ban-types
let onMessage: Function = () => {}
export const clientFunctions = {

View File

@ -12,13 +12,17 @@ This function creates a new Mongoose model with schema. Example usage:
export const User = defineMongooseModel({
name: 'User',
schema: {
name: {
type: String,
email: {
type: 'string',
required: true,
unique: true,
},
},
options: {
},
hooks(schema) {
},
})
```
@ -27,18 +31,31 @@ This function creates a new Mongoose model with schema. Example usage:
import { defineMongooseModel } from '#nuxt/mongoose'
export const User = defineMongooseModel('User', {
name: {
type: String,
email: {
type: 'string',
required: true,
unique: true,
},
}, {
}, (schema) => {
})
```
::
| **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`
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).

View File

@ -1,7 +1,7 @@
{
"name": "nuxt-mongoose",
"type": "module",
"version": "0.0.8",
"version": "0.0.9",
"description": "Nuxt 3 module for MongoDB with Mongoose",
"license": "MIT",
"repository": {
@ -34,36 +34,36 @@
"test:watch": "vitest watch"
},
"dependencies": {
"@nuxt/devtools-kit": "^0.5.5",
"@nuxt/kit": "^3.5.2",
"@nuxt/devtools-kit": "^0.6.7",
"@nuxt/kit": "^3.6.2",
"@types/fs-extra": "^11.0.1",
"birpc": "^0.2.11",
"birpc": "^0.2.12",
"defu": "^6.1.2",
"flatted": "^3.2.7",
"fs-extra": "^11.1.1",
"mongoose": "^7.2.2",
"ofetch": "^1.1.0",
"pathe": "^1.1.0",
"mongoose": "^7.3.2",
"ofetch": "^1.1.1",
"pathe": "^1.1.1",
"pluralize": "^8.0.0",
"sirv": "^2.0.3",
"vite-hot-client": "^0.2.1",
"ws": "^8.13.0"
},
"devDependencies": {
"@antfu/eslint-config": "^0.38.5",
"@nuxt/devtools": "^0.5.5",
"@nuxt/devtools-ui-kit": "^0.5.5",
"@antfu/eslint-config": "^0.39.7",
"@nuxt/devtools": "^0.6.7",
"@nuxt/devtools-ui-kit": "^0.6.7",
"@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.5.2",
"sass": "^1.62.1",
"sass-loader": "^13.3.1",
"@nuxt/schema": "^3.6.2",
"@nuxt/test-utils": "^3.6.2",
"@types/pluralize": "^0.0.30",
"@types/ws": "^8.5.5",
"changelogen": "^0.5.4",
"eslint": "^8.44.0",
"nuxt": "^3.6.2",
"sass": "^1.63.6",
"sass-loader": "^13.3.2",
"splitpanes": "^3.1.5",
"vitest": "^0.31.2"
"vitest": "^0.33.0"
}
}

1
playground/.env.example Normal file
View File

@ -0,0 +1 @@
MONGODB_URI="mongodb://127.0.0.1:27017/nuxt-mongoose"

View File

@ -7,10 +7,20 @@ export const UserSchema = defineMongooseModel({
type: 'string',
required: true,
},
slug: {
email: {
type: 'string',
required: true,
unique: false,
},
password: {
type: 'string',
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

File diff suppressed because it is too large Load Diff

View File

@ -34,7 +34,7 @@ export default defineNuxtModule<ModuleOptions>({
},
setup(options, nuxt) {
const { resolve } = createResolver(import.meta.url)
const runtimeConfig = nuxt.options.runtimeConfig
const runtimeConfig = nuxt.options.runtimeConfig as any
if (nuxt.options.dev) {
$fetch('https://registry.npmjs.org/nuxt-mongoose/latest').then((release) => {

View File

@ -11,14 +11,24 @@ export async function defineMongooseConnection({ uri, options }: { uri?: string;
try {
await mongoose.connect(mongooseUri, { ...mongooseOptions })
logger.info('Connected to MONGODB')
logger.success('Connected to MongoDB database')
}
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
if (typeof nameOrOptions === 'string') {
name = nameOrOptions
@ -27,10 +37,13 @@ export function defineMongooseModel(nameOrOptions: string | { name: string; sche
name = nameOrOptions.name
schema = nameOrOptions.schema
options = nameOrOptions.options
hooks = nameOrOptions.hooks
}
const newSchema = new mongoose.Schema({
...schema,
}, { ...options })
return mongoose.model(name, newSchema)
const newSchema = new mongoose.Schema<T>(schema, options as any)
if (hooks)
hooks(newSchema)
return mongoose.model<T>(name, newSchema)
}

View File

@ -5,7 +5,7 @@ import { generateApiRoute, generateSchemaFile } from '../utils/schematics'
import { capitalize, pluralize, singularize } from '../utils/formatting'
export function setupResourceRPC({ nuxt, rpc }: NuxtDevtoolsServerContext): any {
const runtimeConfig = nuxt.options.runtimeConfig
const runtimeConfig = nuxt.options.runtimeConfig as any
return {
// TODO: maybe separate functions

View File

@ -25,7 +25,7 @@ export interface ClientFunctions {
export interface Collection {
name: string
fields?: {}[]
fields?: object[]
}
export interface Resource {

View File

@ -11,5 +11,5 @@ export interface NuxtDevtoolsServerContext {
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>
}