This repository has been archived by the owner on Feb 10, 2022. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Vadim Demedes
committed
Feb 16, 2012
0 parents
commit 0a7a5fe
Showing
19 changed files
with
1,059 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
.DS_Store | ||
node_modules | ||
*.sock |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
support | ||
test | ||
examples | ||
*.sock |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
Mongorito = require '../lib/mongorito' | ||
|
||
Mongorito.connect 'databaseName', ['127.0.0.1:27017'] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
var Mongorito; | ||
|
||
Mongorito = require('../lib/mongorito'); | ||
|
||
Mongorito.connect('databaseName', ['127.0.0.1:27017']); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) -> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) {}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) {}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
Oops, something went wrong.