mongodb-utils
Advanced tools
Comparing version
@@ -12,2 +12,3 @@ const { ObjectID, Collection } = require('mongodb') | ||
mongoCollection.utils = { | ||
// Documentation https://github.com/terrajs/mongodb-utils#get | ||
get: function (query, fields) { | ||
@@ -21,2 +22,3 @@ if (Array.isArray(fields)) { | ||
}, | ||
// Documentation https://github.com/terrajs/mongodb-utils#create | ||
create: async function (doc) { | ||
@@ -30,2 +32,3 @@ doc.createdAt = new Date() | ||
}, | ||
// Documentation https://github.com/terrajs/mongodb-utils#update | ||
update: async function (query, doc) { | ||
@@ -44,2 +47,21 @@ doc.updatedAt = new Date() | ||
}, | ||
// Documentation https://github.com/terrajs/mongodb-utils#upsert | ||
upsert: async function (query, doc) { | ||
doc.updatedAt = new Date() | ||
const set = { | ||
$set: doc, | ||
$setOnInsert: { | ||
createdAt: new Date() | ||
} | ||
} | ||
const result = await mongoCollection.findOneAndUpdate(getQueryFromArguments(query), set, { | ||
returnOriginal: false, | ||
upsert: true | ||
}) | ||
return result.value | ||
}, | ||
// Documentation https://github.com/terrajs/mongodb-utils#remove | ||
remove: async function (query) { | ||
@@ -50,2 +72,3 @@ const result = await mongoCollection.deleteOne(getQueryFromArguments(query)) | ||
}, | ||
// Documentation https://github.com/terrajs/mongodb-utils#find | ||
find: function (query, options) { | ||
@@ -52,0 +75,0 @@ const cursor = mongoCollection.find(getQueryFromArguments(query)) |
{ | ||
"name": "mongodb-utils", | ||
"version": "1.1.3", | ||
"version": "1.1.4", | ||
"description": "Utils for mongodb for nodejs", | ||
@@ -5,0 +5,0 @@ "main": "lib/", |
@@ -81,2 +81,19 @@ # mongodb-utils | ||
### upsert | ||
```js | ||
upsert(query = { key: value } || string || ObjectID, doc): Promise<doc> | ||
``` | ||
Update or create a document if not exist | ||
Add the `createdAt` if document not exist | ||
```js | ||
// 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' }) | ||
// 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' }) | ||
``` | ||
### remove | ||
@@ -83,0 +100,0 @@ |
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
9208
17.14%73
37.74%136
14.29%0
-100%