@codecraftkit/mongo

mongo handler for mongoose models in express framework

Usage no npm install needed!

<script type="module">
  import codecraftkitMongo from 'https://cdn.skypack.dev/@codecraftkit/mongo';
</script>

README

cc-mongo

Instaling

npm install --save cc-mongo

How To use

const Mongoose = require('mongoose').Mongoose;
const ModelHandler = require('cc-mongo');
const db = new Mongoose();
db.connect('http://localhost:27017/meteor', { useNewUrlParser: true, useFindAndModify: false }).catch(console.error);

// In our Model Definition
const collectionName = 'user';
const userSchema = new db.Schema({
  _id: { type: String, required: true },
  name: { type: String },
  status: { type: Boolean },
  isRemove: { type: Boolean, default: false },
  createdAt: { type: Date, default: Date.now },
  updatedAt: { type: Date, default: Date.now }
}, {
  collection: collectionName
});

//here we use mongo handler
const User = ModelHandler(db, collectionName, userSchema);

module.exports = User;

Available Methods

Methods Params Return
insert model, session model
update props, session model
remove/hide props, session model
delete _id, session model
insert(model, session):
  • model: the new model to insert into the collection.
  • session | optional: tbd.
update(props, session)
  • props | object:
    • model: the object with new changes.
    • selector | optional: thq condition to the update query. By default: { _id: model._id }.
    • options | optional: to use as option on mongoose updateOne method.
remove(props, session) / hide(props, session) [soft delete]
  • props | object: this method is a direct access to update.
    • model: the object with new changes.
    • selector | optional: thq condition to the update query. By default: { _id: model._id }.
    • options | optional: to use as option on mongoose updateOne method.
  • Before update call, model is manipulated with properties below:
    • updatedAt: new Date()
    • removedAt: new Date()
    • isRemove: true
delete(_id, session) [hard delete]
  • _id: model id to delete