jsft-mongodb
Advanced tools
Comparing version 0.6.8 to 0.7.0
@@ -108,23 +108,2 @@ 'use strict'; | ||
} | ||
}, | ||
updateOneOrInsert: { | ||
mock: function mock() { | ||
return _sinon2.default.stub(_Collection2.default, 'updateOneOrInsert', function (col, sch, props) { | ||
return Promise.resolve(props); | ||
}); | ||
}, | ||
unmock: function unmock() { | ||
return _Collection2.default.updateOneOrInsert.restore(); | ||
} | ||
}, | ||
replaceOneOrInsert: { | ||
mock: function mock() { | ||
return _sinon2.default.stub(_Collection2.default, 'replaceOneOrInsert', function (col, sch, props) { | ||
return Promise.resolve(props); | ||
}); | ||
}, | ||
unmock: function unmock() { | ||
return _Collection2.default.replaceOneOrInsert.restore(); | ||
} | ||
} | ||
@@ -492,156 +471,2 @@ }; | ||
describe('.updateOneOrInsert', function () { | ||
before(function () { | ||
mockCollection.updateOne.mock(); | ||
mockCollection.replaceOne.mock(); | ||
mockCollection.insertOne.mock(); | ||
}); | ||
after(function () { | ||
mockCollection.updateOne.unmock(); | ||
mockCollection.replaceOne.unmock(); | ||
mockCollection.insertOne.unmock(); | ||
}); | ||
it('should fail for invalid props', function () { | ||
(0, _chai.expect)(function () { | ||
return _Collection2.default.updateOneOrInsert('tests'); | ||
}).to.throw(Error); | ||
(0, _chai.expect)(function () { | ||
return _Collection2.default.updateOneOrInsert('tests', _testSchema2.default, [updatePropsWithId]); | ||
}).to.throw(Error); | ||
}); | ||
it('should run updateOne for entity with id', function () { | ||
_Collection2.default.updateOne.reset(); | ||
_Collection2.default.replaceOne.reset(); | ||
_Collection2.default.insertOne.reset(); | ||
return _Collection2.default.updateOneOrInsert('tests', _testSchema2.default, updatePropsWithId).then(function (res) { | ||
_sinon2.default.assert.notCalled(_Collection2.default.replaceOne); | ||
_sinon2.default.assert.notCalled(_Collection2.default.insertOne); | ||
_sinon2.default.assert.calledWith(_Collection2.default.updateOne, 'tests', _testSchema2.default, updatePropsWithId); | ||
(0, _chai.expect)(res).to.deep.equal(updatePropsWithId); | ||
}); | ||
}); | ||
it('should run insertOne for entity without id', function () { | ||
_Collection2.default.updateOne.reset(); | ||
_Collection2.default.replaceOne.reset(); | ||
_Collection2.default.insertOne.reset(); | ||
return _Collection2.default.updateOneOrInsert('tests', _testSchema2.default, updateProps).then(function (res) { | ||
_sinon2.default.assert.notCalled(_Collection2.default.updateOne); | ||
_sinon2.default.assert.notCalled(_Collection2.default.replaceOne); | ||
_sinon2.default.assert.calledWith(_Collection2.default.insertOne, 'tests', _testSchema2.default, updateProps); | ||
(0, _chai.expect)(res).to.deep.equal(updateProps); | ||
}); | ||
}); | ||
}); | ||
describe('.updateOrInsert', function () { | ||
before(function () { | ||
mockCollection.updateOneOrInsert.mock(); | ||
}); | ||
after(function () { | ||
mockCollection.updateOneOrInsert.unmock(); | ||
}); | ||
it('should fail for invalid props', function () { | ||
(0, _chai.expect)(function () { | ||
return _Collection2.default.updateOrInsert('tests'); | ||
}).to.throw(Error); | ||
(0, _chai.expect)(function () { | ||
return _Collection2.default.updateOrInsert('tests', _testSchema2.default, updatePropsWithId); | ||
}).to.throw(Error); | ||
}); | ||
it('should run updateOneOrInsert for each entity', function () { | ||
_Collection2.default.updateOneOrInsert.reset(); | ||
return _Collection2.default.updateOrInsert('tests', _testSchema2.default, [updateProps, updatePropsWithId]).then(function (res) { | ||
(0, _chai.expect)(_Collection2.default.updateOneOrInsert.callCount).to.equal(2); | ||
_sinon2.default.assert.calledWith(_Collection2.default.updateOneOrInsert, 'tests', _testSchema2.default, updateProps); | ||
_sinon2.default.assert.calledWith(_Collection2.default.updateOneOrInsert, 'tests', _testSchema2.default, updatePropsWithId); | ||
(0, _chai.expect)(res).to.deep.equal([updateProps, updatePropsWithId]); | ||
}); | ||
}); | ||
}); | ||
describe('.replaceOneOrInsert', function () { | ||
before(function () { | ||
mockCollection.updateOne.mock(); | ||
mockCollection.replaceOne.mock(); | ||
mockCollection.insertOne.mock(); | ||
}); | ||
after(function () { | ||
mockCollection.updateOne.unmock(); | ||
mockCollection.replaceOne.unmock(); | ||
mockCollection.insertOne.unmock(); | ||
}); | ||
it('should fail for invalid props', function () { | ||
(0, _chai.expect)(function () { | ||
return _Collection2.default.replaceOneOrInsert('tests'); | ||
}).to.throw(Error); | ||
(0, _chai.expect)(function () { | ||
return _Collection2.default.replaceOneOrInsert('tests', _testSchema2.default, [updatePropsWithId]); | ||
}).to.throw(Error); | ||
}); | ||
it('should run replaceOne for entity with id', function () { | ||
_Collection2.default.updateOne.reset(); | ||
_Collection2.default.replaceOne.reset(); | ||
_Collection2.default.insertOne.reset(); | ||
return _Collection2.default.replaceOneOrInsert('tests', _testSchema2.default, updatePropsWithId).then(function (res) { | ||
_sinon2.default.assert.notCalled(_Collection2.default.updateOne); | ||
_sinon2.default.assert.notCalled(_Collection2.default.insertOne); | ||
_sinon2.default.assert.calledWith(_Collection2.default.replaceOne, 'tests', _testSchema2.default, updatePropsWithId); | ||
(0, _chai.expect)(res).to.deep.equal(updatePropsWithId); | ||
}); | ||
}); | ||
it('should run insertOne for entity without id', function () { | ||
_Collection2.default.updateOne.reset(); | ||
_Collection2.default.replaceOne.reset(); | ||
_Collection2.default.insertOne.reset(); | ||
return _Collection2.default.replaceOneOrInsert('tests', _testSchema2.default, updateProps).then(function (res) { | ||
_sinon2.default.assert.notCalled(_Collection2.default.updateOne); | ||
_sinon2.default.assert.notCalled(_Collection2.default.replaceOne); | ||
_sinon2.default.assert.calledWith(_Collection2.default.insertOne, 'tests', _testSchema2.default, updateProps); | ||
(0, _chai.expect)(res).to.deep.equal(updateProps); | ||
}); | ||
}); | ||
}); | ||
describe('.replaceOrInsert', function () { | ||
before(function () { | ||
mockCollection.replaceOneOrInsert.mock(); | ||
}); | ||
after(function () { | ||
mockCollection.replaceOneOrInsert.unmock(); | ||
}); | ||
it('should fail for invalid props', function () { | ||
(0, _chai.expect)(function () { | ||
return _Collection2.default.replaceOrInsert('tests'); | ||
}).to.throw(Error); | ||
(0, _chai.expect)(function () { | ||
return _Collection2.default.replaceOrInsert('tests', _testSchema2.default, updatePropsWithId); | ||
}).to.throw(Error); | ||
}); | ||
it('should run replaceOneOrInsert for each entity', function () { | ||
_Collection2.default.replaceOneOrInsert.reset(); | ||
return _Collection2.default.replaceOrInsert('tests', _testSchema2.default, [updateProps, updatePropsWithId]).then(function (res) { | ||
(0, _chai.expect)(_Collection2.default.replaceOneOrInsert.callCount).to.equal(2); | ||
_sinon2.default.assert.calledWith(_Collection2.default.replaceOneOrInsert, 'tests', _testSchema2.default, updateProps); | ||
_sinon2.default.assert.calledWith(_Collection2.default.replaceOneOrInsert, 'tests', _testSchema2.default, updatePropsWithId); | ||
(0, _chai.expect)(res).to.deep.equal([updateProps, updatePropsWithId]); | ||
}); | ||
}); | ||
}); | ||
describe('.deleteOne()', function () { | ||
@@ -711,3 +536,3 @@ it('should fail for props without id', function () { | ||
var funcNames = ['find', 'findOne', 'findByIds', 'findById', 'insertOne', 'updateOne', 'replaceOne', 'upsertOne', 'upsertOneUsing', 'updateOneOrInsert', 'updateOrInsert', 'replaceOneOrInsert', 'replaceOrInsert', 'deleteOne', 'deleteById', 'aggregate', 'count', 'distinct']; | ||
var funcNames = ['find', 'findOne', 'findByIds', 'findById', 'insertOne', 'updateOne', 'replaceOne', 'upsertOne', 'upsertOneUsing', 'deleteOne', 'deleteById', 'aggregate', 'count', 'distinct']; | ||
@@ -714,0 +539,0 @@ before(function () { |
@@ -79,14 +79,2 @@ 'use strict'; | ||
var assertPropsObject = function assertPropsObject(props) { | ||
if (Array.isArray(props)) { | ||
throw new Error('Invalid props, must be a single object'); | ||
} | ||
}; | ||
var assertPropsArray = function assertPropsArray(props) { | ||
if (!Array.isArray(props)) { | ||
throw new Error('Invalid props, must be array of props objects'); | ||
} | ||
}; | ||
// Make partial schema from full schema, by omitting the required field option | ||
@@ -381,66 +369,2 @@ var partialSchema = function partialSchema(schema) { | ||
/** | ||
* Update one entity in a given database collection, or insert if new | ||
* @param {string} collectionName Database collection | ||
* @param {object} schema Document json-schema definition | ||
* @param {object} props Entity properties | ||
* @return {Promise} | ||
*/ | ||
Collection.updateOneOrInsert = function (collectionName, schema, props) { | ||
assertCollectionName(collectionName); | ||
assertSchema(schema); | ||
assertPropsObject(props); | ||
return props.id ? Collection.updateOne(collectionName, schema, props) : Collection.insertOne(collectionName, schema, props); | ||
}; | ||
/** | ||
* Update list of entities in a given database collection, or insert if new | ||
* @param {string} collectionName Database collection | ||
* @param {object} schema Document json-schema definition | ||
* @param {array} propsArray Entity properties | ||
* @return {Promise} | ||
*/ | ||
Collection.updateOrInsert = function (collectionName, schema, propsArray) { | ||
assertCollectionName(collectionName); | ||
assertSchema(schema); | ||
assertPropsArray(propsArray); | ||
return Promise.all(propsArray.map(function (props) { | ||
return Collection.updateOneOrInsert(collectionName, schema, props); | ||
})); | ||
}; | ||
/** | ||
* Replace one entity in a given database collection, or insert if new | ||
* @param {string} collectionName Database collection | ||
* @param {object} schema Document json-schema definition | ||
* @param {object} props Entity properties | ||
* @return {Promise} | ||
*/ | ||
Collection.replaceOneOrInsert = function (collectionName, schema, props) { | ||
assertCollectionName(collectionName); | ||
assertSchema(schema); | ||
assertPropsObject(props); | ||
return props.id ? Collection.replaceOne(collectionName, schema, props) : Collection.insertOne(collectionName, schema, props); | ||
}; | ||
/** | ||
* Replace list of entities in a given database collection, or insert if new | ||
* @param {string} collectionName Database collection | ||
* @param {object} schema Document json-schema definition | ||
* @param {array} propsArray Entity properties | ||
* @return {Promise} | ||
*/ | ||
Collection.replaceOrInsert = function (collectionName, schema, propsArray) { | ||
assertCollectionName(collectionName); | ||
assertSchema(schema); | ||
assertPropsArray(propsArray); | ||
return Promise.all(propsArray.map(function (props) { | ||
return Collection.replaceOneOrInsert(collectionName, schema, props); | ||
})); | ||
}; | ||
/** | ||
* Delete one entity from a given database collection | ||
@@ -558,6 +482,2 @@ * @param {string} collectionName Database collection | ||
upsertOneUsing: (0, _partial3.default)(Collection.upsertOneUsing, collectionName, schema), | ||
updateOneOrInsert: (0, _partial3.default)(Collection.updateOneOrInsert, collectionName, schema), | ||
updateOrInsert: (0, _partial3.default)(Collection.updateOrInsert, collectionName, schema), | ||
replaceOneOrInsert: (0, _partial3.default)(Collection.replaceOneOrInsert, collectionName, schema), | ||
replaceOrInsert: (0, _partial3.default)(Collection.replaceOrInsert, collectionName, schema), | ||
deleteOne: (0, _partial3.default)(Collection.deleteOne, collectionName, schema), | ||
@@ -564,0 +484,0 @@ deleteById: (0, _partial3.default)(Collection.deleteById, collectionName, schema), |
{ | ||
"name": "jsft-mongodb", | ||
"version": "0.6.8", | ||
"version": "0.7.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
47649
993