xpress-mongo
Advanced tools
Comparing version 2.6.0 to 2.7.0
{ | ||
"name": "xpress-mongo", | ||
"version": "2.6.0", | ||
"version": "2.7.0", | ||
"description": "Light Weight ODM for mongoDb NodeJs", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -351,5 +351,19 @@ import ObjectCollection from "object-collection"; | ||
* ==> 300 | ||
* @param field | ||
* @param match | ||
*/ | ||
static sum(field: string, match?: StringToAnyObject): Promise<number>; | ||
/** | ||
* Sum fields in this collection. | ||
* @example | ||
* data: [ | ||
* {name: 'john', credit: 100, debit: 400}, | ||
* {name: 'doe', credit: 200, debit: 300} | ||
* ] | ||
* | ||
* const sumOfBoth = await Model.sum(['credit', 'debit']); | ||
* ==> {credit: 300, debit: 700} | ||
* const sum = await Model.sum(['credit', 'debit']); | ||
* // {credit: 300, debit: 700} | ||
* // OR | ||
* const sum = await Model.sum({income: 'credit', expense: 'debit'}); | ||
* // {income: 300, expense: 700} | ||
* | ||
@@ -359,5 +373,3 @@ * @param fields | ||
*/ | ||
static sum(fields: string | StringToAnyObject | string[], match?: StringToAnyObject): Promise<number | { | ||
[name: string]: number; | ||
}>; | ||
static sumMany<T extends string[] | readonly string[] | StringToAnyObject>(fields: T, match?: StringToAnyObject): Promise<T extends string[] | readonly string[] ? Record<T[number], number> : Record<keyof T, number>>; | ||
/** | ||
@@ -364,0 +376,0 @@ * Count Aggregations |
@@ -1002,5 +1002,22 @@ "use strict"; | ||
* ==> 300 | ||
* @param field | ||
* @param match | ||
*/ | ||
static async sum(field, match) { | ||
const sum = await this.sumMany([field], match); | ||
return sum[field]; | ||
} | ||
/** | ||
* Sum fields in this collection. | ||
* @example | ||
* data: [ | ||
* {name: 'john', credit: 100, debit: 400}, | ||
* {name: 'doe', credit: 200, debit: 300} | ||
* ] | ||
* | ||
* const sumOfBoth = await Model.sum(['credit', 'debit']); | ||
* ==> {credit: 300, debit: 700} | ||
* const sum = await Model.sum(['credit', 'debit']); | ||
* // {credit: 300, debit: 700} | ||
* // OR | ||
* const sum = await Model.sum({income: 'credit', expense: 'debit'}); | ||
* // {income: 300, expense: 700} | ||
* | ||
@@ -1010,7 +1027,5 @@ * @param fields | ||
*/ | ||
static async sum(fields, match) { | ||
static async sumMany(fields, match) { | ||
const $group = { _id: null }; | ||
const $result = {}; | ||
if (typeof fields === "string") | ||
fields = [fields]; | ||
const fieldIsArray = Array.isArray(fields); | ||
@@ -1031,5 +1046,7 @@ if (fieldIsArray) { | ||
} | ||
let result = await this.native() | ||
.aggregate([{ $match: match }, { $group }]) | ||
.toArray(); | ||
const pipeline = []; | ||
if (match) | ||
pipeline.push({ $match: match }); | ||
pipeline.push({ $group }); | ||
let result = await this.native().aggregate(pipeline).toArray(); | ||
if (result.length) { | ||
@@ -1040,3 +1057,3 @@ for (const field of fields) { | ||
} | ||
return fields.length === 1 ? $result[fields[0]] : $result; | ||
return $result; | ||
} | ||
@@ -1043,0 +1060,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
104966
3231