@discue/mongodb-resource-client
Advanced tools
Comparing version 0.26.0 to 0.27.0
@@ -15,2 +15,19 @@ export function EQUALS(field: string, target: unknown): any; | ||
export function LOOKUP({ from, as, localField, foreignField, pipeline }: LookupOptions): any; | ||
export function REDUCE({ input, initialValue, inExpression }: ReduceOptions): any; | ||
export function APPEND_OBJECTS(...expressions: any[]): any; | ||
export function LET({ vars, inExpression }: LetOptions): { | ||
$let: { | ||
vars: any; | ||
in: any; | ||
}; | ||
}; | ||
export function TO_OBJECT({ key, value }: ToObjectOptions): { | ||
$arrayToObject: { | ||
k: string; | ||
v: any; | ||
}[][]; | ||
}; | ||
export function CONCAT_STRINGS(...strings: any[]): any; | ||
export function JOIN_STRINGS(separator: string, ...strings: any[]): any; | ||
export function CONCAT_ARRAYS(): any; | ||
export function ELEMENT_AT(arrayName: string, index: number): any; | ||
@@ -47,1 +64,35 @@ export function AS_ROOT(fieldName: string): any; | ||
}; | ||
export type ReduceOptions = { | ||
/** | ||
* any expression resolving to an array | ||
*/ | ||
input: string; | ||
/** | ||
* the initial value used for reduction | ||
*/ | ||
initialValue: any; | ||
/** | ||
* any expression applied to each element of the array | ||
*/ | ||
inExpression: any; | ||
}; | ||
export type LetOptions = { | ||
/** | ||
* an object defining additional variables for the expression | ||
*/ | ||
vars: any; | ||
/** | ||
* any expression | ||
*/ | ||
inExpression: any; | ||
}; | ||
export type ToObjectOptions = { | ||
/** | ||
* the object key | ||
*/ | ||
key: string; | ||
/** | ||
* any expression | ||
*/ | ||
expression: any; | ||
}; |
@@ -211,3 +211,3 @@ /** | ||
*/ | ||
module.exports.LOOKUP = ({ from, as, localField, foreignField, pipeline = [] }) => { | ||
module.exports.LOOKUP = ({ from, as, localField, foreignField = 'id', pipeline = [] }) => { | ||
return { | ||
@@ -221,3 +221,116 @@ $lookup: { | ||
/** | ||
* @typedef ReduceOptions | ||
* @property {String} input any expression resolving to an array | ||
* @property {any} initialValue the initial value used for reduction | ||
* @property {Object} inExpression any expression applied to each element of the array | ||
*/ | ||
/** | ||
* | ||
* @param {ReduceOptions} options | ||
* @returns {Object} | ||
*/ | ||
module.exports.REDUCE = ({ input, initialValue = {}, inExpression }) => { | ||
return { | ||
$reduce: { | ||
input, initialValue, in: inExpression | ||
} | ||
} | ||
} | ||
/** | ||
* Returns a $mergeObjects expression that appends the result of the given expressions to $$value | ||
* | ||
* @param {...any} expressions a list of expressions that evaluate to an object | ||
* @returns {Object} | ||
*/ | ||
module.exports.APPEND_OBJECTS = (...expressions) => { | ||
return { | ||
$mergeObjects: ["$$value", ...expressions] | ||
} | ||
} | ||
/** | ||
* @typedef LetOptions | ||
* @property {Object} vars an object defining additional variables for the expression | ||
* @property {Object} inExpression any expression | ||
*/ | ||
/** | ||
* | ||
* @param {LetOptions} options | ||
* @returns | ||
*/ | ||
module.exports.LET = ({ vars, inExpression }) => { | ||
return { | ||
$let: { | ||
vars, in: inExpression | ||
} | ||
} | ||
} | ||
/** | ||
* @typedef ToObjectOptions | ||
* @property {String} key the object key | ||
* @property {Object} expression any expression | ||
*/ | ||
/** | ||
* | ||
* @param {ToObjectOptions} options | ||
* @returns | ||
*/ | ||
module.exports.TO_OBJECT = ({ key = "$$this", value }) => { | ||
return { | ||
$arrayToObject: [ | ||
[ | ||
{ | ||
k: key, | ||
v: value | ||
} | ||
] | ||
] | ||
} | ||
} | ||
/** | ||
* | ||
* @param {...any} strings strings to concat | ||
* @returns {Object} | ||
*/ | ||
module.exports.CONCAT_STRINGS = (...strings) => { | ||
return { | ||
$concat: strings | ||
} | ||
} | ||
/** | ||
* | ||
* @param {String} separator separator to be used for joining given strings | ||
* @param {...any} strings strings to concat | ||
* @returns {Object} | ||
*/ | ||
module.exports.JOIN_STRINGS = (separator, ...strings) => { | ||
const array = strings.reduce((context, next) => { | ||
context.push(separator, next) | ||
return context | ||
}, []) | ||
return module.exports.CONCAT_STRINGS.apply(null, array) | ||
} | ||
/** | ||
* | ||
* @returns {Object} | ||
*/ | ||
module.exports.CONCAT_ARRAYS = () => { | ||
return { | ||
$concatArrays: [ | ||
"$$value", | ||
"$$this" | ||
] | ||
} | ||
} | ||
/** | ||
* | ||
* @param {String} arrayName name of the array | ||
@@ -224,0 +337,0 @@ * @param {Number} index |
@@ -1,2 +0,2 @@ | ||
const { PROJECT, EQUALS, LOOKUP } = require('./aggregations.js') | ||
const { PROJECT, EQUALS, LOOKUP, REDUCE, CONCAT_ARRAYS, TO_OBJECT, LET, APPEND_OBJECTS, JOIN_STRINGS } = require('./aggregations.js') | ||
@@ -19,3 +19,2 @@ /** | ||
localField: childCollectionName, | ||
foreignField: 'id', | ||
as: childCollectionName | ||
@@ -44,8 +43,6 @@ }) | ||
const lookupPipeline = [ | ||
{ | ||
"$project": { | ||
"_id": 0, | ||
"_meta_data": options.withMetadata ? 1 : 0 | ||
} | ||
} | ||
PROJECT({ | ||
_id: 0, | ||
_meta_data: options.withMetadata ? 1 : 0 | ||
}) | ||
] | ||
@@ -57,30 +54,48 @@ | ||
const pipeline = [{ | ||
$project: { | ||
const pipeline = [ | ||
PROJECT({ | ||
_id: 0, | ||
[childCollectionName]: { | ||
$reduce: { | ||
parent: `$${parentCollectionName}`, | ||
[childCollectionName]: | ||
REDUCE({ | ||
input: `$${parentCollectionName}.${childCollectionName}`, | ||
initialValue: [], | ||
in: { | ||
$concatArrays: [ | ||
"$$value", | ||
"$$this" | ||
] | ||
} | ||
} | ||
} | ||
} | ||
}, | ||
{ | ||
$lookup: { | ||
inExpression: CONCAT_ARRAYS() | ||
}) | ||
}), | ||
LOOKUP({ | ||
from: childCollectionName, | ||
pipeline: lookupPipeline, | ||
as: childCollectionName, | ||
as: "children", | ||
localField: childCollectionName, | ||
foreignField: "id" | ||
} | ||
}] | ||
}), | ||
PROJECT({ | ||
children: "$children", | ||
resource_paths: REDUCE({ | ||
input: `$parent`, | ||
inExpression: APPEND_OBJECTS( | ||
LET({ | ||
vars: { | ||
parent: "$$this.id", | ||
children: `$$this.${childCollectionName}` | ||
}, | ||
inExpression: REDUCE({ | ||
input: "$$children", | ||
inExpression: | ||
APPEND_OBJECTS( | ||
TO_OBJECT({ | ||
value: JOIN_STRINGS('/', | ||
`${parentCollectionName}`, | ||
"$$parent", | ||
`${childCollectionName}`, | ||
"$$this") | ||
})) | ||
}) | ||
}) | ||
) | ||
}) | ||
}) | ||
] | ||
return pipeline | ||
} |
@@ -101,7 +101,9 @@ /// <reference types="node" /> | ||
/** | ||
* @typedef ChildrenAndResourcePaths | ||
* @property {Array.<Object>} children | ||
* @property {Object} resourcePaths and object mapping child ids to their resource path e.g. { 4: '/queues/123/listeners/4'} | ||
*/ | ||
/** | ||
* 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 | ||
@@ -111,4 +113,11 @@ * @param {String|Array.<String>} resourceIds resource ids that will added to the resource path i.e. /users/${id}/documents/${id} | ||
* @param {GetOptions} options | ||
* @returns {Promise.<ChildrenAndResourcePaths>} | ||
*/ | ||
getAllChildren(resourceIds: string | Array<string>, childName: any, { withMetadata, projection }?: GetOptions): Promise<any>; | ||
getAllChildren(resourceIds: string | Array<string>, childName: any, { withMetadata, projection }?: GetOptions): Promise<{ | ||
children: Array<any>; | ||
/** | ||
* and object mapping child ids to their resource path e.g. { 4: '/queues/123/listeners/4'} | ||
*/ | ||
resourcePaths: any; | ||
}>; | ||
/** | ||
@@ -115,0 +124,0 @@ * Returns all resources that pass the given aggregation stages. |
@@ -295,7 +295,10 @@ const { MongoClient, Timestamp } = require('mongodb') | ||
/** | ||
* @typedef ChildrenAndResourcePaths | ||
* @property {Array.<Object>} children | ||
* @property {Object} resourcePaths and object mapping child ids to their resource path e.g. { 4: '/queues/123/listeners/4'} | ||
*/ | ||
/** | ||
* 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 | ||
@@ -305,5 +308,6 @@ * @param {String|Array.<String>} resourceIds resource ids that will added to the resource path i.e. /users/${id}/documents/${id} | ||
* @param {GetOptions} options | ||
* @returns {Promise.<ChildrenAndResourcePaths>} | ||
*/ | ||
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 () => { | ||
return withActiveSpan(`${name}#get-all-simple-resource-children`, { resourceIds, resourceName: this._collectionName, databaseName: this._databaseName }, async () => { | ||
@@ -317,2 +321,4 @@ const lookupPipeline = getSingleLookupPipeline({ rootId: this._toStringIfArray(resourceIds), childCollectionName: 'queues' }) | ||
console.log(JSON.stringify(result, null, 2)) | ||
if (result.length === 0) { | ||
@@ -322,26 +328,9 @@ return result | ||
const resources = result.at(0)[childName] | ||
if (!resources || resources.length === 0) { | ||
const children = result.at(0).children | ||
let resourcePaths = result.at(0).resource_paths | ||
if (!children || children.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 | ||
return { children, resourcePaths } | ||
}) | ||
@@ -348,0 +337,0 @@ } |
@@ -5,3 +5,3 @@ { | ||
"license": "MIT", | ||
"version": "0.26.0", | ||
"version": "0.27.0", | ||
"description": "Simple wrapper around mongodb client allowing easier managing of resources", | ||
@@ -20,6 +20,9 @@ "main": "lib/index", | ||
"*": { | ||
"aggregations": [ | ||
".": [ | ||
"lib/index.d.ts" | ||
], | ||
"./aggregations": [ | ||
"lib/aggregations.d.ts" | ||
], | ||
"datatypes": [ | ||
"./datatypes": [ | ||
"lib/datatypes.d.ts" | ||
@@ -26,0 +29,0 @@ ] |
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
118505
2970