Skip to content

deeppatel234/mongoorm

Repository files navigation

ORM for Mongodb in Node JS

CircleCI codecov License: MIT NPM Download NPM

Installation

$ npm install mongoorm

Connecting to MongoDB and configure mongoorm

const MongoORM = require('mongoorm')

MongoORM.connect(connectionString, options).then(() => {
  console.log("mongoorm is connected.");
});

For more information on the connection options:

Document Schema Creation

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(),
      },
    };
  }
}

CRUD Operations

//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();

Configure logging

You can configure your own logger using -

mongoorm.setLogger({
  info: console.info,
  error: console.error,
});

About

ORM for Mongodb in Node JS

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 3

  •  
  •  
  •