New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

mongodb-utils

Package Overview
Dependencies
Maintainers
3
Versions
7
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

mongodb-utils - npm Package Compare versions

Comparing version

to
1.1.4

23

lib/index.js

@@ -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))

2

package.json
{
"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 @@