xpress-mongo
Advanced tools
Comparing version 0.0.35 to 0.0.36
@@ -820,2 +820,53 @@ "use strict"; | ||
/** | ||
* Sum fields in this collection. | ||
* @example | ||
* data: [ | ||
* {name: 'john', credit: 100, debit: 400}, | ||
* {name: 'doe', credit: 200, debit: 300} | ||
* ] | ||
* | ||
* const sumOfCredit = await Model.sum('credit'); | ||
* ==> 300 | ||
* | ||
* const sumOfBoth = await Model.sum(['credit', 'debit']); | ||
* ==> {credit: 300, debit: 700} | ||
* | ||
* @param fields | ||
* @param match | ||
*/ | ||
static sum(fields, match) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const $group = { _id: null }; | ||
const $result = {}; | ||
if (typeof fields === 'string') | ||
fields = [fields]; | ||
const fieldIsArray = Array.isArray(fields); | ||
if (fieldIsArray) { | ||
for (const field of fields) { | ||
$group[field] = { $sum: '$' + field }; | ||
$result[field] = 0; | ||
} | ||
} | ||
else if (typeof fields === "object") { | ||
const keys = Object.keys(fields); | ||
for (const field of keys) { | ||
$group[field] = { $sum: '$' + fields[field] }; | ||
$result[field] = 0; | ||
} | ||
fields = keys; | ||
} | ||
let result = yield this.thisCollection().aggregate([ | ||
{ $match: match }, | ||
{ $group } | ||
]).toArray(); | ||
if (result.length) { | ||
result = result[0]; | ||
for (const field of fields) { | ||
$result[field] = result[field] || 0; | ||
} | ||
} | ||
return fields.length === 1 ? $result[fields[0]] : $result; | ||
}); | ||
} | ||
/** | ||
* Count Aggregations | ||
@@ -822,0 +873,0 @@ * @param query |
{ | ||
"name": "xpress-mongo", | ||
"version": "0.0.35", | ||
"version": "0.0.36", | ||
"description": "Light Weight ODM for mongoDb", | ||
@@ -5,0 +5,0 @@ "main": "js/index.js", |
@@ -975,3 +975,58 @@ import ObjectCollection = require('object-collection'); | ||
/** | ||
* Sum fields in this collection. | ||
* @example | ||
* data: [ | ||
* {name: 'john', credit: 100, debit: 400}, | ||
* {name: 'doe', credit: 200, debit: 300} | ||
* ] | ||
* | ||
* const sumOfCredit = await Model.sum('credit'); | ||
* ==> 300 | ||
* | ||
* const sumOfBoth = await Model.sum(['credit', 'debit']); | ||
* ==> {credit: 300, debit: 700} | ||
* | ||
* @param fields | ||
* @param match | ||
*/ | ||
static async sum(fields: string | StringToAnyObject | string[], match?: StringToAnyObject): Promise<number | { [name: string]: number }> { | ||
const $group: StringToAnyObject = {_id: null}; | ||
const $result: StringToAnyObject = {}; | ||
if (typeof fields === 'string') fields = [fields]; | ||
const fieldIsArray = Array.isArray(fields); | ||
if (fieldIsArray) { | ||
for (const field of fields as string[]) { | ||
$group[field] = {$sum: '$' + field}; | ||
$result[field] = 0; | ||
} | ||
} else if (typeof fields === "object") { | ||
const keys = Object.keys(fields); | ||
for (const field of keys as string[]) { | ||
$group[field] = {$sum: '$' + (<any>fields)[field]}; | ||
$result[field] = 0; | ||
} | ||
fields = keys; | ||
} | ||
let result = await this.thisCollection().aggregate([ | ||
{$match: match}, | ||
{$group} | ||
]).toArray(); | ||
if (result.length) { | ||
result = result[0]; | ||
for (const field of fields as string[]) { | ||
$result[field] = result[field as any] || 0; | ||
} | ||
} | ||
return fields.length === 1 ? $result[(<string[]>fields)[0]] : $result; | ||
} | ||
/** | ||
* Count Aggregations | ||
@@ -978,0 +1033,0 @@ * @param query |
@@ -281,2 +281,22 @@ import ObjectCollection = require('object-collection'); | ||
/** | ||
* Sum fields in this collection. | ||
* @example | ||
* data: [ | ||
* {name: 'john', credit: 100, debit: 400}, | ||
* {name: 'doe', credit: 200, debit: 300} | ||
* ] | ||
* | ||
* const sumOfCredit = await Model.sum('credit'); | ||
* ==> 300 | ||
* | ||
* const sumOfBoth = await Model.sum(['credit', 'debit']); | ||
* ==> {credit: 300, debit: 700} | ||
* | ||
* @param fields | ||
* @param match | ||
*/ | ||
static sum(fields: string | StringToAnyObject | string[], match?: StringToAnyObject): Promise<number | { | ||
[name: string]: number; | ||
}>; | ||
/** | ||
* Count Aggregations | ||
@@ -283,0 +303,0 @@ * @param query |
135398
3701