Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@discue/mongodb-resource-client

Package Overview
Dependencies
Maintainers
1
Versions
43
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@discue/mongodb-resource-client - npm Package Compare versions

Comparing version 0.5.0 to 0.6.0

22

lib/one-to-few-resource-storage.js

@@ -147,3 +147,3 @@ const { PROJECT, EQUALS } = require('./aggregations.js')

async create(resourceIds, resource) {
return this._emitUsageEvent('create', resourceIds, async () => {
return this._emitUsageEvent({ context: 'create', resource, resourceIds }, async () => {
const exists = await this.exists(resourceIds)

@@ -184,8 +184,8 @@ if (exists) {

async update(resourceIds, update) {
return this._emitUsageEvent('update', resourceIds, async () => {
const exists = await this.exists(resourceIds)
if (!exists) {
throw new Error(`${resourceIds} does not exist.`)
}
const resource = await this.get(resourceIds)
if (!resource) {
throw new Error(`${resourceIds} does not exist.`)
}
return this._emitUsageEvent({ context: 'update', resource, resourceIds }, async () => {
const hasAtomicOperator = Object.keys(update).find(key => key.startsWith('$'))

@@ -230,8 +230,8 @@ if (!hasAtomicOperator) {

async delete(resourceIds) {
return this._emitUsageEvent('delete', resourceIds, async () => {
const exists = await this.exists(resourceIds)
if (!exists) {
throw new Error(`${resourceIds} does not exist.`)
}
const resource = await this.get(resourceIds)
if (!resource) {
throw new Error(`${resourceIds} does not exist.`)
}
return this._emitUsageEvent({ context: 'delete', resource, resourceIds }, async () => {
const collection = await this._getCollection()

@@ -238,0 +238,0 @@ const result = await collection.findOneAndUpdate({

@@ -91,2 +91,3 @@ const Base = require('./simple-resource-storage.js')

baseOptions.collectionName = options.resourceName
baseOptions.eventEmitter = null
super(baseOptions)

@@ -104,3 +105,4 @@

this._emitUsageEvent = usageEventTrigger(this.usageEventPrefix, options.resourceName, options.eventEmitter)
this._emitUsageEventEnabled = options.eventEmitter != null
this._childEmitUsageEvent = usageEventTrigger(this.usageEventPrefix, options.resourceName, options.eventEmitter)
}

@@ -322,3 +324,3 @@

async create(resourceIds, resource) {
return this._emitUsageEvent('create', resourceIds, async () => {
return this._childEmitUsageEvent({ context: 'create', resource, resourceIds }, async () => {
const newId = await super.create(resourceIds.slice(-1), resource)

@@ -346,9 +348,15 @@ await this._hostStorage.create(this._getResourceIdsForHostStorage(resourceIds), newId)

async update(resourceIds, update) {
return this._emitUsageEvent('update', resourceIds, async () => {
const exists = await this.exists(resourceIds)
if (!exists) {
throw new Error(`${resourceIds} does not exist.`)
}
return super._updateUnsafe(resourceIds.slice(-1), update)
})
const resource = await this.get(resourceIds)
if (!resource) {
throw new Error(`${resourceIds} does not exist.`)
}
const updateResult = await super._updateUnsafe(resourceIds.slice(-1), update)
if (this._emitUsageEventEnabled) {
const newResource = await this.get(resourceIds)
await this._childEmitUsageEvent({ context: 'update', resource: newResource, resourceIds })
}
return updateResult
}

@@ -364,8 +372,9 @@

async delete(resourceIds) {
return this._emitUsageEvent('delete', resourceIds, async () => {
const exists = await this.exists(resourceIds)
if (!exists) {
throw new Error(`${resourceIds} does not exist.`)
}
await super._deleteUnsafe(resourceIds.slice(-1))
const resource = await this.get(resourceIds)
if (!resource) {
throw new Error(`${resourceIds} does not exist.`)
}
return this._childEmitUsageEvent({ context: 'delete', resource, resourceIds }, async () => {
await super._deleteUnsafe(resourceIds.slice(-1), resource)
return this._hostStorage.delete(this._getResourceIdsForHostStorage(resourceIds))

@@ -372,0 +381,0 @@ })

@@ -65,3 +65,4 @@ const { MongoClient, Timestamp } = require('mongodb')

this._emitUsageEvent = eventTrigger(this.usageEventPrefix, collectionName, eventEmitter)
this._emitUsageEventEnabled = eventEmitter != null
this._emiteUsageEvent = eventTrigger(this.usageEventPrefix, collectionName, eventEmitter)

@@ -196,3 +197,3 @@ this._getCollection().then(collection => {

async create(resourceIds, resource) {
return this._emitUsageEvent('create', resourceIds, async () => {
return this._emiteUsageEvent({ context: 'create', resource, resourceIds }, async () => {
const exists = await this.exists(resourceIds)

@@ -235,8 +236,15 @@ if (exists) {

async update(resourceIds, update) {
const exists = await this.exists(resourceIds)
if (!exists) {
const resource = await this.get(resourceIds)
if (!resource) {
throw new Error(`${resourceIds} does not exist.`)
}
return this._updateUnsafe(resourceIds, update)
const updateResult = await this._updateUnsafe(resourceIds, update)
if (this._emitUsageEventEnabled) {
const newResource = await this.get(resourceIds)
await this._emiteUsageEvent({ context: 'update', resource: newResource, resourceIds })
}
return updateResult
}

@@ -252,41 +260,39 @@

async _updateUnsafe(resourceIds, update) {
return this._emitUsageEvent('update', resourceIds, async () => {
const hasAtomicOperator = Object.keys(update).find(key => key.startsWith('$'))
let updateMetadataSeparately = false
if (!hasAtomicOperator) {
update = {
$set: update
}
const hasAtomicOperator = Object.keys(update).find(key => key.startsWith('$'))
let updateMetadataSeparately = false
if (!hasAtomicOperator) {
update = {
$set: update
}
}
const objectKeys = Object.keys(update)
const isSetOperation = objectKeys.at(0) === '$set'
if (isSetOperation) {
update = {
$set: Object.assign({}, update['$set'], this._createUpdateMetadata())
}
} else {
// let's not generically interfere with updates and in this case
// update meta data with another operation
updateMetadataSeparately = true
const objectKeys = Object.keys(update)
const isSetOperation = objectKeys.at(0) === '$set'
if (isSetOperation) {
update = {
$set: Object.assign({}, update['$set'], this._createUpdateMetadata())
}
} else {
// let's not generically interfere with updates and in this case
// update meta data with another operation
updateMetadataSeparately = true
}
const collection = await this._getCollection()
const result = await collection.updateOne({
id: this._toStringIfArray(resourceIds)
}, update)
const collection = await this._getCollection()
const result = await collection.updateOne({
id: this._toStringIfArray(resourceIds)
}, update)
const success = result.acknowledged === true
if (!success) {
throw new Error(`Was not able to update ${resourceIds} with resource ${update}.`)
}
const success = result.acknowledged === true
if (!success) {
throw new Error(`Was not able to update ${resourceIds} with resource ${update}.`)
}
if (updateMetadataSeparately) {
await collection.updateOne({
id: this._toStringIfArray(resourceIds)
}, {
$set: this._createUpdateMetadata()
})
}
})
if (updateMetadataSeparately) {
await collection.updateOne({
id: this._toStringIfArray(resourceIds)
}, {
$set: this._createUpdateMetadata()
})
}
}

@@ -309,8 +315,12 @@

async delete(resourceIds) {
const exists = await this.exists(resourceIds)
if (!exists) {
const resource = await this.get(resourceIds)
if (!resource) {
throw new Error(`${resourceIds} does not exist.`)
}
return this._deleteUnsafe(resourceIds)
if (this._emitUsageEventEnabled) {
return this._emiteUsageEvent({ context: 'delete', resource, resourceIds })
}
return this._deleteUnsafe(resourceIds, resource)
}

@@ -325,13 +335,11 @@

async _deleteUnsafe(resourceIds) {
return this._emitUsageEvent('delete', resourceIds, async () => {
const collection = await this._getCollection()
const result = await collection.deleteOne({
id: this._toStringIfArray(resourceIds)
})
const collection = await this._getCollection()
const result = await collection.deleteOne({
id: this._toStringIfArray(resourceIds)
})
const success = result.acknowledged === true
if (!success) {
throw new Error(`Was not able to delete ${resourceIds}.`)
}
})
const success = result.acknowledged === true
if (!success) {
throw new Error(`Was not able to delete ${resourceIds}.`)
}
}

@@ -338,0 +346,0 @@

@@ -9,5 +9,6 @@ /**

return async (context, resourceIds, callback) => {
return async ({ context, resourceIds, resource }, callback) => {
const event = {
context,
resource,
collectionName,

@@ -18,7 +19,9 @@ resourceIds,

try {
let result = callback.apply()
if (result.then) {
result = await result
if (callback) {
let result = callback.apply()
if (result.then) {
result = await result
}
return result
}
return result
} catch (e) {

@@ -25,0 +28,0 @@ event.error = true

@@ -5,3 +5,3 @@ {

"license": "MIT",
"version": "0.5.0",
"version": "0.6.0",
"description": "Simple wrapper around mongodb client allowing easier managing of resources",

@@ -8,0 +8,0 @@ "main": "lib/index",

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