-
ODM(Object Data Modeling) lib for MongoDB and Node.js
-
manages relationships between data, provides schema validation and it is used to translate between obj in code and the repr of those object in MongoDB
- document data structure (shape of the document) that is enforced via the app layer
-
high-order constructors that take schema and create an instance of a document
-
wrapper on the Mongoose Schema
-
provides an interface to the db for creating, updating etc...
- not persisted to the database
userSchema.virtual('fullname').get(function () {
return `${this.firstName} ${this.lastName}`;
})
userSchema.virtual('fullname').set(function (name) {
const str = name.split(' ');
this.firstName = str[0];
this.lastName = str[1];
})
// Use case
const model = new UserModel();
model.fullname = 'Andrei Gatej';-
helper methods on the schema
-
accessed via the model instandce
-
will have access tot the model object
userSchema.methods.getInitials = function () {
return this.firstName[0] + this.lastName[0];
}
// Use case
console.log(model.getInitials())- accessible via the Model Class
userSchema.statics.getUsers = function () {
return new Promise((resolve, reject) => {
this.find((err, docs) => {
if(err) {
console.error(err)
return reject(err)
}
resolve(docs)
})
});
}
// Use case
UserModel.getUsers()
.then(console.log)
.catch(console.error)
-
functions that run at specific stages of a pipeline
-
for the following operations: Aggregation, Document, Query, Model
-
pre and post take 2 params:
- the type of the event: init, validate, savec, remove
- cb tjat os executed wotj
thisreferencing the Model Instance
User Model -> Pre-save Middl -> save -> Post save Middl
| | |
| | |
generate pass hash write to DB send email when user acc is created