mongorm

1.0.9 • Public • Published

MongORM.js

ORM for MongoDB, promised functions. You can use native async/await too! Yes!

Installation

First install node.js and mongodb. Then:

$ npm install mongorm

Connecting to MongoDB and configure MongORM

const MongoClient = require('mongodb').MongoClient
const MongoORM = require('mongorm')

MongoClient.connect(mongoString, function (err, db) {
  if (err) return console.error(err)
  MongoORM.setDatabase(db)
})

ORM Example

After connect and configure:

// Posts.js

const MongoORM = require('mongorm')

const Posts = module.exports = MongORM('posts', {
  
  // we can use async/await functions
  async getFromUser (user_id) {
    
    // we can find by ObjectId (even if it is a string)
    const posts = await Posts.find(user_id)

    // we can use the "hasError" to check if everything is ok
    if (Posts.hasError) return new Error('Get posts from user failed.')

    // return results as array
    return posts
  }

  // or use promises with callbacks
  getFromCategory (categoryID, callbackSucess, callbackError) {
    return Posts.find({cat: categoryID})
		  .then(function (data) {
		    callbackSucess(data)
		  })
		  .catch(function (err) {
		    callbackError(err)
    	})
  }
})

Using the ORM

// index.js

const MongoClient = require('mongodb').MongoClient
const MongoORM = require('mongorm')

MongoClient.connect(mongoString, async function (err, db) {
  if (err) return console.error(err)
  MongoORM.setDatabase(db)
	
  // with callback
  Posts.getFromCategory(15, function (data) {
    console.log(data)
  })

  // with async/await
  const posts = await Posts.getFromUser('5a208716aa97e3107751b041')
  if (Posts.hasError) {
		return console.error(Posts.hasError)
	}
  console.log(posts)

  // with promises
  Posts.findOne('5a208716aa97e3107751b041')
		.then(function (data) {
	    // ...
	  })
	  .catch(function (err) {
	    // ...
  	})
})

Package Sidebar

Install

npm i mongorm

Weekly Downloads

0

Version

1.0.9

License

ISC

Unpacked Size

45.5 kB

Total Files

4

Last publish

Collaborators

  • webarthur