3.0.18 • Published 1 day ago

monastery v3.0.18

Weekly downloads
50
License
MIT
Repository
github
Last release
1 day ago

Monastery

Light modeling for MongoDB (using monk).

You're probably face palming yourself, wondering "why... oh why another mongodb model library?". Read on.

Goals

  • Simple and flexible modeling;
  • Light codebase (just enough LOC, comprehensible, etc.);
  • No schema lock-in (they're just there to ensure the right type of data gets written).

Install

npm install monastery

Usage

Initialize

Create a connection to the database and get the Model constructor by requiring monastery.

var Monastery = require("monastery")("localhost/db_name");
  , Model = Monastery.Model;

Defining a Model

var User = new Model("users");

Your Model works like a normal JavaScript object instance: prototypal inheritance.

User.prototype.firstName = function(){
  return this.name.split(" ")[0];
}

Schema

You may pass a schema as second argument when constructing a new Model. This will cast the provided data in the right type when saving to MongoDB.

var User = new Model("users", {
    email: String
  , name: String
  , password_hash: String

  , created_at: { type: Date, default: Date.now }
});

Note: As opposed to mongoose, schemas are not enforced. With an instance, you may add or remove any field present or not in the schema. Schemas' only use is typecasting.

Embedded documents

You may define embedded documents' schema just like you'd define a Model's schema.

var Post = new Model("posts", {
    title: String
  , slug: String

  , author: Model.ObjectID

  , comments: [{
        author: Model.ObjectID
      , content: String
      , created_at: { type: Date, default: Date.now }
    }]

  , likes: [Model.ObjectID] // Casts any value in there as a MongoDB ObjectID
})

The documents will be casted according to your schema.

Model inheritance

If you need models that are extensions of a more general model, you can inherit from another Model by using Model.prototype.inherits:

var Doc = new Model("docs", {
  comments: Array
  author: Model.ObjectId
});

var Article = new Model("docs", {
  _type: "article"
}).inherits(Doc);

Inserting an instance

User.insert({

    email: john@doe.com
  , name: "John Doe"
  , password: "password123" // Just use `bcrypt`, example below...

}, function(error, user) {
  // user instanceof User => true
  console.log(user.firstName()); // => "John"
});

Finding, updating, removing, etc.

find, findOne, findById, update, findAndModify, remove are all inherited from monk and they all send back an instance of your Model.

Furthermore, they also all return a promise, just like monk.

Hooks

A hook is a mechanism to perform some actions or alter some data before or after some event. Hooks are all asynchronous and therefore must call the provided callback. Available hooks are: "load", "insert", "update" and "remove".

Actually useful examples:

User.before("insert", function(done){
  bcrypt.hash(this.password, 10, function(error, hash){
    if (error)
      return done(error);

    this.password_hash = hash;
    delete this.password; // You wouldn't want to store that!
    done();
  });
});

You should pass an error to the callback if there is one. The operations will be alted.

Sync

Hooks can be sync (and will run one after the other).

User.before("insert", function(){
  this.prop = "some property value";
});

Async

Hooks can be async and will still run one after the other. Use a callback in your function like so:

User.after("remove", function(done){
  Post.remove({author: this._id}, done);
});

Async and parallel

Hooks can be called in parallel. You need to use two callback for those to work!

User.before("insert", function(next, done){
  // Call `next` whenever you want the next hook to start working
  next(); // the next hook will start doing its thing right away.
  somethingAsync(done);
});

Available hooks (before and after)

  • "load": the schema is applied (like: new User({}));
  • "insert": the instance is inserted in the database;
  • "update": update or findAndModify is called on the collection;
  • "remove": the instance is removed from the database;

Validation

See "hooks".

monastery provides the basic structures for the most flexibility. Hooks are all you need for validation.

var requireName = function(){
  return typeof this.name === "undefined"
}

User.before("insert", requireName);

User.before("insert", function(done){
  checkSomethingAsync(this, done);
});

Returning false will halt the hook queue. It will also halt the operation you were attempting to complete.

Contributing

  • Find a relevant feature to add/remove/change or find a bug to fix (protip: look at the issues);
  • Code (while following the project's coding style);
  • Write tests;
  • Send a self-contained pull request.

License

Licensed under MIT license

Copyright (c) 2012 Jerome Gravel-Niquet

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

3.0.12

2 days ago

3.0.13

2 days ago

3.0.16

1 day ago

3.0.17

1 day ago

3.0.14

2 days ago

3.0.15

2 days ago

2.2.7

2 days ago

3.0.18

1 day ago

2.2.9

1 day ago

2.2.8

2 days ago

3.0.10

4 days ago

3.0.11

3 days ago

2.2.5

4 days ago

2.2.4

4 days ago

2.2.6

4 days ago

1.42.4

4 days ago

1.42.3

4 days ago

1.42.6

3 days ago

1.42.5

4 days ago

3.0.8

4 days ago

3.0.7

4 days ago

3.0.6

4 days ago

3.0.9

4 days ago

3.0.4

4 days ago

3.0.5

4 days ago

4.0.0

5 days ago

2.2.3

5 days ago

3.0.3

5 days ago

3.0.2

5 days ago

3.0.1

5 days ago

3.0.0

5 days ago

2.2.1

26 days ago

2.2.0

26 days ago

2.2.2

26 days ago

2.1.1

28 days ago

2.1.0

29 days ago

2.0.0

5 months ago

1.41.2

7 months ago

1.42.0

7 months ago

1.42.2

6 months ago

1.42.1

7 months ago

1.41.1

1 year ago

1.41.0

1 year ago

1.40.5

1 year ago

1.40.0

1 year ago

1.40.2

1 year ago

1.40.1

1 year ago

1.40.4

1 year ago

1.40.3

1 year ago

1.39.1

2 years ago

1.39.2

2 years ago

1.39.0

2 years ago

1.37.0

2 years ago

1.37.3

2 years ago

1.37.1

2 years ago

1.37.2

2 years ago

1.38.2

2 years ago

1.38.3

2 years ago

1.38.0

2 years ago

1.36.2

2 years ago

1.38.1

2 years ago

1.36.3

2 years ago

1.35.0

2 years ago

1.36.0

2 years ago

1.32.4

2 years ago

1.36.1

2 years ago

1.32.5

2 years ago

1.34.0

2 years ago

1.32.3

2 years ago

1.33.0

2 years ago

1.31.6

2 years ago

1.31.7

2 years ago

1.32.0

2 years ago

1.32.1

2 years ago

1.32.2

2 years ago

1.31.3

2 years ago

1.31.4

2 years ago

1.29.0

2 years ago

1.30.2

2 years ago

1.30.3

2 years ago

1.30.0

2 years ago

1.30.1

2 years ago

1.30.4

2 years ago

1.28.1

2 years ago

1.28.2

2 years ago

1.28.5

2 years ago

1.28.3

2 years ago

1.28.4

2 years ago

1.31.1

2 years ago

1.31.2

2 years ago

1.31.0

2 years ago

1.28.0

3 years ago

1.27.2

3 years ago

1.27.3

3 years ago

1.27.0

3 years ago

1.27.1

3 years ago

1.26.0

3 years ago

1.26.1

3 years ago

1.25.0

3 years ago

1.24.1

3 years ago

1.24.0

3 years ago

1.23.2

3 years ago

1.23.1

3 years ago

1.21.0

3 years ago

1.22.0

3 years ago

1.23.0

3 years ago

1.20.0

3 years ago

1.19.2

3 years ago

1.19.0

3 years ago

1.17.2

3 years ago

1.16.3

3 years ago

1.18.0

3 years ago

1.17.1

3 years ago

1.16.2

3 years ago

1.17.0

3 years ago

1.16.1

3 years ago

1.16.0

3 years ago

1.15.6

3 years ago

1.19.1

3 years ago

1.15.5

3 years ago

1.15.4

3 years ago

1.15.3

3 years ago

1.15.2

3 years ago

1.15.0

3 years ago

1.15.1

3 years ago

1.14.0

3 years ago

1.13.1

3 years ago

1.13.0

3 years ago

1.12.1

3 years ago

1.12.0

3 years ago

1.11.1

3 years ago

1.10.11

3 years ago

1.11.0

3 years ago

1.10.10

3 years ago

1.10.9

3 years ago

0.1.5

12 years ago

0.1.4

12 years ago

0.1.3

12 years ago

0.1.2

12 years ago

0.1.1

12 years ago

0.1.0

12 years ago

0.0.2

12 years ago

0.0.1

12 years ago