$ npm install mongoorm
const MongoORM = require('mongoorm')
MongoORM.connect(connectionString, options).then(() => {
console.log("mongoorm is connected.");
});
For more information on the connection options:
- URI Connection String: MongoDB connection string URI.
- Connection Settings: Reference on the driver-specific connection settings.
Schema can be created by extending Document
const { Document } = mongoorm;
class User extends Document {
initFields(fields) {
return {
firstName: fields.String(),
lastName: fields.String(),
address: {
city: fields.String(),
pin: fields.String(),
},
};
}
}
//get collection
const user = new User({ document: 'user' });
//example data
let userData = {
firstName: 'Deep',
lastName: 'Patel',
address: {
city: 'Mehsana',
pin: '384002',
},
};
//create document with above data
let document = user.create(userData);
//create document
await document.save();
//update document
document.firstName.set('hello');
await document.save();
//delete document
await document.delete();
You can configure your own logger using -
mongoorm.setLogger({
info: console.info,
error: console.error,
});