2.3.2 • Published 9 years ago

mungo v2.3.2

Weekly downloads
-
License
ISC
Repository
-
Last release
9 years ago

Mungo

Mungo is a library to create models for MongoDB.

Install

npm install mungo

Usage

import Mungo from 'mungo';

class User extends Mungo.Model {
  static schema = { username : String };
}

// Connect to MongoDB
Mungo.connect('mongodb://@localhost');

User.create({ username : 'dude' });

User.find({ username : 'dude' });

User.update({ username : 'dude' }, { username : 'mate' });

User.remove({ username : 'dude' });

Syntax

All operations are promises:

Foo.find()
  .then(found => { /*...*/ })
  .catch(error => { /*...*/ });

Some methods are chainable:

Foo
  .find({ foo : 1 })
  .limit(25)
  .then(found => {});

Connect

Mungo.connect(url);

Connecting by entering an url. Connect emits so you can listen to it:

Mungo.connect(url)
  .on('error', error => console.log(error.stack))
  .on('connected', connection => {
    console.log(connection.db)
  });

Connections are stacked in Mungo.connections. When you try to do a op, the first alive connection in array will be chosen.

You can force a connection to be used:

Mungo.connect(url1);
Mungo.connect(url2);

Foo.find().connection(0);
Foo2.find().connection(1);

Or specify the connection directly:

Mungo
  .connect(url)
  .on('connected', connection => Foo.find().connection(connection));

Methods

MethodArgumentsAbout
count- <object> query default {}- <object> projection default {} - <object> options default {}Count documents in collection

Schema

Type

class User extends Mungo.Model {
  static schema = { username : String, score : Number };
}

Types allowed

  • String
  • Number
  • Boolean
  • Date
  • Array for a better array control, see below
  • Object for a better subdocument control, see below
  • Subdocument see below
  • ObjectID MongoDB's object id
  • Mixed accepts any type

Array of types

You can enclose types inside arrays:

// { numbers : [1, 2, 3] }

static schema = { numbers : [Number] }

Sudocuments

Use the Subdocument to embed a document:

// { foo : { bar : true } }

static schema = { "foo" : new (Mungo.Subdocument)({ "bar" : Boolean }) }

You could also use directly the object notation such as:

// { foo : { bar : true } }

static schema = { "foo" : { "bar" : Boolean } }

But you have to make sure your subdocument does not contain a type property - otherwise it will be mistaken with a field description.

References to other model

Use the name of the model you want to refer :

class Team extends Mungo.Model {
  static schema = { "name" : String };
}

class Player extends Mungo.Model {
  static schema = { "team" : Team };
}

Cyclic dependencies

If your model uses references to other models that also refer it (cyclic dependency), you can use the getter syntax to make sure referred models do not end up null.

static get schema () {
  return { "team" : Team };
}

Type declaration

You can use the sugar syntax or the type attribute in the field description:

static schema = { "name" : String }
// Or...
static schema = {
  name : { "type" : String }
}

Default type

If you don't declare a type for a field, Mixed is used.

Required

Require a fill to be set when inserting to DB

static schema = { "name" : { required : true } }

Default

Fill empty field values with default when inserting to DB

static schema = { "score" : { default : 0 } }
2.3.2

9 years ago

2.3.1

9 years ago

2.3.0

9 years ago

2.2.0

9 years ago

2.1.2

9 years ago

2.1.1

9 years ago

2.1.0

9 years ago

2.0.22

9 years ago

2.0.21

9 years ago

2.0.20

9 years ago

2.0.19

9 years ago

2.0.18

9 years ago

2.0.17

9 years ago

2.0.16

9 years ago

2.0.15

9 years ago

2.0.13

9 years ago

2.0.12

9 years ago

2.0.11

9 years ago

2.0.10

9 years ago

2.0.9

9 years ago

2.0.8

9 years ago

2.0.7

9 years ago

2.0.6

9 years ago

2.0.5

9 years ago

2.0.4

9 years ago

2.0.3

9 years ago

2.0.2

9 years ago

2.0.1

9 years ago

2.0.0

9 years ago

1.0.25

10 years ago

1.0.24

10 years ago

1.0.23

10 years ago

1.0.22

10 years ago

1.0.18

10 years ago

1.0.17

10 years ago

1.0.16

10 years ago

1.0.15

10 years ago

1.0.14

10 years ago

1.0.13

10 years ago

1.0.11

10 years ago

1.0.10

10 years ago

1.0.9

10 years ago

1.0.8

10 years ago

1.0.7

10 years ago

1.0.5

10 years ago

1.0.3

10 years ago

1.0.2

10 years ago

1.0.1

10 years ago