Comparing version 0.4.4 to 0.5.0
@@ -18,2 +18,6 @@ 'use strict'; | ||
var _checkTypes = require('check-types'); | ||
var _checkTypes2 = _interopRequireDefault(_checkTypes); | ||
var _eventemitter = require('eventemitter3'); | ||
@@ -47,5 +51,5 @@ | ||
var _Document = require('./Document'); | ||
var _EJSON = require('./EJSON'); | ||
var _Document2 = _interopRequireDefault(_Document); | ||
var _EJSON2 = _interopRequireDefault(_EJSON); | ||
@@ -100,40 +104,9 @@ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
* @param {String|Object} raw | ||
* @return {Document} | ||
* @return {Object} | ||
*/ | ||
value: function create(raw) { | ||
return new _Document2.default(this, raw); | ||
return _checkTypes2.default.string(raw) ? _EJSON2.default.parse(raw) : raw; | ||
} | ||
/** | ||
* Set a static function in a model. Chainable. | ||
* @param {String} name | ||
* @param {Function} fn | ||
* @return {this} | ||
*/ | ||
}, { | ||
key: 'static', | ||
value: function _static(name, fn) { | ||
(0, _invariant2.default)(!Collection.prototype.hasOwnProperty(name) && typeof fn === 'function', 'Static function `%s` must not be an existing one in a model', name); | ||
this[name] = fn; | ||
return this; | ||
} | ||
/** | ||
* Setup a method in a model (all created documents). | ||
* Chainable. | ||
* @param {String} name | ||
* @param {Function} fn | ||
* @return {this} | ||
*/ | ||
}, { | ||
key: 'method', | ||
value: function method(name, fn) { | ||
(0, _invariant2.default)(!this._methods[name] && typeof fn === 'function', 'Method function `%s` already defined in a model', name); | ||
this._methods[name] = fn; | ||
return this; | ||
} | ||
/** | ||
* Ensures index by delegating to IndexManager. | ||
@@ -309,4 +282,8 @@ * @param {String} key | ||
/** | ||
* Make a cursor with given query and return | ||
* Make a cursor with given query and return. | ||
* By default all documents clonned before passed | ||
* to pipeline functions. By setting `options.noClone` | ||
* to `true` clonning may be disabled (for your own risk) | ||
* @param {Object} query | ||
* @param {Number} options.noClone | ||
* @return {CursorObservable} | ||
@@ -329,3 +306,3 @@ */ | ||
* @param {Object} sortObj | ||
* @return {Promise} | ||
* @return {CursorObservable} | ||
*/ | ||
@@ -350,3 +327,3 @@ | ||
* @param {Object} query | ||
* @return {Promise} | ||
* @return {CursorObservable} | ||
*/ | ||
@@ -357,4 +334,7 @@ | ||
value: function count(query) { | ||
return this.ids(query).then(function (ids) { | ||
return ids.length; | ||
var options = arguments.length <= 1 || arguments[1] === undefined ? {} : arguments[1]; | ||
options.noClone = true; | ||
return this.find(query, options).aggregate(function (docs) { | ||
return docs.length; | ||
}); | ||
@@ -367,3 +347,3 @@ } | ||
* @param {Object} query | ||
* @return {Promise} | ||
* @return {CursorObservable} | ||
*/ | ||
@@ -374,3 +354,8 @@ | ||
value: function ids(query) { | ||
return new _defaultCursorClass(this, query).ids(); | ||
var options = arguments.length <= 1 || arguments[1] === undefined ? {} : arguments[1]; | ||
options.noClone = true; | ||
return this.find(query, options).map(function (doc) { | ||
return doc._id; | ||
}); | ||
} | ||
@@ -377,0 +362,0 @@ }, { |
@@ -143,3 +143,6 @@ 'use strict'; | ||
function Cursor(db, query, options) { | ||
function Cursor(db) { | ||
var query = arguments.length <= 1 || arguments[1] === undefined ? {} : arguments[1]; | ||
var options = arguments.length <= 2 || arguments[2] === undefined ? {} : arguments[2]; | ||
_classCallCheck(this, Cursor); | ||
@@ -318,5 +321,11 @@ | ||
}).then(function (docs) { | ||
var clonned = (0, _map3.default)(docs, function (doc) { | ||
return _EJSON2.default.clone(doc); | ||
}); | ||
var clonned = undefined; | ||
if (_this3.options.noClone) { | ||
clonned = docs; | ||
} else { | ||
clonned = (0, _map3.default)(docs, function (doc) { | ||
return _EJSON2.default.clone(doc); | ||
}); | ||
} | ||
return _this3.processPipeline(clonned); | ||
@@ -331,20 +340,2 @@ }).then(function (docs) { | ||
}, { | ||
key: 'ids', | ||
value: function ids() { | ||
var _this4 = this; | ||
this._executing = this._prepareCursor().then(function () { | ||
return _this4._matchObjects(); | ||
}).then(function (docs) { | ||
return (0, _map3.default)(docs, function (x) { | ||
return x._id; | ||
}); | ||
}).then(function (ids) { | ||
_this4._executing = null; | ||
return ids; | ||
}); | ||
return this._executing; | ||
} | ||
}, { | ||
key: 'then', | ||
@@ -369,14 +360,14 @@ value: function then(resolve, reject) { | ||
value: function _matchObjects() { | ||
var _this5 = this; | ||
var _this4 = this; | ||
return new _DocumentRetriver2.default(this.db).retriveForQeury(this._query).then(function (docs) { | ||
var results = []; | ||
var withFastLimit = _this5._limit && !_this5._skip && !_this5._sorter; | ||
var withFastLimit = _this4._limit && !_this4._skip && !_this4._sorter; | ||
(0, _forEach2.default)(docs, function (d) { | ||
var match = _this5._matcher.documentMatches(d); | ||
var match = _this4._matcher.documentMatches(d); | ||
if (match.result) { | ||
results.push(d); | ||
} | ||
if (withFastLimit && results.length === _this5._limit) { | ||
if (withFastLimit && results.length === _this4._limit) { | ||
return false; | ||
@@ -390,8 +381,8 @@ } | ||
if (_this5._sorter) { | ||
var comparator = _this5._sorter.getComparator(); | ||
if (_this4._sorter) { | ||
var comparator = _this4._sorter.getComparator(); | ||
results.sort(comparator); | ||
} | ||
return _this5.processSkipLimits(results); | ||
return _this4.processSkipLimits(results); | ||
}); | ||
@@ -398,0 +389,0 @@ } |
@@ -7,3 +7,2 @@ 'use strict'; | ||
exports.MongoTypeComp = undefined; | ||
exports.Document = Document; | ||
exports.selectorIsId = selectorIsId; | ||
@@ -17,6 +16,2 @@ exports.selectorIsIdPerhapsAsObject = selectorIsIdPerhapsAsObject; | ||
var _assign2 = require('fast.js/object/assign'); | ||
var _assign3 = _interopRequireDefault(_assign2); | ||
var _checkTypes = require('check-types'); | ||
@@ -34,6 +29,2 @@ | ||
var _invariant = require('invariant'); | ||
var _invariant2 = _interopRequireDefault(_invariant); | ||
var _EJSON = require('./EJSON'); | ||
@@ -47,62 +38,2 @@ | ||
/* | ||
* Instance of a model (Document) | ||
* It delegates some useful methods to a given | ||
* collection object(`remove`, `update`). | ||
*/ | ||
function Document(db) { | ||
var _this = this; | ||
var raw = arguments.length <= 1 || arguments[1] === undefined ? {} : arguments[1]; | ||
(0, _invariant2.default)(db, 'Document(...): you must give a collection for the document'); | ||
// Ensure raw object | ||
raw = _checkTypes2.default.string(raw) ? _EJSON2.default.parse(raw) : raw; | ||
// Define internal methods | ||
Object.defineProperty(this, 'remove', { | ||
value: function value() { | ||
(0, _invariant2.default)(_this._id, 'remove(...): document must have an _id for remove'); | ||
return db.remove({ _id: self._id }); | ||
}, | ||
writable: false | ||
}); | ||
Object.defineProperty(this, 'update', { | ||
value: function value(modifier) { | ||
(0, _invariant2.default)(this._id, 'update(...): document must have an _id for update'); | ||
return db.update({ _id: self._id }, modifier); | ||
}, | ||
writable: false | ||
}); | ||
Object.defineProperty(this, 'copy', { | ||
value: function value() { | ||
return new Document(db, _EJSON2.default.clone(_this)); | ||
}, | ||
writable: false | ||
}); | ||
Object.defineProperty(this, 'serialize', { | ||
value: function value() { | ||
return _EJSON2.default.stringify(_this); | ||
}, | ||
writable: false | ||
}); | ||
// Special methods from collection | ||
for (var method in db._methods) { | ||
Object.defineProperty(this, method, { | ||
value: db._methods[method], | ||
writable: false | ||
}); | ||
} | ||
// Move given raw object to a Document | ||
(0, _assign3.default)(this, raw); | ||
} | ||
exports.default = Document; | ||
/** | ||
@@ -114,3 +45,2 @@ * Return true if given selector is an | ||
*/ | ||
function selectorIsId(selector) { | ||
@@ -117,0 +47,0 @@ return typeof selector === 'string' || typeof selector === 'number'; |
@@ -48,2 +48,3 @@ 'use strict'; | ||
* TODO: there is a place for indexes | ||
* | ||
* @param {Object} query | ||
@@ -50,0 +51,0 @@ * @return {Promise} |
import _each from 'fast.js/forEach'; | ||
import _map from 'fast.js/map'; | ||
import _check from 'check-types'; | ||
import EventEmitter from 'eventemitter3'; | ||
@@ -10,3 +11,3 @@ import invariant from 'invariant'; | ||
import Random from './Random'; | ||
import Document from './Document'; | ||
import EJSON from './EJSON'; | ||
@@ -66,42 +67,9 @@ | ||
* @param {String|Object} raw | ||
* @return {Document} | ||
* @return {Object} | ||
*/ | ||
create(raw) { | ||
return new Document(this, raw); | ||
return _check.string(raw) ? EJSON.parse(raw) : raw; | ||
} | ||
/** | ||
* Set a static function in a model. Chainable. | ||
* @param {String} name | ||
* @param {Function} fn | ||
* @return {this} | ||
*/ | ||
static(name, fn) { | ||
invariant( | ||
!Collection.prototype.hasOwnProperty(name) && typeof fn === 'function', | ||
'Static function `%s` must not be an existing one in a model', | ||
name | ||
); | ||
this[name] = fn; | ||
return this; | ||
} | ||
/** | ||
* Setup a method in a model (all created documents). | ||
* Chainable. | ||
* @param {String} name | ||
* @param {Function} fn | ||
* @return {this} | ||
*/ | ||
method(name, fn) { | ||
invariant( | ||
!this._methods[name] && typeof fn === 'function', | ||
'Method function `%s` already defined in a model', | ||
name | ||
); | ||
this._methods[name] = fn; | ||
return this; | ||
} | ||
/** | ||
* Ensures index by delegating to IndexManager. | ||
@@ -244,4 +212,8 @@ * @param {String} key | ||
/** | ||
* Make a cursor with given query and return | ||
* Make a cursor with given query and return. | ||
* By default all documents clonned before passed | ||
* to pipeline functions. By setting `options.noClone` | ||
* to `true` clonning may be disabled (for your own risk) | ||
* @param {Object} query | ||
* @param {Number} options.noClone | ||
* @return {CursorObservable} | ||
@@ -259,3 +231,3 @@ */ | ||
* @param {Object} sortObj | ||
* @return {Promise} | ||
* @return {CursorObservable} | ||
*/ | ||
@@ -275,8 +247,8 @@ findOne(query, sortObj, options = {}) { | ||
* @param {Object} query | ||
* @return {Promise} | ||
* @return {CursorObservable} | ||
*/ | ||
count(query) { | ||
return this.ids(query).then((ids) => { | ||
return ids.length; | ||
}); | ||
count(query, options = {}) { | ||
options.noClone = true; | ||
return this.find(query, options) | ||
.aggregate((docs) => docs.length); | ||
} | ||
@@ -288,6 +260,8 @@ | ||
* @param {Object} query | ||
* @return {Promise} | ||
* @return {CursorObservable} | ||
*/ | ||
ids(query) { | ||
return new _defaultCursorClass(this, query).ids(); | ||
ids(query, options = {}) { | ||
options.noClone = true; | ||
return this.find(query, options) | ||
.map((doc) => doc._id); | ||
} | ||
@@ -294,0 +268,0 @@ } |
@@ -99,3 +99,3 @@ import _bind from 'fast.js/function/bind'; | ||
export class Cursor extends EventEmitter { | ||
constructor(db, query, options) { | ||
constructor(db, query = {}, options = {}) { | ||
super(); | ||
@@ -283,3 +283,9 @@ this.db = db; | ||
.then(docs => { | ||
const clonned = _map(docs, doc => EJSON.clone(doc)); | ||
let clonned; | ||
if (this.options.noClone) { | ||
clonned = docs; | ||
} else { | ||
clonned = _map(docs, doc => EJSON.clone(doc)); | ||
} | ||
return this.processPipeline(clonned); | ||
@@ -295,14 +301,2 @@ }) | ||
ids() { | ||
this._executing = this._prepareCursor() | ||
.then(() => this._matchObjects()) | ||
.then((docs) => _map(docs, x => x._id)) | ||
.then((ids) => { | ||
this._executing = null; | ||
return ids; | ||
}); | ||
return this._executing; | ||
} | ||
then(resolve, reject) { | ||
@@ -309,0 +303,0 @@ return this.exec().then(resolve, reject); |
@@ -1,70 +0,7 @@ | ||
import _assign from 'fast.js/object/assign'; | ||
import _check from 'check-types'; | ||
import _each from 'fast.js/forEach'; | ||
import _keys from 'fast.js/object/keys'; | ||
import invariant from 'invariant'; | ||
import EJSON from './EJSON'; | ||
/* | ||
* Instance of a model (Document) | ||
* It delegates some useful methods to a given | ||
* collection object(`remove`, `update`). | ||
*/ | ||
export function Document(db, raw = {}) { | ||
invariant( | ||
db, | ||
'Document(...): you must give a collection for the document' | ||
); | ||
// Ensure raw object | ||
raw = _check.string(raw) ? EJSON.parse(raw) : raw; | ||
// Define internal methods | ||
Object.defineProperty(this, 'remove', { | ||
value: () => { | ||
invariant( | ||
this._id, | ||
'remove(...): document must have an _id for remove' | ||
); | ||
return db.remove({_id: self._id}); | ||
}, | ||
writable: false, | ||
}); | ||
Object.defineProperty(this, 'update', { | ||
value: function(modifier) { | ||
invariant( | ||
this._id, | ||
'update(...): document must have an _id for update' | ||
); | ||
return db.update({_id: self._id}, modifier); | ||
}, | ||
writable: false, | ||
}); | ||
Object.defineProperty(this, 'copy', { | ||
value: () => new Document(db, EJSON.clone(this)), | ||
writable: false, | ||
}); | ||
Object.defineProperty(this, 'serialize', { | ||
value: () => EJSON.stringify(this), | ||
writable: false, | ||
}); | ||
// Special methods from collection | ||
for (const method in db._methods) { | ||
Object.defineProperty(this, method, { | ||
value: db._methods[method], | ||
writable: false, | ||
}); | ||
} | ||
// Move given raw object to a Document | ||
_assign(this, raw); | ||
} | ||
export default Document; | ||
/** | ||
@@ -71,0 +8,0 @@ * Return true if given selector is an |
@@ -24,2 +24,3 @@ import _check from 'check-types'; | ||
* TODO: there is a place for indexes | ||
* | ||
* @param {Object} query | ||
@@ -26,0 +27,0 @@ * @return {Promise} |
{ | ||
"name": "marsdb", | ||
"version": "0.4.4", | ||
"version": "0.5.0", | ||
"author": { | ||
@@ -5,0 +5,0 @@ "name": "Artem Artemev", |
@@ -40,2 +40,3 @@ <div style="text-align:center"><img src="https://static.studytime.me/marsdb.png" /></div> | ||
* [LevelUP](https://github.com/c58/marsdb-levelup) | ||
* [MongoDB](https://github.com/c58/marsdb-mongo) | ||
@@ -70,3 +71,3 @@ ## Server-side synchronizers | ||
``` | ||
### Find a documents | ||
### Find documents | ||
```javascript | ||
@@ -73,0 +74,0 @@ const posts = new Collection(‘posts’); |
@@ -19,6 +19,2 @@ import {Document} from '../../lib/Document'; | ||
doc.b.should.be.equal(3); | ||
doc.should.have.property('remove'); | ||
doc.should.have.property('update'); | ||
doc.should.have.property('copy'); | ||
doc.should.have.property('serialize'); | ||
}); | ||
@@ -62,39 +58,2 @@ | ||
describe('#static', function () { | ||
it('should set static method of the model', function () { | ||
const db = new Collection('test'); | ||
db.static('testStatic', function() { | ||
return true; | ||
}); | ||
expect(db.testStatic).to.be.a('function'); | ||
expect(db.testStatic()).to.be.equals(true); | ||
}); | ||
it('should throw an exception if static method with given name exists', function () { | ||
const db = new Collection('test'); | ||
db.static('testStatic', function() {}); | ||
(() => db.static('testStatic')).should.throw(Error); | ||
}); | ||
}); | ||
describe('#method', function () { | ||
it('should set object method of each created document', function () { | ||
const db = new Collection('test'); | ||
db.method('testMethod', function() { | ||
return true; | ||
}); | ||
const doc = db.create({a: 1}); | ||
expect(doc.testMethod).to.be.a('function'); | ||
expect(doc.testMethod()).to.be.equals(true); | ||
}); | ||
it('should throw an exception if method with given name exists', function () { | ||
const db = new Collection('test'); | ||
db.method('testMethod', function() {}); | ||
(() => db.method('testMethod')).should.throw(Error); | ||
}); | ||
}); | ||
describe('#ensureIndex', function () { | ||
@@ -101,0 +60,0 @@ /*it('should ensure index', function () { |
@@ -20,3 +20,3 @@ import Collection from '../../lib/Collection'; | ||
db.insert({a: 'e', b: 5, c: 'some text 5', g: 'g2', d: 234, f: 2}), | ||
db.insert({a: 'f', b: 6, c: 'some text 6', g: 'g2', f: 20}), | ||
db.insert({a: 'f', b: 6, c: 'some text 6', g: 'g2', f: 20, k: {a: 1}}), | ||
db.insert({a: 'g', b: 7, c: 'some text 7', g: 'g2', f: 21}), | ||
@@ -31,3 +31,38 @@ ]); | ||
describe('#exec', function () { | ||
it('should clone docs by default', function () { | ||
const cursor = new Cursor(db); | ||
cursor.find({b: {$gt: 4}}).skip(1).sort({b: 1}); | ||
return cursor.exec().then((docs) => { | ||
docs.should.have.length(2); | ||
docs[0].k.a.should.be.equals(1); | ||
docs[0].b.should.be.equals(6); | ||
docs[0].b = 7; | ||
docs[0].k.a = 2; | ||
return cursor.exec().then((docs) => { | ||
docs.should.have.length(2); | ||
docs[0].k.a.should.be.equals(1); | ||
docs[0].b.should.be.equals(6); | ||
}); | ||
}); | ||
}); | ||
it('should NOT clone docs when `options.noClone` passed', function () { | ||
const cursor = new Cursor(db, {}, {noClone: true}); | ||
cursor.find({b: {$gt: 4}}).skip(1).sort({b: 1}); | ||
return cursor.exec().then((docs) => { | ||
docs.should.have.length(2); | ||
docs[0].k.a.should.be.equals(1); | ||
docs[0].b.should.be.equals(6); | ||
docs[0].b = 7; | ||
docs[0].k.a = 2; | ||
return cursor.exec().then((docs) => { | ||
docs.should.have.length(2); | ||
docs[0].b.should.be.equals(7); | ||
docs[0].k.a.should.be.equals(2); | ||
}); | ||
}); | ||
}); | ||
}); | ||
describe('#skip', function () { | ||
@@ -411,14 +446,2 @@ it('should skip documents with sorting', function () { | ||
describe('#ids', function () { | ||
it('should return list of ids without pipeline processing', function () { | ||
const cursor = new Cursor(db); | ||
cursor.find({b: {$gt: 5}}).sort({b: 1}).aggregate(d => { | ||
throw new Error('should not be there'); | ||
}); | ||
return cursor.ids().then((ids) => { | ||
ids.should.have.length(2); | ||
}); | ||
}); | ||
}); | ||
}); |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is too big to display
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
204
808310
69
18883