Socket
Socket
Sign inDemoInstall

cosa

Package Overview
Dependencies
Maintainers
3
Versions
70
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

cosa - npm Package Compare versions

Comparing version 5.0.1 to 6.0.0

30

lib/cursor.js

@@ -18,3 +18,3 @@ const { times } = require('omnibelt');

* @returns {Promise} - resolves when list is exhausted.
* @see {@link http://mongodb.github.io/node-mongodb-native/3.0/api/Cursor.html#forEach}
* @see {@link https://mongodb.github.io/node-mongodb-native/4.0/classes/findcursor.html#foreach}
*/

@@ -68,3 +68,3 @@ async forEach(iterator) {

* @returns {number} the count of items in the list
* @see {@link http://mongodb.github.io/node-mongodb-native/3.0/api/Cursor.html#count}
* @see {@link https://mongodb.github.io/node-mongodb-native/4.0/classes/findcursor.html#count}
*/

@@ -78,3 +78,3 @@ count(applySkipLimit, options) {

* @returns {boolean} `true` if cursor is successfully closed
* @see {@link http://mongodb.github.io/node-mongodb-native/3.0/api/Cursor.html#close}
* @see {@link https://mongodb.github.io/node-mongodb-native/4.0/classes/findcursor.html#close}
*/

@@ -88,6 +88,6 @@ close() {

* @returns {boolean} `true` if the cursor is closed.
* @see {@link http://mongodb.github.io/node-mongodb-native/3.0/api/Cursor.html#isClosed}
* @see {@link https://mongodb.github.io/node-mongodb-native/4.0/classes/findcursor.html#close}
*/
isClosed() {
return this._cursor.isClosed();
return this._cursor.closed;
}

@@ -99,3 +99,3 @@

* @returns {Cursor} the cursor instance
* @see {@link http://mongodb.github.io/node-mongodb-native/3.0/api/Cursor.html#filter}
* @see {@link https://mongodb.github.io/node-mongodb-native/4.0/classes/findcursor.html#filter}
*/

@@ -111,3 +111,3 @@ filter(filter) {

* @returns {Cursor} the cursor instance
* @see {@link http://mongodb.github.io/node-mongodb-native/3.0/api/Cursor.html#limit}
* @see {@link https://mongodb.github.io/node-mongodb-native/4.0/classes/findcursor.html#limit}
*/

@@ -123,3 +123,3 @@ limit(limit) {

* @returns {Cursor} the cursor instance
* @see {@link http://mongodb.github.io/node-mongodb-native/3.0/api/Cursor.html#map}
* @see {@link https://mongodb.github.io/node-mongodb-native/4.0/classes/findcursor.html#map}
*/

@@ -136,3 +136,3 @@ map(transform) {

* @returns {Cursor} the cursor instance
* @see {@link http://mongodb.github.io/node-mongodb-native/3.0/api/Cursor.html#max}
* @see {@link https://mongodb.github.io/node-mongodb-native/4.0/classes/findcursor.html#max}
*/

@@ -148,3 +148,3 @@ max(max) {

* @returns {Cursor} the cursor instance
* @see {@link http://mongodb.github.io/node-mongodb-native/3.0/api/Cursor.html#min}
* @see {@link https://mongodb.github.io/node-mongodb-native/4.0/classes/findcursor.html#min}
*/

@@ -159,3 +159,3 @@ min(min) {

* @returns {Promise} - resolves with the next document from the cursor.
* @see {@link http://mongodb.github.io/node-mongodb-native/3.0/api/Cursor.html#next}
* @see {@link https://mongodb.github.io/node-mongodb-native/4.0/classes/findcursor.html#next}
*/

@@ -171,3 +171,3 @@ async next() {

* @returns {Promise} - resolves when each item in the cursor has been passed to the iterator function
* @see {@link http://mongodb.github.io/node-mongodb-native/3.0/api/Cursor.html#forEach}
* @see {@link https://mongodb.github.io/node-mongodb-native/4.0/classes/findcursor.html#forEach}
*/

@@ -188,3 +188,3 @@ serialForEach(iterator) {

* @returns {Cursor} the cursor instance
* @see {@link http://mongodb.github.io/node-mongodb-native/3.0/api/Cursor.html#skip}
* @see {@link https://mongodb.github.io/node-mongodb-native/4.0/classes/findcursor.html#skip}
*/

@@ -201,3 +201,3 @@ skip(value) {

* @returns {Cursor} the cursor instance
* @see {@link http://mongodb.github.io/node-mongodb-native/3.0/api/Cursor.html#sort}
* @see {@link https://mongodb.github.io/node-mongodb-native/4.0/classes/findcursor.html#sort}
*/

@@ -212,3 +212,3 @@ sort(sortOrList, direction) {

* @returns {Promise} that resolves into an array from the cursor
* @see {@link http://mongodb.github.io/node-mongodb-native/3.0/api/Cursor.html#toArray}
* @see {@link https://mongodb.github.io/node-mongodb-native/4.0/classes/findcursor.html#toArray}
*/

@@ -215,0 +215,0 @@ async toArray() {

const debug = require('debug')('cosa:db');
const defaults = require('defaults');
const EJSON = require('mongodb-extended-json');
const { ObjectId } = require('bson');
const { EventEmitter } = require('events');
const { MongoClient } = require('mongodb');
const {
curry, split, last, pipe, isEmpty, clamp
curry, split, last, pipe, isEmpty, clamp, isPlainObject
} = require('omnibelt');
const RESULT_FIELDS = [ 'insertedCount', 'insertedIds', 'matchedCount', 'modifiedCount', 'upsertedCount', 'deletedCount', 'upsertedIds' ];
const Immutable = require('./immutable');
const RESULT_FIELDS = [ 'insertedId', 'insertedCount', 'insertedIds', 'matchedCount', 'modifiedCount', 'upsertedCount', 'upsertedId', 'deletedCount', 'upsertedIds' ];
const getDbName = (uri) => {

@@ -20,5 +21,5 @@ let name = pipe(split('/'), last)(uri);

const result = {
result: r.result,
ops: r.ops
acknowledged: r.acknowledged
};
RESULT_FIELDS.forEach((type) => {

@@ -40,4 +41,34 @@ if ('undefined' !== typeof r[type]) {

const updateOneOrMany = actionOneOrMany('update');
const removeOneOrMany = actionOneOrMany('remove');
const deleteOneOrMany = actionOneOrMany('delete');
const deserialize = (data) => {
if (Array.isArray(data)) {
return data.map(deserialize);
}
if (typeof data !== 'object' || data === null) {
return data;
}
if (Immutable.isImmutable(data)) {
return data.toObject();
}
if (!isPlainObject(data)) {
return data;
}
const keys = Object.keys(data);
if (keys.length === 1) {
if (keys[0] === '$oid') {
return new ObjectId(data[keys[0]]);
}
}
const result = {};
keys.forEach((key) => {
result[key] = deserialize(data[key]);
});
return result;
};
/**

@@ -83,7 +114,9 @@ * Singleton that provides methods for connecting to a MongoDB collection.

try {
this._client = await MongoClient.connect(this._uri, {
useNewUrlParser: true,
useUnifiedTopology: true
});
this._db = await this._client.db(dbName);
this._client = new MongoClient(this._uri);
this._client = await this._client.connect();
let opts;
if (process.env.COSA_DB_READ_PREFERENCE) {
opts.readPreference = process.env.COSA_DB_READ_PREFERENCE;
}
this._db = await this._client.db(dbName, opts);
} catch (err) {

@@ -97,3 +130,4 @@ debug('failed to connect', err);

this._connectionStatus = 'connected';
this._client.on('close', () => {
// may want to investigate if this is always or if it could be close? not really sure why this changed
this._client.on('connectionPoolClosed', () => {
debug('database connection closed');

@@ -115,3 +149,3 @@ this._db = null;

* @returns {Promise} resolves with the connection
* @see {@link http://mongodb.github.io/node-mongodb-native/3.0/api/Db.html#collection}
* @see {@link https://mongodb.github.io/node-mongodb-native/4.0/classes/db.html#collection}
*/

@@ -155,6 +189,6 @@ async collection(name, options) {

* @returns {Cursor} returns Cursor object
* @see {@link http://mongodb.github.io/node-mongodb-native/3.0/api/Cursor.html}
* @see {@link http://mongodb.github.io/node-mongodb-native/3.0/api/Collection.html#find}
* @see {@link http://mongodb.github.io/node-mongodb-native/3.0/api/Collection.html#findOne}
* @see {@link http://mongodb.github.io/node-mongodb-native/3.0/api/Collection.html#count}
* @see {@link https://mongodb.github.io/node-mongodb-native/4.0/classes/findcursor.html}
* @see {@link https://mongodb.github.io/node-mongodb-native/4.0/classes/collection.html#find}
* @see {@link https://mongodb.github.io/node-mongodb-native/4.0/classes/collection.html#findOne}
* @see {@link https://mongodb.github.io/node-mongodb-native/4.0/classes/collection.html#count}
* @see {@link https://github.com/mongodb/node-mongodb-native/blob/357cbf689735c2447bfb05d73c142f1a5b88ca91/lib/read_preference.js#L69}

@@ -172,3 +206,3 @@ */

});
query = EJSON.deserialize(query);
query = deserialize(query);
debug('getting db collection');

@@ -212,11 +246,28 @@ const collection = await this.collection(collectionName);

* @param {(object|Array)} docs - Documents objects to insert.
* @param {object} options - options on insert
* @param {object} [options.writeConcern] - the write conern options https://mongodb.github.io/node-mongodb-native/4.0/classes/writeconcern.html
* @returns {Promise} resolves with an object with results, and ops as keys
* @see {@link http://mongodb.github.io/node-mongodb-native/3.0/api/Collection.html#insertmany}
* @see {@link http://mongodb.github.io/node-mongodb-native/3.0/api/Collection.html#insertOne}
* @see {@link https://mongodb.github.io/node-mongodb-native/4.0/classes/collection.html#insertmany}
* @see {@link https://mongodb.github.io/node-mongodb-native/4.0/classes/collection.html#insertOne}
*/
async insert(collectionName, docs) {
async insert(collectionName, docs, options) {
debug('getting db collection');
docs = EJSON.deserialize(docs);
docs = deserialize(docs);
const collection = await this.collection(collectionName);
return insertOneOrMany(collection, Array.isArray(docs), docs);
let insertOpts;
if (options?.writeConcern) {
insertOpts = { writeConcern: options.writeConcern };
}
const results = await insertOneOrMany(collection, Array.isArray(docs), docs, insertOpts);
if (Array.isArray(docs)) {
docs.forEach((doc, i) => {
doc._id = results.insertedIds[`${i}`];
});
} else {
docs._id = results.insertedId;
}
results.ops = docs;
return results;
}

@@ -232,5 +283,6 @@

* @param {boolean} [options.upsert=false] - Should documents be inserted if they don't already exist..
* @param {object} [options.writeConcern] - the write conern options https://mongodb.github.io/node-mongodb-native/4.0/classes/writeconcern.html
* @returns {Promise} resolves with an object with results, and ops as keys
* @see {@link http://mongodb.github.io/node-mongodb-native/3.0/api/Collection.html#updateMany}
* @see {@link http://mongodb.github.io/node-mongodb-native/3.0/api/Collection.html#updateOne}
* @see {@link https://mongodb.github.io/node-mongodb-native/4.0/classes/collection.html#updateMany}
* @see {@link https://mongodb.github.io/node-mongodb-native/4.0/classes/collection.html#updateOne}
*/

@@ -243,6 +295,9 @@ async update(collectionName, query, update, options) {

debug('getting db collection');
query = EJSON.deserialize(query);
update = EJSON.deserialize(update);
query = deserialize(query);
update = deserialize(update);
const collection = await this.collection(collectionName);
const updateOptions = { upsert: options.upsert };
if (options.writeConcern) {
updateOptions.writeConcern = options.writeConcern;
}
return updateOneOrMany(collection, options.multiple, query, update, updateOptions);

@@ -257,5 +312,6 @@ }

* @param {boolean} [options.multiple=false] - Should multiple documents be removed.
* @param {object} [options.writeConcern] - the write conern options https://mongodb.github.io/node-mongodb-native/4.0/classes/writeconcern.html
* @returns {Promise} resolves with an object with results, and ops as keys
* @see {@link http://mongodb.github.io/node-mongodb-native/3.0/api/Collection.html#deleteMany}
* @see {@link http://mongodb.github.io/node-mongodb-native/3.0/api/Collection.html#deleteOne}
* @see {@link https://mongodb.github.io/node-mongodb-native/4.0/classes/collection.html#deleteMany}
* @see {@link https://mongodb.github.io/node-mongodb-native/4.0/classes/collection.html#deleteOne}
*/

@@ -267,5 +323,9 @@ async remove(collectionName, query, options) {

debug('getting db collection');
query = EJSON.deserialize(query);
query = deserialize(query);
const collection = await this.collection(collectionName);
return removeOneOrMany(collection, options.multiple, query);
let removeOpts;
if (options.writeConcern) {
removeOpts = { writeConcern: options.writeConcern };
}
return deleteOneOrMany(collection, options.multiple, query, removeOpts);
}

@@ -281,3 +341,3 @@

* @returns {Promise} resolves with the result of the aggregation from mongo
* @see {@link http://mongodb.github.io/node-mongodb-native/3.0/api/Collection.html#aggregate}
* @see {@link https://mongodb.github.io/node-mongodb-native/4.0/classes/collection.html#aggregate}
* @see {@link https://github.com/mongodb/node-mongodb-native/blob/357cbf689735c2447bfb05d73c142f1a5b88ca91/lib/read_preference.js#L69}

@@ -291,8 +351,3 @@ */

const collection = await this.collection(collectionName);
return new Promise((resolve, reject) => {
collection.aggregate(pipeline, options, function(err, r) {
if (err) { return reject(err); }
return resolve(r);
});
});
return collection.aggregate(pipeline, options);
}

@@ -306,5 +361,5 @@

* @param {object} [options] - Optional settings see mongo documentation
* @param {object} [options.readPreference] - the read preference for the query
* @param {string} [options.readPreference] - the read preference for the query
* @returns {Promise} resolves with the result of the distinct query from mongo
* @see {@link http://mongodb.github.io/node-mongodb-native/3.0/api/Collection.html#distinct}
* @see {@link https://mongodb.github.io/node-mongodb-native/4.0/classes/collection.html#distinct}
* @see {@link https://github.com/mongodb/node-mongodb-native/blob/357cbf689735c2447bfb05d73c142f1a5b88ca91/lib/read_preference.js#L69}

@@ -314,3 +369,3 @@ */

debug('getting db collection');
query = EJSON.deserialize(query);
query = deserialize(query);
const collection = await this.collection(collectionName);

@@ -320,9 +375,23 @@ return collection.distinct(key, query, options);

async replace(collectionName, query, replace) {
/**
* Replace a doc in a collection.
* @param {string} collectionName - Name of the collection.
* @param {object} query - Query to find which documents evaluate.
* @param {object} replace - doc to save on the collection
* @param {object} [options] - Optional settings see mongo documentation
* @param {object} [options.writeConcern] - the write conern options https://mongodb.github.io/node-mongodb-native/4.0/classes/writeconcern.html
* @returns {Promise} resolves with the result of the distinct query from mongo
* @see {@link http://mongodb.github.io/node-mongodb-native/3.0/api/Collection.html#replace}
*/
async replace(collectionName, query, replace, options) {
const collection = await this.collection(collectionName);
query = EJSON.deserialize(query);
replace = EJSON.deserialize(replace);
query = deserialize(query);
replace = deserialize(replace);
debug(`db.${collection.collectionName}.replaceOne`, query, replace);
const r = await collection.replaceOne(query, replace);
let replaceOpts;
if (options?.writeConcern) {
replaceOpts = { writeConcern: options.writeConcern };
}
const r = await collection.replaceOne(query, replace, replaceOpts);
r.ops = replace;
return normalizeResult(r);

@@ -329,0 +398,0 @@ }

@@ -8,10 +8,8 @@ const assign = require('object-assign');

const clone = require('clone');
const EJSON = require('mongodb-extended-json');
const { EJSON } = require('bson');
const Cursor = require('./cursor');
const errors = require('./errors');
const { ObjectID } = require('bson');
const { ObjectId } = require('bson');
const { pathEq, complement, pick, omit } = require('omnibelt');
const { buildPropertySchema } = require('./utils');
const Immutable = require('./immutable');

@@ -115,3 +113,2 @@ Immutable.use(require('./defined-object'));

const _create = (data, definition) => {
definition.methods.is = function(obj) {

@@ -137,3 +134,2 @@ if (this === obj) { return true; }

obj = removeMeta(obj);
obj = EJSON.serialize(obj);
const newEtag = etag(JSON.stringify(obj));

@@ -147,3 +143,3 @@ const collection = definition.collection;

try {
result = await db.replace(collection, query, obj);
result = await db.replace(collection, query, obj, options);
} catch (e) {

@@ -169,3 +165,3 @@ let error = e;

try {
result = await db.insert(collection, obj);
result = await db.insert(collection, obj, options);
} catch (e) {

@@ -299,3 +295,3 @@ let error = e;

definition.methods.save = async function(options) {
return _saveHelper(this, options, null);
return _saveHelper(this, options);
};

@@ -307,3 +303,3 @@

}
return _saveHelper(this, options, new ObjectID(id));
return _saveHelper(this, options, new ObjectId(id));
};

@@ -319,3 +315,3 @@

debug(`removing ${JSON.stringify(query)} in ${collection}`);
const result = await db.remove(collection, query);
const result = await db.remove(collection, query, options);
if (result.deletedCount === 0) {

@@ -332,11 +328,12 @@ throw errors.Conflict({ message: 'Document remove conflict' });

definition.methods.reload = async function() {
definition.methods.reload = async function(options = {}) {
options.findOne = true;
const collection = definition.collection;
const query = { _id: this._id };
debug(`reloading ${this._id} in ${collection}`);
const result = await db.find(collection, query, { findOne: true });
const result = await db.find(collection, query, options);
return _create(result, definition);
};
return Immutable.create(data, { definition: definition });
return Immutable.create(data, { definition });
};

@@ -496,3 +493,3 @@

module.exports = {
define: define
define
};
const joi = require('@hapi/joi');
const { ObjectID } = require('bson');
const { ObjectId } = require('bson');
const buildPropertySchema = (name, propertyDef) => { // eslint-disable-line complexity
let schema = null;
switch (propertyDef.type.trim().toLowerCase()) {
switch ((propertyDef.type || 'any').trim().toLowerCase()) {
case 'array':

@@ -69,3 +69,3 @@ schema = joi.array();

case 'objectid':
schema = joi.object().instance(ObjectID);
schema = joi.object().instance(ObjectId);
break;

@@ -82,3 +82,8 @@ case 'any':

if ('undefined' !== typeof propertyDef.length) { schema = schema.length(propertyDef.length); }
if ('undefined' !== typeof propertyDef.allow) { schema = schema.allow(propertyDef.allow); }
if ('undefined' !== typeof propertyDef.allow) {
if (!Array.isArray(propertyDef.allow)) {
propertyDef.allow = [ propertyDef.allow ];
}
schema = schema.allow(...propertyDef.allow);
}
if ('undefined' !== typeof propertyDef.valid) {

@@ -85,0 +90,0 @@ if (!Array.isArray(propertyDef.valid)) {

{
"name": "cosa",
"version": "5.0.1",
"version": "6.0.0",
"description": "Cosa Models for MongoDB",
"main": "lib/index.js",
"engines": {
"node": ">=12",
"node": ">=14",
"yarn": ">=1.2.1"

@@ -18,3 +18,4 @@ },

"test-debug": "yarn test --debug-brk",
"doc": "documentation build lib/index.js -f md --github > API.md"
"doc": "documentation build lib/index.js -f md --github > API.md",
"prepare": "husky install"
},

@@ -38,5 +39,5 @@ "author": "Losant <hello@losant.com>",

"dependencies": {
"bson": "~1.1.5",
"bson": "~4.5.1",
"clone": "^2.1.2",
"debug": "^4.3.1",
"debug": "^4.3.2",
"defaults": "^1.0.3",

@@ -46,17 +47,16 @@ "error": "^7.0.2",

"@hapi/joi": "^17.1.1",
"mongodb": "~3.6.4",
"mongodb-extended-json": "~1.11.0",
"mongodb": "~4.1.1",
"object-assign": "^4.1.1",
"object-path": "^0.11.5",
"omnibelt": "^2.0.1"
"object-path": "^0.11.7",
"omnibelt": "^2.1.0"
},
"devDependencies": {
"@losant/eslint-config-losant": "^1.4.3",
"husky": "^4.3.0",
"lint-staged": "^10.5.4",
"chai": "^4.3.0",
"husky": "^7.0.2",
"lint-staged": "^11.1.2",
"chai": "^4.3.4",
"chai-as-promised": "^7.1.1",
"chai-datetime": "^1.8.0",
"documentation": "^13.1.1",
"mocha": "^8.3.0",
"documentation": "^13.2.5",
"mocha": "^9.1.1",
"string-template": "^1.0.0"

@@ -74,7 +74,2 @@ },

],
"husky": {
"hooks": {
"pre-commit": "lint-staged"
}
},
"mocha": {

@@ -81,0 +76,0 @@ "require": "chai",

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