1.0.2 • Published 4 years ago
@trevorthompson/mongo-model v1.0.2
@trevorthompson/mongo-model
Install
npm install @trevorthompson/mongo-model
Usage
Node
class Model {
constructor(schema) {
this.schema = schema;
}
get(_id) {
if (_id) {
return this.schema.findOne({ _id });
}
else {
return this.schema.find({});
}
}
post(record) {
let newRecord = new this.schema(record);
return newRecord.save();
}
put(_id, record) {
return this.schema.findByIdAndUpdate(_id, record, {new: true});
}
delete(_id) {
if (_id) {
return this.schema.findByIdAndDelete(_id);
} else {
return 'Undefined ID';
}
}
}
Verbage
This module requires a schema to be passed into the module to ensure that the methods work.
get() - Checks database for object matching the id, if it's not found then it will return all objects in the database
post() - Instantiate object through the proper schema provided, then saves the object to the database
put() - Checks database based on ID provided and updates that object with the given object passed into the method
delete() - Finds object based on ID and deletes that object. If the object isn't found, then return 'Undefined ID