Security News
PyPI’s New Archival Feature Closes a Major Security Gap
PyPI now allows maintainers to archive projects, improving security and helping users make informed decisions about their dependencies.
ES6 generator-based MongoDB ODM for Node.js v0.11.x.
npm install mongorito --save
Warning: You should have Node.js v0.11.x installed (or newer). Run node with --harmony
option:
node --harmony something.js
Note: In order for the following examples to work, you need use something like co.
var Mongorito = require('mongorito');
var Model = Mongorito.Model;
Mongorito.connect('localhost/database_name');
Mongorito.connect('db1/database_name', 'db2/database_name'); // replica set
Mongorito.disconnect();
Mongorito.close(); // alias for disconnect
That's right. No schema.
var Post = Model.extend({
collection: 'posts'
});
Returns all documents in the collection.
var posts = yield Post.all();
Always returns an array.
var posts;
posts = yield Post.find(); // same as .all()
// match documents with title = "Great title"
posts = yield Post.find({ title: 'Great title' });
posts = yield Post.where('title', 'Great title').find();
posts = yield Post.where('title').equals('Great title').find();
// match documents with regex
posts = yield Post.where('title', /great/i).find();
// match documents matching sub-property
posts = yield Post.where('author.name', 'John Doe').find();
// match documents matching sub-documents
// matching posts where at least one comment.body is "Nice comment"
posts = yield Post.where('comments', { body: 'Nice comment' }).find();
// match documents with limit
posts = yield Post.limit(5).find();
// match documents with skip
posts = yield Post.skip(5).find();
// match documents where some field exists
posts = yield Post.where('title').exists().find();
// match documents with $in
posts = yield Post.where('title').in(['First title', 'Second title']).find();
// match documents with $gt, $gte, $lt, $lte
posts = yield Post.where('position').gt(5).find();
posts = yield Post.gt('position', 5).find();
Let's say you have such Post document:
{
"_id": ObjectId("5234d25244e937489c000004"),
"title": "Great title",
"body": "Great body",
"comments": [
ObjectId("5234d25244e937489c000005"),
ObjectId("5234d25244e937489c000006"),
ObjectId("5234d25244e937489c000007")
]
}
And you need to fetch each Comment document from comments field:
var post = yield Post.findOne();
var comments = post.get('comments');
var index = 0;
var commentId;
while (commentId = comments[index]) {
comments[index] = yield Comment.findById(commentId);
index++;
}
With populating, you don't need to write all that and instead do this:
// .populate() tells query to populate comments field
// with documents fetched using Comment model
var post = yield Post.populate('comments', Comment).findOne();
var comments = post.get('comments');
// comments is an array of Comment models now
Note: When you will try to save a document with populated fields, they will be reverted back to _id's.
Finds only one document. Returns either Model instance (Post, in these examples) or undefined.
var post;
post = yield Post.findOne(); // returns first document in collection
post = yield Post.findOne({ title: 'Great title' });
Automatically determines when to create or update a document based on _id field.
var post = new Post({ // can pass fields directly to constructor
title: 'Some title'
});
post.set('title', 'Good title'); // or using .set() method by passing key and value
post.set({ // or by passing object of fields
title: 'Great title',
body: 'Hot body'
});
yield post.save(); // document created
var id = post.get('_id'); // _id automatically had been set after .save()
post.set('title', 'Updated title');
yield post.save(); // document updated
yield post.remove();
Or you can remove multiple documents using:
yield Post.remove({ title: 'Some title' }); // query
Mongorito models support before, create and around (before + create) hooks for these events:
To setup a hook for some event:
var Post = Model.extend({
collection: 'posts',
customHandling: function *(next) {
// processing, validating, etc
yield next; // MUST be present
}.before('save')
});
Same goes for other hooks:
myAfterCreateHook: function *(next) {
// executes after document was created
yield next;
}.after('create'),
myAroundRemoveHook: function *(next) {
// executes before and after document was removed
yield next;
}.around('remove')
If you want to break the chain and prevent executing of the next hooks, just throw an Error:
myHook: function *(next) {
throw new Error('Next hooks will not be executed');
}.before('update')
To execute tests run:
npm test
The MIT License (MIT) Copyright © 2014 Vadim Demedes vdemedes@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.
FAQs
ES6 generator-based MongoDB ODM.
The npm package mongorito receives a total of 104 weekly downloads. As such, mongorito popularity was classified as not popular.
We found that mongorito demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
PyPI now allows maintainers to archive projects, improving security and helping users make informed decisions about their dependencies.
Research
Security News
Malicious npm package postcss-optimizer delivers BeaverTail malware, targeting developer systems; similarities to past campaigns suggest a North Korean connection.
Security News
CISA's KEV data is now on GitHub, offering easier access, API integration, commit history tracking, and automated updates for security teams and researchers.