Socket
Socket
Sign inDemoInstall

@mongez/mongodb

Package Overview
Dependencies
235
Maintainers
1
Versions
73
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

Comparing version 1.0.55 to 1.0.56

76

cjs/aggregate/aggregate.d.ts

@@ -6,2 +6,3 @@ import { GenericObject } from "@mongez/reinforcements";

import { LookupPipelineOptions } from "./LookupPipeline";
import { UnwindOptions } from "./UnwindPipeline";
import { Pipeline } from "./pipeline";

@@ -42,7 +43,7 @@ import { WhereOperator } from "./types";

*/
orderByDesc(column: string): this;
sortByDesc(column: string): this;
/**
* Order by latest created records
* Order by descending
*/
latest(): this;
orderByDesc(column: string): this;
/**

@@ -57,2 +58,10 @@ * Sort by multiple columns

/**
* Order by latest created records
*/
latest(column?: string): this;
/**
* Order by oldest created records
*/
oldest(column?: string): this;
/**
* Group by aggregate

@@ -62,2 +71,3 @@ */

groupBy(GroupByPipeline: GenericObject, groupByData?: GenericObject): this;
groupBy(groupByColumns: string[], groupByData?: GenericObject): this;
groupBy(groupBy_id: string | null): this;

@@ -90,2 +100,22 @@ groupBy(groupBy_id: string | null, groupByData: GenericObject): this;

/**
* Get average of the given column
*/
avg(column: string): Promise<any>;
/**
* {@alias} avg
*/
average(column: string): Promise<any>;
/**
* Sum values of the given column
*/
sum(column: string): Promise<any>;
/**
* Get minimum value of the given column
*/
min(column: string): Promise<any>;
/**
* Get maximum value of the given column
*/
max(column: string): Promise<any>;
/**
* Get distinct value for the given column using aggregation

@@ -95,2 +125,6 @@ */

/**
* {@alias} distinct
*/
unique(column: string): Promise<any[]>;
/**
* Get distinct values that are not empty

@@ -100,2 +134,6 @@ */

/**
* {@alias} distinctHeavy
*/
uniqueHeavy(column: string): Promise<any[]>;
/**
* Limit the number of results

@@ -119,5 +157,5 @@ */

*/
unwind(column: string): this;
unwind(column: string, options?: UnwindOptions): this;
/**
* Add where pipeline
* Add where stage
*/

@@ -128,3 +166,3 @@ where(column: string, value: any): this;

/**
* Or Where pipeline
* Or Where stage
*/

@@ -137,6 +175,2 @@ orWhere(column: GenericObject): this;

/**
* Where using expression
*/
whereExpression(column: string, expression: any): this;
/**
* Where not null

@@ -170,5 +204,5 @@ */

/**
* Delete records
* Where date not between operator
*/
delete(): Promise<number>;
whereDateNotBetween(column: string, value: [Date, Date]): this;
/**

@@ -225,7 +259,7 @@ * Where not between operator

/**
* Add mongodb plain pipeline
* Add mongodb plain stage
*/
addPipeline(pipeline: any): this;
/**
* Add mongodb plain pipelines
* Add mongodb plain stages
*/

@@ -236,3 +270,3 @@ addPipelines(pipelines: any[]): this;

*/
first(filters?: Filter): Promise<any>;
first(mapData?: (data: any) => any): Promise<any>;
/**

@@ -243,2 +277,6 @@ * Get last result

/**
* Delete records
*/
delete(): Promise<number>;
/**
* Get the data

@@ -248,2 +286,6 @@ */

/**
* Paginate records based on the given filter
*/
paginate<T = any>(page?: number, limit?: number): Promise<PaginationListing<T>>;
/**
* Explain the query

@@ -253,6 +295,2 @@ */

/**
* Paginate records based on the given filter
*/
paginate<T = any>(page?: number, limit?: number): Promise<PaginationListing<T>>;
/**
* Update the given data

@@ -259,0 +297,0 @@ */

@@ -20,3 +20,2 @@ "use strict";

WhereExpression = require("./WhereExpression.js"),
WhereExpressionPipeline = require("./WhereExpressionPipeline.js"),
WherePipeline = require("./WherePipeline.js"),

@@ -70,10 +69,10 @@ expressions = require("./expressions.js"),

*/
orderByDesc(column) {
sortByDesc(column) {
return this.sort(column, "desc");
}
/**
* Order by latest created records
* Order by descending
*/
latest() {
return this.orderByDesc("createdAt");
orderByDesc(column) {
return this.sort(column, "desc");
}

@@ -107,2 +106,14 @@ /**

}
/**
* Order by latest created records
*/
latest(column = "createdAt") {
return this.sort(column, "desc");
}
/**
* Order by oldest created records
*/
oldest(column = "createdAt") {
return this.sort(column, "asc");
}
groupBy(...args) {

@@ -123,3 +134,3 @@ const [groupBy_id, groupByData] = args;

{
year: expressions.year(column),
year: expressions.year(expressions.$agg.columnName(column)),
},

@@ -133,2 +144,3 @@ groupByData

groupByMonthAndYear(column, groupByData) {
column = expressions.$agg.columnName(column);
return this.groupBy(

@@ -146,2 +158,3 @@ {

groupByMonth(column, groupByData) {
column = expressions.$agg.columnName(column);
return this.groupBy(

@@ -158,2 +171,3 @@ {

groupByDate(column, groupByData) {
column = expressions.$agg.columnName(column);
return this.groupBy(

@@ -172,2 +186,3 @@ {

groupByDayOfMonth(column, groupByData) {
column = expressions.$agg.columnName(column);
return this.groupBy(

@@ -189,2 +204,44 @@ {

/**
* Get average of the given column
*/
async avg(column) {
const document = await this.groupBy(null, {
avg: expressions.$agg.avg(column),
}).first((document) => document);
return document?.avg || 0;
}
/**
* {@alias} avg
*/
average(column) {
return this.avg(column);
}
/**
* Sum values of the given column
*/
async sum(column) {
const document = await this.groupBy(null, {
sum: expressions.$agg.sum(column),
}).first((document) => document);
return document?.sum || 0;
}
/**
* Get minimum value of the given column
*/
async min(column) {
const document = await this.groupBy(null, {
min: expressions.$agg.min(column),
}).first((document) => document);
return document?.min || 0;
}
/**
* Get maximum value of the given column
*/
async max(column) {
const document = await this.groupBy(null, {
max: expressions.$agg.max(column),
}).first((document) => document);
return document?.max || 0;
}
/**
* Get distinct value for the given column using aggregation

@@ -198,2 +255,8 @@ */

/**
* {@alias} distinct
*/
unique(column) {
return this.distinct(column);
}
/**
* Get distinct values that are not empty

@@ -205,2 +268,8 @@ */

/**
* {@alias} distinctHeavy
*/
async uniqueHeavy(column) {
return await this.distinctHeavy(column);
}
/**
* Limit the number of results

@@ -232,4 +301,4 @@ */

*/
unwind(column) {
return this.pipeline(new UnwindPipeline.UnwindPipeline(column));
unwind(column, options) {
return this.pipeline(new UnwindPipeline.UnwindPipeline(column, options));
}

@@ -244,3 +313,3 @@ where(...args) {

/**
* Or Where pipeline
* Or Where stage
*/

@@ -257,10 +326,2 @@ orWhere(column) {

/**
* Where using expression
*/
whereExpression(column, expression) {
return this.pipeline(
new WhereExpressionPipeline.WhereExpressionPipeline(column, expression)
);
}
/**
* Where not null

@@ -308,11 +369,6 @@ */

/**
* Delete records
* Where date not between operator
*/
async delete() {
const ids = await (
await this.select(["_id"]).pluck("_id")
).map((_id) => new mongodb.ObjectId(_id));
return await query.query.delete(this.collection, {
_id: ids,
});
whereDateNotBetween(column, value) {
return this.where(column, "notBetween", value);
}

@@ -342,3 +398,3 @@ /**

[column + "_size"]: {
$size: "$" + column,
$size: expressions.$agg.columnName(column),
},

@@ -414,3 +470,3 @@ });

/**
* Add mongodb plain pipeline
* Add mongodb plain stage
*/

@@ -422,3 +478,3 @@ addPipeline(pipeline) {

/**
* Add mongodb plain pipelines
* Add mongodb plain stages
*/

@@ -432,7 +488,4 @@ addPipelines(pipelines) {

*/
async first(filters) {
if (filters) {
this.where(filters);
}
const results = await this.limit(1).get();
async first(mapData) {
const results = await this.limit(1).get(mapData);
return results[0];

@@ -451,2 +504,13 @@ }

/**
* Delete records
*/
async delete() {
const ids = await (
await this.select(["_id"]).pluck("_id")
).map((_id) => new mongodb.ObjectId(_id));
return await query.query.delete(this.collection, {
_id: ids,
});
}
/**
* Get the data

@@ -459,12 +523,2 @@ */

/**
* Explain the query
*/
async explain() {
return (
await this.query.aggregate(this.collection, this.parse(), {
explain: true,
})
).explain();
}
/**
* Paginate records based on the given filter

@@ -491,2 +545,12 @@ */

/**
* Explain the query
*/
async explain() {
return (
await this.query.aggregate(this.collection, this.parse(), {
explain: true,
})
).explain();
}
/**
* Update the given data

@@ -493,0 +557,0 @@ */

/**
* Get count expression
*/
export declare function count(): {
$sum: number;
};
export declare function count(column?: string): any;
/**
* Parse the given column
*/
export declare const columnName: (column: string) => string;
/**
* Get sum expression
*/
export declare function sum(column: string): {
$sum: string;
};
export declare function sum(column: string, baseColumn?: string): any;
/**

@@ -137,5 +137,3 @@ * Get average expression

export declare const greaterThan: typeof gt;
export declare function gt(value: any): {
$gt: any;
};
export declare function gt(value: any, column?: string): any;
/**

@@ -145,5 +143,3 @@ * Get greater than or equal expression

export declare const greaterThanOrEqual: typeof gt;
export declare function gte(value: any): {
$gte: any;
};
export declare function gte(value: any, column?: string): any;
/**

@@ -153,5 +149,3 @@ * Get less than expression

export declare const lessThan: typeof lt;
export declare function lt(value: any): {
$lt: any;
};
export declare function lt(value: any, column?: string): any;
/**

@@ -161,5 +155,3 @@ * Get less than or equal expression

export declare const lessThanOrEqual: typeof lt;
export declare function lte(value: any): {
$lte: any;
};
export declare function lte(value: any, column?: string): any;
/**

@@ -196,55 +188,31 @@ * Get equal expression

*/
export declare function exists(value: any): {
$exists: any;
};
export declare function exists(value: any, column?: string): any;
/**
* Get not exists expression
*/
export declare function notExists(value: any): {
$not: {
$exists: any;
};
};
export declare function notExists(value: any, column?: string): any;
/**
* Get like expression
*/
export declare function like(value: any): {
$regex: any;
};
export declare function like(value: any, column?: string): any;
/**
* Get not like expression
*/
export declare function notLike(value: any): {
$not: {
$regex: any;
};
};
export declare function notLike(value: any, column?: string): any;
/**
* Get not null expression
*/
export declare function notNull(): {
$ne: any;
};
export declare function notNull(column?: string): any;
/**
* Get null expression
*/
export declare function isNull(): {
$eq: any;
};
export declare function isNull(column?: string): any;
/**
* Get between expression
*/
export declare function between(minValue: any, maxValue: any): {
$gte: any;
$lte: any;
};
export declare function between(minValue: any, maxValue: any, column?: string): any;
/**
* Get not between expression
*/
export declare function notBetween(minValue: any, maxValue: any): {
$not: {
$gte: any;
$lte: any;
};
};
export declare function notBetween(minValue: any, maxValue: any, column?: string): any;
/**

@@ -256,40 +224,51 @@ * Get concat expression

};
export declare const merge: typeof concat;
/**
* Concat columns with separator
* Concat columns with separator between each column
*/
export declare function concatWith(separator: string, ...columns: string[]): {
$concat: string[];
$concat: any[];
};
export declare const mergeWith: typeof concatWith;
/**
* Get cond expression
*/
export declare function cond(condition: any, ifTrue: any, ifFalse: any): {
$cond: {
if: any;
then: any;
else: any;
};
};
export declare function cond(condition: any, ifTrue: any, ifFalse: any, column?: string): any;
export declare const condition: typeof cond;
/**
* Boolean condition
*/
export declare function booleanCond(condition: any): {
$cond: {
if: any;
then: boolean;
else: boolean;
};
};
export declare function booleanCond(condition: any, column?: string): any;
/**
* Get regex expression
*/
export declare function regex(value: RegExp): {
$regex: RegExp;
};
export declare function regex(value: RegExp, column?: string): any;
/**
* You can use it when you want a field to match all the given values
*/
export declare function all(values: any[]): {
$all: any[];
export declare function all(values: any[], column?: string): any;
/**
* Multiple expressions
*/
export declare function _multiply(...expressions: any[]): {
$multiply: any[];
};
/**
* Multiple columns
*/
export declare function multiply(...columns: string[]): {
$multiply: string[];
};
/**
* Divide expressions
*/
export declare function _divide(...expressions: any[]): {
$divide: any[];
};
/**
* Divide columns
*/
export declare function divide(...columns: string[]): {
$divide: string[];
};
export declare const $agg: {

@@ -299,2 +278,6 @@ count: typeof count;

avg: typeof avg;
multiply: typeof multiply;
divide: typeof divide;
_divide: typeof _divide;
_multiply: typeof _multiply;
average: typeof avg;

@@ -344,7 +327,10 @@ min: typeof min;

concat: typeof concat;
merge: typeof concat;
concatWith: typeof concatWith;
mergeWith: typeof concatWith;
columnName: (column: string) => string;
booleanCond: typeof booleanCond;
cond: typeof cond;
booleanCond: typeof booleanCond;
regex: typeof regex;
};
//# sourceMappingURL=expressions.d.ts.map

@@ -12,14 +12,32 @@ "use strict";

*/
function count() {
return {
$sum: 1,
};
function count(column) {
return wrapExpressionWithColumn(
{
$sum: 1,
},
column
);
}
/**
* Parse the given column
*/
const columnName = (column) => `$${reinforcements.ltrim(column, "$")}`;
function wrapExpressionWithColumn(expression, column) {
if (column) {
return {
[columnName(column)]: expression,
};
}
return expression;
}
/**
* Get sum expression
*/
function sum(column) {
return {
$sum: `$${column}`,
};
function sum(column, baseColumn) {
return wrapExpressionWithColumn(
{
$sum: columnName(column),
},
baseColumn
);
}

@@ -32,3 +50,3 @@ /**

return {
$avg: `$${column}`,
$avg: columnName(column),
};

@@ -41,3 +59,3 @@ }

return {
$min: `$${column}`,
$min: columnName(column),
};

@@ -50,3 +68,3 @@ }

return {
$max: `$${column}`,
$max: columnName(column),
};

@@ -59,3 +77,3 @@ }

return {
$first: `$${column}`,
$first: columnName(column),
};

@@ -68,3 +86,3 @@ }

return {
$last: `$${column}`,
$last: columnName(column),
};

@@ -77,3 +95,3 @@ }

if (typeof data === "string") {
data = "$" + reinforcements.ltrim(data, "$");
data = columnName(data);
}

@@ -89,3 +107,3 @@ return {

return {
$addToSet: `$${column}`,
$addToSet: columnName(column),
};

@@ -98,3 +116,3 @@ }

return {
$year: `$${column}`,
$year: columnName(column),
};

@@ -108,3 +126,3 @@ }

$first: {
$year: `$${column}`,
$year: columnName(column),
},

@@ -119,3 +137,3 @@ };

$last: {
$year: `$${column}`,
$year: columnName(column),
},

@@ -129,3 +147,3 @@ };

return {
$month: `$${column}`,
$month: columnName(column),
};

@@ -139,3 +157,3 @@ }

$first: {
$month: `$${column}`,
$month: columnName(column),
},

@@ -150,3 +168,3 @@ };

$last: {
$month: `$${column}`,
$month: columnName(column),
},

@@ -160,3 +178,3 @@ };

return {
$dayOfMonth: `$${column}`,
$dayOfMonth: columnName(column),
};

@@ -170,3 +188,3 @@ }

$first: {
$dayOfMonth: `$${column}`,
$dayOfMonth: columnName(column),
},

@@ -181,3 +199,3 @@ };

$last: {
$dayOfMonth: `$${column}`,
$dayOfMonth: columnName(column),
},

@@ -191,3 +209,3 @@ };

return {
$dayOfWeek: `$${column}`,
$dayOfWeek: columnName(column),
};

@@ -200,3 +218,3 @@ }

return columns.reduce((selections, column) => {
selections[column] = `$${column}`;
selections[column] = columnName(column);
return selections;

@@ -210,6 +228,9 @@ }, {});

const greaterThan = gt;
function gt(value) {
return {
$gt: value,
};
function gt(value, column) {
return wrapExpressionWithColumn(
{
$gt: value,
},
column
);
}

@@ -220,6 +241,9 @@ /**

const greaterThanOrEqual = gt;
function gte(value) {
return {
$gte: value,
};
function gte(value, column) {
return wrapExpressionWithColumn(
{
$gte: value,
},
column
);
}

@@ -230,6 +254,9 @@ /**

const lessThan = lt;
function lt(value) {
return {
$lt: value,
};
function lt(value, column) {
return wrapExpressionWithColumn(
{
$lt: value,
},
column
);
}

@@ -240,6 +267,9 @@ /**

const lessThanOrEqual = lt;
function lte(value) {
return {
$lte: value,
};
function lte(value, column) {
return wrapExpressionWithColumn(
{
$lte: value,
},
column
);
}

@@ -285,6 +315,9 @@ /**

*/
function exists(value) {
return {
$exists: value,
};
function exists(value, column) {
return wrapExpressionWithColumn(
{
$exists: value,
},
column
);
}

@@ -294,8 +327,9 @@ /**

*/
function notExists(value) {
return {
$not: {
function notExists(value, column) {
return wrapExpressionWithColumn(
{
$exists: value,
},
};
column
);
}

@@ -305,9 +339,12 @@ /**

*/
function like(value) {
function like(value, column) {
if (Is__default.default.scalar(value)) {
value = new RegExp(value, "i");
}
return {
$regex: value,
};
return wrapExpressionWithColumn(
{
$regex: value,
},
column
);
}

@@ -317,11 +354,14 @@ /**

*/
function notLike(value) {
function notLike(value, column) {
if (Is__default.default.scalar(value)) {
value = new RegExp(value, "i");
}
return {
$not: {
$regex: value,
return wrapExpressionWithColumn(
{
$not: {
$regex: value,
},
},
};
column
);
}

@@ -331,6 +371,9 @@ /**

*/
function notNull() {
return {
$ne: null,
};
function notNull(column) {
return wrapExpressionWithColumn(
{
$ne: null,
},
column
);
}

@@ -340,6 +383,9 @@ /**

*/
function isNull() {
return {
$eq: null,
};
function isNull(column) {
return wrapExpressionWithColumn(
{
$eq: null,
},
column
);
}

@@ -349,7 +395,10 @@ /**

*/
function between(minValue, maxValue) {
return {
$gte: minValue,
$lte: maxValue,
};
function between(minValue, maxValue, column) {
return wrapExpressionWithColumn(
{
$gte: minValue,
$lte: maxValue,
},
column
);
}

@@ -359,9 +408,12 @@ /**

*/
function notBetween(minValue, maxValue) {
return {
$not: {
$gte: minValue,
$lte: maxValue,
function notBetween(minValue, maxValue, column) {
return wrapExpressionWithColumn(
{
$not: {
$gte: minValue,
$lte: maxValue,
},
},
};
column
);
}

@@ -373,39 +425,49 @@ /**

return {
$concat: columns.map((column) => "$" + reinforcements.ltrim(column, "$")),
$concat: columns,
};
}
const merge = concat;
/**
* Concat columns with separator
* Concat columns with separator between each column
*/
function concatWith(separator, ...columns) {
const columnsList = [];
for (const column of columns) {
columnsList.push(columnName(column), separator);
}
return {
$concat: [
separator,
...columns.map((column) => "$" + reinforcements.ltrim(column, "$")),
],
$concat: columnsList,
};
}
const mergeWith = concatWith;
/**
* Get cond expression
*/
function cond(condition, ifTrue, ifFalse) {
return {
$cond: {
if: condition,
then: ifTrue,
else: ifFalse,
function cond(condition, ifTrue, ifFalse, column) {
return wrapExpressionWithColumn(
{
$cond: {
if: condition,
then: ifTrue,
else: ifFalse,
},
},
};
column
);
}
const condition = cond;
/**
* Boolean condition
*/
function booleanCond(condition) {
return {
$cond: {
if: condition,
then: true,
else: false,
function booleanCond(condition, column) {
return wrapExpressionWithColumn(
{
$cond: {
if: condition,
then: true,
else: false,
},
},
};
column
);
}

@@ -415,15 +477,53 @@ /**

*/
function regex(value) {
function regex(value, column) {
return wrapExpressionWithColumn(
{
$regex: value,
},
column
);
}
/**
* You can use it when you want a field to match all the given values
*/
function all(values, column) {
return wrapExpressionWithColumn(
{
$all: values,
},
column
);
}
/**
* Multiple expressions
*/
function _multiply(...expressions) {
return {
$regex: value,
$multiply: expressions,
};
}
/**
* You can use it when you want a field to match all the given values
* Multiple columns
*/
function all(values) {
function multiply(...columns) {
return {
$all: values,
$multiply: columns.map(columnName),
};
}
/**
* Divide expressions
*/
function _divide(...expressions) {
return {
$divide: expressions,
};
}
/**
* Divide columns
*/
function divide(...columns) {
return {
$divide: columns.map(columnName),
};
}
const $agg = {

@@ -434,2 +534,6 @@ // list all aggregation functions

avg,
multiply: multiply,
divide: divide,
_divide,
_multiply,
average,

@@ -479,8 +583,13 @@ min,

concat,
merge,
concatWith,
mergeWith,
columnName,
booleanCond,
cond,
booleanCond,
regex,
};
exports.$agg = $agg;
exports._divide = _divide;
exports._multiply = _multiply;
exports.addToSet = addToSet;

@@ -492,2 +601,3 @@ exports.all = all;

exports.booleanCond = booleanCond;
exports.columnName = columnName;
exports.columns = columns;

@@ -497,5 +607,7 @@ exports.concat = concat;

exports.cond = cond;
exports.condition = condition;
exports.count = count;
exports.dayOfMonth = dayOfMonth;
exports.dayOfWeek = dayOfWeek;
exports.divide = divide;
exports.eq = eq;

@@ -524,4 +636,7 @@ exports.equal = equal;

exports.max = max;
exports.merge = merge;
exports.mergeWith = mergeWith;
exports.min = min;
exports.month = month;
exports.multiply = multiply;
exports.ne = ne;

@@ -528,0 +643,0 @@ exports.nin = nin;

import { GenericObject } from "@mongez/reinforcements";
import { Pipeline } from "./pipeline";
export declare class GroupByPipeline extends Pipeline {
protected readonly _id: string | null | GenericObject;
protected readonly _id: string | null | GenericObject | string[];
protected groupByData: GenericObject;

@@ -9,5 +9,5 @@ /**

*/
constructor(_id: string | null | GenericObject, groupByData?: GenericObject);
constructor(_id: string | null | GenericObject | string[], groupByData?: GenericObject);
}
export declare function groupByPipeline(column: string | null | GenericObject, groupByData: Record<string, any>): GroupByPipeline;
//# sourceMappingURL=GroupByPipeline.d.ts.map
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var reinforcements = require("@mongez/reinforcements"),
var expressions = require("./expressions.js"),
pipeline = require("./pipeline.js");

@@ -16,4 +16,10 @@ class GroupByPipeline extends pipeline.Pipeline {

if (typeof _id === "string") {
_id = "$" + reinforcements.ltrim(_id, "$");
_id = expressions.$agg.columnName(_id);
}
if (Array.isArray(_id)) {
_id = _id.reduce((result, column) => {
result[column] = expressions.$agg.columnName(column);
return result;
}, {});
}
this.data({

@@ -20,0 +26,0 @@ _id: _id,

import { Pipeline } from "./pipeline";
export type UnwindOptions = {
preserveNullAndEmptyArrays?: boolean;
includeArrayIndex?: string | null;
};
export declare class UnwindPipeline extends Pipeline {
protected readonly column: string;
protected readonly preserveNullAndEmptyArrays: boolean;
/**
* Constructor
*/
constructor(column: string, preserveNullAndEmptyArrays?: boolean);
constructor(column: string, options?: UnwindOptions);
}
export declare function unwindPipeline(column: string, preserveNullAndEmptyArrays?: boolean): UnwindPipeline;
export declare function unwindPipeline(column: string, options?: UnwindOptions): UnwindPipeline;
//# sourceMappingURL=UnwindPipeline.d.ts.map
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var reinforcements = require("@mongez/reinforcements"),
var expressions = require("./expressions.js"),
pipeline = require("./pipeline.js");
class UnwindPipeline extends pipeline.Pipeline {
column;
preserveNullAndEmptyArrays;
/**
* Constructor
*/
constructor(column, preserveNullAndEmptyArrays = false) {
constructor(column, options = {}) {
super("unwind");
this.column = column;
this.preserveNullAndEmptyArrays = preserveNullAndEmptyArrays;
const { preserveNullAndEmptyArrays = false, includeArrayIndex = null } =
options;
this.data({
path: "$" + reinforcements.ltrim(column, "$"),
preserveNullAndEmptyArrays: preserveNullAndEmptyArrays,
path: expressions.$agg.columnName(column),
preserveNullAndEmptyArrays,
includeArrayIndex,
});
}
}
function unwindPipeline(column, preserveNullAndEmptyArrays = false) {
return new UnwindPipeline(column, preserveNullAndEmptyArrays);
function unwindPipeline(column, options) {
return new UnwindPipeline(column, options);
}
exports.UnwindPipeline = UnwindPipeline;
exports.unwindPipeline = unwindPipeline;
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var reinforcements = require("@mongez/reinforcements"),
Is = require("@mongez/supportive-is"),
timeWizard = require("@mongez/time-wizard");
var Is = require("@mongez/supportive-is"),
timeWizard = require("@mongez/time-wizard"),
expressions = require("./expressions.js");
function _interopDefault(e) {

@@ -10,2 +10,12 @@ return e && e.__esModule ? e : { default: e };

var Is__default = /*#__PURE__*/ _interopDefault(Is);
function escapeRegex(value, escapeOnly = false) {
if (value instanceof RegExp === false) {
// escape the value special characters
value = String(value).replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&");
if (escapeOnly === false) {
value = new RegExp(value, "i");
}
}
return value;
}
class WhereExpression {

@@ -57,9 +67,5 @@ /**

if (operator === "like") {
// escape the value special characters
value = String(value).replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&");
value = new RegExp(value, "i");
value = escapeRegex(value);
} else if (operator === "notLike") {
// escape the value special characters
value = value.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&");
value = new RegExp(value, "i");
value = escapeRegex(value);
operator = "not";

@@ -70,9 +76,7 @@ value = {

} else if (operator === "startsWith") {
// escape the value special characters
value = value.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&");
value = new RegExp(`^${value}`, "i");
value = escapeRegex(value, true);
value = new RegExp(`^${value}`);
} else if (operator === "endsWith") {
// escape the value special characters
value = value.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&");
value = new RegExp(`${value}$`, "i");
value = escapeRegex(value, true);
value = new RegExp(`${value}$`);
}

@@ -94,3 +98,3 @@ let expression = {

expression = {
$in: "$" + reinforcements.ltrim(value, "$"),
$in: expressions.$agg.columnName(value),
};

@@ -100,3 +104,3 @@ } else if (operator === "notIn" && typeof value === "string") {

$not: {
$in: "$" + reinforcements.ltrim(value, "$"),
$in: expressions.$agg.columnName(value),
},

@@ -103,0 +107,0 @@ };

@@ -19,5 +19,11 @@ "use strict";

if (Array.isArray(value)) {
if (value[0]?.id) {
return value;
const results = [];
for (const item of value) {
if (value instanceof model.Model) {
results.push(getModelData(item, embeddedKey));
} else if (item?.id) {
results.push(item);
}
}
if (results.length > 0) return results;
const records = await model$1

@@ -32,19 +38,19 @@ .aggregate()

.map((record) => {
if (Array.isArray(embeddedKey)) {
return record.only(embeddedKey);
}
return record[embeddedKey];
return getModelData(record, embeddedKey);
})
.filter((value) => !Is__default.default.empty(value));
}
if (value instanceof model.Model) return getModelData(value, embeddedKey);
if (value?.id) return value;
const record =
value instanceof model.Model ? value : await model$1.find(Number(value));
const record = await model$1.find(Number(value));
if (!record) return null;
if (Array.isArray(embeddedKey)) {
return record.only(embeddedKey);
}
return record[embeddedKey];
return getModelData(record, embeddedKey);
};
}
function getModelData(model, embeddedKey) {
if (Array.isArray(embeddedKey)) {
return model.only(embeddedKey);
}
return model[embeddedKey];
}
exports.castModel = castModel;

@@ -86,2 +86,4 @@ "use strict";

exports.$agg = expressions.$agg;
exports._divide = expressions._divide;
exports._multiply = expressions._multiply;
exports.addToSet = expressions.addToSet;

@@ -93,2 +95,3 @@ exports.all = expressions.all;

exports.booleanCond = expressions.booleanCond;
exports.columnName = expressions.columnName;
exports.columns = expressions.columns;

@@ -98,5 +101,7 @@ exports.concat = expressions.concat;

exports.cond = expressions.cond;
exports.condition = expressions.condition;
exports.count = expressions.count;
exports.dayOfMonth = expressions.dayOfMonth;
exports.dayOfWeek = expressions.dayOfWeek;
exports.divide = expressions.divide;
exports.eq = expressions.eq;

@@ -125,4 +130,7 @@ exports.equal = expressions.equal;

exports.max = expressions.max;
exports.merge = expressions.merge;
exports.mergeWith = expressions.mergeWith;
exports.min = expressions.min;
exports.month = expressions.month;
exports.multiply = expressions.multiply;
exports.ne = expressions.ne;

@@ -129,0 +137,0 @@ exports.nin = expressions.nin;

@@ -12,7 +12,7 @@ import { Aggregate } from "../aggregate";

*/
get(mapData?: (record: any) => any): Promise<T[]>;
get<Output = T>(mapData?: (record: any) => any): Promise<Output[]>;
/**
* {@inheritDoc}
*/
first(filters?: Filter): Promise<T>;
first(mapData?: (data: any) => any): Promise<T>;
/**

@@ -19,0 +19,0 @@ * {@inheritDoc}

@@ -26,4 +26,4 @@ "use strict";

*/
async first(filters) {
return await super.first(filters);
async first(mapData) {
return await super.first(mapData);
}

@@ -30,0 +30,0 @@ /**

@@ -6,2 +6,3 @@ import { GenericObject } from "@mongez/reinforcements";

import { LookupPipelineOptions } from "./LookupPipeline";
import { UnwindOptions } from "./UnwindPipeline";
import { Pipeline } from "./pipeline";

@@ -42,7 +43,7 @@ import { WhereOperator } from "./types";

*/
orderByDesc(column: string): this;
sortByDesc(column: string): this;
/**
* Order by latest created records
* Order by descending
*/
latest(): this;
orderByDesc(column: string): this;
/**

@@ -57,2 +58,10 @@ * Sort by multiple columns

/**
* Order by latest created records
*/
latest(column?: string): this;
/**
* Order by oldest created records
*/
oldest(column?: string): this;
/**
* Group by aggregate

@@ -62,2 +71,3 @@ */

groupBy(GroupByPipeline: GenericObject, groupByData?: GenericObject): this;
groupBy(groupByColumns: string[], groupByData?: GenericObject): this;
groupBy(groupBy_id: string | null): this;

@@ -90,2 +100,22 @@ groupBy(groupBy_id: string | null, groupByData: GenericObject): this;

/**
* Get average of the given column
*/
avg(column: string): Promise<any>;
/**
* {@alias} avg
*/
average(column: string): Promise<any>;
/**
* Sum values of the given column
*/
sum(column: string): Promise<any>;
/**
* Get minimum value of the given column
*/
min(column: string): Promise<any>;
/**
* Get maximum value of the given column
*/
max(column: string): Promise<any>;
/**
* Get distinct value for the given column using aggregation

@@ -95,2 +125,6 @@ */

/**
* {@alias} distinct
*/
unique(column: string): Promise<any[]>;
/**
* Get distinct values that are not empty

@@ -100,2 +134,6 @@ */

/**
* {@alias} distinctHeavy
*/
uniqueHeavy(column: string): Promise<any[]>;
/**
* Limit the number of results

@@ -119,5 +157,5 @@ */

*/
unwind(column: string): this;
unwind(column: string, options?: UnwindOptions): this;
/**
* Add where pipeline
* Add where stage
*/

@@ -128,3 +166,3 @@ where(column: string, value: any): this;

/**
* Or Where pipeline
* Or Where stage
*/

@@ -137,6 +175,2 @@ orWhere(column: GenericObject): this;

/**
* Where using expression
*/
whereExpression(column: string, expression: any): this;
/**
* Where not null

@@ -170,5 +204,5 @@ */

/**
* Delete records
* Where date not between operator
*/
delete(): Promise<number>;
whereDateNotBetween(column: string, value: [Date, Date]): this;
/**

@@ -225,7 +259,7 @@ * Where not between operator

/**
* Add mongodb plain pipeline
* Add mongodb plain stage
*/
addPipeline(pipeline: any): this;
/**
* Add mongodb plain pipelines
* Add mongodb plain stages
*/

@@ -236,3 +270,3 @@ addPipelines(pipelines: any[]): this;

*/
first(filters?: Filter): Promise<any>;
first(mapData?: (data: any) => any): Promise<any>;
/**

@@ -243,2 +277,6 @@ * Get last result

/**
* Delete records
*/
delete(): Promise<number>;
/**
* Get the data

@@ -248,2 +286,6 @@ */

/**
* Paginate records based on the given filter
*/
paginate<T = any>(page?: number, limit?: number): Promise<PaginationListing<T>>;
/**
* Explain the query

@@ -253,6 +295,2 @@ */

/**
* Paginate records based on the given filter
*/
paginate<T = any>(page?: number, limit?: number): Promise<PaginationListing<T>>;
/**
* Update the given data

@@ -259,0 +297,0 @@ */

@@ -18,6 +18,6 @@ import { log } from "@mongez/logger";

import { WhereExpression } from "./WhereExpression.js";
import { WhereExpressionPipeline } from "./WhereExpressionPipeline.js";
import { WherePipeline } from "./WherePipeline.js";
import {
year,
$agg,
month,

@@ -75,10 +75,10 @@ dayOfMonth,

*/
orderByDesc(column) {
sortByDesc(column) {
return this.sort(column, "desc");
}
/**
* Order by latest created records
* Order by descending
*/
latest() {
return this.orderByDesc("createdAt");
orderByDesc(column) {
return this.sort(column, "desc");
}

@@ -112,2 +112,14 @@ /**

}
/**
* Order by latest created records
*/
latest(column = "createdAt") {
return this.sort(column, "desc");
}
/**
* Order by oldest created records
*/
oldest(column = "createdAt") {
return this.sort(column, "asc");
}
groupBy(...args) {

@@ -126,3 +138,3 @@ const [groupBy_id, groupByData] = args;

{
year: year(column),
year: year($agg.columnName(column)),
},

@@ -136,2 +148,3 @@ groupByData

groupByMonthAndYear(column, groupByData) {
column = $agg.columnName(column);
return this.groupBy(

@@ -149,2 +162,3 @@ {

groupByMonth(column, groupByData) {
column = $agg.columnName(column);
return this.groupBy(

@@ -161,2 +175,3 @@ {

groupByDate(column, groupByData) {
column = $agg.columnName(column);
return this.groupBy(

@@ -175,2 +190,3 @@ {

groupByDayOfMonth(column, groupByData) {
column = $agg.columnName(column);
return this.groupBy(

@@ -190,2 +206,44 @@ {

/**
* Get average of the given column
*/
async avg(column) {
const document = await this.groupBy(null, {
avg: $agg.avg(column),
}).first((document) => document);
return document?.avg || 0;
}
/**
* {@alias} avg
*/
average(column) {
return this.avg(column);
}
/**
* Sum values of the given column
*/
async sum(column) {
const document = await this.groupBy(null, {
sum: $agg.sum(column),
}).first((document) => document);
return document?.sum || 0;
}
/**
* Get minimum value of the given column
*/
async min(column) {
const document = await this.groupBy(null, {
min: $agg.min(column),
}).first((document) => document);
return document?.min || 0;
}
/**
* Get maximum value of the given column
*/
async max(column) {
const document = await this.groupBy(null, {
max: $agg.max(column),
}).first((document) => document);
return document?.max || 0;
}
/**
* Get distinct value for the given column using aggregation

@@ -199,2 +257,8 @@ */

/**
* {@alias} distinct
*/
unique(column) {
return this.distinct(column);
}
/**
* Get distinct values that are not empty

@@ -206,2 +270,8 @@ */

/**
* {@alias} distinctHeavy
*/
async uniqueHeavy(column) {
return await this.distinctHeavy(column);
}
/**
* Limit the number of results

@@ -233,4 +303,4 @@ */

*/
unwind(column) {
return this.pipeline(new UnwindPipeline(column));
unwind(column, options) {
return this.pipeline(new UnwindPipeline(column, options));
}

@@ -243,3 +313,3 @@ where(...args) {

/**
* Or Where pipeline
* Or Where stage
*/

@@ -256,8 +326,2 @@ orWhere(column) {

/**
* Where using expression
*/
whereExpression(column, expression) {
return this.pipeline(new WhereExpressionPipeline(column, expression));
}
/**
* Where not null

@@ -305,11 +369,6 @@ */

/**
* Delete records
* Where date not between operator
*/
async delete() {
const ids = await (
await this.select(["_id"]).pluck("_id")
).map((_id) => new ObjectId(_id));
return await query.delete(this.collection, {
_id: ids,
});
whereDateNotBetween(column, value) {
return this.where(column, "notBetween", value);
}

@@ -339,3 +398,3 @@ /**

[column + "_size"]: {
$size: "$" + column,
$size: $agg.columnName(column),
},

@@ -411,3 +470,3 @@ });

/**
* Add mongodb plain pipeline
* Add mongodb plain stage
*/

@@ -419,3 +478,3 @@ addPipeline(pipeline) {

/**
* Add mongodb plain pipelines
* Add mongodb plain stages
*/

@@ -429,7 +488,4 @@ addPipelines(pipelines) {

*/
async first(filters) {
if (filters) {
this.where(filters);
}
const results = await this.limit(1).get();
async first(mapData) {
const results = await this.limit(1).get(mapData);
return results[0];

@@ -448,2 +504,13 @@ }

/**
* Delete records
*/
async delete() {
const ids = await (
await this.select(["_id"]).pluck("_id")
).map((_id) => new ObjectId(_id));
return await query.delete(this.collection, {
_id: ids,
});
}
/**
* Get the data

@@ -456,12 +523,2 @@ */

/**
* Explain the query
*/
async explain() {
return (
await this.query.aggregate(this.collection, this.parse(), {
explain: true,
})
).explain();
}
/**
* Paginate records based on the given filter

@@ -488,2 +545,12 @@ */

/**
* Explain the query
*/
async explain() {
return (
await this.query.aggregate(this.collection, this.parse(), {
explain: true,
})
).explain();
}
/**
* Update the given data

@@ -490,0 +557,0 @@ */

/**
* Get count expression
*/
export declare function count(): {
$sum: number;
};
export declare function count(column?: string): any;
/**
* Parse the given column
*/
export declare const columnName: (column: string) => string;
/**
* Get sum expression
*/
export declare function sum(column: string): {
$sum: string;
};
export declare function sum(column: string, baseColumn?: string): any;
/**

@@ -137,5 +137,3 @@ * Get average expression

export declare const greaterThan: typeof gt;
export declare function gt(value: any): {
$gt: any;
};
export declare function gt(value: any, column?: string): any;
/**

@@ -145,5 +143,3 @@ * Get greater than or equal expression

export declare const greaterThanOrEqual: typeof gt;
export declare function gte(value: any): {
$gte: any;
};
export declare function gte(value: any, column?: string): any;
/**

@@ -153,5 +149,3 @@ * Get less than expression

export declare const lessThan: typeof lt;
export declare function lt(value: any): {
$lt: any;
};
export declare function lt(value: any, column?: string): any;
/**

@@ -161,5 +155,3 @@ * Get less than or equal expression

export declare const lessThanOrEqual: typeof lt;
export declare function lte(value: any): {
$lte: any;
};
export declare function lte(value: any, column?: string): any;
/**

@@ -196,55 +188,31 @@ * Get equal expression

*/
export declare function exists(value: any): {
$exists: any;
};
export declare function exists(value: any, column?: string): any;
/**
* Get not exists expression
*/
export declare function notExists(value: any): {
$not: {
$exists: any;
};
};
export declare function notExists(value: any, column?: string): any;
/**
* Get like expression
*/
export declare function like(value: any): {
$regex: any;
};
export declare function like(value: any, column?: string): any;
/**
* Get not like expression
*/
export declare function notLike(value: any): {
$not: {
$regex: any;
};
};
export declare function notLike(value: any, column?: string): any;
/**
* Get not null expression
*/
export declare function notNull(): {
$ne: any;
};
export declare function notNull(column?: string): any;
/**
* Get null expression
*/
export declare function isNull(): {
$eq: any;
};
export declare function isNull(column?: string): any;
/**
* Get between expression
*/
export declare function between(minValue: any, maxValue: any): {
$gte: any;
$lte: any;
};
export declare function between(minValue: any, maxValue: any, column?: string): any;
/**
* Get not between expression
*/
export declare function notBetween(minValue: any, maxValue: any): {
$not: {
$gte: any;
$lte: any;
};
};
export declare function notBetween(minValue: any, maxValue: any, column?: string): any;
/**

@@ -256,40 +224,51 @@ * Get concat expression

};
export declare const merge: typeof concat;
/**
* Concat columns with separator
* Concat columns with separator between each column
*/
export declare function concatWith(separator: string, ...columns: string[]): {
$concat: string[];
$concat: any[];
};
export declare const mergeWith: typeof concatWith;
/**
* Get cond expression
*/
export declare function cond(condition: any, ifTrue: any, ifFalse: any): {
$cond: {
if: any;
then: any;
else: any;
};
};
export declare function cond(condition: any, ifTrue: any, ifFalse: any, column?: string): any;
export declare const condition: typeof cond;
/**
* Boolean condition
*/
export declare function booleanCond(condition: any): {
$cond: {
if: any;
then: boolean;
else: boolean;
};
};
export declare function booleanCond(condition: any, column?: string): any;
/**
* Get regex expression
*/
export declare function regex(value: RegExp): {
$regex: RegExp;
};
export declare function regex(value: RegExp, column?: string): any;
/**
* You can use it when you want a field to match all the given values
*/
export declare function all(values: any[]): {
$all: any[];
export declare function all(values: any[], column?: string): any;
/**
* Multiple expressions
*/
export declare function _multiply(...expressions: any[]): {
$multiply: any[];
};
/**
* Multiple columns
*/
export declare function multiply(...columns: string[]): {
$multiply: string[];
};
/**
* Divide expressions
*/
export declare function _divide(...expressions: any[]): {
$divide: any[];
};
/**
* Divide columns
*/
export declare function divide(...columns: string[]): {
$divide: string[];
};
export declare const $agg: {

@@ -299,2 +278,6 @@ count: typeof count;

avg: typeof avg;
multiply: typeof multiply;
divide: typeof divide;
_divide: typeof _divide;
_multiply: typeof _multiply;
average: typeof avg;

@@ -344,7 +327,10 @@ min: typeof min;

concat: typeof concat;
merge: typeof concat;
concatWith: typeof concatWith;
mergeWith: typeof concatWith;
columnName: (column: string) => string;
booleanCond: typeof booleanCond;
cond: typeof cond;
booleanCond: typeof booleanCond;
regex: typeof regex;
};
//# sourceMappingURL=expressions.d.ts.map

@@ -6,14 +6,32 @@ import { ltrim } from "@mongez/reinforcements";

*/
function count() {
return {
$sum: 1,
};
function count(column) {
return wrapExpressionWithColumn(
{
$sum: 1,
},
column
);
}
/**
* Parse the given column
*/
const columnName = (column) => `$${ltrim(column, "$")}`;
function wrapExpressionWithColumn(expression, column) {
if (column) {
return {
[columnName(column)]: expression,
};
}
return expression;
}
/**
* Get sum expression
*/
function sum(column) {
return {
$sum: `$${column}`,
};
function sum(column, baseColumn) {
return wrapExpressionWithColumn(
{
$sum: columnName(column),
},
baseColumn
);
}

@@ -26,3 +44,3 @@ /**

return {
$avg: `$${column}`,
$avg: columnName(column),
};

@@ -35,3 +53,3 @@ }

return {
$min: `$${column}`,
$min: columnName(column),
};

@@ -44,3 +62,3 @@ }

return {
$max: `$${column}`,
$max: columnName(column),
};

@@ -53,3 +71,3 @@ }

return {
$first: `$${column}`,
$first: columnName(column),
};

@@ -62,3 +80,3 @@ }

return {
$last: `$${column}`,
$last: columnName(column),
};

@@ -71,3 +89,3 @@ }

if (typeof data === "string") {
data = "$" + ltrim(data, "$");
data = columnName(data);
}

@@ -83,3 +101,3 @@ return {

return {
$addToSet: `$${column}`,
$addToSet: columnName(column),
};

@@ -92,3 +110,3 @@ }

return {
$year: `$${column}`,
$year: columnName(column),
};

@@ -102,3 +120,3 @@ }

$first: {
$year: `$${column}`,
$year: columnName(column),
},

@@ -113,3 +131,3 @@ };

$last: {
$year: `$${column}`,
$year: columnName(column),
},

@@ -123,3 +141,3 @@ };

return {
$month: `$${column}`,
$month: columnName(column),
};

@@ -133,3 +151,3 @@ }

$first: {
$month: `$${column}`,
$month: columnName(column),
},

@@ -144,3 +162,3 @@ };

$last: {
$month: `$${column}`,
$month: columnName(column),
},

@@ -154,3 +172,3 @@ };

return {
$dayOfMonth: `$${column}`,
$dayOfMonth: columnName(column),
};

@@ -164,3 +182,3 @@ }

$first: {
$dayOfMonth: `$${column}`,
$dayOfMonth: columnName(column),
},

@@ -175,3 +193,3 @@ };

$last: {
$dayOfMonth: `$${column}`,
$dayOfMonth: columnName(column),
},

@@ -185,3 +203,3 @@ };

return {
$dayOfWeek: `$${column}`,
$dayOfWeek: columnName(column),
};

@@ -194,3 +212,3 @@ }

return columns.reduce((selections, column) => {
selections[column] = `$${column}`;
selections[column] = columnName(column);
return selections;

@@ -204,6 +222,9 @@ }, {});

const greaterThan = gt;
function gt(value) {
return {
$gt: value,
};
function gt(value, column) {
return wrapExpressionWithColumn(
{
$gt: value,
},
column
);
}

@@ -214,6 +235,9 @@ /**

const greaterThanOrEqual = gt;
function gte(value) {
return {
$gte: value,
};
function gte(value, column) {
return wrapExpressionWithColumn(
{
$gte: value,
},
column
);
}

@@ -224,6 +248,9 @@ /**

const lessThan = lt;
function lt(value) {
return {
$lt: value,
};
function lt(value, column) {
return wrapExpressionWithColumn(
{
$lt: value,
},
column
);
}

@@ -234,6 +261,9 @@ /**

const lessThanOrEqual = lt;
function lte(value) {
return {
$lte: value,
};
function lte(value, column) {
return wrapExpressionWithColumn(
{
$lte: value,
},
column
);
}

@@ -279,6 +309,9 @@ /**

*/
function exists(value) {
return {
$exists: value,
};
function exists(value, column) {
return wrapExpressionWithColumn(
{
$exists: value,
},
column
);
}

@@ -288,8 +321,9 @@ /**

*/
function notExists(value) {
return {
$not: {
function notExists(value, column) {
return wrapExpressionWithColumn(
{
$exists: value,
},
};
column
);
}

@@ -299,9 +333,12 @@ /**

*/
function like(value) {
function like(value, column) {
if (Is.scalar(value)) {
value = new RegExp(value, "i");
}
return {
$regex: value,
};
return wrapExpressionWithColumn(
{
$regex: value,
},
column
);
}

@@ -311,11 +348,14 @@ /**

*/
function notLike(value) {
function notLike(value, column) {
if (Is.scalar(value)) {
value = new RegExp(value, "i");
}
return {
$not: {
$regex: value,
return wrapExpressionWithColumn(
{
$not: {
$regex: value,
},
},
};
column
);
}

@@ -325,6 +365,9 @@ /**

*/
function notNull() {
return {
$ne: null,
};
function notNull(column) {
return wrapExpressionWithColumn(
{
$ne: null,
},
column
);
}

@@ -334,6 +377,9 @@ /**

*/
function isNull() {
return {
$eq: null,
};
function isNull(column) {
return wrapExpressionWithColumn(
{
$eq: null,
},
column
);
}

@@ -343,7 +389,10 @@ /**

*/
function between(minValue, maxValue) {
return {
$gte: minValue,
$lte: maxValue,
};
function between(minValue, maxValue, column) {
return wrapExpressionWithColumn(
{
$gte: minValue,
$lte: maxValue,
},
column
);
}

@@ -353,9 +402,12 @@ /**

*/
function notBetween(minValue, maxValue) {
return {
$not: {
$gte: minValue,
$lte: maxValue,
function notBetween(minValue, maxValue, column) {
return wrapExpressionWithColumn(
{
$not: {
$gte: minValue,
$lte: maxValue,
},
},
};
column
);
}

@@ -367,36 +419,49 @@ /**

return {
$concat: columns.map((column) => "$" + ltrim(column, "$")),
$concat: columns,
};
}
const merge = concat;
/**
* Concat columns with separator
* Concat columns with separator between each column
*/
function concatWith(separator, ...columns) {
const columnsList = [];
for (const column of columns) {
columnsList.push(columnName(column), separator);
}
return {
$concat: [separator, ...columns.map((column) => "$" + ltrim(column, "$"))],
$concat: columnsList,
};
}
const mergeWith = concatWith;
/**
* Get cond expression
*/
function cond(condition, ifTrue, ifFalse) {
return {
$cond: {
if: condition,
then: ifTrue,
else: ifFalse,
function cond(condition, ifTrue, ifFalse, column) {
return wrapExpressionWithColumn(
{
$cond: {
if: condition,
then: ifTrue,
else: ifFalse,
},
},
};
column
);
}
const condition = cond;
/**
* Boolean condition
*/
function booleanCond(condition) {
return {
$cond: {
if: condition,
then: true,
else: false,
function booleanCond(condition, column) {
return wrapExpressionWithColumn(
{
$cond: {
if: condition,
then: true,
else: false,
},
},
};
column
);
}

@@ -406,15 +471,53 @@ /**

*/
function regex(value) {
function regex(value, column) {
return wrapExpressionWithColumn(
{
$regex: value,
},
column
);
}
/**
* You can use it when you want a field to match all the given values
*/
function all(values, column) {
return wrapExpressionWithColumn(
{
$all: values,
},
column
);
}
/**
* Multiple expressions
*/
function _multiply(...expressions) {
return {
$regex: value,
$multiply: expressions,
};
}
/**
* You can use it when you want a field to match all the given values
* Multiple columns
*/
function all(values) {
function multiply(...columns) {
return {
$all: values,
$multiply: columns.map(columnName),
};
}
/**
* Divide expressions
*/
function _divide(...expressions) {
return {
$divide: expressions,
};
}
/**
* Divide columns
*/
function divide(...columns) {
return {
$divide: columns.map(columnName),
};
}
const $agg = {

@@ -425,2 +528,6 @@ // list all aggregation functions

avg,
multiply: multiply,
divide: divide,
_divide,
_multiply,
average,

@@ -470,5 +577,8 @@ min,

concat,
merge,
concatWith,
mergeWith,
columnName,
booleanCond,
cond,
booleanCond,
regex,

@@ -478,2 +588,4 @@ };

$agg,
_divide,
_multiply,
addToSet,

@@ -485,2 +597,3 @@ all,

booleanCond,
columnName,
columns,

@@ -490,5 +603,7 @@ concat,

cond,
condition,
count,
dayOfMonth,
dayOfWeek,
divide,
eq,

@@ -517,4 +632,7 @@ equal,

max,
merge,
mergeWith,
min,
month,
multiply,
ne,

@@ -521,0 +639,0 @@ nin,

import { GenericObject } from "@mongez/reinforcements";
import { Pipeline } from "./pipeline";
export declare class GroupByPipeline extends Pipeline {
protected readonly _id: string | null | GenericObject;
protected readonly _id: string | null | GenericObject | string[];
protected groupByData: GenericObject;

@@ -9,5 +9,5 @@ /**

*/
constructor(_id: string | null | GenericObject, groupByData?: GenericObject);
constructor(_id: string | null | GenericObject | string[], groupByData?: GenericObject);
}
export declare function groupByPipeline(column: string | null | GenericObject, groupByData: Record<string, any>): GroupByPipeline;
//# sourceMappingURL=GroupByPipeline.d.ts.map

@@ -1,2 +0,2 @@

import { ltrim } from "@mongez/reinforcements";
import { $agg } from "./expressions.js";
import { Pipeline } from "./pipeline.js";

@@ -14,4 +14,10 @@ class GroupByPipeline extends Pipeline {

if (typeof _id === "string") {
_id = "$" + ltrim(_id, "$");
_id = $agg.columnName(_id);
}
if (Array.isArray(_id)) {
_id = _id.reduce((result, column) => {
result[column] = $agg.columnName(column);
return result;
}, {});
}
this.data({

@@ -18,0 +24,0 @@ _id: _id,

import { Pipeline } from "./pipeline";
export type UnwindOptions = {
preserveNullAndEmptyArrays?: boolean;
includeArrayIndex?: string | null;
};
export declare class UnwindPipeline extends Pipeline {
protected readonly column: string;
protected readonly preserveNullAndEmptyArrays: boolean;
/**
* Constructor
*/
constructor(column: string, preserveNullAndEmptyArrays?: boolean);
constructor(column: string, options?: UnwindOptions);
}
export declare function unwindPipeline(column: string, preserveNullAndEmptyArrays?: boolean): UnwindPipeline;
export declare function unwindPipeline(column: string, options?: UnwindOptions): UnwindPipeline;
//# sourceMappingURL=UnwindPipeline.d.ts.map

@@ -1,22 +0,23 @@

import { ltrim } from "@mongez/reinforcements";
import { $agg } from "./expressions.js";
import { Pipeline } from "./pipeline.js";
class UnwindPipeline extends Pipeline {
column;
preserveNullAndEmptyArrays;
/**
* Constructor
*/
constructor(column, preserveNullAndEmptyArrays = false) {
constructor(column, options = {}) {
super("unwind");
this.column = column;
this.preserveNullAndEmptyArrays = preserveNullAndEmptyArrays;
const { preserveNullAndEmptyArrays = false, includeArrayIndex = null } =
options;
this.data({
path: "$" + ltrim(column, "$"),
preserveNullAndEmptyArrays: preserveNullAndEmptyArrays,
path: $agg.columnName(column),
preserveNullAndEmptyArrays,
includeArrayIndex,
});
}
}
function unwindPipeline(column, preserveNullAndEmptyArrays = false) {
return new UnwindPipeline(column, preserveNullAndEmptyArrays);
function unwindPipeline(column, options) {
return new UnwindPipeline(column, options);
}
export { UnwindPipeline, unwindPipeline };

@@ -1,4 +0,14 @@

import { ltrim } from "@mongez/reinforcements";
import Is from "@mongez/supportive-is";
import { toUTC } from "@mongez/time-wizard";
import { $agg } from "./expressions.js";
function escapeRegex(value, escapeOnly = false) {
if (value instanceof RegExp === false) {
// escape the value special characters
value = String(value).replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&");
if (escapeOnly === false) {
value = new RegExp(value, "i");
}
}
return value;
}
class WhereExpression {

@@ -49,9 +59,5 @@ /**

if (operator === "like") {
// escape the value special characters
value = String(value).replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&");
value = new RegExp(value, "i");
value = escapeRegex(value);
} else if (operator === "notLike") {
// escape the value special characters
value = value.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&");
value = new RegExp(value, "i");
value = escapeRegex(value);
operator = "not";

@@ -62,9 +68,7 @@ value = {

} else if (operator === "startsWith") {
// escape the value special characters
value = value.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&");
value = new RegExp(`^${value}`, "i");
value = escapeRegex(value, true);
value = new RegExp(`^${value}`);
} else if (operator === "endsWith") {
// escape the value special characters
value = value.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&");
value = new RegExp(`${value}$`, "i");
value = escapeRegex(value, true);
value = new RegExp(`${value}$`);
}

@@ -86,3 +90,3 @@ let expression = {

expression = {
$in: "$" + ltrim(value, "$"),
$in: $agg.columnName(value),
};

@@ -92,3 +96,3 @@ } else if (operator === "notIn" && typeof value === "string") {

$not: {
$in: "$" + ltrim(value, "$"),
$in: $agg.columnName(value),
},

@@ -95,0 +99,0 @@ };

@@ -13,5 +13,11 @@ import Is from "@mongez/supportive-is";

if (Array.isArray(value)) {
if (value[0]?.id) {
return value;
const results = [];
for (const item of value) {
if (value instanceof Model) {
results.push(getModelData(item, embeddedKey));
} else if (item?.id) {
results.push(item);
}
}
if (results.length > 0) return results;
const records = await model

@@ -26,19 +32,19 @@ .aggregate()

.map((record) => {
if (Array.isArray(embeddedKey)) {
return record.only(embeddedKey);
}
return record[embeddedKey];
return getModelData(record, embeddedKey);
})
.filter((value) => !Is.empty(value));
}
if (value instanceof Model) return getModelData(value, embeddedKey);
if (value?.id) return value;
const record =
value instanceof Model ? value : await model.find(Number(value));
const record = await model.find(Number(value));
if (!record) return null;
if (Array.isArray(embeddedKey)) {
return record.only(embeddedKey);
}
return record[embeddedKey];
return getModelData(record, embeddedKey);
};
}
function getModelData(model, embeddedKey) {
if (Array.isArray(embeddedKey)) {
return model.only(embeddedKey);
}
return model[embeddedKey];
}
export { castModel };

@@ -34,2 +34,4 @@ export {

$agg,
_divide,
_multiply,
addToSet,

@@ -41,2 +43,3 @@ all,

booleanCond,
columnName,
columns,

@@ -46,5 +49,7 @@ concat,

cond,
condition,
count,
dayOfMonth,
dayOfWeek,
divide,
eq,

@@ -73,4 +78,7 @@ equal,

max,
merge,
mergeWith,
min,
month,
multiply,
ne,

@@ -77,0 +85,0 @@ nin,

@@ -12,7 +12,7 @@ import { Aggregate } from "../aggregate";

*/
get(mapData?: (record: any) => any): Promise<T[]>;
get<Output = T>(mapData?: (record: any) => any): Promise<Output[]>;
/**
* {@inheritDoc}
*/
first(filters?: Filter): Promise<T>;
first(mapData?: (data: any) => any): Promise<T>;
/**

@@ -19,0 +19,0 @@ * {@inheritDoc}

@@ -25,4 +25,4 @@ import { toStudlyCase } from "@mongez/reinforcements";

*/
async first(filters) {
return await super.first(filters);
async first(mapData) {
return await super.first(mapData);
}

@@ -29,0 +29,0 @@ /**

{
"name": "@mongez/mongodb",
"version": "1.0.55",
"version": "1.0.56",
"description": "Powerful Mongodb Database Manager for Node Js",
"main": "./cjs/index.js",
"dependencies": {
"@mongez/events": "^2.0.0",
"@mongez/events": "^2.1.0",
"@mongez/logger": "^1.0.9",
"@mongez/reinforcements": "^2.3.5",
"@mongez/reinforcements": "^2.3.6",
"@mongez/supportive-is": "^1.0.12",

@@ -14,4 +14,4 @@ "@mongez/time-wizard": "^1.0.5",

"console-table-printer": "^2.11.1",
"dayjs": "^1.11.7",
"mongodb": "^5.4.0"
"dayjs": "^1.11.8",
"mongodb": "^5.6.0"
},

@@ -31,5 +31,5 @@ "scripts": {

"devDependencies": {
"@types/jest": "^29.5.1",
"@types/jest": "^29.5.2",
"chalk": "^4",
"eslint": "^8.40.0",
"eslint": "^8.42.0",
"eslint-config-prettier": "^8.8.0",

@@ -43,3 +43,3 @@ "eslint-plugin-prettier": "^4.2.1",

"ts-jest": "^29.1.0",
"typescript": "^5.0.4"
"typescript": "^5.1.3"
},

@@ -46,0 +46,0 @@ "keywords": [

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc