@ts-awesome/orm 1.5.3 → 1.5.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/operators.d.ts +8 -0
- package/dist/operators.js +17 -3
- package/dist/operators.js.map +1 -1
- package/package.json +1 -1
- package/tests/builder.spec.js +39 -0
- package/tests/builder.spec.ts +44 -1
package/dist/operators.d.ts
CHANGED
|
@@ -8,6 +8,7 @@ export declare function exists(subquery: IBuildableSubSelectQuery): IOperandable
|
|
|
8
8
|
export declare function avg<T>(value: IOperandable<T>): IOperandable<T>;
|
|
9
9
|
export declare function sum<T>(value: IOperandable<T>): IOperandable<T>;
|
|
10
10
|
export declare function count<T>(value?: IOperandable<T>): IOperandable<number>;
|
|
11
|
+
export declare function count<T>(value: IOperandable<T>, unique: true): IOperandable<number>;
|
|
11
12
|
export declare function max<T>(value: IOperandable<T>): IOperandable<T>;
|
|
12
13
|
export declare function min<T>(value: IOperandable<T>): IOperandable<T>;
|
|
13
14
|
export declare function asc<T>(value: Column<T> | IOperandable<T> | number): Order;
|
|
@@ -17,3 +18,10 @@ export declare function of<X extends TableMetaProvider, R = InstanceType<X>, F e
|
|
|
17
18
|
export declare function of<X extends TableMetaProvider, R = InstanceType<X>, F extends keyof R = keyof R>(_: ITableRef<X>, field: F): IOperandable<R[F]>;
|
|
18
19
|
export declare function alias<T>(expr: T | IOperandable<T>, name: string): IOperandable<T>;
|
|
19
20
|
export declare function cast<R = unknown, T = unknown>(expr: T | IOperandable<T>, type: string): IOperandable<R>;
|
|
21
|
+
export type CaseOperand<T> = {
|
|
22
|
+
when: IOperandable<boolean> | boolean;
|
|
23
|
+
then: IOperandable<T> | T;
|
|
24
|
+
} | {
|
|
25
|
+
else: IOperandable<T> | T;
|
|
26
|
+
};
|
|
27
|
+
export declare function case_<T = unknown>(...args: CaseOperand<T>[]): IOperandable<T>;
|
package/dist/operators.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.cast = exports.alias = exports.of = exports.desc = exports.asc = exports.min = exports.max = exports.count = exports.sum = exports.avg = exports.exists = exports.any = exports.all = exports.not = exports.or = exports.and = void 0;
|
|
3
|
+
exports.case_ = exports.cast = exports.alias = exports.of = exports.desc = exports.asc = exports.min = exports.max = exports.count = exports.sum = exports.avg = exports.exists = exports.any = exports.all = exports.not = exports.or = exports.and = void 0;
|
|
4
4
|
const wrappers_1 = require("./wrappers");
|
|
5
5
|
const symbols_1 = require("./symbols");
|
|
6
6
|
function and(...operands) {
|
|
@@ -35,8 +35,8 @@ function sum(value) {
|
|
|
35
35
|
return new wrappers_1.FunctionCall('SUM', [value]);
|
|
36
36
|
}
|
|
37
37
|
exports.sum = sum;
|
|
38
|
-
function count(value) {
|
|
39
|
-
return new wrappers_1.FunctionCall('COUNT', [value || '*']);
|
|
38
|
+
function count(value, unique = false) {
|
|
39
|
+
return new wrappers_1.FunctionCall('COUNT', unique ? ['DISTINCT', value] : [value || '*']);
|
|
40
40
|
}
|
|
41
41
|
exports.count = count;
|
|
42
42
|
function max(value) {
|
|
@@ -76,4 +76,18 @@ function cast(expr, type) {
|
|
|
76
76
|
return new wrappers_1.Operandable('CAST', [expr, type]);
|
|
77
77
|
}
|
|
78
78
|
exports.cast = cast;
|
|
79
|
+
function case_(...args) {
|
|
80
|
+
let count = 0;
|
|
81
|
+
for (const block of args) {
|
|
82
|
+
count += ('else' in block) ? 1 : 0;
|
|
83
|
+
}
|
|
84
|
+
if (count > 1) {
|
|
85
|
+
throw new Error(`CASE can contain only one ELSE block`);
|
|
86
|
+
}
|
|
87
|
+
if (count == 1 && !('else' in args[args.length - 1])) {
|
|
88
|
+
throw new Error(`CASE should have ELSE as last block`);
|
|
89
|
+
}
|
|
90
|
+
return new wrappers_1.Operandable('CASE', args);
|
|
91
|
+
}
|
|
92
|
+
exports.case_ = case_;
|
|
79
93
|
//# sourceMappingURL=operators.js.map
|
package/dist/operators.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"operators.js","sourceRoot":"","sources":["../src/operators.ts"],"names":[],"mappings":";;;AASA,yCAAoE;AACpE,uCAA8C;AAE9C,SAAgB,GAAG,CAAC,GAAG,QAA6C;IAClE,OAAO,IAAI,sBAAW,CAAC,KAAK,EAAE,QAAQ,CAA0B,CAAA;AAClE,CAAC;AAFD,kBAEC;AAED,SAAgB,EAAE,CAAC,GAAG,QAA6C;IACjE,OAAO,IAAI,sBAAW,CAAC,IAAI,EAAE,QAAQ,CAA0B,CAAA;AACjE,CAAC;AAFD,gBAEC;AAED,SAAgB,GAAG,CAAC,SAA0C;IAC5D,OAAO,IAAI,sBAAW,CAAC,KAAK,EAAE,CAAC,SAAS,CAAC,CAA0B,CAAA;AACrE,CAAC;AAFD,kBAEC;AAED,SAAgB,GAAG,CAAQ,QAAkC;IAC3D,OAAO,IAAI,sBAAW,CAAC,KAAK,EAAE,CAAC,QAAQ,CAAC,CAAoB,CAAA;AAC9D,CAAC;AAFD,kBAEC;AAED,SAAgB,GAAG,CAAQ,QAAkC;IAC3D,OAAO,IAAI,sBAAW,CAAC,KAAK,EAAE,CAAC,QAAQ,CAAC,CAAoB,CAAA;AAC9D,CAAC;AAFD,kBAEC;AAED,SAAgB,MAAM,CAAC,QAAkC;IACvD,OAAO,IAAI,sBAAW,CAAC,QAAQ,EAAE,CAAC,QAAQ,CAAC,CAA0B,CAAA;AACvE,CAAC;AAFD,wBAEC;AAED,SAAgB,GAAG,CAAI,KAAsB;IAC3C,OAAO,IAAI,uBAAY,CAAC,KAAK,EAAE,CAAC,KAAK,CAAC,CAAoB,CAAA;AAC5D,CAAC;AAFD,kBAEC;AAED,SAAgB,GAAG,CAAI,KAAsB;IAC3C,OAAO,IAAI,uBAAY,CAAC,KAAK,EAAE,CAAC,KAAK,CAAC,CAAoB,CAAA;AAC5D,CAAC;AAFD,kBAEC;
|
|
1
|
+
{"version":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527
527
|
}]);
|
|
528
528
|
});
|
|
529
|
+
it('CASE operator', () => {
|
|
530
|
+
const query = (0, dist_1.Select)(models_1.Person)
|
|
531
|
+
.columns(x => [(0, dist_1.alias)((0, dist_2.case_)({ when: x.age.gte(2), then: 'yes' }, { else: 'no' }), 'dynamic')]);
|
|
532
|
+
expect(query._columns).toStrictEqual([{
|
|
533
|
+
"_alias": "dynamic",
|
|
534
|
+
"_operands": [{
|
|
535
|
+
"_operator": "CASE",
|
|
536
|
+
"_operands": [
|
|
537
|
+
{
|
|
538
|
+
"when": {
|
|
539
|
+
"_operator": ">=",
|
|
540
|
+
"_operands": [
|
|
541
|
+
{ "_column": { "name": "age", "table": "Person" } },
|
|
542
|
+
2
|
|
543
|
+
]
|
|
544
|
+
},
|
|
545
|
+
"then": "yes",
|
|
546
|
+
},
|
|
547
|
+
{ "else": "no" }
|
|
548
|
+
]
|
|
549
|
+
}]
|
|
550
|
+
}]);
|
|
551
|
+
});
|
|
529
552
|
it('intersect operator', () => {
|
|
530
553
|
const query = (0, dist_1.Select)(models_1.Person)
|
|
531
554
|
.columns(['name'])
|
|
@@ -582,6 +605,22 @@ describe('Select', () => {
|
|
|
582
605
|
}
|
|
583
606
|
}]);
|
|
584
607
|
});
|
|
608
|
+
it('COUNT unique', () => {
|
|
609
|
+
const query = (0, dist_1.Select)(models_1.Person)
|
|
610
|
+
.columns(x => [(0, dist_2.count)(x.id, true)]);
|
|
611
|
+
expect(query._columns).toStrictEqual([{
|
|
612
|
+
_func: "COUNT",
|
|
613
|
+
_args: [
|
|
614
|
+
"DISTINCT",
|
|
615
|
+
{
|
|
616
|
+
_column: {
|
|
617
|
+
name: 'id',
|
|
618
|
+
table: 'Person'
|
|
619
|
+
}
|
|
620
|
+
}
|
|
621
|
+
]
|
|
622
|
+
}]);
|
|
623
|
+
});
|
|
585
624
|
});
|
|
586
625
|
describe('Insert', () => {
|
|
587
626
|
it('Check query info', () => {
|
package/tests/builder.spec.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import {alias, and, asc, Delete, desc, Insert, max, Select, sum, TableMetadataSymbol, Update, Upsert, of} from '../dist';
|
|
2
2
|
import { Employee, Person, Tag } from './models';
|
|
3
3
|
import { TableRef, readModelMeta } from '../dist/builder';
|
|
4
|
-
import {count, dbField, dbTable, exists} from "../dist";
|
|
4
|
+
import {count, dbField, dbTable, exists, case_} from "../dist";
|
|
5
5
|
|
|
6
6
|
|
|
7
7
|
const tableInfo = readModelMeta(Person);
|
|
@@ -567,6 +567,31 @@ describe('Select', () => {
|
|
|
567
567
|
}])
|
|
568
568
|
})
|
|
569
569
|
|
|
570
|
+
it('CASE operator', () => {
|
|
571
|
+
const query = Select(Person)
|
|
572
|
+
.columns(x => [alias(case_({when: x.age.gte(2), then: 'yes'}, {else: 'no'}), 'dynamic')])
|
|
573
|
+
|
|
574
|
+
expect(query._columns).toStrictEqual([{
|
|
575
|
+
"_alias": "dynamic",
|
|
576
|
+
"_operands": [{
|
|
577
|
+
"_operator": "CASE",
|
|
578
|
+
"_operands": [
|
|
579
|
+
{
|
|
580
|
+
"when": {
|
|
581
|
+
"_operator": ">=",
|
|
582
|
+
"_operands": [
|
|
583
|
+
{ "_column": {"name": "age", "table": "Person"}},
|
|
584
|
+
2
|
|
585
|
+
]
|
|
586
|
+
},
|
|
587
|
+
"then": "yes",
|
|
588
|
+
},
|
|
589
|
+
{"else": "no"}
|
|
590
|
+
]
|
|
591
|
+
}]
|
|
592
|
+
}])
|
|
593
|
+
})
|
|
594
|
+
|
|
570
595
|
it ('intersect operator', () => {
|
|
571
596
|
const query = Select(Person)
|
|
572
597
|
.columns(['name'])
|
|
@@ -628,6 +653,24 @@ describe('Select', () => {
|
|
|
628
653
|
}
|
|
629
654
|
}])
|
|
630
655
|
})
|
|
656
|
+
|
|
657
|
+
it('COUNT unique', () => {
|
|
658
|
+
const query = Select(Person)
|
|
659
|
+
.columns(x => [count(x.id, true)]);
|
|
660
|
+
|
|
661
|
+
expect(query._columns).toStrictEqual([{
|
|
662
|
+
_func: "COUNT",
|
|
663
|
+
_args: [
|
|
664
|
+
"DISTINCT",
|
|
665
|
+
{
|
|
666
|
+
_column: {
|
|
667
|
+
name: 'id',
|
|
668
|
+
table: 'Person'
|
|
669
|
+
}
|
|
670
|
+
}
|
|
671
|
+
]
|
|
672
|
+
}])
|
|
673
|
+
});
|
|
631
674
|
});
|
|
632
675
|
|
|
633
676
|
describe('Insert', () => {
|