jsft-mongodb
Advanced tools
Comparing version 0.6.3 to 0.6.4
@@ -109,2 +109,12 @@ 'use strict'; | ||
}, | ||
upsertOne: { | ||
mock: function mock() { | ||
return _sinon2.default.stub(_Collection2.default, 'upsertOne', function (col, sch, props) { | ||
return Promise.resolve(props); | ||
}); | ||
}, | ||
unmock: function unmock() { | ||
return _Collection2.default.upsertOne.restore(); | ||
} | ||
}, | ||
@@ -398,10 +408,2 @@ // upsertOne | ||
// const updateProps = { | ||
// nickname: 'Dead Stark' | ||
// }; | ||
// const updatePropsWithId = { | ||
// id: testObj.id, | ||
// nickname: 'Dead Stark' | ||
// }; | ||
var entity = { id: _testObj2.default.id }; | ||
@@ -451,2 +453,26 @@ var updateProps = { | ||
describe('.upsertOneUsing', function () { | ||
before(function () { | ||
mockCollection.upsertOne.mock(); | ||
}); | ||
after(function () { | ||
mockCollection.upsertOne.unmock(); | ||
}); | ||
var updateProps = { is: 'updateProps' }; | ||
var insertProps = { is: 'insertProps' }; | ||
var entity = { is: 'entity' }; | ||
it('should create function that calls main function with correct args', function () { | ||
_Collection2.default.upsertOne.reset(); | ||
var hof = _Collection2.default.upsertOneUsing('tests', _testSchema2.default, 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); | ||
}); | ||
}); | ||
}); | ||
describe('.updateOneOrInsert', function () { | ||
@@ -670,3 +696,3 @@ before(function () { | ||
var funcNames = ['find', 'findOne', 'findByIds', 'findById', 'insertOne', 'updateOne', 'replaceOne', 'upsertOne', 'updateOneOrInsert', 'updateOrInsert', 'replaceOneOrInsert', 'replaceOrInsert', 'deleteOne', 'deleteById', 'aggregate', 'count', 'distinct']; | ||
var funcNames = ['find', 'findOne', 'findByIds', 'findById', 'insertOne', 'updateOne', 'replaceOne', 'upsertOne', 'upsertOneUsing', 'updateOneOrInsert', 'updateOrInsert', 'replaceOneOrInsert', 'replaceOrInsert', 'deleteOne', 'deleteById', 'aggregate', 'count', 'distinct']; | ||
@@ -673,0 +699,0 @@ before(function () { |
@@ -9,4 +9,5 @@ 'use strict'; | ||
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; | ||
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; /* eslint-disable max-lines */ | ||
require('./polyfills'); | ||
@@ -364,2 +365,20 @@ | ||
/** | ||
* Generate higher order function for updating one entity by predefined props (for use in find-upsert chains) | ||
* @param {string} collectionName Database collection | ||
* @param {object} schema Document json-schema definition | ||
* @param {object} [updateProps] Properties to set (used for both update and insert) | ||
* @param {object} [insertOnlyProps] Properties to set only on insert operation | ||
* @return {function} Function taking only entity prop | ||
*/ | ||
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] : {}; | ||
return function () { | ||
var entity = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; | ||
return Collection.upsertOne(collectionName, schema, entity, updateProps, insertOnlyProps); | ||
}; | ||
}; | ||
/** | ||
* Update one entity in a given database collection, or insert if new | ||
@@ -540,2 +559,3 @@ * @param {string} collectionName Database collection | ||
upsertOne: (0, _partial3.default)(Collection.upsertOne, collectionName, schema), | ||
upsertOneUsing: (0, _partial3.default)(Collection.upsertOneUsing, collectionName, schema), | ||
updateOneOrInsert: (0, _partial3.default)(Collection.updateOneOrInsert, collectionName, schema), | ||
@@ -542,0 +562,0 @@ updateOrInsert: (0, _partial3.default)(Collection.updateOrInsert, collectionName, schema), |
{ | ||
"name": "jsft-mongodb", | ||
"version": "0.6.3", | ||
"version": "0.6.4", | ||
"description": "Functional abstraction layer for mongodb-storage of objects with structures defined with json-schema", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
58132
1209