@warlock.js/cascade
Version:
ORM for managing databases
537 lines (536 loc) • 9.65 kB
JavaScript
import {ltrim}from'@mongez/reinforcements';import {isScalar}from'@mongez/supportive-is';/**
* Get count expression
*/
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, baseColumn) {
return wrapExpressionWithColumn({
$sum: columnName(column),
}, baseColumn);
}
/**
* Get average expression
*/
const average = avg;
function avg(column) {
return {
$avg: columnName(column),
};
}
/**
* Get min expression
*/
function min(column) {
return {
$min: columnName(column),
};
}
/**
* Get max expression
*/
function max(column) {
return {
$max: columnName(column),
};
}
/**
* Get first expression
*/
function first(column) {
return {
$first: columnName(column),
};
}
/**
* Get last expression
*/
function last(column) {
return {
$last: columnName(column),
};
}
/**
* Get push expression
*/
function push(data) {
if (typeof data === "string") {
data = columnName(data);
}
return {
$push: data,
};
}
/**
* Get addToSet expression
*/
function addToSet(column) {
return {
$addToSet: columnName(column),
};
}
/**
* Get year expression
*/
function year(column) {
return {
$year: columnName(column),
};
}
/**
* Get first value of year expression
*/
function firstYear(column) {
return {
$first: {
$year: columnName(column),
},
};
}
/**
* Get last value of year expression
*/
function lastYear(column) {
return {
$last: {
$year: columnName(column),
},
};
}
/**
* Get month expression
*/
function month(column) {
return {
$month: columnName(column),
};
}
/**
* Get week expression
*/
function week(column) {
return {
$isoWeek: columnName(column),
};
}
/**
* Get first value of month expression
*/
function firstMonth(column) {
return {
$first: {
$month: columnName(column),
},
};
}
/**
* Get last value of month expression
*/
function lastMonth(column) {
return {
$last: {
$month: columnName(column),
},
};
}
/**
* Get day of month expression
*/
function dayOfMonth(column) {
return {
$dayOfMonth: columnName(column),
};
}
/**
* Get first day of month expression
*/
function firstDayOfMonth(column) {
return {
$first: {
$dayOfMonth: columnName(column),
},
};
}
/**
* Get last day of month expression
*/
function lastDayOfMonth(column) {
return {
$last: {
$dayOfMonth: columnName(column),
},
};
}
/**
* Get day of week expression
*/
function dayOfWeek(column) {
return {
$dayOfWeek: columnName(column),
};
}
/**
* Return list of columns
*/
function columns(...columns) {
return columns.reduce((selections, column) => {
selections[column] = columnName(column);
return selections;
}, {});
}
/** Match helpers */
/**
* Get greater than expression
*/
const greaterThan = gt;
function gt(value, column) {
return wrapExpressionWithColumn({
$gt: value,
}, column);
}
/**
* Get greater than or equal expression
*/
const greaterThanOrEqual = gt;
function gte(value, column) {
return wrapExpressionWithColumn({
$gte: value,
}, column);
}
/**
* Get less than expression
*/
const lessThan = lt;
function lt(value, column) {
return wrapExpressionWithColumn({
$lt: value,
}, column);
}
/**
* Get less than or equal expression
*/
const lessThanOrEqual = lt;
function lte(value, column) {
return wrapExpressionWithColumn({
$lte: value,
}, column);
}
/**
* Get equal expression
*/
const equal = eq;
function eq(...values) {
return {
$eq: values,
};
}
/**
* Get not equal expression
*/
const notEqual = ne;
function ne(value) {
return {
$ne: value,
};
}
/**
* Get in array expression
*/
function inArray(value) {
return {
$in: value,
};
}
/**
* Get not in array expression
*/
const notIn = nin;
const notInArray = nin;
function nin(value) {
return {
$nin: value,
};
}
/**
* Get exists expression
*/
function exists(value, column) {
return wrapExpressionWithColumn({
$exists: value,
}, column);
}
/**
* Get not exists expression
*/
function notExists(value, column) {
return wrapExpressionWithColumn({
$exists: value,
}, column);
}
/**
* Get like expression
*/
function like(value, column) {
if (isScalar(value)) {
value = new RegExp(value, "gi");
}
return wrapExpressionWithColumn({
$regex: value,
}, column);
}
/**
* Get not like expression
*/
function notLike(value, column) {
if (isScalar(value)) {
value = new RegExp(value, "gi");
}
return wrapExpressionWithColumn({
$not: {
$regex: value,
},
}, column);
}
/**
* Get not null expression
*/
function notNull(column) {
return wrapExpressionWithColumn({
$ne: null,
}, column);
}
/**
* Get null expression
*/
function isNull(column) {
return wrapExpressionWithColumn({
$eq: null,
}, column);
}
/**
* Get between expression
*/
function between(minValue, maxValue, column) {
return wrapExpressionWithColumn({
$gte: minValue,
$lte: maxValue,
}, column);
}
/**
* Get not between expression
*/
function notBetween(minValue, maxValue, column) {
return wrapExpressionWithColumn({
$not: {
$gte: minValue,
$lte: maxValue,
},
}, column);
}
/**
* Get concat expression
*/
function concat(...columns) {
return {
$concat: columns,
};
}
const merge = concat;
/**
* 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: columnsList,
};
}
const mergeWith = concatWith;
/**
* Get cond expression
*/
function cond(condition, ifTrue, ifFalse, column) {
return wrapExpressionWithColumn({
$cond: {
if: condition,
then: ifTrue,
else: ifFalse,
},
}, column);
}
const condition = cond;
/**
* Boolean condition
*/
function booleanCond(condition, column) {
return wrapExpressionWithColumn({
$cond: {
if: condition,
then: true,
else: false,
},
}, column);
}
/**
* Get regex expression
*/
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 {
$multiply: expressions,
};
}
/**
* Multiple columns
*/
function multiply(...columns) {
return {
$multiply: columns.map(columnName),
};
}
/**
* Divide expressions
*/
function _divide(...expressions) {
return {
$divide: expressions,
};
}
/**
* Divide columns
*/
function divide(...columns) {
return {
$divide: columns.map(columnName),
};
}
/**
* Get size expression
*/
function size(column) {
return {
$size: columnName(column),
};
}
/**
* Get round expression
*/
function round(column, decimalPlaces) {
return {
$round: [columnName(column), decimalPlaces],
};
}
function _round(value, decimalPlaces) {
return {
$round: [value, decimalPlaces],
};
}
/**
* Get expression
*/
function expr(expression) {
return {
$expr: expression,
};
}
const $agg = {
// list all aggregation functions
count,
sum,
round,
_round,
avg,
multiply: multiply,
divide: divide,
_divide,
_multiply,
average,
min,
max,
first,
last,
push,
addToSet,
all,
year,
firstYear,
lastYear,
month,
firstMonth,
lastMonth,
firstDayOfMonth,
lastDayOfMonth,
dayOfMonth,
dayOfWeek,
columns,
gt,
greaterThan,
gte,
greaterThanOrEqual,
lt,
lessThan,
lte,
lessThanOrEqual,
eq,
equal,
ne,
notEqual,
inArray,
in: inArray,
nin,
notIn,
notInArray,
exists,
notExists,
like,
notLike,
notNull,
isNull,
between,
notBetween,
concat,
merge,
concatWith,
mergeWith,
columnName,
booleanCond,
cond,
regex,
expr,
size,
};export{$agg,_divide,_multiply,_round,addToSet,all,average,avg,between,booleanCond,columnName,columns,concat,concatWith,cond,condition,count,dayOfMonth,dayOfWeek,divide,eq,equal,exists,expr,first,firstDayOfMonth,firstMonth,firstYear,greaterThan,greaterThanOrEqual,gt,gte,inArray,isNull,last,lastDayOfMonth,lastMonth,lastYear,lessThan,lessThanOrEqual,like,lt,lte,max,merge,mergeWith,min,month,multiply,ne,nin,notBetween,notEqual,notExists,notIn,notInArray,notLike,notNull,push,regex,round,size,sum,week,year};//# sourceMappingURL=expressions.js.map