Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

jsft-mongodb

Package Overview
Dependencies
Maintainers
1
Versions
31
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

jsft-mongodb - npm Package Compare versions

Comparing version 0.1.4 to 0.2.0

lib/_tests/Collection.test.js

5

lib/_tests/stubs.js

@@ -31,4 +31,5 @@ 'use strict';

},
insert: function insert() {
return Promise.resolve();
//insert: () => Promise.resolve(),
insert: function insert(props, callback) {
callback(null, _testDoc2.default);
},

@@ -35,0 +36,0 @@ remove: function remove() {

26

lib/_tests/testDoc.js

@@ -7,11 +7,23 @@ 'use strict';

var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
var _omitBy2 = require('lodash/fp/omitBy');
var _omitBy3 = _interopRequireDefault(_omitBy2);
var _isUndefined2 = require('lodash/fp/isUndefined');
var _isUndefined3 = _interopRequireDefault(_isUndefined2);
var _mongodb = require('mongodb');
/**
* Mock of a mongodb database document
*/
exports.default = {
var _testObj = require('./testObj');
var _testObj2 = _interopRequireDefault(_testObj);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
exports.default = (0, _omitBy3.default)(_isUndefined3.default, _extends({}, _testObj2.default, {
_id: (0, _mongodb.ObjectId)('1234567890abcdef12345678'),
name: 'John Doe',
answer: 42
};
id: undefined
}));

@@ -6,9 +6,13 @@ 'use strict';

});
/**
* Mock of an entity object that can be saved to the db
*/
exports.default = {
id: '1234567890abcdef12345678',
name: 'John Doe',
answer: 42
};
var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; };
var _eddardStark = require('testdata/got/characters/eddard-stark.json');
var _eddardStark2 = _interopRequireDefault(_eddardStark);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
exports.default = _extends({}, _eddardStark2.default, {
id: '1234567890abcdef12345678'
});

@@ -9,2 +9,4 @@ '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 _omitBy2 = require('lodash/omitBy');

@@ -18,4 +20,12 @@

var _partial2 = require('lodash/partial');
var _partial3 = _interopRequireDefault(_partial2);
var _mongodb = require('mongodb');
var _jsftValidate = require('jsft-validate');
var _jsftValidate2 = _interopRequireDefault(_jsftValidate);
var _Link = require('./Link');

@@ -27,4 +37,31 @@

// Helper functions
var omitUndefined = function omitUndefined(obj) {
return (0, _omitBy3.default)(obj, _isUndefined3.default);
};
var assertCollectionName = function assertCollectionName(name) {
if (typeof name !== 'string') {
throw new Error('Invalid collection name.');
}
};
var assertSchema = function assertSchema(schema) {
if ((typeof schema === 'undefined' ? 'undefined' : _typeof(schema)) !== 'object' || !schema.type || schema.type !== 'object') {
throw new Error('Invalid schema.');
}
};
// Make partial schema from full schema, by omitting the required field option
var partialSchema = function partialSchema(schema) {
return omitUndefined(_extends({}, schema, {
required: undefined
}));
};
// Transform between entity and db structures
var Collection = {};
/**

@@ -65,10 +102,12 @@ * Make the id entity-structured

* @param {string} collectionName Database collection
* @param {object} conditions Query conditions
* @param {object} conditions Query conditions (mongodb query conditions)
* @param {object} [options] Query options
* @return {Promise}
*/
function find(collectionName) {
Collection.find = function (collectionName) {
var conditions = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
assertCollectionName(collectionName);
return _Link2.default.getCollection(collectionName).find(conditions, options).then(function (docs) {

@@ -79,3 +118,3 @@ return (docs || []).map(function (doc) {

});
}
};

@@ -85,14 +124,16 @@ /**

* @param {string} collectionName Database collection
* @param {object} conditions Query conditions
* @param {object} conditions Query conditions (mongodb query conditions)
* @param {object} [options] Query options
* @return {Promise}
*/
function findOne(collectionName) {
Collection.findOne = function (collectionName) {
var conditions = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
assertCollectionName(collectionName);
return _Link2.default.getCollection(collectionName).findOne(conditions, options).then(function (doc) {
return doc ? fromDocument(doc) : undefined;
});
}
};

@@ -106,5 +147,7 @@ /**

*/
function findByIds(collectionName, ids) {
Collection.findByIds = function (collectionName, ids) {
var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
assertCollectionName(collectionName);
return _Link2.default.getCollection(collectionName).find({

@@ -119,3 +162,3 @@ _id: { $in: ids.map(function (id) {

});
}
};

@@ -129,9 +172,11 @@ /**

*/
function findById(collectionName, id) {
Collection.findById = function (collectionName, id) {
var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
assertCollectionName(collectionName);
return _Link2.default.getCollection(collectionName).findOne({ _id: (0, _mongodb.ObjectId)(id) }).then(function (doc) {
return doc ? fromDocument(doc) : undefined;
});
}
};

@@ -141,4 +186,4 @@ /**

* @param {string} collectionName Database collection
* @param {function} makeDefaults Create an object containing default values for inserting a given set of props
* @param {object} conditions Query conditions
* @param {function} schema Document json-schema definition
* @param {object} conditions Query conditions (mongodb query conditions)
* @param {object} props Entity properties

@@ -148,6 +193,9 @@ * @param {object} [insertProps] Properties to set only if inserting

*/
function upsertOne(collectionName, makeDefaults, conditions) {
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] : {};
assertCollectionName(collectionName);
assertSchema(schema);
var options = {

@@ -158,12 +206,21 @@ returnNewDocument: true,

var setOnInsert = _extends({}, makeDefaults(props), insertProps);
var updates = (0, _omitBy3.default)({
_jsftValidate2.default.assertValid(partialSchema(schema), props, {
messagePrefix: (schema.title || collectionName) + ' upsertOne update properties is invalid;'
});
_jsftValidate2.default.assertValid(schema, _extends({}, props, insertProps, {
id: '1234567890abcdef12345678' // Fake post-insert id, in case schema as id as required
}), {
messagePrefix: (schema.title || collectionName) + ' upsertOne insert properties is invalid;'
});
var collection = _Link2.default.getCollection(collectionName);
var updates = omitUndefined({
$set: toDocument(props),
$setOnInsert: Object.keys(setOnInsert).length > 0 ? setOnInsert : undefined
}, _isUndefined3.default);
$setOnInsert: Object.keys(insertProps).length > 0 ? insertProps : undefined
});
return _Link2.default.getCollection(collectionName).findOneAndUpdate(conditions, updates, options).then(function (doc) {
return collection.findOneAndUpdate(conditions, updates, options).then(function (doc) {
return doc ? fromDocument(doc) : undefined;
});
}
};

@@ -173,11 +230,18 @@ /**

* @param {string} collectionName Database collection
* @param {function} schema Document json-schema definition
* @param {object} props Entity properties
* @return {Promise}
*/
function updateOne(collectionName) {
var props = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
Collection.updateOne = function (collectionName, schema) {
var props = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
assertCollectionName(collectionName);
assertSchema(schema);
if (props.id === undefined) {
throw new Error('updateOne require id property.');
}
_jsftValidate2.default.assertValid(partialSchema(schema), props, {
messagePrefix: (schema.title || collectionName) + ' updateOne properties is invalid;'
});

@@ -187,3 +251,5 @@ var collection = _Link2.default.getCollection(collectionName);

var updates = {
$set: toDocument(props)
$set: omitUndefined(_extends({}, props, {
id: undefined
}))
};

@@ -196,3 +262,3 @@

});
}
};

@@ -202,11 +268,18 @@ /**

* @param {string} collectionName Database collection
* @param {function} schema Document json-schema definition
* @param {object} props Entity properties
* @return {Promise}
*/
function replaceOne(collectionName) {
var props = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
Collection.replaceOne = function (collectionName, schema) {
var props = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
assertCollectionName(collectionName);
assertSchema(schema);
if (props.id === undefined) {
throw new Error('replaceOne require id property.');
}
_jsftValidate2.default.assertValid(schema, props, {
messagePrefix: (schema.title || collectionName) + ' replaceOne properties is invalid;'
});

@@ -221,3 +294,3 @@ var collection = _Link2.default.getCollection(collectionName);

});
}
};

@@ -227,16 +300,31 @@ /**

* @param {string} collectionName Database collection
* @param {function} makeDefaults Create an object containing default values for inserting a given set of props
* @param {function} schema Document json-schema definition
* @param {object} props Entity properties
* @return {Promise}
*/
function insertOne(collectionName, makeDefaults) {
Collection.insertOne = function (collectionName, schema) {
var props = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
var insertProps = _extends({}, makeDefaults(props), props);
assertCollectionName(collectionName);
assertSchema(schema);
_jsftValidate2.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;'
});
var collection = _Link2.default.getCollection(collectionName);
return collection.insert(toDocument(insertProps)).then(function (doc) {
return doc ? fromDocument(doc) : undefined;
return new Promise(function (resolve, reject) {
collection.insert(toDocument(props), function (err, doc) {
if (err) {
reject(err);
return;
}
resolve(doc ? fromDocument(doc) : undefined);
});
});
}
};

@@ -249,5 +337,11 @@ /**

*/
function deleteOne(collectionName) {
Collection.deleteOne = function (collectionName) {
var props = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
assertCollectionName(collectionName);
if (props.id === undefined) {
throw new Error('deleteOne require id property.');
}
var conditions = { _id: (0, _mongodb.ObjectId)(props.id) };

@@ -257,3 +351,3 @@ var collection = _Link2.default.getCollection(collectionName);

return collection.remove(conditions);
}
};

@@ -266,3 +360,5 @@ /**

*/
function deleteById(collectionName, id) {
Collection.deleteById = function (collectionName, id) {
assertCollectionName(collectionName);
var conditions = { _id: (0, _mongodb.ObjectId)(id) };

@@ -272,15 +368,28 @@ var collection = _Link2.default.getCollection(collectionName);

return collection.remove(conditions);
}
};
exports.default = {
find: find,
findOne: findOne,
findByIds: findByIds,
findById: findById,
upsertOne: upsertOne,
updateOne: updateOne,
replaceOne: replaceOne,
insertOne: insertOne,
deleteOne: deleteOne,
deleteById: deleteById
};
/**
* Generate a set of functions for CRUD operations og a given database collection
* @param {string} collectionName Database collection name
* @param {object} schema JSON-Schema for the given document type
* @return {object} Literal containing the db-functions
*/
Collection.all = function (collectionName, schema) {
assertCollectionName(collectionName);
assertSchema(schema);
return {
find: (0, _partial3.default)(Collection.find, collectionName),
findOne: (0, _partial3.default)(Collection.findOne, collectionName),
findByIds: (0, _partial3.default)(Collection.findByIds, collectionName),
findById: (0, _partial3.default)(Collection.findById, collectionName),
upsertOne: (0, _partial3.default)(Collection.upsertOne, 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),
deleteOne: (0, _partial3.default)(Collection.deleteOne, collectionName),
deleteById: (0, _partial3.default)(Collection.deleteById, collectionName)
};
};
exports.default = Collection;

@@ -11,9 +11,4 @@ 'use strict';

exports.dbConfig = dbConfig;
exports.generateCollectionFunctions = generateCollectionFunctions;
exports.sinonTestStubGenerators = sinonTestStubGenerators;
var _partial2 = require('lodash/partial');
var _partial3 = _interopRequireDefault(_partial2);
var _sinon = require('sinon');

@@ -51,31 +46,2 @@

/**
* Generate a set of functions for CRUD operations og a given database collection
* @param {string} name Database collection name
* @param {function} makeDefaults Create an object containing default values for inserting a given set of props
* @return {object} Literal containing the db-functions
*/
function generateCollectionFunctions(name) {
var makeDefaults = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : function () {
return {};
};
if (typeof name !== 'string') {
throw new Error('Invalid collection name.');
}
return {
find: (0, _partial3.default)(_Collection2.default.find, name),
findOne: (0, _partial3.default)(_Collection2.default.findOne, name),
findByIds: (0, _partial3.default)(_Collection2.default.findByIds, name),
findById: (0, _partial3.default)(_Collection2.default.findById, name),
upsertOne: (0, _partial3.default)(_Collection2.default.upsertOne, name, makeDefaults),
updateOne: (0, _partial3.default)(_Collection2.default.updateOne, name),
replaceOne: (0, _partial3.default)(_Collection2.default.replaceOne, name),
insertOne: (0, _partial3.default)(_Collection2.default.insertOne, name, makeDefaults),
deleteOne: (0, _partial3.default)(_Collection2.default.deleteOne, name),
deleteById: (0, _partial3.default)(_Collection2.default.deleteById, name)
};
}
/**
* Generate a set of apply/cease functions for replacing the

@@ -82,0 +48,0 @@ * collection functions of the given object with reusable sinon test stubs

{
"name": "jsft-mongodb",
"version": "0.1.4",
"version": "0.2.0",
"description": "Functional abstraction layer for mongodb-storage of objects with structures defined with json-schema",

@@ -11,3 +11,3 @@ "main": "lib/index.js",

"test": "npm run test:mocha && npm run test:eslint",
"test:mocha": "NODE_PATH=. mocha -S --compilers js:babel-register --timeout 8000 \"src/**/_tests/test.*\"",
"test:mocha": "NODE_PATH=. mocha -S --compilers js:babel-register --timeout 8000 \"src/**/*.test.js\"",
"test:eslint": "NODE_PATH=. eslint .",

@@ -31,2 +31,3 @@ "compile": "babel src --out-dir lib",

"dependencies": {
"jsft-validate": "^0.4.1",
"lodash": "^4.17.4",

@@ -45,4 +46,5 @@ "mongodb": "^2.2.22",

"rimraf": "^2.5.4",
"sinon": "^1.17.7"
"sinon": "^1.17.7",
"testdata": "^0.1.2"
}
}
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc