jsft-mongodb
Advanced tools
Comparing version 0.4.1 to 0.5.0
@@ -6,4 +6,2 @@ 'use strict'; | ||
require('./stubs'); | ||
var _omitBy2 = require('lodash/fp/omitBy'); | ||
@@ -51,8 +49,91 @@ | ||
var mockCollection = { | ||
find: { | ||
mock: function mock(res) { | ||
return _sinon2.default.stub(_Collection2.default, 'find', function () { | ||
return Promise.resolve(res); | ||
}); | ||
}, | ||
unmock: function unmock() { | ||
return _Collection2.default.find.restore(); | ||
} | ||
}, | ||
insertOne: { | ||
mock: function mock() { | ||
return _sinon2.default.stub(_Collection2.default, 'insertOne', function (col, sch, props) { | ||
return Promise.resolve(props); | ||
}); | ||
}, | ||
unmock: function unmock() { | ||
return _Collection2.default.insertOne.restore(); | ||
} | ||
}, | ||
updateOne: { | ||
mock: function mock() { | ||
return _sinon2.default.stub(_Collection2.default, 'updateOne', function (col, sch, props) { | ||
return Promise.resolve(props); | ||
}); | ||
}, | ||
unmock: function unmock() { | ||
return _Collection2.default.updateOne.restore(); | ||
} | ||
}, | ||
replaceOne: { | ||
mock: function mock() { | ||
return _sinon2.default.stub(_Collection2.default, 'replaceOne', function (col, sch, props) { | ||
return Promise.resolve(props); | ||
}); | ||
}, | ||
unmock: function unmock() { | ||
return _Collection2.default.replaceOne.restore(); | ||
} | ||
} | ||
}; | ||
describe('Collection', function () { | ||
var testCollection = _Link2.default.getCollection('test'); | ||
var testCollection = void 0; | ||
var conditions = { | ||
name: _testObj2.default.name | ||
}; | ||
before(function () { | ||
testCollection = { | ||
find: _sinon2.default.spy(function () { | ||
return Promise.resolve([_testDoc2.default, _testDoc2.default]); | ||
}), | ||
findOne: _sinon2.default.spy(function () { | ||
return Promise.resolve(_testDoc2.default); | ||
}), | ||
findOneAndUpdate: _sinon2.default.spy(function () { | ||
return Promise.resolve(_testDoc2.default); | ||
}), | ||
update: _sinon2.default.spy(function () { | ||
return Promise.resolve(); | ||
}), | ||
//insert: sinon.spy(() => Promise.resolve()), | ||
insert: _sinon2.default.spy(function (props, callback) { | ||
callback(null, _testDoc2.default); | ||
}), | ||
remove: _sinon2.default.spy(function () { | ||
return Promise.resolve(); | ||
}), | ||
aggregate: _sinon2.default.spy(function () { | ||
return Promise.resolve(); | ||
}), | ||
count: _sinon2.default.spy(function () { | ||
return Promise.resolve(); | ||
}), | ||
distinct: _sinon2.default.spy(function () { | ||
return Promise.resolve(); | ||
}) | ||
}; | ||
_sinon2.default.stub(_Link2.default, 'getCollection', function () { | ||
return testCollection; | ||
}); | ||
}); | ||
after(function () { | ||
_Link2.default.getCollection.restore(); | ||
}); | ||
var testObjWithoutId = omitUndefined(_extends({}, _testObj2.default, { | ||
id: undefined | ||
})); | ||
var invalidProps = { | ||
@@ -66,13 +147,7 @@ foo: 'BAR' | ||
var updateProps = { | ||
name: 'Dead Stark' | ||
nickname: 'Dead Stark' | ||
}; | ||
var insertProps = { | ||
nickname: 'Handy' | ||
}; | ||
var insertPropsFull = omitUndefined(_extends({}, _testObj2.default, { | ||
id: undefined | ||
})); | ||
var updatePropsWithId = { | ||
id: _testObj2.default.id, | ||
name: 'Dead Stark' | ||
nickname: 'Dead Stark' | ||
}; | ||
@@ -175,32 +250,32 @@ var options = { | ||
describe('.upsertOne()', function () { | ||
it('should fail for invalid update props', function () { | ||
describe('.insertOne()', function () { | ||
it('should fail for invalid props', function () { | ||
(0, _chai.expect)(function () { | ||
return _Collection2.default.upsertOne('tests', _testSchema2.default, conditions, invalidProps); | ||
return _Collection2.default.insertOne('tests', _testSchema2.default, invalidPropsWithId); | ||
}).to.throw('Invalid Character'); | ||
}); | ||
it('should pass for valid update props', function () { | ||
_Collection2.default.upsertOne('test', _testSchema2.default, conditions, updateProps); | ||
it('should request db and return entity objects', function () { | ||
testCollection.insert.reset(); | ||
return _Collection2.default.insertOne('tests', _testSchema2.default, testObjWithoutId).then(function (entity) { | ||
_sinon2.default.assert.calledWith(testCollection.insert, testObjWithoutId); | ||
(0, _chai.expect)(entity).to.deep.equal(_testObj2.default); | ||
}); | ||
}); | ||
}); | ||
it('should fail for invalid insert props', function () { | ||
describe('.updateBy', function () { | ||
it('should fail for invalid props', function () { | ||
(0, _chai.expect)(function () { | ||
return _Collection2.default.upsertOne('tests', _testSchema2.default, conditions, updateProps, invalidProps); | ||
}).to.throw('Invalid Character'); | ||
return _Collection2.default.updateBy('tests'); | ||
}).to.throw(Error); | ||
}); | ||
it('should pass valid insert props', function () { | ||
_Collection2.default.upsertOne('tests', _testSchema2.default, conditions, updateProps, insertProps); | ||
}); | ||
it('should request db and return entity objects', function () { | ||
testCollection.findOneAndUpdate.reset(); | ||
testCollection.update.reset(); | ||
return _Collection2.default.upsertOne('tests', _testSchema2.default, conditions, updateProps, insertProps).then(function (entity) { | ||
_sinon2.default.assert.calledWith(testCollection.findOneAndUpdate, conditions, { | ||
$set: updateProps, | ||
$setOnInsert: insertProps | ||
}); | ||
(0, _chai.expect)(entity).to.deep.equal(_testObj2.default); | ||
return _Collection2.default.updateBy('tests', _testSchema2.default, { is: 'update-conditions' }).then(function (entities) { | ||
_sinon2.default.assert.calledWith(testCollection.update, { is: 'update-conditions' }); | ||
(0, _chai.expect)(entities).to.deep.equal([_testObj2.default, _testObj2.default]); | ||
}); | ||
@@ -260,19 +335,102 @@ }); | ||
describe('.insertOne()', function () { | ||
describe('.upsertBy', function () { | ||
before(function () { | ||
mockCollection.replaceOne.mock(); | ||
mockCollection.insertOne.mock(); | ||
}); | ||
after(function () { | ||
mockCollection.replaceOne.unmock(); | ||
mockCollection.insertOne.unmock(); | ||
}); | ||
var conditions = { is: 'upsertBy-conditions' }; | ||
var callback = function callback(props) { | ||
return _extends({}, props, { name: 'Someone unknown' }); | ||
}; | ||
it('should fail for invalid props', function () { | ||
(0, _chai.expect)(function () { | ||
return _Collection2.default.insertOne('tests', _testSchema2.default, invalidPropsWithId); | ||
}).to.throw('Invalid Character'); | ||
return _Collection2.default.upsertBy('tests'); | ||
}).to.throw(Error); | ||
}); | ||
it('should request db and return entity objects', function () { | ||
testCollection.insert.reset(); | ||
describe('for conditions with result entities', function () { | ||
before(function () { | ||
mockCollection.find.mock([_testObj2.default, _testObj2.default]); | ||
}); | ||
after(function () { | ||
mockCollection.find.unmock(); | ||
}); | ||
return _Collection2.default.insertOne('tests', _testSchema2.default, insertPropsFull).then(function (entity) { | ||
_sinon2.default.assert.calledWith(testCollection.insert, insertPropsFull); | ||
(0, _chai.expect)(entity).to.deep.equal(_testObj2.default); | ||
it('should fetch from db and update entities by callback return entity (update)', function () { | ||
_Collection2.default.find.reset(); | ||
_Collection2.default.replaceOne.reset(); | ||
_Collection2.default.insertOne.reset(); | ||
return _Collection2.default.upsertBy('tests', _testSchema2.default, conditions, callback).then(function (entities) { | ||
_sinon2.default.assert.calledWith(_Collection2.default.find, 'tests', _testSchema2.default, conditions); | ||
_sinon2.default.assert.notCalled(_Collection2.default.insertOne); | ||
_sinon2.default.assert.calledTwice(_Collection2.default.replaceOne); | ||
_sinon2.default.assert.calledWith(_Collection2.default.replaceOne, 'tests', _testSchema2.default, _extends({}, _testObj2.default, { name: 'Someone unknown' })); | ||
(0, _chai.expect)(entities).to.deep.equal([_extends({}, _testObj2.default, { name: 'Someone unknown' }), _extends({}, _testObj2.default, { name: 'Someone unknown' })]); | ||
}); | ||
}); | ||
}); | ||
describe('for conditions with empty result', function () { | ||
before(function () { | ||
mockCollection.find.mock(); | ||
}); | ||
after(function () { | ||
mockCollection.find.unmock(); | ||
}); | ||
it('should insert callback return entity when no entities was found by given conditions (insert)', function () { | ||
_Collection2.default.find.reset(); | ||
_Collection2.default.replaceOne.reset(); | ||
_Collection2.default.insertOne.reset(); | ||
return _Collection2.default.upsertBy('tests', _testSchema2.default, conditions, callback).then(function (entities) { | ||
_sinon2.default.assert.calledWith(_Collection2.default.find, 'tests', _testSchema2.default, conditions); | ||
_sinon2.default.assert.notCalled(_Collection2.default.replaceOne); | ||
_sinon2.default.assert.calledOnce(_Collection2.default.insertOne); | ||
_sinon2.default.assert.calledWith(_Collection2.default.insertOne, 'tests', _testSchema2.default, { name: 'Someone unknown' }); | ||
(0, _chai.expect)(entities).to.deep.equal([{ name: 'Someone unknown' }]); | ||
}); | ||
}); | ||
}); | ||
}); | ||
describe('.upsertOne', function () { | ||
before(function () { | ||
mockCollection.updateOne.mock(); | ||
mockCollection.insertOne.mock(); | ||
}); | ||
after(function () { | ||
mockCollection.updateOne.unmock(); | ||
mockCollection.insertOne.unmock(); | ||
}); | ||
it('should run update for entity with id', function () { | ||
_Collection2.default.updateOne.reset(); | ||
_Collection2.default.insertOne.reset(); | ||
return _Collection2.default.upsertOne('tests', _testSchema2.default, updatePropsWithId).then(function (entity) { | ||
_sinon2.default.assert.notCalled(_Collection2.default.insertOne); | ||
_sinon2.default.assert.calledWith(_Collection2.default.updateOne, 'tests', _testSchema2.default, updatePropsWithId); | ||
}); | ||
}); | ||
it('should run insert for entity without id', function () { | ||
_Collection2.default.updateOne.reset(); | ||
_Collection2.default.insertOne.reset(); | ||
return _Collection2.default.upsertOne('tests', _testSchema2.default, testObjWithoutId).then(function (entity) { | ||
_sinon2.default.assert.notCalled(_Collection2.default.updateOne); | ||
_sinon2.default.assert.calledWith(_Collection2.default.insertOne, 'tests', _testSchema2.default, testObjWithoutId); | ||
}); | ||
}); | ||
}); | ||
describe('.deleteOne()', function () { | ||
@@ -308,3 +466,3 @@ it('should fail for props without id', function () { | ||
return _Collection2.default.aggregate('tests', 'foo1', 'bar1').then(function () { | ||
return _Collection2.default.aggregate('tests', _testSchema2.default, 'foo1', 'bar1').then(function () { | ||
_sinon2.default.assert.calledWith(testCollection.aggregate, 'foo1', 'bar1'); | ||
@@ -319,3 +477,3 @@ }); | ||
return _Collection2.default.count('tests', 'foo2', 'bar2').then(function () { | ||
return _Collection2.default.count('tests', _testSchema2.default, 'foo2', 'bar2').then(function () { | ||
_sinon2.default.assert.calledWith(testCollection.count, 'foo2', 'bar2'); | ||
@@ -330,3 +488,3 @@ }); | ||
return _Collection2.default.distinct('tests', 'foo3', 'bar3').then(function () { | ||
return _Collection2.default.distinct('tests', _testSchema2.default, 'foo3', 'bar3').then(function () { | ||
_sinon2.default.assert.calledWith(testCollection.distinct, 'foo3', 'bar3'); | ||
@@ -345,31 +503,13 @@ }); | ||
var funcNames = ['find', 'findOne', 'findByIds', 'findById', 'insertOne', 'updateBy', 'updateOne', 'replaceOne', 'upsertBy', 'upsertOne', 'deleteOne', 'deleteById', 'aggregate', 'count', 'distinct']; | ||
before(function () { | ||
_sinon2.default.stub(_Collection2.default, 'find'); | ||
_sinon2.default.stub(_Collection2.default, 'findOne'); | ||
_sinon2.default.stub(_Collection2.default, 'findByIds'); | ||
_sinon2.default.stub(_Collection2.default, 'findById'); | ||
_sinon2.default.stub(_Collection2.default, 'upsertOne'); | ||
_sinon2.default.stub(_Collection2.default, 'updateOne'); | ||
_sinon2.default.stub(_Collection2.default, 'replaceOne'); | ||
_sinon2.default.stub(_Collection2.default, 'insertOne'); | ||
_sinon2.default.stub(_Collection2.default, 'deleteOne'); | ||
_sinon2.default.stub(_Collection2.default, 'deleteById'); | ||
_sinon2.default.stub(_Collection2.default, 'aggregate'); | ||
_sinon2.default.stub(_Collection2.default, 'count'); | ||
_sinon2.default.stub(_Collection2.default, 'distinct'); | ||
funcNames.forEach(function (funcName) { | ||
_sinon2.default.stub(_Collection2.default, funcName); | ||
}); | ||
}); | ||
after(function () { | ||
_Collection2.default.find.restore(); | ||
_Collection2.default.findOne.restore(); | ||
_Collection2.default.findByIds.restore(); | ||
_Collection2.default.findById.restore(); | ||
_Collection2.default.upsertOne.restore(); | ||
_Collection2.default.updateOne.restore(); | ||
_Collection2.default.replaceOne.restore(); | ||
_Collection2.default.insertOne.restore(); | ||
_Collection2.default.deleteOne.restore(); | ||
_Collection2.default.deleteById.restore(); | ||
_Collection2.default.aggregate.restore(); | ||
_Collection2.default.count.restore(); | ||
_Collection2.default.distinct.restore(); | ||
funcNames.forEach(function (funcName) { | ||
_Collection2.default[funcName].restore(); | ||
}); | ||
}); | ||
@@ -381,109 +521,15 @@ | ||
(0, _chai.expect)(funcs).to.be.an('object'); | ||
(0, _chai.expect)(funcs).to.have.keys(['find', 'findOne', 'findByIds', 'findById', 'upsertOne', 'updateOne', 'replaceOne', 'insertOne', 'deleteOne', 'deleteById', 'aggregate', 'count', 'distinct']); | ||
(0, _chai.expect)(funcs).to.have.keys(funcNames); | ||
}); | ||
describe('.find()', function () { | ||
it('should call the reusable method with the correct arguments', function () { | ||
_Collection2.default.find.reset(); | ||
funcs.find(arg1, arg2, arg3, arg4, arg5); | ||
_sinon2.default.assert.calledWith(_Collection2.default.find, 'test', _testSchema2.default, arg1, arg2, arg3, arg4, arg5); | ||
describe('collection functions', function () { | ||
it('should call the reusable method with the given arguments', function () { | ||
funcNames.forEach(function (funcName) { | ||
_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); | ||
}); | ||
}); | ||
}); | ||
describe('.findOne()', function () { | ||
it('should call the reusable method with the correct arguments', function () { | ||
_Collection2.default.findOne.reset(); | ||
funcs.findOne(arg1, arg2, arg3, arg4, arg5); | ||
_sinon2.default.assert.calledWith(_Collection2.default.findOne, 'test', _testSchema2.default, arg1, arg2, arg3, arg4, arg5); | ||
}); | ||
}); | ||
describe('.findByIds()', function () { | ||
it('should call the reusable method with the correct arguments', function () { | ||
_Collection2.default.findByIds.reset(); | ||
funcs.findByIds(arg1, arg2, arg3, arg4, arg5); | ||
_sinon2.default.assert.calledWith(_Collection2.default.findByIds, 'test', _testSchema2.default, arg1, arg2, arg3, arg4, arg5); | ||
}); | ||
}); | ||
describe('.findById()', function () { | ||
it('should call the reusable method with the correct arguments', function () { | ||
_Collection2.default.findById.reset(); | ||
funcs.findById(arg1, arg2, arg3, arg4, arg5); | ||
_sinon2.default.assert.calledWith(_Collection2.default.findById, 'test', _testSchema2.default, arg1, arg2, arg3, arg4, arg5); | ||
}); | ||
}); | ||
describe('.upsertOne()', function () { | ||
it('should call the reusable method with the correct arguments', function () { | ||
_Collection2.default.upsertOne.reset(); | ||
funcs.upsertOne(arg1, arg2, arg3, arg4, arg5); | ||
_sinon2.default.assert.calledWith(_Collection2.default.upsertOne, 'test', _testSchema2.default, arg1, arg2, arg3, arg4, arg5); | ||
}); | ||
}); | ||
describe('.updateOne()', function () { | ||
it('should call the reusable method with the correct arguments', function () { | ||
_Collection2.default.updateOne.reset(); | ||
funcs.updateOne(arg1, arg2, arg3, arg4, arg5); | ||
_sinon2.default.assert.calledWith(_Collection2.default.updateOne, 'test', _testSchema2.default, arg1, arg2, arg3, arg4, arg5); | ||
}); | ||
}); | ||
describe('.replaceOne()', function () { | ||
it('should call the reusable method with the correct arguments', function () { | ||
_Collection2.default.replaceOne.reset(); | ||
funcs.replaceOne(arg1, arg2, arg3, arg4, arg5); | ||
_sinon2.default.assert.calledWith(_Collection2.default.replaceOne, 'test', _testSchema2.default, arg1, arg2, arg3, arg4, arg5); | ||
}); | ||
}); | ||
describe('.insertOne()', function () { | ||
it('should call the reusable method with the correct arguments', function () { | ||
_Collection2.default.insertOne.reset(); | ||
funcs.insertOne(arg1, arg2, arg3, arg4, arg5); | ||
_sinon2.default.assert.calledWith(_Collection2.default.insertOne, 'test', _testSchema2.default, arg1, arg2, arg3, arg4, arg5); | ||
}); | ||
}); | ||
describe('.deleteOne()', function () { | ||
it('should call the reusable method with the correct arguments', function () { | ||
_Collection2.default.deleteOne.reset(); | ||
funcs.deleteOne(arg1, arg2, arg3, arg4, arg5); | ||
_sinon2.default.assert.calledWith(_Collection2.default.deleteOne, 'test', _testSchema2.default, arg1, arg2, arg3, arg4, arg5); | ||
}); | ||
}); | ||
describe('.deleteById()', function () { | ||
it('should call the reusable method with the correct arguments', function () { | ||
_Collection2.default.deleteById.reset(); | ||
funcs.deleteById(arg1, arg2, arg3, arg4, arg5); | ||
_sinon2.default.assert.calledWith(_Collection2.default.deleteById, 'test', _testSchema2.default, arg1, arg2, arg3, arg4, arg5); | ||
}); | ||
}); | ||
describe('.aggregate()', function () { | ||
it('should call the reusable method with the correct arguments', function () { | ||
_Collection2.default.aggregate.reset(); | ||
funcs.aggregate(arg1, arg2, arg3, arg4, arg5); | ||
_sinon2.default.assert.calledWith(_Collection2.default.aggregate, 'test', arg1, arg2, arg3, arg4, arg5); | ||
}); | ||
}); | ||
describe('.count()', function () { | ||
it('should call the reusable method with the correct arguments', function () { | ||
_Collection2.default.count.reset(); | ||
funcs.count(arg1, arg2, arg3, arg4, arg5); | ||
_sinon2.default.assert.calledWith(_Collection2.default.count, 'test', arg1, arg2, arg3, arg4, arg5); | ||
}); | ||
}); | ||
describe('.distinct()', function () { | ||
it('should call the reusable method with the correct arguments', function () { | ||
_Collection2.default.distinct.reset(); | ||
funcs.distinct(arg1, arg2, arg3, arg4, arg5); | ||
_sinon2.default.assert.calledWith(_Collection2.default.distinct, 'test', arg1, arg2, arg3, arg4, arg5); | ||
}); | ||
}); | ||
}); | ||
}); |
@@ -122,3 +122,3 @@ 'use strict'; | ||
* @param {string} collectionName Database collection | ||
* @param {function} schema Document json-schema definition | ||
* @param {object} schema Document json-schema definition | ||
* @param {object} conditions Query conditions (mongodb query conditions) | ||
@@ -147,3 +147,3 @@ * @param {object} [options] Query options | ||
* @param {string} collectionName Database collection | ||
* @param {function} schema Document json-schema definition | ||
* @param {object} schema Document json-schema definition | ||
* @param {object} conditions Query conditions (mongodb query conditions) | ||
@@ -166,3 +166,3 @@ * @return {Promise} | ||
* @param {string} collectionName Database collection | ||
* @param {function} schema Document json-schema definition | ||
* @param {object} schema Document json-schema definition | ||
* @param {array} ids Entity ids | ||
@@ -194,3 +194,3 @@ * @param {object} [options] Query options | ||
* @param {string} collectionName Database collection | ||
* @param {function} schema Document json-schema definition | ||
* @param {object} schema Document json-schema definition | ||
* @param {string} id Entity id | ||
@@ -209,13 +209,10 @@ * @return {Promise} | ||
/** | ||
* Update or insert one entity in a given database collection | ||
* Insert one entity into a given database collection | ||
* @param {string} collectionName Database collection | ||
* @param {function} schema Document json-schema definition | ||
* @param {object} conditions Query conditions (mongodb query conditions) | ||
* @param {object} schema Document json-schema definition | ||
* @param {object} props Entity properties | ||
* @param {object} [insertProps] Properties to set only if inserting | ||
* @return {Promise} | ||
*/ | ||
Collection.upsertOne = function (collectionName, schema, conditions) { | ||
var props = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {}; | ||
var insertProps = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : {}; | ||
Collection.insertOne = function (collectionName, schema) { | ||
var props = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {}; | ||
@@ -225,24 +222,36 @@ assertCollectionName(collectionName); | ||
var options = { | ||
returnNewDocument: true, | ||
upsert: true | ||
}; | ||
_jsft2.default.assertValid(partialSchema(schema), props, { | ||
messagePrefix: (schema.title || collectionName) + ' upsertOne update properties is invalid;' | ||
}); | ||
_jsft2.default.assertValid(schema, _extends({}, props, insertProps, { | ||
_jsft2.default.assertValid(schema, _extends({}, props, { | ||
id: '1234567890abcdef12345678' // Fake post-insert id, in case schema as id as required | ||
}), { | ||
messagePrefix: (schema.title || collectionName) + ' upsertOne insert properties is invalid;' | ||
messagePrefix: (schema.title || collectionName) + ' insertOne properties is invalid;' | ||
}); | ||
var collection = _Link2.default.getCollection(collectionName); | ||
var updates = omitUndefined({ | ||
$set: toDocument(props), | ||
$setOnInsert: Object.keys(insertProps).length > 0 ? insertProps : undefined | ||
return new Promise(function (resolve, reject) { | ||
collection.insert(toDocument(props), function (err, doc) { | ||
if (err) { | ||
reject(err); | ||
return; | ||
} | ||
resolve(doc ? fromDocument(doc) : undefined); | ||
}); | ||
}); | ||
}; | ||
return collection.findOneAndUpdate(conditions, updates, options).then(function (doc) { | ||
return doc ? fromDocument(doc) : undefined; | ||
Collection.updateBy = function (collectionName, schema, conditions, update) { | ||
var options = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : {}; | ||
assertCollectionName(collectionName); | ||
assertSchema(schema); | ||
var collection = _Link2.default.getCollection(collectionName); | ||
return collection.update(conditions, update, options).then(function () { | ||
return collection.find(conditions); | ||
}).then(function (docs) { | ||
return (docs || []).map(function (doc) { | ||
return fromDocument(doc); | ||
}); | ||
}); | ||
@@ -254,3 +263,3 @@ }; | ||
* @param {string} collectionName Database collection | ||
* @param {function} schema Document json-schema definition | ||
* @param {object} schema Document json-schema definition | ||
* @param {object} props Entity properties | ||
@@ -290,3 +299,3 @@ * @return {Promise} | ||
* @param {string} collectionName Database collection | ||
* @param {function} schema Document json-schema definition | ||
* @param {object} schema Document json-schema definition | ||
* @param {object} props Entity properties | ||
@@ -319,32 +328,50 @@ * @return {Promise} | ||
/** | ||
* Insert one entity into a given database collection | ||
* Upsert one or more entities in a given database collection, passing them through | ||
* a given callback function for possible modifications. If no documents is found, the | ||
* callback function will be called with an empty object as basis for insert. | ||
* @param {string} collectionName Database collection | ||
* @param {function} schema Document json-schema definition | ||
* @param {object} props Entity properties | ||
* @param {object} schema Document json-schema definition | ||
* @param {object} conditions Query conditions (mongodb query conditions) | ||
* @param {function} callback Callback function | ||
* @return {Promise} | ||
*/ | ||
Collection.insertOne = function (collectionName, schema) { | ||
var props = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {}; | ||
Collection.upsertBy = function (collectionName, schema, conditions, callback) { | ||
assertCollectionName(collectionName); | ||
assertSchema(schema); | ||
_jsft2.default.assertValid(schema, _extends({}, props, { | ||
id: '1234567890abcdef12345678' // Fake post-insert id, in case schema as id as required | ||
}), { | ||
messagePrefix: (schema.title || collectionName) + ' insertOne properties is invalid;' | ||
return Collection.find(collectionName, schema, conditions).then(function (entities) { | ||
if (entities && entities.length > 0) { | ||
// One or more docs found | ||
return Promise.all(entities.map(function (entity) { | ||
return Collection.replaceOne(collectionName, schema, callback(entity)); | ||
})); | ||
} else { | ||
// No docs found | ||
return Collection.insertOne(collectionName, schema, callback({})).then(function (entity) { | ||
return [entity]; | ||
}); | ||
} | ||
}); | ||
}; | ||
var collection = _Link2.default.getCollection(collectionName); | ||
/** | ||
* Upsert one entity in a given database collection, updating or inserting | ||
* the given properties based on having an id or not | ||
* @param {string} collectionName Database collection | ||
* @param {object} schema Document json-schema definition | ||
* @param {object} props Update properties | ||
* @param {object} [insertProps] Insert only props | ||
* @return {Promise} | ||
*/ | ||
Collection.upsertOne = function (collectionName, schema, props) { | ||
var insertProps = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : null; | ||
return new Promise(function (resolve, reject) { | ||
collection.insert(toDocument(props), function (err, doc) { | ||
if (err) { | ||
reject(err); | ||
return; | ||
} | ||
assertCollectionName(collectionName); | ||
assertSchema(schema); | ||
resolve(doc ? fromDocument(doc) : undefined); | ||
}); | ||
}); | ||
if (props.id) { | ||
return Collection.updateOne(collectionName, schema, props); | ||
} else { | ||
return Collection.insertOne(collectionName, schema, _extends({}, props, insertProps)); | ||
} | ||
}; | ||
@@ -355,3 +382,3 @@ | ||
* @param {string} collectionName Database collection | ||
* @param {function} schema Document json-schema definition | ||
* @param {object} schema Document json-schema definition | ||
* @param {object} props Entity properties | ||
@@ -379,3 +406,3 @@ * @return {Promise} | ||
* @param {string} collectionName Database collection | ||
* @param {function} schema Document json-schema definition | ||
* @param {object} schema Document json-schema definition | ||
* @param {string} id Entity id | ||
@@ -399,8 +426,9 @@ * @return {Promise} | ||
* @param {string} collectionName Database collection name | ||
* @param {object} schema Document json-schema definition | ||
* @param {mixed} args Aggregate function arguments | ||
* @return {Promise} | ||
*/ | ||
Collection.aggregate = function (collectionName) { | ||
for (var _len = arguments.length, args = Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) { | ||
args[_key - 1] = arguments[_key]; | ||
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]; | ||
} | ||
@@ -418,8 +446,9 @@ | ||
* @param {string} collectionName Database collection name | ||
* @param {object} schema Document json-schema definition | ||
* @param {mixed} args Count function arguments | ||
* @return {Promise} | ||
*/ | ||
Collection.count = function (collectionName) { | ||
for (var _len2 = arguments.length, args = Array(_len2 > 1 ? _len2 - 1 : 0), _key2 = 1; _key2 < _len2; _key2++) { | ||
args[_key2 - 1] = arguments[_key2]; | ||
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]; | ||
} | ||
@@ -437,8 +466,9 @@ | ||
* @param {string} collectionName Database collection name | ||
* @param {object} schema Document json-schema definition | ||
* @param {mixed} args Distincs function arguments | ||
* @return {Promise} | ||
*/ | ||
Collection.distinct = function (collectionName) { | ||
for (var _len3 = arguments.length, args = Array(_len3 > 1 ? _len3 - 1 : 0), _key3 = 1; _key3 < _len3; _key3++) { | ||
args[_key3 - 1] = arguments[_key3]; | ||
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]; | ||
} | ||
@@ -468,11 +498,13 @@ | ||
findById: (0, _partial3.default)(Collection.findById, collectionName, schema), | ||
upsertOne: (0, _partial3.default)(Collection.upsertOne, collectionName, schema), | ||
insertOne: (0, _partial3.default)(Collection.insertOne, collectionName, schema), | ||
updateBy: (0, _partial3.default)(Collection.updateBy, collectionName, schema), | ||
updateOne: (0, _partial3.default)(Collection.updateOne, collectionName, schema), | ||
replaceOne: (0, _partial3.default)(Collection.replaceOne, collectionName, schema), | ||
insertOne: (0, _partial3.default)(Collection.insertOne, collectionName, schema), | ||
upsertBy: (0, _partial3.default)(Collection.upsertBy, collectionName, schema), | ||
upsertOne: (0, _partial3.default)(Collection.upsertOne, 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), | ||
count: (0, _partial3.default)(Collection.count, collectionName), | ||
distinct: (0, _partial3.default)(Collection.distinct, collectionName) | ||
aggregate: (0, _partial3.default)(Collection.aggregate, collectionName, schema), | ||
count: (0, _partial3.default)(Collection.count, collectionName, schema), | ||
distinct: (0, _partial3.default)(Collection.distinct, collectionName, schema) | ||
}; | ||
@@ -479,0 +511,0 @@ }; |
@@ -17,2 +17,4 @@ 'use strict'; | ||
var Link = {}; | ||
/** | ||
@@ -22,5 +24,5 @@ * Set database url | ||
*/ | ||
function setUrl(url) { | ||
Link.setUrl = function (url) { | ||
dbUrl = url; | ||
} | ||
}; | ||
@@ -32,3 +34,3 @@ /** | ||
*/ | ||
function getCollection(name) { | ||
Link.getCollection = function (name) { | ||
if (!dbUrl) { | ||
@@ -44,10 +46,7 @@ throw new Error('jsft-mongodb - Database url must be configured before accessing collection. Please run .configure().'); // eslint-disable-line max-len | ||
collections[name] = collections.name || db.get(name); | ||
collections[name] = collections[name] || db.get(name); | ||
return collections[name]; | ||
} | ||
}; | ||
exports.default = { | ||
setUrl: setUrl, | ||
getCollection: getCollection | ||
}; | ||
exports.default = Link; |
{ | ||
"name": "jsft-mongodb", | ||
"version": "0.4.1", | ||
"version": "0.5.0", | ||
"description": "Functional abstraction layer for mongodb-storage of objects with structures defined with json-schema", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
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
46618
964