@discue/mongodb-resource-client
Advanced tools
Comparing version 0.38.1 to 0.39.0
@@ -121,2 +121,14 @@ export = exports; | ||
/** | ||
* Returns the count of all children of a certain type/collection. | ||
* | ||
* Currently only supports trees with three levels. | ||
* | ||
* @name countAllChildren | ||
* @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 {GetChildrenOptions} [options] | ||
* @returns {Promise.<number>} | ||
*/ | ||
countAllChildren(resourceIds: string | Array<string>, childPath: string | Array<string>, { withMetadata, projection, match }?: GetChildrenOptions): Promise<number>; | ||
/** | ||
* Returns all resources that pass the given aggregation stages. | ||
@@ -123,0 +135,0 @@ * |
@@ -267,3 +267,2 @@ 'use strict' | ||
const result = await toArrayAndClose(cursor) | ||
if (result.length === 0) { | ||
@@ -281,2 +280,40 @@ return {} | ||
/** | ||
* Returns the count of all children of a certain type/collection. | ||
* | ||
* Currently only supports trees with three levels. | ||
* | ||
* @name countAllChildren | ||
* @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 {GetChildrenOptions} [options] | ||
* @returns {Promise.<number>} | ||
*/ | ||
async countAllChildren(resourceIds, childPath, { withMetadata = false, projection, match } = {}) { | ||
return this._withActiveSpan('count-all-simple-resource-children', resourceIds, async () => { | ||
if (childPath.startsWith('/')) { | ||
childPath = childPath.substring(1) | ||
} | ||
const [parent, child] = childPath.split('/') | ||
if (!parent || !child) { | ||
throw new Error(`childPath with value ${childPath} does not match expected value "parent/child"`) | ||
} | ||
const lookupPipeline = getSingleLookupPipeline({ rootId: this._toStringIfArray(resourceIds), childCollectionName: parent }) | ||
const childResourcesPipeline = joinAndQueryChildResourcesPipeline({ parentCollectionName: parent, childCollectionName: child, options: { withMetadata, projection, match } }) | ||
lookupPipeline.push(...childResourcesPipeline) | ||
lookupPipeline.push(PROJECT({ count: { $size: '$children' } })) | ||
const collection = await this._getCollection() | ||
const cursor = collection.aggregate(lookupPipeline) | ||
const result = await toArrayAndClose(cursor) | ||
if (result.length === 0) { | ||
return 0 | ||
} | ||
return result.at(0).count | ||
}) | ||
} | ||
/** | ||
* Returns all resources that pass the given aggregation stages. | ||
@@ -283,0 +320,0 @@ * |
@@ -5,3 +5,3 @@ { | ||
"license": "MIT", | ||
"version": "0.38.1", | ||
"version": "0.39.0", | ||
"description": "Simple wrapper around mongodb client allowing easier managing of resources", | ||
@@ -8,0 +8,0 @@ "main": "lib/index", |
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
138922
3506