Skip to content
This repository has been archived by the owner on Feb 10, 2022. It is now read-only.

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
Vadim Demedes committed Feb 16, 2012
0 parents commit 0a7a5fe
Show file tree
Hide file tree
Showing 19 changed files with 1,059 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
@@ -0,0 +1,3 @@
.DS_Store
node_modules
*.sock
4 changes: 4 additions & 0 deletions .npmignore
@@ -0,0 +1,4 @@
support
test
examples
*.sock
70 changes: 70 additions & 0 deletions Readme.md
@@ -0,0 +1,70 @@
# Mongorito

*Awesome* MongoDB ODM for Node.js. Just take a look on its pretty models. Uses Mongolian for interacting with MongoDB.

# Installation

```
npm install mongorito
```

# Usage

```coffee-script
Mongorito = require 'mongorito'

Mongorito.connect 'database', ['127.0.0.1:27017'], 'user', 'password'

class Post extends Mongorito.Model
constructor: ->
super 'posts' # telling our collection name

Post = Mongorito.bake Post # Now, we are ready to go!

post = new Post
post.title = 'Very interesting article.'
post.content = 'Really, really, exciting.'
post.save (err) ->
# saved!

post.title = 'Edited title!'
post.save (err) ->
# updated!

post.remove ->
# removed!

Post.find { title : 'Some title!' }, (err, posts) ->
for post in posts
# post is an instance of Post model, so you can perform usual methods on it
post.remove ->
```

# Interested?

Check out **examples** folder, it has a lot of code, which describes all parts of Mongorito.

# License

(The MIT License)

Copyright (c) 2011 Vadim Demedes <sbioko@gmail.com>

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 changes: 3 additions & 0 deletions examples/connection.coffee
@@ -0,0 +1,3 @@
Mongorito = require '../lib/mongorito'

Mongorito.connect 'databaseName', ['127.0.0.1:27017']
5 changes: 5 additions & 0 deletions examples/connection.js
@@ -0,0 +1,5 @@
var Mongorito;

Mongorito = require('../lib/mongorito');

Mongorito.connect('databaseName', ['127.0.0.1:27017']);
9 changes: 9 additions & 0 deletions examples/definition.coffee
@@ -0,0 +1,9 @@
Mongorito = require '../lib/mongorito'

Mongorito.connect 'databaseName', ['127.0.0.1:27017']

class Post extends Mongorito.Model
constructor: ->
super 'posts' # REQUIRED STEP

Post = Mongorito.bake Post # REQUIRED STEP
21 changes: 21 additions & 0 deletions examples/definition.js
@@ -0,0 +1,21 @@
var Mongorito, Post,
__hasProp = Object.prototype.hasOwnProperty,
__extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor; child.__super__ = parent.prototype; return child; };

Mongorito = require('../lib/mongorito');

Mongorito.connect('databaseName', ['127.0.0.1:27017']);

Post = (function(_super) {

__extends(Post, _super);

function Post() {
Post.__super__.constructor.call(this, 'posts');
}

return Post;

})(Mongorito.Model);

Post = Mongorito.bake(Post);
21 changes: 21 additions & 0 deletions examples/hooks.coffee
@@ -0,0 +1,21 @@
Mongorito = require '../lib/mongorito'

Mongorito.connect 'databaseName', ['127.0.0.1:27017']

class Post extends Mongorito.Model
constructor: ->
super 'posts'

beforeCreate: -> # before creating post

aroundCreate: -> # before and after creating post

afterCreate: -> # after creating post

beforeUpdate: -> # before updating post

aroundUpdate: -> # before and after updating post

afterUpdate: -> # after updating post

Post = Mongorito.bake Post
33 changes: 33 additions & 0 deletions examples/hooks.js
@@ -0,0 +1,33 @@
var Mongorito, Post,
__hasProp = Object.prototype.hasOwnProperty,
__extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor; child.__super__ = parent.prototype; return child; };

Mongorito = require('../lib/mongorito');

Mongorito.connect('databaseName', ['127.0.0.1:27017']);

Post = (function(_super) {

__extends(Post, _super);

function Post() {
Post.__super__.constructor.call(this, 'posts');
}

Post.prototype.beforeCreate = function() {};

Post.prototype.aroundCreate = function() {};

Post.prototype.afterCreate = function() {};

Post.prototype.beforeUpdate = function() {};

Post.prototype.aroundUpdate = function() {};

Post.prototype.afterUpdate = function() {};

return Post;

})(Mongorito.Model);

Post = Mongorito.bake(Post);
26 changes: 26 additions & 0 deletions examples/operations.coffee
@@ -0,0 +1,26 @@
Mongorito = require '../lib/mongorito'

Mongorito.connect 'databaseName', ['127.0.0.1:27017']

class Post extends Mongorito.Model
constructor: ->
super 'posts'

Post = Mongorito.bake Post

# Creating new post
post = new Post
post.title = 'Title of the post'
post.content = 'Content of the post'
post.save (err, results) ->
# results will be populated with invalid fields, if validators will be defined

# Updating post

post.title = 'Changed title of the previously saved post'
post.save (err, results) ->
# results will be populated with invalid fields, if validators will be defined

# Removing post

post.remove (err) ->
35 changes: 35 additions & 0 deletions examples/operations.js
@@ -0,0 +1,35 @@
var Mongorito, Post, post,
__hasProp = Object.prototype.hasOwnProperty,
__extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor; child.__super__ = parent.prototype; return child; };

Mongorito = require('../lib/mongorito');

Mongorito.connect('databaseName', ['127.0.0.1:27017']);

Post = (function(_super) {

__extends(Post, _super);

function Post() {
Post.__super__.constructor.call(this, 'posts');
}

return Post;

})(Mongorito.Model);

Post = Mongorito.bake(Post);

post = new Post;

post.title = 'Title of the post';

post.content = 'Content of the post';

post.save(function(err, results) {});

post.title = 'Changed title of the previously saved post';

post.save(function(err, results) {});

post.remove(function(err) {});
31 changes: 31 additions & 0 deletions examples/queries.coffee
@@ -0,0 +1,31 @@
Mongorito = require '../lib/mongorito'

Mongorito.connect 'databaseName', ['127.0.0.1:27017']

class Post extends Mongorito.Model
constructor: ->
super 'posts'

Post = Mongorito.bake Post

Post.find (err, posts) ->
for post in posts
console.log post.title # post is a Post model, so you can perform all usual operations

Post.find { title: 'Nice title!' }, (err, posts) ->

Post.findWithLimit 5, (err, posts) -> # getting only first 5 posts

Post.findWithLimit { author: 'Drew' }, 5, (err, posts) -> # getting only first 5 posts with author = Drew

Post.findWithLimit 5, 2, (err, posts) -> # getting only 5 posts, skipping first 2

Post.findWithLimit { author: 'Drew' }, 5, 2, (err, posts) -> # getting only 5 posts, skipping first 2, with author = Drew

Post.findWithOrder { _id: -1 }, (err, posts) -> # getting posts, sorted by _id

Post.findWithOrderAndLimit { _id: -1 }, 5, (err, posts) -> # getting first 5 posts, sorted by _id

Post.findWithOrderAndLimit { _id: -1 }, 5, 2, (err, posts) -> # getting 5 posts, skipping first 2, sorted by _id

Post.findWithOrderAndLimit { author: 'Drew' }, { _id: -1 }, 5, 2, (err, posts) -> # getting 5 posts, skipping first 2, sorted by _id, with author = Drew
65 changes: 65 additions & 0 deletions examples/queries.js
@@ -0,0 +1,65 @@
var Mongorito, Post,
__hasProp = Object.prototype.hasOwnProperty,
__extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor; child.__super__ = parent.prototype; return child; };

Mongorito = require('../lib/mongorito');

Mongorito.connect('databaseName', ['127.0.0.1:27017']);

Post = (function(_super) {

__extends(Post, _super);

function Post() {
Post.__super__.constructor.call(this, 'posts');
}

return Post;

})(Mongorito.Model);

Post = Mongorito.bake(Post);

Post.find(function(err, posts) {
var post, _i, _len, _results;
_results = [];
for (_i = 0, _len = posts.length; _i < _len; _i++) {
post = posts[_i];
_results.push(console.log(post.title));
}
return _results;
});

Post.find({
title: 'Nice title!'
}, function(err, posts) {});

Post.findWithLimit(5, function(err, posts) {});

Post.findWithLimit({
author: 'Drew'
}, 5, function(err, posts) {});

Post.findWithLimit(5, 2, function(err, posts) {});

Post.findWithLimit({
author: 'Drew'
}, 5, 2, function(err, posts) {});

Post.findWithOrder({
_id: -1
}, function(err, posts) {});

Post.findWithOrderAndLimit({
_id: -1
}, 5, function(err, posts) {});

Post.findWithOrderAndLimit({
_id: -1
}, 5, 2, function(err, posts) {});

Post.findWithOrderAndLimit({
author: 'Drew'
}, {
_id: -1
}, 5, 2, function(err, posts) {});
20 changes: 20 additions & 0 deletions examples/validations.coffee
@@ -0,0 +1,20 @@
Mongorito = require '../lib/mongorito'

Mongorito.connect 'databaseName', ['127.0.0.1:27017']

class Tweet extends Mongorito.Model
constructor: ->
super 'tweets'

validateBody: (callback) -> # you should pass false, if invalid and true, if valid
if @body.length >= 140
callback false
else
callback true

Tweet = Mongorito.bake Tweet

tweet = new Tweet
tweet.body = 'I want to be super-super-super-super long! Reallly, reallly, long!!!! In fact, I am VEEERY long! You\'ve never seen such a looooooong tweeeeeet!'
tweet.save (err, results) ->
# results will be ['body'], because body field did not pass validation

0 comments on commit 0a7a5fe

Please sign in to comment.