Socket
Socket
Sign inDemoInstall

@brickyang/easy-mongodb

Package Overview
Dependencies
21
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 1.0.0 to 1.1.0

.vscode/settings.json

24

index.d.ts

@@ -23,2 +23,7 @@ import EventEmitter from 'events';

UpdateWriteOpResult,
MongoClient,
FindOneOptions,
SessionOptions,
ClientSession,
TransactionOptions,
} from 'mongodb';

@@ -34,3 +39,5 @@

public db: Db;
public client: MongoClient;
public config: IMongoConfig;
public featureCompatibilityVersion: string;

@@ -51,2 +58,7 @@ constructor(config: IMongoConfig);

public findOne<T = Default>(
name: string,
args: { query: object; options?: FindOneOptions }
): Promise<T | null>;
public findOneAndUpdate<T = Default>(

@@ -164,2 +176,8 @@ name: string,

): Promise<T[]>;
public startSession(args?: { options?: SessionOptions }): ClientSession;
public startTransaction(args?: {
options?: TransactionOptions;
}): ClientSession;
}

@@ -177,7 +195,1 @@

}
declare class ClientSession extends EventEmitter {
endSession(callback?: MongoCallback<void>): void;
endSession(options: any, callback?: MongoCallback<void>): void;
equals(session: ClientSession): boolean;
}

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

);
this.db;
this.client;
this.db;
this.featureCompatibilityVersion;
}

@@ -76,5 +77,14 @@

)
.then(client => {
.then(async client => {
this.client = client;
this.db = client.db(this.config.name);
const {
featureCompatibilityVersion,
} = await this.db.executeDbAdminCommand({
getParameter: 1,
featureCompatibilityVersion: 1,
});
/* istanbul ignore next */
this.featureCompatibilityVersion =
featureCompatibilityVersion.version || featureCompatibilityVersion;
Object.freeze(this.db);

@@ -175,2 +185,18 @@ this.emit('connect');

/**
* Find a document.
*
* @see https://mongodb.github.io/node-mongodb-native/3.1/api/Collection.html#findOne
* @param {string} name - The name of collection to find.
* @param {object} args - The arguments of command.
* @param {object} args.query - Document selection filter.
* @param {object} [args.options={}] - Optional settings. Check node-mongodb-native api document for detail.
*
* @return {Promise<object>} result
*/
findOne(name, args = {}) {
const { query, options } = args;
return this.db.collection(name).findOne(query, options);
}
/**
* Find a document and update it in one atomic operation, requires a write lock for the duration of the operation.

@@ -385,2 +411,4 @@ *

*
* @deprecated
*
* @see https://mongodb.github.io/node-mongodb-native/3.1/api/Collection.html#count

@@ -408,2 +436,41 @@ *

/**
* Gets the number of documents matching the filter.
*
* @see https://mongodb.github.io/node-mongodb-native/3.1/api/Collection.html#countDocuments
*
* @param {string} name - The name of collection to find.
* @param {object} args - The arguments of find command.
* @param {object} [args.query=null] - The query for the count.
* @param {object} [args.options=null] - Optional settings.
* @param {object} [args.options.collation] - Specifies a collation.
* @param {string|object} [args.options.hint] - The index to use.
* @param {number} [args.options.limit] - The maximum number of document to count.
* @param {number} [args.options.maxTimeMS] - The maximum amount of time to allow the operation to run.
* @param {number} [args.options.skip] - The number of documents to skip before counting.
*
* @return {Promise<number>} result
*/
countDocuments(name, args = {}) {
const { query, options } = args;
return this.db.collection(name).countDocuments(query, options);
}
/**
* Gets an estimate of the count of documents in a collection using collection metadata.
*
* @see https://mongodb.github.io/node-mongodb-native/3.1/api/Collection.html#estimatedDocumentCount
*
* @param {string} name - The name of collection to find.
* @param {object} [args] - The arguments of find command.
* @param {object} [options] - Optional settings.
* @param {number} [options.maxTimeMS] - The maximum amount of time to allow the operation to run.
*
* @return {Promise<number>} result
*/
estimatedDocumentCount(name, args = {}) {
return this.db.collection(name).countDocuments(args.options);
}
/**
* The distinct command returns a list of distinct values for the given key across a collection.

@@ -562,4 +629,51 @@ *

}
/**
* Starts a new session on the server
*
* @see http://mongodb.github.io/node-mongodb-native/3.1/api/MongoClient.html#startSession
*
* @param {object} options - optional settings for a driver session
* @param {boolean} causalConsistency - Whether causal consistency should be enabled on this session
* @param {object} defaultTransactionOptions - The default TransactionOptions to use for transactions started on this session
*
* @return {object} clientSession
*/
/* istanbul ignore next */
startSession(args = {}) {
if (parseFloat(this.featureCompatibilityVersion) < 3.6) {
throw new Error(
'Current topology does not support sessions, need 3.6 or above'
);
}
const options = Object.assign({}, args.options);
return this.client.startSession(options);
}
/**
* Starts a new transaction with the given options
*
* @see http://mongodb.github.io/node-mongodb-native/3.1/api/ClientSession.html#startTransaction
*
* @param {object} args
* @param {object} args.options - Options for the transaction
*
* @return {object} clientSeesion
*/
/* istanbul ignore next */
startTransaction(args = {}) {
if (parseFloat(this.featureCompatibilityVersion) < 4) {
throw new Error(
'Current topology does not support transactions, need 4.0 or above'
);
}
const options = Object.assign({}, args.options);
const session = this.startSession();
session.startTransaction(options);
return session;
}
}
module.exports = MongoDB;
{
"name": "@brickyang/easy-mongodb",
"version": "1.0.0",
"version": "1.1.0",
"description": "Based on MongoDB Native Node.js Driver.",

@@ -13,9 +13,9 @@ "main": "index.js",

"dependencies": {
"@types/mongodb": "^3.1.4",
"mongodb": "^3.1.4"
"@types/mongodb": "^3.1.18",
"mongodb": "^3.1.10"
},
"devDependencies": {
"eslint": "^5.4.0",
"eslint": "^5.12.0",
"mocha": "^5.2.0",
"nyc": "^12.0.2"
"nyc": "^13.1.0"
},

@@ -22,0 +22,0 @@ "repository": {

@@ -13,8 +13,8 @@ [![NPM version][npm-image]][npm-url]

[quality-url]: http://packagequality.com/#?package=@brickyang/easy-mongodb
[travis-image]: https://img.shields.io/travis/brickyang/@brickyang/easy-mongodb.svg?branch=master&style=flat-square
[travis-url]: https://travis-ci.org/brickyang/@brickyang/easy-mongodb
[codecov-image]: https://img.shields.io/codecov/c/github/brickyang/@brickyang/easy-mongodb.svg?style=flat-square
[codecov-url]: https://codecov.io/github/brickyang/@brickyang/easy-mongodb?branch=master
[david-image]: https://img.shields.io/david/brickyang/@brickyang/easy-mongodb.svg?branch=master&style=flat-square
[david-url]: https://david-dm.org/brickyang/@brickyang/easy-mongodb?branch=master
[travis-image]: https://img.shields.io/travis-ci/brickyang/easy-mongodb.svg?style=flat-square
[travis-url]: https://travis-ci.org/brickyang/easy-mongodb
[codecov-image]: https://img.shields.io/codecov/c/github/brickyang/easy-mongodb.svg?style=flat-square
[codecov-url]: https://codecov.io/github/brickyang/easy-mongodb?branch=master
[david-image]: https://david-dm.org/brickyang/easy-mongodb/status.svg?style=flat-square
[david-url]: https://david-dm.org/brickyang/easy-mongodb?branch=master
[snyk-image]: https://snyk.io/test/npm/@brickyang/easy-mongodb/badge.svg?style=flat-square

@@ -25,2 +25,4 @@ [snyk-url]: https://snyk.io/test/npm/@brickyang/easy-mongodb

[**中文版**](https://github.com/brickyang/easy-mongodb/blob/master/README.zh_CN.md)
This lib base on

@@ -49,2 +51,4 @@ [node-mongodb-native](https://github.com/mongodb/node-mongodb-native), provides

If you are using Egg.js, please see [egg-mongo-native](https://github.com/brickyang/egg-mongo-native).
## Installation

@@ -93,2 +97,6 @@

The APIs provided by this lib usually need two arguments. The first is commonly
the collection name, and the second is an object keeps the arguments of official
API.
```js

@@ -99,2 +107,3 @@ const MongoDB = require('@brickyang/easy-mongodb');

// connection
mongo

@@ -118,22 +127,22 @@ .connect()

});
```
## Example
// insert one doc
const args = { doc, options };
mongo.insertOne('collection', args);
APIs provided by this lib usually need two arguments. The first is commonly
the collection name, and the second is an object keeps the arguments of official
API. For example, to insert one document using official API
```js
db.collection('name').insertOne(doc, options);
// transaction
const session = mongo.startTransaction();
const args = { doc, { session } };
mongo.insertOne('collection1', args);
mongo.insertOne('collection2', args);
session.commitTransaction();
```
and using lib API
## Members
```js
const args = { doc, options };
mongo.insertOne('name', args);
```
- **db**: Db instance
- **client**: MongoClient instance
- **featureCompatibilityVersion**: Transaction need '4.0' or above
## Methods
## API

@@ -145,2 +154,3 @@ Until now, this plugin provides these functions:

- **insertMany**
- **findOne**
- **findOneAndUpdate**

@@ -152,3 +162,5 @@ - **findOneAndReplace**

- **find**
- **count**
- **count**: 已过时
- **countDocuments**
- **estimatedDocumentCount**
- **distinct**

@@ -159,4 +171,6 @@ - **createIndex**

- **aggregate**
- **startSession**
- **startTransaction**
You can always use `mongo.db` to use all official APIs. Check the
You can always use `mongo.db` and `mongo.client` to use all official APIs. Check the
APIs here:

@@ -163,0 +177,0 @@ [Node.js MongoDB Driver API](https://mongodb.github.io/node-mongodb-native/3.1/api/).

@@ -8,3 +8,4 @@ const { ObjectID } = require('mongodb');

let config;
let db;
let mongo;
let version;
before(async () => {

@@ -17,13 +18,15 @@ NAME = 'test';

};
db = new MongoDB(config);
await db.connect();
mongo = new MongoDB(config);
await mongo.connect();
version = parseFloat(mongo.featureCompatibilityVersion);
});
afterEach(async () => await db.deleteMany(NAME, { filter: {} }));
after(async () => await db.close());
afterEach(async () => await mongo.deleteMany(NAME, { filter: {} }));
after(async () => await mongo.close());
describe('connect()', () => {
it('should OK', async () => {
db.on('connect', () => {
assert.ok(db.client.isConnected());
mongo.on('connect', () => {
assert.ok(mongo.client.isConnected());
assert.equal(typeof mongo.featureCompatibilityVersion, 'string');
});

@@ -36,3 +39,3 @@ });

const doc = { title: 'new doc' };
const result = await db.insertOne(NAME, { doc });
const result = await mongo.insertOne(NAME, { doc });

@@ -56,3 +59,3 @@ const {

it('should insert empty document success', async () => {
const result = await db.insertOne(NAME);
const result = await mongo.insertOne(NAME);
const {

@@ -77,3 +80,3 @@ insertedCount,

const docs = [{ title: 'doc1' }, { title: 'doc2' }, { title: 'doc3' }];
const result = await db.insertMany(NAME, { docs });
const result = await mongo.insertMany(NAME, { docs });

@@ -97,3 +100,3 @@ const {

try {
await db.insertMany(NAME);
await mongo.insertMany(NAME);
} catch (error) {

@@ -107,3 +110,3 @@ assert(error instanceof Error);

try {
await db.insertMany(NAME, { docs: 'docs' });
await mongo.insertMany(NAME, { docs: 'docs' });
} catch (error) {

@@ -116,2 +119,49 @@ assert(error instanceof Error);

describe('findOne()', () => {
beforeEach(
async () =>
await mongo.insertMany(NAME, {
docs: [
{ index: 1, type: 'doc' },
{ index: 2, type: 'doc' },
{ index: 3, type: 'doc' },
],
})
);
it('should success', async () => {
const result = await mongo.findOne(NAME, {
query: { type: 'doc' },
});
assert.equal(result.index, 1);
assert.equal(result.type, 'doc');
});
it('should success with projection', async () => {
const result = await mongo.findOne(NAME, {
options: { projection: { index: 1 } },
});
assert(result.hasOwnProperty('index'));
assert(!result.hasOwnProperty('type'));
});
it('should success with sort', async () => {
const result = await mongo.findOne(NAME, {
options: { sort: { index: -1 } },
});
assert.equal(result.index, 3);
});
it('should success with empty args', async () => {
const result = await mongo.findOne(NAME);
assert.equal(result.index, 1);
assert.equal(result.type, 'doc');
});
it('should success when document not found', async () => {
const result = await mongo.findOne(NAME, { query: { index: 0 } });
assert.equal(result, null);
});
});
describe('findOneAndUpdate()', () => {

@@ -126,3 +176,3 @@ let _id;

];
const result = await db.insertMany(NAME, { docs });
const result = await mongo.insertMany(NAME, { docs });
_id = result.insertedIds[0];

@@ -134,3 +184,3 @@ });

const update = { $set: { title: 'update doc' } };
const result = await db.findOneAndUpdate(NAME, { filter, update });
const result = await mongo.findOneAndUpdate(NAME, { filter, update });

@@ -150,3 +200,3 @@ const {

it('should success and return updated', async () => {
const result = await db.findOneAndUpdate(NAME, {
const result = await mongo.findOneAndUpdate(NAME, {
filter: { _id },

@@ -170,3 +220,3 @@ update: { $set: { title: 'update doc' } },

it('should success with sort', async () => {
const result = await db.findOneAndUpdate(NAME, {
const result = await mongo.findOneAndUpdate(NAME, {
filter: { _id },

@@ -188,3 +238,3 @@ update: { $set: { title: 'update doc' } },

it('should upsert', async () => {
const result = await db.findOneAndUpdate(NAME, {
const result = await mongo.findOneAndUpdate(NAME, {
filter: { title: 'upsert' },

@@ -210,3 +260,3 @@ update: { $setOnInsert: { title: 'upsert' } },

try {
await db.findOneAndUpdate(NAME);
await mongo.findOneAndUpdate(NAME);
} catch (error) {

@@ -220,3 +270,3 @@ assert.equal(error.name, 'MongoError');

try {
await db.findOneAndUpdate(NAME, { filter: {} });
await mongo.findOneAndUpdate(NAME, { filter: {} });
} catch (error) {

@@ -231,9 +281,11 @@ assert.equal(error.name, 'MongoError');

let _id;
beforeEach(async () =>
({ insertedId: _id } = await db.insertOne(NAME, {
doc: { title: 'new doc' },
})));
beforeEach(
async () =>
({ insertedId: _id } = await mongo.insertOne(NAME, {
doc: { title: 'new doc' },
}))
);
it('should success', async () => {
const result = await db.findOneAndReplace(NAME, {
const result = await mongo.findOneAndReplace(NAME, {
filter: { _id },

@@ -256,3 +308,3 @@ replacement: { doc: 'replace' },

it('should success and return replaced', async () => {
const result = await db.findOneAndReplace(NAME, {
const result = await mongo.findOneAndReplace(NAME, {
filter: { _id: new ObjectID(_id) },

@@ -276,3 +328,3 @@ replacement: { doc: 'replace' },

it('should upsert', async () => {
const result = await db.findOneAndReplace(NAME, {
const result = await mongo.findOneAndReplace(NAME, {
filter: { title: 'upsert' },

@@ -296,3 +348,3 @@ replacement: { doc: 'replace' },

try {
await db.findOneAndReplace(NAME);
await mongo.findOneAndReplace(NAME);
} catch (error) {

@@ -306,3 +358,3 @@ assert.equal(error.name, 'MongoError');

try {
await db.findOneAndReplace(NAME, { filter: {} });
await mongo.findOneAndReplace(NAME, { filter: {} });
} catch (error) {

@@ -318,3 +370,3 @@ assert.equal(error.name, 'MongoError');

beforeEach(async () => {
const result = await db.insertMany(NAME, {
const result = await mongo.insertMany(NAME, {
docs: [{ title: 'new doc' }, { title: 'new doc' }],

@@ -326,3 +378,3 @@ });

it('should success', async () => {
const result = await db.findOneAndDelete(NAME, {
const result = await mongo.findOneAndDelete(NAME, {
filter: { _id },

@@ -343,3 +395,3 @@ });

it('should success with sort', async () => {
const result = await db.findOneAndDelete(NAME, {
const result = await mongo.findOneAndDelete(NAME, {
filter: {},

@@ -362,3 +414,3 @@ options: { sort: { id: 1 } },

try {
await db.findOneAndDelete(NAME);
await mongo.findOneAndDelete(NAME);
} catch (error) {

@@ -372,16 +424,18 @@ assert.equal(error.name, 'MongoError');

describe('updateMany()', async () => {
beforeEach(async () =>
await db.insertMany(NAME, {
docs: [
{ title: 'doc1', type: 'doc' },
{ title: 'doc2', type: 'doc' },
{ title: 'doc3', type: 'text' },
{ title: 'doc4', type: 'text' },
],
}));
beforeEach(
async () =>
await mongo.insertMany(NAME, {
docs: [
{ title: 'doc1', type: 'doc' },
{ title: 'doc2', type: 'doc' },
{ title: 'doc3', type: 'text' },
{ title: 'doc4', type: 'text' },
],
})
);
afterEach(async () => await db.deleteMany(NAME, { filter: {} }));
afterEach(async () => await mongo.deleteMany(NAME, { filter: {} }));
it('should success', async () => {
const result = await db.updateMany(NAME, {
const result = await mongo.updateMany(NAME, {
filter: { type: 'doc' },

@@ -410,3 +464,3 @@ update: { $set: { type: 'update' } },

it('should success all doc', async () => {
const result = await db.updateMany(NAME, {
const result = await mongo.updateMany(NAME, {
filter: {},

@@ -435,3 +489,3 @@ update: { $set: { type: 'update' } },

it('should upsert', async () => {
const result = await db.updateMany(NAME, {
const result = await mongo.updateMany(NAME, {
filter: { doc: 'doc5' },

@@ -462,3 +516,3 @@ update: { $set: { type: 'update' } },

try {
await db.updateMany(NAME);
await mongo.updateMany(NAME);
} catch (error) {

@@ -471,3 +525,3 @@ assert(error);

try {
await db.updateMany(NAME, {
await mongo.updateMany(NAME, {
update: { $set: { type: 'update' } },

@@ -485,3 +539,3 @@ });

try {
await db.updateMany(NAME, { filter: {}, update: {} });
await mongo.updateMany(NAME, { filter: {}, update: {} });
} catch (error) {

@@ -497,16 +551,18 @@ assert.equal(

describe('deleteMany()', () => {
beforeEach(async () =>
await db.insertMany(NAME, {
docs: [
{ title: 'doc1', type: 'doc' },
{ title: 'doc2', type: 'doc' },
{ title: 'doc3', type: 'text' },
{ title: 'doc4', type: 'text' },
],
}));
beforeEach(
async () =>
await mongo.insertMany(NAME, {
docs: [
{ title: 'doc1', type: 'doc' },
{ title: 'doc2', type: 'doc' },
{ title: 'doc3', type: 'text' },
{ title: 'doc4', type: 'text' },
],
})
);
afterEach(async () => await db.deleteMany(NAME, { filter: {} }));
afterEach(async () => await mongo.deleteMany(NAME, { filter: {} }));
it('should success', async () => {
const result = await db.deleteMany(NAME, {
const result = await mongo.deleteMany(NAME, {
filter: { type: 'doc' },

@@ -525,3 +581,3 @@ });

it('should delete all', async () => {
const result = await db.deleteMany(NAME, { filter: {} });
const result = await mongo.deleteMany(NAME, { filter: {} });

@@ -539,3 +595,3 @@ const {

try {
await db.deleteMany(NAME);
await mongo.deleteMany(NAME);
} catch (error) {

@@ -548,13 +604,15 @@ assert.equal(error.message, 'filter parameter must be an object');

describe('find()', () => {
beforeEach(async () =>
await db.insertMany(NAME, {
docs: [
{ index: 1, type: 'doc' },
{ index: 2, type: 'doc' },
{ index: 3, type: 'doc' },
],
}));
beforeEach(
async () =>
await mongo.insertMany(NAME, {
docs: [
{ index: 1, type: 'doc' },
{ index: 2, type: 'doc' },
{ index: 3, type: 'doc' },
],
})
);
it('should success', async () => {
const result = await db.find(NAME, {
const result = await mongo.find(NAME, {
query: { type: 'doc' },

@@ -567,3 +625,3 @@ });

it('should success with limit', async () => {
const result = await db.find(NAME, { limit: 1 });
const result = await mongo.find(NAME, { limit: 1 });
assert(Array.isArray(result));

@@ -574,3 +632,3 @@ assert.equal(result.length, 1);

it('should success with skip', async () => {
const result = await db.find(NAME, { skip: 1 });
const result = await mongo.find(NAME, { skip: 1 });
assert(Array.isArray(result));

@@ -581,3 +639,3 @@ assert.equal(result.length, 2);

it('should success with projection', async () => {
const result = await db.find(NAME, { projection: { index: 1 } });
const result = await mongo.find(NAME, { projection: { index: 1 } });
assert(result[0].hasOwnProperty('index'));

@@ -588,3 +646,3 @@ assert(!result[0].hasOwnProperty('type'));

it('#DEPRECATED# should success with project', async () => {
const result = await db.find(NAME, { project: { index: 1 } });
const result = await mongo.find(NAME, { project: { index: 1 } });
assert(result[0].hasOwnProperty('index'));

@@ -595,3 +653,3 @@ assert(!result[0].hasOwnProperty('type'));

it('should success with sort', async () => {
const result = await db.find(NAME, { sort: { index: -1 } });
const result = await mongo.find(NAME, { sort: { index: -1 } });
assert(result[0].index > result[1].index);

@@ -602,3 +660,3 @@ assert(result[1].index > result[2].index);

it('should success with empty args', async () => {
const result = await db.find(NAME);
const result = await mongo.find(NAME);
assert(Array.isArray(result));

@@ -609,3 +667,3 @@ assert.equal(result.length, 3);

it('should cursor', async () => {
const result = await db.find(NAME, {}, true);
const result = await mongo.find(NAME, {}, true);
assert.equal(typeof result, 'object');

@@ -616,14 +674,16 @@ });

describe('count()', () => {
beforeEach(async () =>
await db.insertMany(NAME, {
docs: [
{ type: 'doc' },
{ type: 'doc' },
{ type: 'text' },
{ type: 'text' },
],
}));
beforeEach(
async () =>
await mongo.insertMany(NAME, {
docs: [
{ type: 'doc' },
{ type: 'doc' },
{ type: 'text' },
{ type: 'text' },
],
})
);
it('should success', async () => {
const result = await db.count(NAME, {
const result = await mongo.count(NAME, {
query: { type: 'doc' },

@@ -635,3 +695,3 @@ });

it('should count all', async () => {
const result = await db.count(NAME);
const result = await mongo.count(NAME);
assert.equal(result, 4);

@@ -641,15 +701,62 @@ });

describe('countDocuments()', () => {
beforeEach(
async () =>
await mongo.insertMany(NAME, {
docs: [
{ type: 'doc' },
{ type: 'doc' },
{ type: 'text' },
{ type: 'text' },
],
})
);
it('should success', async () => {
const result = await mongo.countDocuments(NAME, {
query: { type: 'doc' },
});
assert.equal(result, 2);
});
it('should count all', async () => {
const result = await mongo.countDocuments(NAME);
assert.equal(result, 4);
});
});
describe('estimatedDocumentCount()', () => {
beforeEach(
async () =>
await mongo.insertMany(NAME, {
docs: [
{ type: 'doc' },
{ type: 'doc' },
{ type: 'text' },
{ type: 'text' },
],
})
);
it('should success', async () => {
const result = await mongo.estimatedDocumentCount(NAME);
assert.equal(result, 4);
});
});
describe('distinct()', () => {
beforeEach(async () =>
await db.insertMany(NAME, {
docs: [
{ type: 'doc' },
{ type: 'doc' },
{ type: 'text' },
{ type: 'text' },
],
}));
beforeEach(
async () =>
await mongo.insertMany(NAME, {
docs: [
{ type: 'doc' },
{ type: 'doc' },
{ type: 'text' },
{ type: 'text' },
],
})
);
it('should success', async () => {
const result = await db.distinct(NAME, {
const result = await mongo.distinct(NAME, {
key: 'type',

@@ -661,3 +768,3 @@ });

it('should success with query', async () => {
const result = await db.distinct(NAME, {
const result = await mongo.distinct(NAME, {
key: 'type',

@@ -672,3 +779,3 @@ query: { type: 'doc' },

try {
await db.distinct(NAME);
await mongo.distinct(NAME);
} catch (error) {

@@ -685,3 +792,3 @@ assert.equal(

it('should success', async () => {
const result = await db.createIndex(NAME, {
const result = await mongo.createIndex(NAME, {
fieldOrSpec: { title: -1 },

@@ -693,3 +800,3 @@ });

it('should success', async () => {
const result = await db.createIndex(NAME, {
const result = await mongo.createIndex(NAME, {
fieldOrSpec: 'title',

@@ -702,3 +809,3 @@ });

try {
await db.createIndex(NAME, { fieldOrSpec: {} });
await mongo.createIndex(NAME, { fieldOrSpec: {} });
} catch (error) {

@@ -711,3 +818,3 @@ assert.equal(error.message, 'Index keys cannot be empty.');

try {
await db.createIndex(NAME);
await mongo.createIndex(NAME);
} catch (error) {

@@ -721,4 +828,4 @@ assert(error instanceof Error);

it('should create && list collection success', async () => {
await db.createCollection({ name: 'create' });
const result = await db.listCollections();
await mongo.createCollection({ name: 'create' });
const result = await mongo.listCollections();
assert.notEqual(result.indexOf('create'), -1);

@@ -729,5 +836,5 @@ });

try {
await db.createCollection();
await mongo.createCollection();
} catch (error) {
assert.equal(error.message, 'must pass name of collection to create');
assert.ok(error);
}

@@ -744,3 +851,3 @@ });

];
beforeEach(async () => await db.insertMany(NAME, { docs }));
beforeEach(async () => await mongo.insertMany(NAME, { docs }));

@@ -757,3 +864,3 @@ it('should success', async () => {

];
const [result] = await db.aggregate(NAME, { pipeline });
const [result] = await mongo.aggregate(NAME, { pipeline });
assert.equal(result.count, docs.length);

@@ -764,3 +871,3 @@ });

try {
await db.aggregate(NAME, { pipeline: {} });
await mongo.aggregate(NAME, { pipeline: {} });
} catch (error) {

@@ -773,3 +880,3 @@ assert.equal(error.name, 'MongoError');

try {
await db.aggregate(NAME);
await mongo.aggregate(NAME);
} catch (error) {

@@ -780,2 +887,44 @@ assert(error);

});
describe('startSession()', () => {
it('should OK with MongoDB 3.6 above', () => {
if (version < 3.6) return;
const session = mongo.startSession();
assert.equal(session.constructor.name, 'ClientSession');
});
it('should error with MongoDB under 3.6', () => {
if (version >= 3.6) return;
assert.throws(() => {
try {
mongo.startSession();
} catch (error) {
throw error;
}
}, Error);
});
});
describe('startTransaction()', () => {
it('should OK with MongoDB 4.0 above', () => {
if (version < 4) return;
const sess = mongo.startTransaction();
assert.ok(sess.inTransaction());
});
it('should error with MongoDB under 4.0', () => {
if (version >= 4) return;
assert.throws(() => {
try {
mongo.startTransaction();
} catch (error) {
throw error;
}
}, Error);
});
});
});

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

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

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc