Files
nuxt-mongoose/docs/content/2.api/1.utils.md
2023-07-09 14:21:33 +03:00

1.8 KiB

Utils

Discover all available utils.

defineMongooseModel

This function creates a new Mongoose model with schema. Example usage:

::code-group

export const User = defineMongooseModel({
  name: 'User',
  schema: {
    email: {
      type: 'string',
      required: true,
      unique: true,
    },
  },
  options: {
    
  },
  hooks(schema) {

  },
})
import { defineMongooseModel } from '#nuxt/mongoose'

export const User = defineMongooseModel('User', {
  email: {
    type: 'string',
    required: true,
    unique: true,
  },
}, {
  
}, (schema) => {

})

::

Key Type Require Description
name string true Name of Model
schema SchemaDefinition true Schema Definition of Model
options SchemaOptions false Schema Options for Model
hooks (schema: Schema<T>) => void 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.

Example usage:

import { defineMongooseConnection } from '#nuxt/mongoose'

export const connection = defineMongooseConnection('mongodb://127.0.0.1/nuxt-mongoose')