nesquirk
Version:
Ties Nes + minimongo together for gloryful reactive apps.
134 lines (103 loc) • 4.76 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
var _mingo = require('mingo');
var _mingo2 = _interopRequireDefault(_mingo);
var _bsonObjectid = require('bson-objectid');
var _bsonObjectid2 = _interopRequireDefault(_bsonObjectid);
var _ejson = require('ejson');
var _ejson2 = _interopRequireDefault(_ejson);
var _events = require('events');
var _events2 = _interopRequireDefault(_events);
var _object = require('./object');
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
var Collection = function (_EventEmitter) {
_inherits(Collection, _EventEmitter);
function Collection(name) {
_classCallCheck(this, Collection);
var _this = _possibleConstructorReturn(this, (Collection.__proto__ || Object.getPrototypeOf(Collection)).call(this));
_this.name = name;
_this._docs = [];
_this.setMaxListeners(Infinity);
return _this;
}
_createClass(Collection, [{
key: 'find',
value: function find(criteria, projection) {
return _mingo2.default.find(this._docs, criteria, projection);
}
}, {
key: 'insert',
value: function insert(doc) {
// console.log('Insert', doc)
doc = _ejson2.default.clone(doc);
doc._id = doc._id || (0, _bsonObjectid2.default)().toHexString();
this._docs.push(doc);
this.emit('change');
return doc._id;
}
}, {
key: 'update',
value: function update(criteria, _update, options) {
var _this2 = this;
options = options || {};
var modifiers = Object.keys(_update);
if (modifiers.length > 1 || modifiers[0] !== '$set') {
throw new Error('Unsupported modifier(s) ' + modifiers.filter(function (m) {
return m !== '$set';
}));
}
var upsert = options.upsert || false;
var multi = options.multi || false;
var nMatched = 0;
var nUpserted = 0;
var nModified = 0;
var cursor = _mingo2.default.find(this._docs, criteria);
nMatched = cursor.count();
if (nMatched) {
var updateDoc = function updateDoc(doc) {
var index = _this2._docs.findIndex(function (d) {
return d._id === doc._id;
});
_this2._docs[index] = (0, _object.assign)(doc, _update.$set);
};
if (multi) {
cursor.forEach(updateDoc);
nModified = nMatched;
} else {
updateDoc(cursor.first());
nModified = 1;
}
} else {
if (upsert) {
// console.log('Upsert', update.$set)
this.insert((0, _object.assign)({}, _update.$set));
nUpserted = 1;
}
}
this.emit('change');
return { nMatched: nMatched, nModified: nModified, nUpserted: nUpserted };
}
}, {
key: 'remove',
value: function remove(criteria) {
var docs = _mingo2.default.remove(this._docs, criteria);
var nRemoved = this._docs.length - docs.length;
this._docs = docs;
this.emit('change');
return { nRemoved: nRemoved };
}
}, {
key: 'aggregate',
value: function aggregate(expressions) {
return _mingo2.default.aggregate(this._docs, expressions);
}
}]);
return Collection;
}(_events2.default);
exports.default = Collection;