arashsheyda / nuxt-mongoose

A Nuxt module for simplifying the use of Mongoose in your project.
https://docs.arashsheyda.me/nuxt-mongoose
75 stars 13 forks source link

Quick questions #46

Open kamran-12 opened 8 months ago

kamran-12 commented 8 months ago

I have not used this plugin yet but I am planning to. My questions:

  1. If I put all my models into a single file and name it models.ts, then put it directly into server folder, in place of models folder, will it work? Will it need any non-default configurations?
  2. I noticed defineMongooseConnection and defineMongooseModel methods in readme.md but cannot find them in Mongoose documentation. Why? Are these exclusive to nuxt-mongoose? If so, what about regular Mongoose functionality? Do I need something else to import to access them or importing them from '#nuxt/mongoose' will be enough?
  3. In which folder/file do I put the following code?
    
    import { defineMongooseConnection } from '#nuxt/mongoose'

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

arashsheyda commented 8 months ago

hey @kamran-12

let me know if you any other question

kamran-12 commented 8 months ago

hey @kamran-12

  • yes you can define all your models in one file and you'll be able to use it.
  • you don't need to define another connection (defineMongooseConnection) as nuxt-mongoose auto define it for you, you just need to pass your connections url. check configuration

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.

  • defineMongooseModel is like a shortcut, it create a new schema and return a model. it has all the functionalities of a normal mongoose model. learn more about defineMongooseModel

let me know if you any other question

@arashsheyda Thanks, I would like to see an actual project using your plugin for better understanding. I don't have any questions other than those I asked in the original post, but I feel they were not completely answered. In the question number 2, what I am asking is, does your library contain all Mongoose functionality or if I need, say, Mongoose.Schema, schema.index, schema.virtual, do I need to import them separately from the regular Mongoose library? Other parts will be clear to me when I see an actual project, it will be hard to understand otherwise.

Edit: I found this: https://nuxt-mongoose.nuxt.space/docs/api/examples