mongodb-utils
Advanced tools
Comparing version
@@ -1,2 +0,2 @@ | ||
const { ObjectID, Collection } = require('mongodb') | ||
const { ObjectID } = require('mongodb') | ||
@@ -9,3 +9,3 @@ function getQueryFromArguments(query) { | ||
module.exports = function (mongoCollection) { | ||
if (!(mongoCollection instanceof Collection)) throw new Error('no-mongo-collection-provided') | ||
if (!mongoCollection || !mongoCollection.findOne) throw new Error('no-mongo-collection-provided') | ||
@@ -12,0 +12,0 @@ mongoCollection.utils = { |
{ | ||
"name": "mongodb-utils", | ||
"version": "1.1.5", | ||
"version": "1.1.6", | ||
"description": "Utils for mongodb for nodejs", | ||
@@ -40,10 +40,10 @@ "main": "lib/", | ||
"devDependencies": { | ||
"@terrajs/mono": "^0.6.0", | ||
"ava": "^0.22.0", | ||
"codecov": "^2.3.1", | ||
"eslint": "^4.9.0", | ||
"mongodb": "^2.2.33", | ||
"@terrajs/mono": "^0.8.0", | ||
"ava": "^0.24.0", | ||
"codecov": "^3.0.0", | ||
"eslint": "^4.16.0", | ||
"mongodb": "^3.0.1", | ||
"mono-mongodb": "^2.3.1", | ||
"mono-test-utils": "^1.2.1", | ||
"nyc": "^11.2.1", | ||
"mono-test-utils": "^1.2.3", | ||
"nyc": "^11.4.1", | ||
"rimraf": "^2.6.2", | ||
@@ -50,0 +50,0 @@ "std-mocks": "^1.0.1" |
@@ -1,4 +0,4 @@ | ||
# mongodb-utils | ||
<h1 align="left"><img src="https://user-images.githubusercontent.com/904724/31862915-8a718272-b746-11e7-912f-9ad658a96907.png" alt="MongoDB Utils"/></h1> | ||
MongoDB utils library for node | ||
> MongoDB utils for Node.js | ||
@@ -31,2 +31,9 @@ [](https://www.npmjs.com/package/mongodb-utils) | ||
- [get](#get) | ||
- [create](#create) | ||
- [update](#update) | ||
- [upsert](#upsert) | ||
- [remove](#remove) | ||
- [find](#find) | ||
### get | ||
@@ -56,3 +63,3 @@ | ||
```js | ||
create(doc): Promise<doc> | ||
create(doc: object): Promise<doc> | ||
``` | ||
@@ -70,3 +77,3 @@ | ||
```js | ||
update(query = { key: value } || string || ObjectID, doc): Promise<doc> | ||
update(query = { key: value } || string || ObjectID, doc: object): Promise<doc> | ||
``` | ||
@@ -87,3 +94,3 @@ | ||
```js | ||
upsert(query = { key: value } || string || ObjectID, doc): Promise<doc> | ||
upsert(query = { key: value } || string || ObjectID, doc: object): Promise<doc> | ||
``` | ||
@@ -96,6 +103,6 @@ | ||
// Update the document that match the query { _id: ObjectID('59c0de2dfe8fa448605b1d89') } and update its username or create it if not exist | ||
await collection.utils.update('59c0de2dfe8fa448605b1d89', { username: 'terrajs2' }) | ||
await collection.utils.upsert('59c0de2dfe8fa448605b1d89', { username: 'terrajs2' }) | ||
// Update the document that match the query { username: 'terrajs2' } and update its username or create it if not exist | ||
await collection.utils.update({ username: 'terrajs2' }, { username: 'terrajs' }) | ||
// Update the document that match the query { username: 'terrajs2' } and update its username OR create it if not found | ||
await collection.utils.upsert({ username: 'terrajs2' }, { username: 'terrajs' }) | ||
``` | ||
@@ -122,3 +129,3 @@ | ||
```js | ||
find(query = { key: value } || string || ObjectID, [options = { fields: ..., limit: ..., offset: ..., sort: ... }]): Promise<cursor> | ||
find(query = { key: value } || string || ObjectID, [options = { fields: ..., limit: ..., offset: ..., sort: ... }]): cursor | ||
``` | ||
@@ -134,9 +141,9 @@ The find method return a mongo cursor from a specific query and options. | ||
```js | ||
// We find document that match the query { username: new RegExp(/^test/g) }, options with { username: 1, createdAt: 1 } projection and limit at 1 element | ||
const request = await userCollection.mono.find({ | ||
// Find documents that matches the query { username: new RegExp(/^test/g) }, options with { username: 1, createdAt: 1 } projection and limit at 10 elements | ||
const users = await userCollection.mono.find({ | ||
username: new RegExp(/^test/g) | ||
}, { | ||
fields: ['username', 'createdAt'], | ||
limit: 1 | ||
}) | ||
limit: 10 | ||
}).toArray() | ||
``` |
9471
2.87%143
5.15%