jsft-mongodb
Advanced tools
Comparing version 0.7.0 to 0.8.0
@@ -20,4 +20,10 @@ 'use strict'; | ||
var _mongodb = require('mongodb'); | ||
var _monk = require('monk'); | ||
var _monk2 = _interopRequireDefault(_monk); | ||
var _Link = require('../Link'); | ||
var _Link2 = _interopRequireDefault(_Link); | ||
var _Collection = require('../Collection'); | ||
@@ -27,5 +33,5 @@ | ||
var _Link = require('../Link'); | ||
var _Document = require('../Document'); | ||
var _Link2 = _interopRequireDefault(_Link); | ||
var _Document2 = _interopRequireDefault(_Document); | ||
@@ -73,3 +79,3 @@ var _testSchema = require('./testSchema'); | ||
mock: function mock() { | ||
return _sinon2.default.stub(_Collection2.default, 'insertOne', function (col, sch, props) { | ||
return _sinon2.default.stub(_Collection2.default, 'insertOne', function (col, sch, oip, props) { | ||
return Promise.resolve(props); | ||
@@ -84,3 +90,3 @@ }); | ||
mock: function mock() { | ||
return _sinon2.default.stub(_Collection2.default, 'updateOne', function (col, sch, props) { | ||
return _sinon2.default.stub(_Collection2.default, 'updateOne', function (col, sch, oip, props) { | ||
return Promise.resolve(props); | ||
@@ -95,3 +101,3 @@ }); | ||
mock: function mock() { | ||
return _sinon2.default.stub(_Collection2.default, 'replaceOne', function (col, sch, props) { | ||
return _sinon2.default.stub(_Collection2.default, 'replaceOne', function (col, sch, oip, props) { | ||
return Promise.resolve(props); | ||
@@ -106,3 +112,3 @@ }); | ||
mock: function mock() { | ||
return _sinon2.default.stub(_Collection2.default, 'upsertOne', function (col, sch, props) { | ||
return _sinon2.default.stub(_Collection2.default, 'upsertOne', function (col, sch, oip, props) { | ||
return Promise.resolve(props); | ||
@@ -117,2 +123,7 @@ }); | ||
var testColname = 'tests'; | ||
var testObjectIdPaths = []; | ||
var stdArgs = [testColname, _testSchema2.default, testObjectIdPaths]; | ||
describe('Collection', function () { | ||
@@ -156,2 +167,5 @@ var testCollection = void 0; | ||
}); | ||
_sinon2.default.spy(_Document2.default, 'fromDocument'); | ||
_sinon2.default.spy(_Document2.default, 'toDocument'); | ||
}); | ||
@@ -192,9 +206,9 @@ after(function () { | ||
(0, _chai.expect)(function () { | ||
return _Collection2.default.find('tests', _testSchema2.default, { is: 'find-conditions' }, { foo: 'bar' }); | ||
return _Collection2.default.find.apply(_Collection2.default, stdArgs.concat([{ is: 'find-conditions' }, { foo: 'bar' }])); | ||
}).to.throw(Error); | ||
(0, _chai.expect)(function () { | ||
return _Collection2.default.find('tests', _testSchema2.default, { is: 'find-conditions' }, { limit: 'abc' }); | ||
return _Collection2.default.find.apply(_Collection2.default, stdArgs.concat([{ is: 'find-conditions' }, { limit: 'abc' }])); | ||
}).to.throw(Error); | ||
(0, _chai.expect)(function () { | ||
return _Collection2.default.find('tests', _testSchema2.default, { is: 'find-conditions' }, { sort: 'foo' }); | ||
return _Collection2.default.find.apply(_Collection2.default, stdArgs.concat([{ is: 'find-conditions' }, { sort: 'foo' }])); | ||
}).to.throw(Error); | ||
@@ -205,4 +219,5 @@ }); | ||
testCollection.find.reset(); | ||
_Document2.default.fromDocument.reset(); | ||
return _Collection2.default.find('tests', _testSchema2.default, { is: 'find-conditions' }, options).then(function (entities) { | ||
return _Collection2.default.find.apply(_Collection2.default, stdArgs.concat([{ is: 'find-conditions' }, options])).then(function (entities) { | ||
_sinon2.default.assert.calledWith(testCollection.find, { is: 'find-conditions' }, options); | ||
@@ -212,2 +227,5 @@ (0, _chai.expect)(entities).to.deep.equal([_testObj2.default, _testObj2.default]); | ||
(0, _chai.expect)(entities[0]._id).to.be.undefined; | ||
_sinon2.default.assert.calledTwice(_Document2.default.fromDocument); | ||
_sinon2.default.assert.calledWith(_Document2.default.fromDocument, _testDoc2.default, testObjectIdPaths); | ||
}); | ||
@@ -226,4 +244,5 @@ }); | ||
testCollection.findOne.reset(); | ||
_Document2.default.fromDocument.reset(); | ||
return _Collection2.default.findOne('tests', _testSchema2.default, { is: 'findOne-conditions' }).then(function (entity) { | ||
return _Collection2.default.findOne.apply(_Collection2.default, stdArgs.concat([{ is: 'findOne-conditions' }])).then(function (entity) { | ||
_sinon2.default.assert.calledWith(testCollection.findOne, { is: 'findOne-conditions' }); | ||
@@ -233,2 +252,5 @@ (0, _chai.expect)(entity).to.deep.equal(_testObj2.default); | ||
(0, _chai.expect)(entity._id).to.be.undefined; | ||
_sinon2.default.assert.calledOnce(_Document2.default.fromDocument); | ||
_sinon2.default.assert.calledWith(_Document2.default.fromDocument, _testDoc2.default, testObjectIdPaths); | ||
}); | ||
@@ -247,6 +269,7 @@ }); | ||
testCollection.find.reset(); | ||
_Document2.default.fromDocument.reset(); | ||
return _Collection2.default.findByIds('tests', _testSchema2.default, [_testObj2.default.id, _testObj2.default.id], options).then(function (entities) { | ||
return _Collection2.default.findByIds.apply(_Collection2.default, stdArgs.concat([[_testObj2.default.id, _testObj2.default.id], options])).then(function (entities) { | ||
_sinon2.default.assert.calledWith(testCollection.find, { | ||
_id: { $in: [(0, _mongodb.ObjectId)(_testObj2.default.id), (0, _mongodb.ObjectId)(_testObj2.default.id)] } | ||
_id: { $in: [_monk2.default.id(_testObj2.default.id), _monk2.default.id(_testObj2.default.id)] } | ||
}); | ||
@@ -256,2 +279,5 @@ (0, _chai.expect)(entities).to.deep.equal([_testObj2.default, _testObj2.default]); | ||
(0, _chai.expect)(entities[0]._id).to.be.undefined; | ||
_sinon2.default.assert.calledTwice(_Document2.default.fromDocument); | ||
_sinon2.default.assert.calledWith(_Document2.default.fromDocument, _testDoc2.default, testObjectIdPaths); | ||
}); | ||
@@ -270,6 +296,7 @@ }); | ||
testCollection.findOne.reset(); | ||
_Document2.default.fromDocument.reset(); | ||
return _Collection2.default.findById('tests', _testSchema2.default, _testObj2.default.id).then(function (entity) { | ||
return _Collection2.default.findById.apply(_Collection2.default, stdArgs.concat([_testObj2.default.id])).then(function (entity) { | ||
_sinon2.default.assert.calledWith(testCollection.findOne, { | ||
_id: (0, _mongodb.ObjectId)(_testObj2.default.id) | ||
_id: _monk2.default.id(_testObj2.default.id) | ||
}); | ||
@@ -279,2 +306,5 @@ (0, _chai.expect)(entity).to.deep.equal(_testObj2.default); | ||
(0, _chai.expect)(entity._id).to.be.undefined; | ||
_sinon2.default.assert.calledOnce(_Document2.default.fromDocument); | ||
_sinon2.default.assert.calledWith(_Document2.default.fromDocument, _testDoc2.default, testObjectIdPaths); | ||
}); | ||
@@ -287,3 +317,3 @@ }); | ||
(0, _chai.expect)(function () { | ||
return _Collection2.default.insertOne('tests', _testSchema2.default, invalidPropsWithId); | ||
return _Collection2.default.insertOne.apply(_Collection2.default, stdArgs.concat([invalidPropsWithId])); | ||
}).to.throw('Character insertOne properties is invalid.'); | ||
@@ -300,3 +330,3 @@ }); | ||
return _Collection2.default.insertOne('tests', _testSchema2.default, propsWithUndefinedProp).then(function (entity) { | ||
return _Collection2.default.insertOne.apply(_Collection2.default, stdArgs.concat([propsWithUndefinedProp])).then(function (entity) { | ||
var queryInsert = testCollection.insert.args[0][0]; | ||
@@ -310,6 +340,12 @@ (0, _chai.expect)(queryInsert.name).to.equal('Hello'); | ||
testCollection.insert.reset(); | ||
_Document2.default.fromDocument.reset(); | ||
_Document2.default.toDocument.reset(); | ||
return _Collection2.default.insertOne('tests', _testSchema2.default, testObjWithoutId).then(function (entity) { | ||
return _Collection2.default.insertOne.apply(_Collection2.default, stdArgs.concat([testObjWithoutId])).then(function (entity) { | ||
_sinon2.default.assert.calledWith(testCollection.insert, testObjWithoutId); | ||
(0, _chai.expect)(entity).to.deep.equal(_testObj2.default); | ||
_sinon2.default.assert.calledOnce(_Document2.default.fromDocument); | ||
_sinon2.default.assert.calledWith(_Document2.default.fromDocument, _testDoc2.default, testObjectIdPaths); | ||
_sinon2.default.assert.calledOnce(_Document2.default.toDocument); | ||
_sinon2.default.assert.calledWith(_Document2.default.toDocument, testObjWithoutId, testObjectIdPaths); | ||
}); | ||
@@ -322,3 +358,3 @@ }); | ||
(0, _chai.expect)(function () { | ||
return _Collection2.default.updateOne('tests', _testSchema2.default, invalidProps); | ||
return _Collection2.default.updateOne.apply(_Collection2.default, stdArgs.concat([invalidProps])); | ||
}).to.throw('updateOne require id property.'); | ||
@@ -328,3 +364,3 @@ }); | ||
(0, _chai.expect)(function () { | ||
return _Collection2.default.updateOne('tests', _testSchema2.default, invalidPropsWithId); | ||
return _Collection2.default.updateOne.apply(_Collection2.default, stdArgs.concat([invalidPropsWithId])); | ||
}).to.throw('Character updateOne properties is invalid.'); | ||
@@ -341,3 +377,3 @@ }); | ||
return _Collection2.default.updateOne('tests', _testSchema2.default, propsWithUndefinedProp).then(function (entity) { | ||
return _Collection2.default.updateOne.apply(_Collection2.default, stdArgs.concat([propsWithUndefinedProp])).then(function (entity) { | ||
var queryUpdate = testCollection.update.args[0][1]; | ||
@@ -352,4 +388,4 @@ (0, _chai.expect)(queryUpdate.$set.name).to.equal('Hello'); | ||
return _Collection2.default.updateOne('tests', _testSchema2.default, updatePropsWithId).then(function (entity) { | ||
_sinon2.default.assert.calledWith(testCollection.update, { _id: (0, _mongodb.ObjectId)(updatePropsWithId.id) }, { | ||
return _Collection2.default.updateOne.apply(_Collection2.default, stdArgs.concat([updatePropsWithId])).then(function (entity) { | ||
_sinon2.default.assert.calledWith(testCollection.update, { _id: _monk2.default.id(updatePropsWithId.id) }, { | ||
$set: updateProps | ||
@@ -365,3 +401,3 @@ }); | ||
(0, _chai.expect)(function () { | ||
return _Collection2.default.replaceOne('tests', _testSchema2.default, invalidProps); | ||
return _Collection2.default.replaceOne.apply(_Collection2.default, stdArgs.concat([invalidProps])); | ||
}).to.throw('replaceOne require id property.'); | ||
@@ -371,3 +407,3 @@ }); | ||
(0, _chai.expect)(function () { | ||
return _Collection2.default.replaceOne('tests', _testSchema2.default, invalidPropsWithId); | ||
return _Collection2.default.replaceOne.apply(_Collection2.default, stdArgs.concat([invalidPropsWithId])); | ||
}).to.throw('Character replaceOne properties is invalid.'); | ||
@@ -384,3 +420,3 @@ }); | ||
return _Collection2.default.replaceOne('tests', _testSchema2.default, propsWithUndefinedProp).then(function (entity) { | ||
return _Collection2.default.replaceOne.apply(_Collection2.default, stdArgs.concat([propsWithUndefinedProp])).then(function (entity) { | ||
var queryUpdate = testCollection.update.args[0][1]; | ||
@@ -394,6 +430,10 @@ (0, _chai.expect)(queryUpdate.name).to.equal('Hello'); | ||
testCollection.update.reset(); | ||
_Document2.default.toDocument.reset(); | ||
return _Collection2.default.replaceOne('tests', _testSchema2.default, _testObj2.default).then(function (entity) { | ||
_sinon2.default.assert.calledWith(testCollection.update, { _id: (0, _mongodb.ObjectId)(_testObj2.default.id) }, _testDoc2.default); | ||
return _Collection2.default.replaceOne.apply(_Collection2.default, stdArgs.concat([_testObj2.default])).then(function (entity) { | ||
_sinon2.default.assert.calledWith(testCollection.update, { _id: _monk2.default.id(_testObj2.default.id) }, _testDoc2.default); | ||
(0, _chai.expect)(entity).to.deep.equal(_testObj2.default); | ||
_sinon2.default.assert.calledOnce(_Document2.default.toDocument); | ||
_sinon2.default.assert.calledWith(_Document2.default.toDocument, _testObj2.default, testObjectIdPaths); | ||
}); | ||
@@ -415,3 +455,3 @@ }); | ||
(0, _chai.expect)(function () { | ||
return _Collection2.default.upsertOne('tests'); | ||
return _Collection2.default.upsertOne(testColname); | ||
}).to.throw(Error); | ||
@@ -434,3 +474,5 @@ }); | ||
return _Collection2.default.upsertOne('tests', _testSchema2.default, entity, updateProps, insertProps).then(function (res) { | ||
return _Collection2.default.upsertOne.apply(_Collection2.default, stdArgs.concat([entity, updateProps, insertProps])).then(function (res) { | ||
var _sinon$assert; | ||
_sinon2.default.assert.notCalled(_Collection2.default.insertOne); | ||
@@ -442,3 +484,3 @@ var expected = { | ||
}; | ||
_sinon2.default.assert.calledWith(_Collection2.default.updateOne, 'tests', _testSchema2.default, expected); | ||
(_sinon$assert = _sinon2.default.assert).calledWith.apply(_sinon$assert, [_Collection2.default.updateOne].concat(stdArgs, [expected])); | ||
(0, _chai.expect)(res).to.deep.equal(expected); | ||
@@ -452,3 +494,3 @@ }); | ||
return _Collection2.default.upsertOne('tests', _testSchema2.default, entity, {}, {}).then(function (res) { | ||
return _Collection2.default.upsertOne.apply(_Collection2.default, stdArgs.concat([entity, {}, {}])).then(function (res) { | ||
_sinon2.default.assert.notCalled(_Collection2.default.insertOne); | ||
@@ -464,3 +506,5 @@ _sinon2.default.assert.notCalled(_Collection2.default.updateOne); | ||
return _Collection2.default.upsertOne('tests', _testSchema2.default, {}, updateProps, insertProps).then(function (res) { | ||
return _Collection2.default.upsertOne.apply(_Collection2.default, stdArgs.concat([{}, updateProps, insertProps])).then(function (res) { | ||
var _sinon$assert2; | ||
_sinon2.default.assert.notCalled(_Collection2.default.updateOne); | ||
@@ -473,3 +517,3 @@ var expected = { | ||
}; | ||
_sinon2.default.assert.calledWith(_Collection2.default.insertOne, 'tests', _testSchema2.default, expected); | ||
(_sinon$assert2 = _sinon2.default.assert).calledWith.apply(_sinon$assert2, [_Collection2.default.insertOne].concat(stdArgs, [expected])); | ||
(0, _chai.expect)(res).to.deep.equal(expected); | ||
@@ -495,7 +539,9 @@ }); | ||
var hof = _Collection2.default.upsertOneUsing('tests', _testSchema2.default, updateProps, insertProps); | ||
var hof = _Collection2.default.upsertOneUsing.apply(_Collection2.default, stdArgs.concat([updateProps, insertProps])); | ||
_sinon2.default.assert.notCalled(_Collection2.default.upsertOne); | ||
return hof(entity).then(function (res) { | ||
_sinon2.default.assert.calledWith(_Collection2.default.upsertOne, 'tests', _testSchema2.default, entity, updateProps, insertProps); | ||
var _sinon$assert3; | ||
(_sinon$assert3 = _sinon2.default.assert).calledWith.apply(_sinon$assert3, [_Collection2.default.upsertOne].concat(stdArgs, [entity, updateProps, insertProps])); | ||
}); | ||
@@ -508,3 +554,3 @@ }); | ||
(0, _chai.expect)(function () { | ||
return _Collection2.default.deleteOne('tests', _testSchema2.default, updateProps); | ||
return _Collection2.default.deleteOne.apply(_Collection2.default, stdArgs.concat([updateProps])); | ||
}).to.throw('deleteOne require id property.'); | ||
@@ -516,4 +562,4 @@ }); | ||
return _Collection2.default.deleteOne('tests', _testSchema2.default, _testObj2.default).then(function () { | ||
_sinon2.default.assert.calledWith(testCollection.remove, { _id: (0, _mongodb.ObjectId)(_testObj2.default.id) }); | ||
return _Collection2.default.deleteOne.apply(_Collection2.default, stdArgs.concat([_testObj2.default])).then(function () { | ||
_sinon2.default.assert.calledWith(testCollection.remove, { _id: _monk2.default.id(_testObj2.default.id) }); | ||
}); | ||
@@ -527,4 +573,4 @@ }); | ||
return _Collection2.default.deleteById('tests', _testSchema2.default, _testObj2.default.id).then(function () { | ||
_sinon2.default.assert.calledWith(testCollection.remove, { _id: (0, _mongodb.ObjectId)(_testObj2.default.id) }); | ||
return _Collection2.default.deleteById.apply(_Collection2.default, stdArgs.concat([_testObj2.default.id])).then(function () { | ||
_sinon2.default.assert.calledWith(testCollection.remove, { _id: _monk2.default.id(_testObj2.default.id) }); | ||
}); | ||
@@ -538,3 +584,3 @@ }); | ||
return _Collection2.default.aggregate('tests', _testSchema2.default, 'foo1', 'bar1').then(function () { | ||
return _Collection2.default.aggregate.apply(_Collection2.default, stdArgs.concat(['foo1', 'bar1'])).then(function () { | ||
_sinon2.default.assert.calledWith(testCollection.aggregate, 'foo1', 'bar1'); | ||
@@ -549,3 +595,3 @@ }); | ||
return _Collection2.default.count('tests', _testSchema2.default, 'foo2', 'bar2').then(function () { | ||
return _Collection2.default.count.apply(_Collection2.default, stdArgs.concat(['foo2', 'bar2'])).then(function () { | ||
_sinon2.default.assert.calledWith(testCollection.count, 'foo2', 'bar2'); | ||
@@ -560,3 +606,3 @@ }); | ||
return _Collection2.default.distinct('tests', _testSchema2.default, 'foo3', 'bar3').then(function () { | ||
return _Collection2.default.distinct.apply(_Collection2.default, stdArgs.concat(['foo3', 'bar3'])).then(function () { | ||
_sinon2.default.assert.calledWith(testCollection.distinct, 'foo3', 'bar3'); | ||
@@ -589,3 +635,3 @@ }); | ||
it('should provide all the entity database methods, and they should ', function () { | ||
funcs = _Collection2.default.all('test', _testSchema2.default); | ||
funcs = _Collection2.default.all.apply(_Collection2.default, stdArgs); | ||
@@ -599,5 +645,7 @@ (0, _chai.expect)(funcs).to.be.an('object'); | ||
funcNames.forEach(function (funcName) { | ||
var _sinon$assert4; | ||
_Collection2.default[funcName].reset(); | ||
funcs[funcName](arg1, arg2, arg3, arg4, arg5); | ||
_sinon2.default.assert.calledWith(_Collection2.default[funcName], 'test', _testSchema2.default, arg1, arg2, arg3, arg4, arg5); | ||
(_sinon$assert4 = _sinon2.default.assert).calledWith.apply(_sinon$assert4, [_Collection2.default[funcName]].concat(stdArgs, [arg1, arg2, arg3, arg4, arg5])); | ||
}); | ||
@@ -604,0 +652,0 @@ }); |
@@ -12,4 +12,2 @@ 'use strict'; | ||
require('./polyfills'); | ||
var _omitBy2 = require('lodash/fp/omitBy'); | ||
@@ -27,4 +25,6 @@ | ||
var _mongodb = require('mongodb'); | ||
var _monk = require('monk'); | ||
var _monk2 = _interopRequireDefault(_monk); | ||
var _jsft = require('jsft'); | ||
@@ -38,2 +38,6 @@ | ||
var _Document = require('./Document'); | ||
var _Document2 = _interopRequireDefault(_Document); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
@@ -93,32 +97,2 @@ | ||
/** | ||
* Make the id entity-structured | ||
* @param {object} doc Database document | ||
* @return {object} Entity object | ||
*/ | ||
function fromDocument(doc) { | ||
var obj = _extends({ | ||
id: doc._id.toString() | ||
}, doc); | ||
delete obj._id; | ||
delete obj.__v; // Temporary, cleanup from mongoose usage | ||
return obj; | ||
} | ||
/** | ||
* Make the id dbDocument-structured | ||
* @param {object} obj Entity object | ||
* @return {object} Database document | ||
*/ | ||
function toDocument(obj) { | ||
var doc = _extends({}, obj); | ||
if (obj.id) { | ||
doc._id = (0, _mongodb.ObjectId)(obj.id); | ||
} | ||
delete doc.id; | ||
return doc; | ||
} | ||
// Document i/o functions | ||
@@ -130,2 +104,3 @@ | ||
* @param {object} schema Document json-schema definition | ||
* @param {array} objectIdPaths Paths (strings) to document properties that is stored as ObjectId | ||
* @param {object} query Query conditions (mongodb query structure) | ||
@@ -136,4 +111,5 @@ * @param {object} [options] Query options | ||
Collection.find = function (collectionName, schema) { | ||
var query = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {}; | ||
var options = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : undefined; | ||
var objectIdPaths = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : []; | ||
var query = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {}; | ||
var options = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : undefined; | ||
@@ -149,3 +125,3 @@ assertCollectionName(collectionName); | ||
return (docs || []).map(function (doc) { | ||
return fromDocument(doc); | ||
return _Document2.default.fromDocument(doc, objectIdPaths); | ||
}); | ||
@@ -159,2 +135,3 @@ }); | ||
* @param {object} schema Document json-schema definition | ||
* @param {array} objectIdPaths Paths (strings) to document properties that is stored as ObjectId | ||
* @param {object} query Query conditions (mongodb query structure) | ||
@@ -165,4 +142,5 @@ * @param {object} [options] Query options | ||
Collection.findOne = function (collectionName, schema) { | ||
var query = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {}; | ||
var options = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : undefined; | ||
var objectIdPaths = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : []; | ||
var query = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {}; | ||
var options = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : undefined; | ||
@@ -177,3 +155,3 @@ assertCollectionName(collectionName); | ||
return _Link2.default.getCollection(collectionName).findOne(query, options).then(function (doc) { | ||
return doc ? fromDocument(doc) : undefined; | ||
return doc ? _Document2.default.fromDocument(doc, objectIdPaths) : undefined; | ||
}); | ||
@@ -186,2 +164,3 @@ }; | ||
* @param {object} schema Document json-schema definition | ||
* @param {array} objectIdPaths Paths (strings) to document properties that is stored as ObjectId | ||
* @param {array} ids Entity ids | ||
@@ -191,4 +170,6 @@ * @param {object} [options] Query options | ||
*/ | ||
Collection.findByIds = function (collectionName, schema, ids) { | ||
var options = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : undefined; | ||
Collection.findByIds = function (collectionName, schema) { | ||
var objectIdPaths = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : []; | ||
var ids = arguments[3]; | ||
var options = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : undefined; | ||
@@ -204,7 +185,7 @@ assertCollectionName(collectionName); | ||
_id: { $in: ids.map(function (id) { | ||
return (0, _mongodb.ObjectId)(id); | ||
return _monk2.default.id(id); | ||
}) } | ||
}, options).then(function (docs) { | ||
return (docs || []).map(function (doc) { | ||
return fromDocument(doc); | ||
return _Document2.default.fromDocument(doc, objectIdPaths); | ||
}); | ||
@@ -218,2 +199,3 @@ }); | ||
* @param {object} schema Document json-schema definition | ||
* @param {array} objectIdPaths Paths (strings) to document properties that is stored as ObjectId | ||
* @param {string} id Entity id | ||
@@ -223,4 +205,6 @@ * @param {object} [options] Query options | ||
*/ | ||
Collection.findById = function (collectionName, schema, id) { | ||
var options = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : undefined; | ||
Collection.findById = function (collectionName, schema) { | ||
var objectIdPaths = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : []; | ||
var id = arguments[3]; | ||
var options = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : undefined; | ||
@@ -230,4 +214,4 @@ assertCollectionName(collectionName); | ||
return _Link2.default.getCollection(collectionName).findOne({ _id: (0, _mongodb.ObjectId)(id) }, options).then(function (doc) { | ||
return doc ? fromDocument(doc) : undefined; | ||
return _Link2.default.getCollection(collectionName).findOne({ _id: _monk2.default.id(id) }, options).then(function (doc) { | ||
return doc ? _Document2.default.fromDocument(doc, objectIdPaths) : undefined; | ||
}); | ||
@@ -240,2 +224,3 @@ }; | ||
* @param {object} schema Document json-schema definition | ||
* @param {array} objectIdPaths Paths (strings) to document properties that is stored as ObjectId | ||
* @param {object} props Entity properties | ||
@@ -245,3 +230,4 @@ * @return {Promise} | ||
Collection.insertOne = function (collectionName, schema) { | ||
var props = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {}; | ||
var objectIdPaths = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : []; | ||
var props = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {}; | ||
@@ -252,2 +238,3 @@ assertCollectionName(collectionName); | ||
var cleanProps = omitUndefined(props); | ||
// const cleanProps = props; | ||
@@ -263,3 +250,3 @@ _jsft2.default.assertValid(schema, _extends({}, cleanProps, { | ||
return new Promise(function (resolve, reject) { | ||
collection.insert(toDocument(cleanProps), function (err, doc) { | ||
collection.insert(_Document2.default.toDocument(cleanProps, objectIdPaths), function (err, doc) { | ||
if (err) { | ||
@@ -270,3 +257,3 @@ reject(err); | ||
resolve(doc ? fromDocument(doc) : undefined); | ||
resolve(doc ? _Document2.default.fromDocument(doc, objectIdPaths) : undefined); | ||
}); | ||
@@ -280,2 +267,3 @@ }); | ||
* @param {object} schema Document json-schema definition | ||
* @param {array} objectIdPaths Paths (strings) to document properties that is stored as ObjectId | ||
* @param {object} props Entity properties | ||
@@ -285,3 +273,4 @@ * @return {Promise} | ||
Collection.updateOne = function (collectionName, schema) { | ||
var props = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {}; | ||
var objectIdPaths = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : []; | ||
var props = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {}; | ||
@@ -301,3 +290,3 @@ assertCollectionName(collectionName); | ||
var query = { _id: (0, _mongodb.ObjectId)(cleanProps.id) }; | ||
var query = { _id: _monk2.default.id(cleanProps.id) }; | ||
var updates = { | ||
@@ -310,3 +299,3 @@ $set: omitUndefined(_extends({}, cleanProps, { | ||
return _Link2.default.getCollection(collectionName).update(query, updates).then(function () { | ||
return Collection.findOne(collectionName, schema, query); | ||
return Collection.findOne(collectionName, schema, objectIdPaths, query); | ||
}); | ||
@@ -319,2 +308,3 @@ }; | ||
* @param {object} schema Document json-schema definition | ||
* @param {array} objectIdPaths Paths (strings) to document properties that is stored as ObjectId | ||
* @param {object} props Entity properties | ||
@@ -324,3 +314,4 @@ * @return {Promise} | ||
Collection.replaceOne = function (collectionName, schema) { | ||
var props = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {}; | ||
var objectIdPaths = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : []; | ||
var props = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {}; | ||
@@ -339,6 +330,6 @@ assertCollectionName(collectionName); | ||
var query = { _id: (0, _mongodb.ObjectId)(cleanProps.id) }; | ||
var query = { _id: _monk2.default.id(cleanProps.id) }; | ||
return _Link2.default.getCollection(collectionName).update(query, toDocument(cleanProps)).then(function () { | ||
return Collection.findOne(collectionName, schema, query); | ||
return _Link2.default.getCollection(collectionName).update(query, _Document2.default.toDocument(cleanProps, objectIdPaths)).then(function () { | ||
return Collection.findOne(collectionName, schema, objectIdPaths, query); | ||
}); | ||
@@ -351,2 +342,3 @@ }; | ||
* @param {object} schema Document json-schema definition | ||
* @param {array} objectIdPaths Paths (strings) to document properties that is stored as ObjectId | ||
* @param {object} [entity] Existing entity to update | ||
@@ -358,6 +350,7 @@ * @param {object} [updateProps] Properties to set (used for both update and insert) | ||
Collection.upsertOne = function (collectionName, schema) { | ||
var entity = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {}; | ||
var updateProps = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {}; | ||
var insertOnlyProps = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : {}; | ||
var objectIdPaths = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : []; | ||
var entity = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {}; | ||
var updateProps = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : {}; | ||
var insertOnlyProps = arguments.length > 5 && arguments[5] !== undefined ? arguments[5] : {}; | ||
// eslint-disable-line max-len | ||
assertCollectionName(collectionName); | ||
@@ -372,6 +365,6 @@ assertSchema(schema); | ||
} | ||
return Collection.updateOne(collectionName, schema, _extends({}, updateProps, { id: entity.id })); | ||
return Collection.updateOne(collectionName, schema, objectIdPaths, _extends({}, updateProps, { id: entity.id })); | ||
} else { | ||
// Insert new entity | ||
return Collection.insertOne(collectionName, schema, _extends({}, insertOnlyProps, updateProps)); | ||
return Collection.insertOne(collectionName, schema, objectIdPaths, _extends({}, insertOnlyProps, updateProps)); | ||
} | ||
@@ -384,2 +377,3 @@ }; | ||
* @param {object} schema Document json-schema definition | ||
* @param {array} objectIdPaths Paths (strings) to document properties that is stored as ObjectId | ||
* @param {object} [updateProps] Properties to set (used for both update and insert) | ||
@@ -390,9 +384,10 @@ * @param {object} [insertOnlyProps] Properties to set only on insert operation | ||
Collection.upsertOneUsing = function (collectionName, schema) { | ||
var updateProps = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {}; | ||
var insertOnlyProps = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {}; | ||
var objectIdPaths = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : []; | ||
var updateProps = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {}; | ||
var insertOnlyProps = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : {}; | ||
return function () { | ||
var entity = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; | ||
return Collection.upsertOne(collectionName, schema, entity, updateProps, insertOnlyProps); | ||
}; | ||
return Collection.upsertOne(collectionName, schema, objectIdPaths, entity, updateProps, insertOnlyProps); | ||
}; // eslint-disable-line max-len | ||
}; | ||
@@ -404,2 +399,3 @@ | ||
* @param {object} schema Document json-schema definition | ||
* @param {array} objectIdPaths Paths (strings) to document properties that is stored as ObjectId | ||
* @param {object} props Entity properties | ||
@@ -409,3 +405,4 @@ * @return {Promise} | ||
Collection.deleteOne = function (collectionName, schema) { | ||
var props = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {}; | ||
var objectIdPaths = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : []; | ||
var props = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {}; | ||
@@ -419,3 +416,3 @@ assertCollectionName(collectionName); | ||
return _Link2.default.getCollection(collectionName).remove({ _id: (0, _mongodb.ObjectId)(props.id) }); | ||
return _Link2.default.getCollection(collectionName).remove({ _id: _monk2.default.id(props.id) }); | ||
}; | ||
@@ -427,10 +424,14 @@ | ||
* @param {object} schema Document json-schema definition | ||
* @param {array} objectIdPaths Paths (strings) to document properties that is stored as ObjectId | ||
* @param {string} id Entity id | ||
* @return {Promise} | ||
*/ | ||
Collection.deleteById = function (collectionName, schema, id) { | ||
Collection.deleteById = function (collectionName, schema) { | ||
var objectIdPaths = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : []; | ||
var id = arguments[3]; | ||
assertCollectionName(collectionName); | ||
assertSchema(schema); | ||
return _Link2.default.getCollection(collectionName).remove({ _id: (0, _mongodb.ObjectId)(id) }); | ||
return _Link2.default.getCollection(collectionName).remove({ _id: _monk2.default.id(id) }); | ||
}; | ||
@@ -444,2 +445,3 @@ | ||
* @param {object} schema Document json-schema definition | ||
* @param {array} objectIdPaths Paths (strings) to document properties that is stored as ObjectId | ||
* @param {mixed} args Aggregate function arguments | ||
@@ -449,6 +451,8 @@ * @return {Promise} | ||
Collection.aggregate = function (collectionName, schema) { | ||
for (var _len = arguments.length, args = Array(_len > 2 ? _len - 2 : 0), _key = 2; _key < _len; _key++) { | ||
args[_key - 2] = arguments[_key]; | ||
for (var _len = arguments.length, args = Array(_len > 3 ? _len - 3 : 0), _key = 3; _key < _len; _key++) { | ||
args[_key - 3] = arguments[_key]; | ||
} | ||
var objectIdPaths = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : []; | ||
assertCollectionName(collectionName); | ||
@@ -465,2 +469,3 @@ | ||
* @param {object} schema Document json-schema definition | ||
* @param {array} objectIdPaths Paths (strings) to document properties that is stored as ObjectId | ||
* @param {mixed} args Count function arguments | ||
@@ -470,6 +475,8 @@ * @return {Promise} | ||
Collection.count = function (collectionName, schema) { | ||
for (var _len2 = arguments.length, args = Array(_len2 > 2 ? _len2 - 2 : 0), _key2 = 2; _key2 < _len2; _key2++) { | ||
args[_key2 - 2] = arguments[_key2]; | ||
for (var _len2 = arguments.length, args = Array(_len2 > 3 ? _len2 - 3 : 0), _key2 = 3; _key2 < _len2; _key2++) { | ||
args[_key2 - 3] = arguments[_key2]; | ||
} | ||
var objectIdPaths = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : []; | ||
assertCollectionName(collectionName); | ||
@@ -486,2 +493,3 @@ | ||
* @param {object} schema Document json-schema definition | ||
* @param {array} objectIdPaths Paths (strings) to document properties that is stored as ObjectId | ||
* @param {mixed} args Distincs function arguments | ||
@@ -491,6 +499,8 @@ * @return {Promise} | ||
Collection.distinct = function (collectionName, schema) { | ||
for (var _len3 = arguments.length, args = Array(_len3 > 2 ? _len3 - 2 : 0), _key3 = 2; _key3 < _len3; _key3++) { | ||
args[_key3 - 2] = arguments[_key3]; | ||
for (var _len3 = arguments.length, args = Array(_len3 > 3 ? _len3 - 3 : 0), _key3 = 3; _key3 < _len3; _key3++) { | ||
args[_key3 - 3] = arguments[_key3]; | ||
} | ||
var objectIdPaths = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : []; | ||
assertCollectionName(collectionName); | ||
@@ -507,5 +517,8 @@ | ||
* @param {object} schema JSON-Schema for the given document type | ||
* @param {array} objectIdPaths Paths (strings) to document properties that is stored as ObjectId | ||
* @return {object} Literal containing the db-functions | ||
*/ | ||
Collection.all = function (collectionName, schema) { | ||
var objectIdPaths = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : []; | ||
assertCollectionName(collectionName); | ||
@@ -515,16 +528,16 @@ assertSchema(schema); | ||
return { | ||
find: (0, _partial3.default)(Collection.find, collectionName, schema), | ||
findOne: (0, _partial3.default)(Collection.findOne, collectionName, schema), | ||
findByIds: (0, _partial3.default)(Collection.findByIds, collectionName, schema), | ||
findById: (0, _partial3.default)(Collection.findById, collectionName, schema), | ||
insertOne: (0, _partial3.default)(Collection.insertOne, collectionName, schema), | ||
updateOne: (0, _partial3.default)(Collection.updateOne, collectionName, schema), | ||
replaceOne: (0, _partial3.default)(Collection.replaceOne, collectionName, schema), | ||
upsertOne: (0, _partial3.default)(Collection.upsertOne, collectionName, schema), | ||
upsertOneUsing: (0, _partial3.default)(Collection.upsertOneUsing, collectionName, schema), | ||
deleteOne: (0, _partial3.default)(Collection.deleteOne, collectionName, schema), | ||
deleteById: (0, _partial3.default)(Collection.deleteById, collectionName, schema), | ||
aggregate: (0, _partial3.default)(Collection.aggregate, collectionName, schema), | ||
count: (0, _partial3.default)(Collection.count, collectionName, schema), | ||
distinct: (0, _partial3.default)(Collection.distinct, collectionName, schema) | ||
find: (0, _partial3.default)(Collection.find, collectionName, schema, objectIdPaths), | ||
findOne: (0, _partial3.default)(Collection.findOne, collectionName, schema, objectIdPaths), | ||
findByIds: (0, _partial3.default)(Collection.findByIds, collectionName, schema, objectIdPaths), | ||
findById: (0, _partial3.default)(Collection.findById, collectionName, schema, objectIdPaths), | ||
insertOne: (0, _partial3.default)(Collection.insertOne, collectionName, schema, objectIdPaths), | ||
updateOne: (0, _partial3.default)(Collection.updateOne, collectionName, schema, objectIdPaths), | ||
replaceOne: (0, _partial3.default)(Collection.replaceOne, collectionName, schema, objectIdPaths), | ||
upsertOne: (0, _partial3.default)(Collection.upsertOne, collectionName, schema, objectIdPaths), | ||
upsertOneUsing: (0, _partial3.default)(Collection.upsertOneUsing, collectionName, schema, objectIdPaths), | ||
deleteOne: (0, _partial3.default)(Collection.deleteOne, collectionName, schema, objectIdPaths), | ||
deleteById: (0, _partial3.default)(Collection.deleteById, collectionName, schema, objectIdPaths), | ||
aggregate: (0, _partial3.default)(Collection.aggregate, collectionName, schema, objectIdPaths), | ||
count: (0, _partial3.default)(Collection.count, collectionName, schema, objectIdPaths), | ||
distinct: (0, _partial3.default)(Collection.distinct, collectionName, schema, objectIdPaths) | ||
}; | ||
@@ -531,0 +544,0 @@ }; |
{ | ||
"name": "jsft-mongodb", | ||
"version": "0.7.0", | ||
"version": "0.8.0", | ||
"description": "Functional abstraction layer for mongodb-storage of objects with structures defined with json-schema", | ||
@@ -30,6 +30,8 @@ "main": "lib/index.js", | ||
"dependencies": { | ||
"core-js": "^2.4.1", | ||
"jsft": "^0.2.1", | ||
"lodash": "^4.17.4", | ||
"mongodb": "^2.2.22", | ||
"monk": "^4.0.0" | ||
"monk": "^4.0.0", | ||
"object-path": "^0.11.4", | ||
"object-path-immutable": "^0.5.1" | ||
}, | ||
@@ -36,0 +38,0 @@ "devDependencies": { |
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
61673
13
1185
6
+ Addedcore-js@^2.4.1
+ Addedobject-path@^0.11.4
+ Addedobject-path-immutable@^0.5.1
+ Addedcore-js@2.6.12(transitive)
+ Addedobject-path@0.11.8(transitive)
+ Addedobject-path-immutable@0.5.3(transitive)
- Removedmongodb@^2.2.22