New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

mongorito

Package Overview
Dependencies
Maintainers
1
Versions
50
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

mongorito - npm Package Compare versions

Comparing version 0.4.1 to 0.4.2

155

lib/mongorito.js

@@ -0,1 +1,5 @@

/**
* Module dependencies
*/
var Class = require('backbone-class');

@@ -202,8 +206,16 @@ var compose = require('koa-compose');

var collection = this.collection();
var docs = yield collection.find(query || {});
var model = this;
var query = new Query(collection, model).find(query);
return yield query;
},
count: function *(query) {
var collection = this.collection();
var model = this;
return docs.map(function (doc) {
return new model(doc);
});
var count = new Query(collection, model).count(query);
return yield count;
},

@@ -242,4 +254,139 @@

['where', 'limit', 'sort', 'exists', 'lt', 'lte', 'gt', 'gte', 'in', 'nin', 'and', 'or', 'ne', 'nor'].forEach(function (method) {
StaticMethods[method] = function () {
var collection = this.collection();
var model = this;
var query = new Query(collection, model);
query[method].apply(query, arguments);
return query;
};
});
Model = Mongorito.Model = Class.extend(InstanceMethods, StaticMethods);
/**
* Query
*
*/
var Query = Class.extend({
initialize: function (collection, model, key) {
this.collection = collection;
this.model = model;
this.query = {};
this.options = {};
this.lastKey = key;
},
where: function (key, value) {
if (typeof arguments[0] == 'object') {
var conditions = key;
for (key in conditions) {
this.where(key, conditions[key]);
}
} else {
if (!value) {
this.lastKey = key;
return this;
}
if (value instanceof RegExp) {
value = { $regex: value };
} else if (typeof value == 'object') {
value = { $elemMatch: value };
}
this.query[key] = value;
}
return this;
},
limit: function (limit) {
this.options.limit = limit;
return this;
},
skip: function (skip) {
this.options.skip = skip;
return this;
},
sort: function (sort) {
this.options.sort = sort;
return this;
},
equals: function (value) {
var key = this.lastKey;
delete this.lastKey;
this.query[key] = value;
return this;
},
exists: function (key, exists) {
if (this.lastKey) {
exists = key;
key = this.lastKey;
delete this.lastKey;
}
this.query[key] = { $exists: exists || true };
return this;
},
count: function *(query) {
this.where(query);
var collection = this.collection;
var model = this.model;
var count = collection.count(this.query);
return yield count;
},
find: function *(query) {
this.where(query);
var collection = this.collection;
var model = this.model;
var docs = yield collection.find(this.query, this.options);
return docs.map(function (doc) {
return new model(doc);
});
}
});
['lt', 'lte', 'gt', 'gte', 'in', 'nin', 'and', 'or', 'ne', 'nor'].forEach(function (method) {
Query.prototype[method] = function (key, value) {
if (this.lastKey) {
value = key;
key = this.lastKey;
delete this.lastKey;
}
this.query[key] = {};
this.query[key]['$' + method] = value;
return this;
}
});
/**
* Module exports
*/
module.exports = Mongorito;

2

package.json
{
"name": "mongorito",
"version": "0.4.1",
"version": "0.4.2",
"description": "ES6 generator-based MongoDB ODM. It rocks.",

@@ -5,0 +5,0 @@ "author": "Vadim Demedes <vdemedes@gmail.com>",

@@ -74,3 +74,33 @@ # Mongorito

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();
```

@@ -77,0 +107,0 @@

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc