@discue/mongodb-resource-client
Advanced tools
Comparing version 0.25.0 to 0.26.0
@@ -9,8 +9,2 @@ export = exports; | ||
_resourceName: string; | ||
_emitUsageEvent: ({ context, resourceIds, before, after }: { | ||
context: any; | ||
resourceIds: any; | ||
before: any; | ||
after: any; | ||
}, callback: any) => Promise<any>; | ||
/** | ||
@@ -17,0 +11,0 @@ * Returns a resource by ids. |
@@ -17,3 +17,3 @@ /// <reference types="node" /> | ||
_emitUsageEventEnabled: boolean; | ||
_emiteUsageEvent: ({ context, resourceIds, before, after }: { | ||
_emitUsageEvent: ({ context, resourceIds, before, after }: { | ||
context: any; | ||
@@ -102,2 +102,14 @@ resourceIds: any; | ||
/** | ||
* Returns all children of a certain type/collection. Imagine this method walking a tree and returning all leaves at a certain level. | ||
* | ||
* Example: | ||
* | ||
* | ||
* @name getAll | ||
* @param {String|Array.<String>} resourceIds resource ids that will added to the resource path i.e. /users/${id}/documents/${id} | ||
* @param {String|Array.<String>} childPath the path of the children to query e.g. /api_clients/queues/messages | ||
* @param {GetOptions} options | ||
*/ | ||
getAllChildren(resourceIds: string | Array<string>, childName: any, { withMetadata, projection }?: GetOptions): Promise<any>; | ||
/** | ||
* Returns all resources that pass the given aggregation stages. | ||
@@ -104,0 +116,0 @@ * |
@@ -6,2 +6,3 @@ const { MongoClient, Timestamp } = require('mongodb') | ||
const { name } = require('../package.json') | ||
const { getSingleLookupPipeline, joinAndQueryChildResourcesPipeline } = require('./lookup-pipeline.js') | ||
@@ -91,3 +92,3 @@ /** | ||
this._emitUsageEventEnabled = eventEmitter != null | ||
this._emiteUsageEvent = eventTrigger(this.usageEventPrefix, collectionName, eventEmitter) | ||
this._emitUsageEvent = eventTrigger(this.usageEventPrefix, collectionName, eventEmitter) | ||
@@ -113,3 +114,3 @@ this._transactionsEnabled = process.env.DSQ_MONGOD_ENABLE_TRANSACTIONS === 'true' | ||
this._emitUsageEventEnabled = eventEmitter != null | ||
this._emiteUsageEvent = eventTrigger(this.usageEventPrefix, this._collectionName, eventEmitter) | ||
this._emitUsageEvent = eventTrigger(this.usageEventPrefix, this._collectionName, eventEmitter) | ||
} | ||
@@ -122,3 +123,3 @@ | ||
this._emitUsageEventEnabled = false | ||
this._emiteUsageEvent = null | ||
this._emitUsageEvent = null | ||
} | ||
@@ -298,2 +299,54 @@ | ||
/** | ||
* Returns all children of a certain type/collection. Imagine this method walking a tree and returning all leaves at a certain level. | ||
* | ||
* Example: | ||
* | ||
* | ||
* @name getAll | ||
* @param {String|Array.<String>} resourceIds resource ids that will added to the resource path i.e. /users/${id}/documents/${id} | ||
* @param {String|Array.<String>} childPath the path of the children to query e.g. /api_clients/queues/messages | ||
* @param {GetOptions} options | ||
*/ | ||
async getAllChildren(resourceIds, childName, { withMetadata = false, projection } = {}) { | ||
return withActiveSpan(`${name}#get-all-one-to-many-resources-by-id`, { resourceIds, resourceName: this._collectionName, databaseName: this._databaseName }, async () => { | ||
const lookupPipeline = getSingleLookupPipeline({ rootId: this._toStringIfArray(resourceIds), childCollectionName: 'queues' }) | ||
const childResourcesPipeline = joinAndQueryChildResourcesPipeline({ parentCollectionName: 'queues', childCollectionName: 'listeners', options: { withMetadata, projection } }) | ||
lookupPipeline.push(...childResourcesPipeline) | ||
const collection = await this._getCollection() | ||
const result = await collection.aggregate(lookupPipeline).toArray() | ||
if (result.length === 0) { | ||
return result | ||
} | ||
const resources = result.at(0)[childName] | ||
if (!resources || resources.length === 0) { | ||
return [] | ||
} | ||
// if (addDocumentPath) { | ||
// resources.forEach((result) => { | ||
// const { id } = result | ||
// // not ideal but right now lets only support one reference per document | ||
// // refsArray.forEach((refs) => { | ||
// const path = this._resourcePath.reduce((context, path, index) => { | ||
// if (!this._hiddenResourcePath.includes(path)) { | ||
// context.push(path, resourceIds.at(index)) | ||
// } | ||
// return context | ||
// // }, []) | ||
// }, ['']) | ||
// path.push(this._resourceName, id) | ||
// result.$path = path.join('/') | ||
// }) | ||
// } | ||
return resources | ||
}) | ||
} | ||
/** | ||
* Returns all resources that pass the given aggregation stages. | ||
@@ -370,3 +423,3 @@ * | ||
if (this._emitUsageEventEnabled) { | ||
await this._emiteUsageEvent({ context: 'create', after: resource, resourceIds }) | ||
await this._emitUsageEvent({ context: 'create', after: resource, resourceIds }) | ||
} | ||
@@ -411,3 +464,3 @@ | ||
const newResource = await this.get(resourceIds) | ||
await this._emiteUsageEvent({ context: 'update', before: resource, after: newResource, resourceIds }) | ||
await this._emitUsageEvent({ context: 'update', before: resource, after: newResource, resourceIds }) | ||
} | ||
@@ -499,3 +552,3 @@ | ||
if (this._emitUsageEventEnabled) { | ||
await this._emiteUsageEvent({ context: 'delete', before: resource, resourceIds }) | ||
await this._emitUsageEvent({ context: 'delete', before: resource, resourceIds }) | ||
} | ||
@@ -536,3 +589,3 @@ | ||
if (this._emitUsageEventEnabled) { | ||
await this._emiteUsageEvent({ context: 'close' }) | ||
await this._emitUsageEvent({ context: 'close' }) | ||
} | ||
@@ -539,0 +592,0 @@ return this._mongoDbClient.close() |
@@ -5,3 +5,3 @@ { | ||
"license": "MIT", | ||
"version": "0.25.0", | ||
"version": "0.26.0", | ||
"description": "Simple wrapper around mongodb client allowing easier managing of resources", | ||
@@ -57,10 +57,10 @@ "main": "lib/index", | ||
"@types/mocha": "^10.0.6", | ||
"@types/node": "^20.10.4", | ||
"@types/node": "^20.11.17", | ||
"chai": "^4.3.10", | ||
"documentation": "^14.0.2", | ||
"documentation": "^14.0.3", | ||
"eslint": "^8.56.0", | ||
"mocha": "^10.2.0", | ||
"mocha": "^10.3.0", | ||
"mongodb": "^6.3.0", | ||
"mongodb-memory-server": "^9.1.3", | ||
"nodemon": "^3.0.2", | ||
"mongodb-memory-server": "^9.1.6", | ||
"nodemon": "^3.0.3", | ||
"standard-version": "^9.5.0", | ||
@@ -67,0 +67,0 @@ "typescript": "^5.3.3" |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
113873
26
2803