Git Product home page Git Product logo

mongorito's Introduction

Mongorito

Build Status Coverage Status

Awesome MongoDB ODM for Node.js apps. Just take a look at its beautiful models and API.

Uses official mongodb driver under the hood.




Quick overview

const mongorito = require('mongorito');
const Model = mongorito.Model;

class Post extends Model {
	
}

mongorito.connect('localhost/blog');

let post = new Post({
	title: 'Steve Angello rocks',
	author: {
		name: 'Emma'
	}
});

post.save();
// post saved

Features

  • Based on Promises, which means no callbacks
  • Established API you've already used to
  • Hooks (before:save, around:create, after:remove, etc)
  • Fully covered by tests
  • Using this module results in a beautiful code

Installation

$ npm install mongorito --save

Usage

Connection

Check out connection example.

Here's how to connect to a blog database on localhost:

await mongorito.connect('localhost/blog');

To disconnect, use mongorito.disconnect():

await mongorito.disconnect();

Models

Use classes to define models:

const Model = mongorito.Model;

class Post extends Model {
	
}

This defines model Post with documents in posts collection. To use a custom collection, add collection() method, which returns the name of the desired collection:

class Post extends Model {
	collection () {
		return 'super_cool_posts';
	}
}

Mongorito models can also be defined old-fashioned Backbone way:

const Post = Model.extend({
	collection: 'posts'
});

Note: collection property has to be set.

Attributes

Check out attributes example.

To create a new instance of a model, simply use new:

let post = new Post({
	title: 'Great title',
	author: {
		name: 'Emma'
	}
});

To retrieve a specific attribute (even a nested one):

let title = post.get('title');
let author = post.get('author.name');

All attributes can be retrieved at once using either toJSON() or get():

let attrs = post.toJSON();
let attrs = post.get();

Set new values via set() method:

post.set('title', 'New title');
post.set('author.name', 'Rachel');

Save & Remove

Check out manage example.

Use a save() method to create/update (Mongorito handles that for you) a model:

let post = new Post();

await post.save(); // creates a new post

post.set('title', 'New title');
await post.save(); // updates an existing post

To remove a model from collection:

await post.remove();

You can also remove all models matching a certain criteria:

await Post.remove({ title: 'New title' });

Queries

Find all

To fetch all models find() or all() can be used (they're identical):

Post.find();
Post.all();

Find one

Post.findOne({ title: 'New title' });

Find by ID

Post.findById('56c9e0922cc9215110ab26dc');

Find where value equals

Post.where('title', 'New title').find();
Post.where('author.name', 'Emma').find();

Find where value matches a regular expression

Post.where('title', /something/i).find();

Find where attribute exists

Post.exists('title').find();

Find where value is less/greater than

Post.where('comments_number').lt(5).find(); // less than 5
Post.where('comments_number').lte(5).find(); // less than or equal 5
Post.where('comments_number').gt(5).find(); // greater than 5
Post.where('comments_number').gte(5).find(); // greater than or equal 5

Find where value is one of

Post.in('comments_number', [4, 8]).find();

Find using OR

Find all models where title equals either "First" or "Second":

Post.or({ title: 'First' }, { title: 'Second' }).find();

Limit results

Post.limit(10).find();

Skip results

Skip first N results:

Post.skip(4).find();

Sort results

// descending
Post.sort('comments_number', -1);

// ascending
Post.sort('comments_number', 1);

Count results

Count all documents in collection:

Post.count();

Count all documents matching a certain criteria:

Post.count({ awesome: true });

Tests

$ npm test

License

Mongorito is released under the MIT License.

mongorito's People

Contributors

brianvanburken avatar greenkeeperio-bot avatar hbbbs avatar mdmellis avatar mrusme avatar olistic avatar paullryan avatar saikirandaripelli avatar sirbyalive avatar zaaack avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.