Models
Models Provide Clean Interface to interact with your MongoDB database through moongoose library.
Creating Models
Models Can be created in two ways :
Through Command Line
Models Can Be Created From Command Line : First Run -
node artisan
then
make:model <Name-of-your-model>
We don't have any restrictions on naming your Models, But the preferred way of creating Models is -
NameOfYourModel
for example:-
make:model Users
This will automatically create your model in app\models
Directory and migration in
app\database\migrations
directory
Manually
Models can be created manually also-
- Create a file name of your choice in
app\models
- Create Migration in
app\database\migrations
example: - You have created a model - User.jsconst mongoose = require("mongoose");
- Now Create a schema in
app\database\migrations
directory example:- You have createdcreate_Users_model.js
inapp\database\migrations
- Now in your
app\models\User.js
const mongoose = require("mongoose"); const UsersObject = require("../../database/migrations/create_Users_model"); const Users = mongoose.model("Users", new mongoose.Schema(UsersObject)); module.exports = Users;
- Done !!! Enjoy !
of course if you create a model from CLI, we will take care of everything.