@snowtop/ent 0.1.19-test2 → 0.1.20

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/core/clause.js CHANGED
@@ -24,7 +24,7 @@ var __importStar = (this && this.__importStar) || function (mod) {
24
24
  };
25
25
  Object.defineProperty(exports, "__esModule", { value: true });
26
26
  exports.JSONKeyInList = exports.JSONBKeyInList = exports.JSONKeyExists = exports.JSONPathValuePredicate = exports.JSONObjectFieldKeyAsText = exports.JSONObjectFieldKeyASJSON = exports.sensitiveValue = exports.TsVectorWebsearchToTsQuery = exports.TsVectorPhraseToTsQuery = exports.TsVectorPlainToTsQuery = exports.TsVectorColTsQuery = exports.WebsearchToTsQuery = exports.PhraseToTsQuery = exports.PlainToTsQuery = exports.TsQuery = exports.DBTypeNotIn = exports.TextNotIn = exports.IntegerNotIn = exports.UuidNotIn = exports.DBTypeIn = exports.TextIn = exports.IntegerIn = exports.UuidIn = exports.In = exports.OrOptional = exports.Or = exports.AndOptional = exports.And = exports.LessEq = exports.GreaterEq = exports.Less = exports.Greater = exports.NotEq = exports.ContainsIgnoreCase = exports.EndsWithIgnoreCase = exports.StartsWithIgnoreCase = exports.Contains = exports.EndsWith = exports.StartsWith = exports.Eq = exports.ArrayNotEq = exports.ArrayEq = exports.PostgresArrayNotOverlaps = exports.PostgresArrayOverlaps = exports.PostgresArrayNotContains = exports.PostgresArrayNotContainsValue = exports.PostgresArrayContains = exports.PostgresArrayContainsValue = exports.notInClause = exports.inClause = void 0;
27
- exports.Expression = exports.getCombinedClause = exports.Modulo = exports.Divide = exports.Multiply = exports.Subtract = exports.Add = exports.PaginationMultipleColsSubQuery = void 0;
27
+ exports.Expression = exports.getCombinedClause = exports.Modulo = exports.Divide = exports.Multiply = exports.Subtract = exports.Add = exports.PaginationMultipleColsQuery = exports.PaginationMultipleColsSubQuery = void 0;
28
28
  const db_1 = __importStar(require("./db"));
29
29
  const query_impl_1 = require("./query_impl");
30
30
  function isSensitive(val) {
@@ -38,18 +38,21 @@ function rawValue(val) {
38
38
  }
39
39
  return val;
40
40
  }
41
- function renderCol(col, alias) {
41
+ function renderCol(col, overrideAlias, alias) {
42
+ if (overrideAlias) {
43
+ return `${overrideAlias}.${col}`;
44
+ }
42
45
  if (alias) {
43
46
  return `${alias}.${col}`;
44
47
  }
45
48
  return col;
46
49
  }
47
50
  class simpleClause {
48
- constructor(col, value, op, handleNull) {
51
+ constructor(col, value, op, opts) {
49
52
  this.col = col;
50
53
  this.value = value;
51
54
  this.op = op;
52
- this.handleNull = handleNull;
55
+ this.opts = opts;
53
56
  }
54
57
  clause(idx, alias) {
55
58
  const nullClause = this.nullClause();
@@ -57,15 +60,15 @@ class simpleClause {
57
60
  return nullClause.clause(idx, alias);
58
61
  }
59
62
  if (db_1.default.getDialect() === db_1.Dialect.Postgres) {
60
- return `${renderCol(this.col, alias)} ${this.op} $${idx}`;
63
+ return `${renderCol(this.col, this.opts?.overrideAlias, alias)} ${this.op} $${idx}`;
61
64
  }
62
- return `${renderCol(this.col, alias)} ${this.op} ?`;
65
+ return `${renderCol(this.col, this.opts?.overrideAlias, alias)} ${this.op} ?`;
63
66
  }
64
67
  nullClause() {
65
- if (!this.handleNull || this.value !== null) {
68
+ if (!this.opts?.handleNull || this.value !== null) {
66
69
  return;
67
70
  }
68
- return this.handleNull;
71
+ return this.opts.handleNull;
69
72
  }
70
73
  columns() {
71
74
  return [this.col];
@@ -95,6 +98,9 @@ class simpleClause {
95
98
  if (nullClause) {
96
99
  return nullClause.instanceKey();
97
100
  }
101
+ if (this.opts?.overrideAlias) {
102
+ return `${this.opts.overrideAlias}.${this.col}${this.op}${rawValue(this.value)}`;
103
+ }
98
104
  return `${this.col}${this.op}${rawValue(this.value)}`;
99
105
  }
100
106
  }
@@ -131,11 +137,12 @@ class existsQueryClause extends queryClause {
131
137
  }
132
138
  }
133
139
  class isNullClause {
134
- constructor(col) {
140
+ constructor(col, overrideAlias) {
135
141
  this.col = col;
142
+ this.overrideAlias = overrideAlias;
136
143
  }
137
144
  clause(_idx, alias) {
138
- return `${renderCol(this.col, alias)} IS NULL`;
145
+ return `${renderCol(this.col, this.overrideAlias, alias)} IS NULL`;
139
146
  }
140
147
  columns() {
141
148
  return [];
@@ -147,27 +154,15 @@ class isNullClause {
147
154
  return [];
148
155
  }
149
156
  instanceKey() {
150
- return `${this.col} IS NULL`;
157
+ return `${this.col}${this.overrideAlias ?? ""} IS NULL`;
151
158
  }
152
159
  }
153
- class isNotNullClause {
154
- constructor(col) {
155
- this.col = col;
156
- }
160
+ class isNotNullClause extends isNullClause {
157
161
  clause(idx, alias) {
158
- return `${renderCol(this.col, alias)} IS NOT NULL`;
159
- }
160
- columns() {
161
- return [];
162
- }
163
- values() {
164
- return [];
165
- }
166
- logValues() {
167
- return [];
162
+ return `${renderCol(this.col, this.overrideAlias, alias)} IS NOT NULL`;
168
163
  }
169
164
  instanceKey() {
170
- return `${this.col} IS NOT NULL`;
165
+ return `${this.col}${this.overrideAlias ?? ""} IS NOT NULL`;
171
166
  }
172
167
  }
173
168
  class simpleExpression {
@@ -191,14 +186,15 @@ class simpleExpression {
191
186
  }
192
187
  }
193
188
  class arraySimpleClause {
194
- constructor(col, value, op) {
189
+ constructor(col, value, op, overrideAlias) {
195
190
  this.col = col;
196
191
  this.value = value;
197
192
  this.op = op;
193
+ this.overrideAlias = overrideAlias;
198
194
  }
199
195
  clause(idx, alias) {
200
196
  if (db_1.default.getDialect() === db_1.Dialect.Postgres) {
201
- return `$${idx} ${this.op} ANY(${renderCol(this.col, alias)})`;
197
+ return `$${idx} ${this.op} ANY(${renderCol(this.col, this.overrideAlias, alias)})`;
202
198
  }
203
199
  return `${renderCol(this.col, alias)} ${this.op} ?`;
204
200
  }
@@ -218,22 +214,22 @@ class arraySimpleClause {
218
214
  return [this.value];
219
215
  }
220
216
  instanceKey() {
221
- return `${this.col}${this.op}${rawValue(this.value)}`;
217
+ return `${this.col}${this.overrideAlias ?? ""}${this.op}${rawValue(this.value)}`;
222
218
  }
223
219
  }
224
220
  class postgresArrayOperator {
225
- constructor(col, value, op, not) {
221
+ constructor(col, value, op, opts) {
226
222
  this.col = col;
227
223
  this.value = value;
228
224
  this.op = op;
229
- this.not = not;
225
+ this.opts = opts;
230
226
  }
231
227
  clause(idx, alias) {
232
228
  if (db_1.default.getDialect() === db_1.Dialect.Postgres) {
233
- if (this.not) {
234
- return `NOT ${renderCol(this.col, alias)} ${this.op} $${idx}`;
229
+ if (this.opts?.not) {
230
+ return `NOT ${renderCol(this.col, this.opts.overrideAlias, alias)} ${this.op} $${idx}`;
235
231
  }
236
- return `${renderCol(this.col, alias)} ${this.op} $${idx}`;
232
+ return `${renderCol(this.col, this.opts?.overrideAlias, alias)} ${this.op} $${idx}`;
237
233
  }
238
234
  throw new Error(`not supported`);
239
235
  }
@@ -253,15 +249,21 @@ class postgresArrayOperator {
253
249
  return [`{${this.value}}`];
254
250
  }
255
251
  instanceKey() {
256
- if (this.not) {
257
- return `NOT:${this.col}${this.op}${rawValue(this.value)}`;
252
+ if (this.opts?.not) {
253
+ if (this.opts.overrideAlias) {
254
+ return `NOT:${this.opts.overrideAlias}.${this.col}${this.op}${rawValue(this.value)}`;
255
+ }
256
+ return `NOT:${this.col}${this.opts?.overrideAlias ?? ""}${this.op}${rawValue(this.value)}`;
257
+ }
258
+ if (this.opts?.overrideAlias) {
259
+ return `${this.opts.overrideAlias}.${this.col}${this.op}${rawValue(this.value)}`;
258
260
  }
259
261
  return `${this.col}${this.op}${rawValue(this.value)}`;
260
262
  }
261
263
  }
262
264
  class postgresArrayOperatorList extends postgresArrayOperator {
263
- constructor(col, value, op, not) {
264
- super(col, value, op, not);
265
+ constructor(col, value, op, opts) {
266
+ super(col, value, op, opts);
265
267
  }
266
268
  values() {
267
269
  return [
@@ -292,20 +294,25 @@ class inClause {
292
294
  static getPostgresInClauseValuesThreshold() {
293
295
  return 70;
294
296
  }
295
- constructor(col, value, type = "uuid") {
297
+ constructor(col, value, type = "uuid", overrideAlias) {
296
298
  this.col = col;
297
299
  this.value = value;
298
300
  this.type = type;
301
+ this.overrideAlias = overrideAlias;
299
302
  this.op = "IN";
300
303
  }
301
304
  clause(idx, alias) {
302
305
  // do a simple = when only one item
303
306
  if (this.value.length === 1) {
304
307
  if (this.op === "IN") {
305
- return new simpleClause(this.col, this.value[0], "=").clause(idx, alias);
308
+ return new simpleClause(this.col, this.value[0], "=", {
309
+ overrideAlias: this.overrideAlias,
310
+ }).clause(idx, alias);
306
311
  }
307
312
  else {
308
- return new simpleClause(this.col, this.value[0], "!=").clause(idx, alias);
313
+ return new simpleClause(this.col, this.value[0], "!=", {
314
+ overrideAlias: this.overrideAlias,
315
+ }).clause(idx, alias);
309
316
  }
310
317
  }
311
318
  const postgres = db_1.default.getDialect() === db_1.Dialect.Postgres;
@@ -338,7 +345,7 @@ class inClause {
338
345
  if (postgresValuesList) {
339
346
  inValue = `VALUES${inValue}`;
340
347
  }
341
- return `${renderCol(this.col, alias)} ${this.op} (${inValue})`;
348
+ return `${renderCol(this.col, this.overrideAlias, alias)} ${this.op} (${inValue})`;
342
349
  // TODO we need to return idx at end to query builder...
343
350
  // or anything that's doing a composite query so next clause knows where to start
344
351
  // or change to a sqlx.Rebind format
@@ -362,6 +369,9 @@ class inClause {
362
369
  return result;
363
370
  }
364
371
  instanceKey() {
372
+ if (this.overrideAlias) {
373
+ return `${this.op.toLowerCase()}:${this.overrideAlias}.${this.col}:${this.values().join(",")}`;
374
+ }
365
375
  return `${this.op.toLowerCase()}:${this.col}:${this.values().join(",")}`;
366
376
  }
367
377
  }
@@ -427,10 +437,10 @@ class compositeClause {
427
437
  }
428
438
  }
429
439
  class tsQueryClause {
430
- constructor(col, val, tsVectorCol) {
440
+ constructor(col, val, opts) {
431
441
  this.col = col;
432
442
  this.val = val;
433
- this.tsVectorCol = tsVectorCol;
443
+ this.opts = opts;
434
444
  }
435
445
  isTsQuery(val) {
436
446
  return typeof val !== "string";
@@ -447,13 +457,13 @@ class tsQueryClause {
447
457
  clause(idx, alias) {
448
458
  const { language } = this.getInfo();
449
459
  if (db_1.Dialect.Postgres === db_1.default.getDialect()) {
450
- if (this.tsVectorCol) {
451
- return `to_tsvector(${renderCol(this.col, alias)}) @@ ${this.getFunction()}('${language}', $${idx})`;
460
+ if (this.opts?.tsVectorCol) {
461
+ return `to_tsvector(${renderCol(this.col, this.opts.overrideAlias, alias)}) @@ ${this.getFunction()}('${language}', $${idx})`;
452
462
  }
453
- return `${renderCol(this.col, alias)} @@ ${this.getFunction()}('${language}', $${idx})`;
463
+ return `${renderCol(this.col, this.opts?.overrideAlias, alias)} @@ ${this.getFunction()}('${language}', $${idx})`;
454
464
  }
455
465
  // FYI this doesn't actually work for sqlite since different
456
- return `${renderCol(this.col, alias)} @@ ${this.getFunction()}('${language}', ?)`;
466
+ return `${renderCol(this.col, this.opts?.overrideAlias, alias)} @@ ${this.getFunction()}('${language}', ?)`;
457
467
  }
458
468
  columns() {
459
469
  return [this.col];
@@ -471,9 +481,15 @@ class tsQueryClause {
471
481
  }
472
482
  instanceKey() {
473
483
  const { language, value } = this.getInfo();
474
- if (this.tsVectorCol) {
484
+ if (this.opts?.tsVectorCol) {
485
+ if (this.opts.overrideAlias) {
486
+ return `to_tsvector(${this.opts.overrideAlias}.${this.col})@@${this.getFunction()}:${language}:${value}`;
487
+ }
475
488
  return `to_tsvector(${this.col})@@${this.getFunction()}:${language}:${value}`;
476
489
  }
490
+ if (this.opts?.overrideAlias) {
491
+ return `${this.opts.overrideAlias}.${this.col}@@${this.getFunction()}:${language}:${value}`;
492
+ }
477
493
  return `${this.col}@@${this.getFunction()}:${language}:${value}`;
478
494
  }
479
495
  }
@@ -499,8 +515,8 @@ class websearchTosQueryClause extends tsQueryClause {
499
515
  * only works with postgres gin indexes
500
516
  * https://www.postgresql.org/docs/current/indexes-types.html#INDEXES-TYPES-GIN
501
517
  */
502
- function PostgresArrayContainsValue(col, value) {
503
- return new postgresArrayOperator(col, value, "@>");
518
+ function PostgresArrayContainsValue(col, value, overrideAlias) {
519
+ return new postgresArrayOperator(col, value, "@>", { overrideAlias });
504
520
  }
505
521
  exports.PostgresArrayContainsValue = PostgresArrayContainsValue;
506
522
  /**
@@ -508,8 +524,8 @@ exports.PostgresArrayContainsValue = PostgresArrayContainsValue;
508
524
  * only works with postgres gin indexes
509
525
  * https://www.postgresql.org/docs/current/indexes-types.html#INDEXES-TYPES-GIN
510
526
  */
511
- function PostgresArrayContains(col, value) {
512
- return new postgresArrayOperatorList(col, value, "@>");
527
+ function PostgresArrayContains(col, value, overrideAlias) {
528
+ return new postgresArrayOperatorList(col, value, "@>", { overrideAlias });
513
529
  }
514
530
  exports.PostgresArrayContains = PostgresArrayContains;
515
531
  /**
@@ -517,8 +533,11 @@ exports.PostgresArrayContains = PostgresArrayContains;
517
533
  * only works with postgres gin indexes
518
534
  * https://www.postgresql.org/docs/current/indexes-types.html#INDEXES-TYPES-GIN
519
535
  */
520
- function PostgresArrayNotContainsValue(col, value) {
521
- return new postgresArrayOperator(col, value, "@>", true);
536
+ function PostgresArrayNotContainsValue(col, value, overrideAlias) {
537
+ return new postgresArrayOperator(col, value, "@>", {
538
+ not: true,
539
+ overrideAlias,
540
+ });
522
541
  }
523
542
  exports.PostgresArrayNotContainsValue = PostgresArrayNotContainsValue;
524
543
  /**
@@ -526,8 +545,11 @@ exports.PostgresArrayNotContainsValue = PostgresArrayNotContainsValue;
526
545
  * only works with postgres gin indexes
527
546
  * https://www.postgresql.org/docs/current/indexes-types.html#INDEXES-TYPES-GIN
528
547
  */
529
- function PostgresArrayNotContains(col, value) {
530
- return new postgresArrayOperatorList(col, value, "@>", true);
548
+ function PostgresArrayNotContains(col, value, overrideAlias) {
549
+ return new postgresArrayOperatorList(col, value, "@>", {
550
+ not: true,
551
+ overrideAlias,
552
+ });
531
553
  }
532
554
  exports.PostgresArrayNotContains = PostgresArrayNotContains;
533
555
  /**
@@ -535,8 +557,10 @@ exports.PostgresArrayNotContains = PostgresArrayNotContains;
535
557
  * only works with postgres gin indexes
536
558
  * https://www.postgresql.org/docs/current/indexes-types.html#INDEXES-TYPES-GIN
537
559
  */
538
- function PostgresArrayOverlaps(col, value) {
539
- return new postgresArrayOperatorList(col, value, "&&");
560
+ function PostgresArrayOverlaps(col, value, overrideAlias) {
561
+ return new postgresArrayOperatorList(col, value, "&&", {
562
+ overrideAlias,
563
+ });
540
564
  }
541
565
  exports.PostgresArrayOverlaps = PostgresArrayOverlaps;
542
566
  /**
@@ -544,8 +568,11 @@ exports.PostgresArrayOverlaps = PostgresArrayOverlaps;
544
568
  * only works with postgres gin indexes
545
569
  * https://www.postgresql.org/docs/current/indexes-types.html#INDEXES-TYPES-GIN
546
570
  */
547
- function PostgresArrayNotOverlaps(col, value) {
548
- return new postgresArrayOperatorList(col, value, "&&", true);
571
+ function PostgresArrayNotOverlaps(col, value, overrideAlias) {
572
+ return new postgresArrayOperatorList(col, value, "&&", {
573
+ not: true,
574
+ overrideAlias,
575
+ });
549
576
  }
550
577
  exports.PostgresArrayNotOverlaps = PostgresArrayNotOverlaps;
551
578
  /**
@@ -562,52 +589,70 @@ function ArrayNotEq(col, value) {
562
589
  return new arraySimpleClause(col, value, "!=");
563
590
  }
564
591
  exports.ArrayNotEq = ArrayNotEq;
565
- function Eq(col, value) {
566
- return new simpleClause(col, value, "=", new isNullClause(col));
592
+ function Eq(col, value, overrideAlias) {
593
+ return new simpleClause(col, value, "=", {
594
+ handleNull: new isNullClause(col),
595
+ overrideAlias,
596
+ });
567
597
  }
568
598
  exports.Eq = Eq;
569
- function StartsWith(col, value) {
570
- return new simpleClause(col, `${value}%`, "LIKE");
599
+ function StartsWith(col, value, overrideAlias) {
600
+ return new simpleClause(col, `${value}%`, "LIKE", {
601
+ overrideAlias,
602
+ });
571
603
  }
572
604
  exports.StartsWith = StartsWith;
573
- function EndsWith(col, value) {
574
- return new simpleClause(col, `%${value}`, "LIKE");
605
+ function EndsWith(col, value, overrideAlias) {
606
+ return new simpleClause(col, `%${value}`, "LIKE", {
607
+ overrideAlias,
608
+ });
575
609
  }
576
610
  exports.EndsWith = EndsWith;
577
- function Contains(col, value) {
578
- return new simpleClause(col, `%${value}%`, "LIKE");
611
+ function Contains(col, value, overrideAlias) {
612
+ return new simpleClause(col, `%${value}%`, "LIKE", {
613
+ overrideAlias,
614
+ });
579
615
  }
580
616
  exports.Contains = Contains;
581
- function StartsWithIgnoreCase(col, value) {
582
- return new simpleClause(col, `${value}%`, "ILIKE");
617
+ function StartsWithIgnoreCase(col, value, overrideAlias) {
618
+ return new simpleClause(col, `${value}%`, "ILIKE", {
619
+ overrideAlias,
620
+ });
583
621
  }
584
622
  exports.StartsWithIgnoreCase = StartsWithIgnoreCase;
585
- function EndsWithIgnoreCase(col, value) {
586
- return new simpleClause(col, `%${value}`, "ILIKE");
623
+ function EndsWithIgnoreCase(col, value, overrideAlias) {
624
+ return new simpleClause(col, `%${value}`, "ILIKE", { overrideAlias });
587
625
  }
588
626
  exports.EndsWithIgnoreCase = EndsWithIgnoreCase;
589
- function ContainsIgnoreCase(col, value) {
590
- return new simpleClause(col, `%${value}%`, "ILIKE");
627
+ function ContainsIgnoreCase(col, value, overrideAlias) {
628
+ return new simpleClause(col, `%${value}%`, "ILIKE", {
629
+ overrideAlias,
630
+ });
591
631
  }
592
632
  exports.ContainsIgnoreCase = ContainsIgnoreCase;
593
- function NotEq(col, value) {
594
- return new simpleClause(col, value, "!=", new isNotNullClause(col));
633
+ function NotEq(col, value, overrideAlias) {
634
+ return new simpleClause(col, value, "!=", {
635
+ handleNull: new isNotNullClause(col),
636
+ overrideAlias,
637
+ });
595
638
  }
596
639
  exports.NotEq = NotEq;
597
- function Greater(col, value) {
598
- return new simpleClause(col, value, ">");
640
+ function Greater(col, value, overrideAlias) {
641
+ return new simpleClause(col, value, ">", {
642
+ overrideAlias,
643
+ });
599
644
  }
600
645
  exports.Greater = Greater;
601
- function Less(col, value) {
602
- return new simpleClause(col, value, "<");
646
+ function Less(col, value, overrideAlias) {
647
+ return new simpleClause(col, value, "<", { overrideAlias });
603
648
  }
604
649
  exports.Less = Less;
605
- function GreaterEq(col, value) {
606
- return new simpleClause(col, value, ">=");
650
+ function GreaterEq(col, value, overrideAlias) {
651
+ return new simpleClause(col, value, ">=", { overrideAlias });
607
652
  }
608
653
  exports.GreaterEq = GreaterEq;
609
- function LessEq(col, value) {
610
- return new simpleClause(col, value, "<=");
654
+ function LessEq(col, value, overrideAlias) {
655
+ return new simpleClause(col, value, "<=", { overrideAlias });
611
656
  }
612
657
  exports.LessEq = LessEq;
613
658
  function And(...args) {
@@ -647,44 +692,44 @@ function In(...args) {
647
692
  return new inClause(args[0], args.slice(1));
648
693
  }
649
694
  exports.In = In;
650
- function UuidIn(col, values) {
651
- return new inClause(col, values, "uuid");
695
+ function UuidIn(col, values, overrideAlias) {
696
+ return new inClause(col, values, "uuid", overrideAlias);
652
697
  }
653
698
  exports.UuidIn = UuidIn;
654
- function IntegerIn(col, values) {
655
- return new inClause(col, values, "integer");
699
+ function IntegerIn(col, values, overrideAlias) {
700
+ return new inClause(col, values, "integer", overrideAlias);
656
701
  }
657
702
  exports.IntegerIn = IntegerIn;
658
- function TextIn(col, values) {
659
- return new inClause(col, values, "text");
703
+ function TextIn(col, values, overrideAlias) {
704
+ return new inClause(col, values, "text", overrideAlias);
660
705
  }
661
706
  exports.TextIn = TextIn;
662
707
  /*
663
708
  * if not uuid or text, pass the db type that can be used to cast this query
664
709
  * if we end up with a large list of ids
665
710
  */
666
- function DBTypeIn(col, values, typ) {
667
- return new inClause(col, values, typ);
711
+ function DBTypeIn(col, values, typ, overrideAlias) {
712
+ return new inClause(col, values, typ, overrideAlias);
668
713
  }
669
714
  exports.DBTypeIn = DBTypeIn;
670
- function UuidNotIn(col, values) {
671
- return new notInClause(col, values, "uuid");
715
+ function UuidNotIn(col, values, overrideAlias) {
716
+ return new notInClause(col, values, "uuid", overrideAlias);
672
717
  }
673
718
  exports.UuidNotIn = UuidNotIn;
674
- function IntegerNotIn(col, values) {
675
- return new notInClause(col, values, "integer");
719
+ function IntegerNotIn(col, values, overrideAlias) {
720
+ return new notInClause(col, values, "integer", overrideAlias);
676
721
  }
677
722
  exports.IntegerNotIn = IntegerNotIn;
678
- function TextNotIn(col, values) {
679
- return new notInClause(col, values, "text");
723
+ function TextNotIn(col, values, overrideAlias) {
724
+ return new notInClause(col, values, "text", overrideAlias);
680
725
  }
681
726
  exports.TextNotIn = TextNotIn;
682
727
  /*
683
728
  * if not uuid or text, pass the db type that can be used to cast this query
684
729
  * if we end up with a large list of ids
685
730
  */
686
- function DBTypeNotIn(col, values, typ) {
687
- return new notInClause(col, values, typ);
731
+ function DBTypeNotIn(col, values, typ, overrideAlias) {
732
+ return new notInClause(col, values, typ, overrideAlias);
688
733
  }
689
734
  exports.DBTypeNotIn = DBTypeNotIn;
690
735
  // if string defaults to english
@@ -693,46 +738,55 @@ exports.DBTypeNotIn = DBTypeNotIn;
693
738
  // plainto_tsquery
694
739
  // phraseto_tsquery;
695
740
  // websearch_to_tsquery
696
- function TsQuery(col, val) {
697
- return new tsQueryClause(col, val);
741
+ function TsQuery(col, val, overrideAlias) {
742
+ return new tsQueryClause(col, val, { overrideAlias });
698
743
  }
699
744
  exports.TsQuery = TsQuery;
700
- function PlainToTsQuery(col, val) {
701
- return new plainToTsQueryClause(col, val);
745
+ function PlainToTsQuery(col, val, overrideAlias) {
746
+ return new plainToTsQueryClause(col, val, { overrideAlias });
702
747
  }
703
748
  exports.PlainToTsQuery = PlainToTsQuery;
704
- function PhraseToTsQuery(col, val) {
705
- return new phraseToTsQueryClause(col, val);
749
+ function PhraseToTsQuery(col, val, overrideAlias) {
750
+ return new phraseToTsQueryClause(col, val, { overrideAlias });
706
751
  }
707
752
  exports.PhraseToTsQuery = PhraseToTsQuery;
708
- function WebsearchToTsQuery(col, val) {
709
- return new websearchTosQueryClause(col, val);
753
+ function WebsearchToTsQuery(col, val, overrideAlias) {
754
+ return new websearchTosQueryClause(col, val, { overrideAlias });
710
755
  }
711
756
  exports.WebsearchToTsQuery = WebsearchToTsQuery;
712
757
  // TsVectorColTsQuery is used when the column is not a tsvector field e.g.
713
758
  // when there's an index just on the field and is not a combination of multiple fields
714
- function TsVectorColTsQuery(col, val) {
715
- return new tsQueryClause(col, val, true);
759
+ function TsVectorColTsQuery(col, val, overrideAlias) {
760
+ return new tsQueryClause(col, val, { tsVectorCol: true, overrideAlias });
716
761
  }
717
762
  exports.TsVectorColTsQuery = TsVectorColTsQuery;
718
763
  // TsVectorPlainToTsQuery is used when the column is not a tsvector field e.g.
719
764
  // when there's an index just on the field and is not a combination of multiple fields
720
765
  // TODO do these 4 need TsQuery because would be nice to have language?
721
766
  // it seems to default to the config of the column
722
- function TsVectorPlainToTsQuery(col, val) {
723
- return new plainToTsQueryClause(col, val, true);
767
+ function TsVectorPlainToTsQuery(col, val, overrideAlias) {
768
+ return new plainToTsQueryClause(col, val, {
769
+ tsVectorCol: true,
770
+ overrideAlias,
771
+ });
724
772
  }
725
773
  exports.TsVectorPlainToTsQuery = TsVectorPlainToTsQuery;
726
774
  // TsVectorPhraseToTsQuery is used when the column is not a tsvector field e.g.
727
775
  // when there's an index just on the field and is not a combination of multiple fields
728
- function TsVectorPhraseToTsQuery(col, val) {
729
- return new phraseToTsQueryClause(col, val, true);
776
+ function TsVectorPhraseToTsQuery(col, val, overrideAlias) {
777
+ return new phraseToTsQueryClause(col, val, {
778
+ tsVectorCol: true,
779
+ overrideAlias,
780
+ });
730
781
  }
731
782
  exports.TsVectorPhraseToTsQuery = TsVectorPhraseToTsQuery;
732
783
  // TsVectorWebsearchToTsQuery is used when the column is not a tsvector field e.g.
733
784
  // when there's an index just on the field and is not a combination of multiple fields
734
- function TsVectorWebsearchToTsQuery(col, val) {
735
- return new websearchTosQueryClause(col, val, true);
785
+ function TsVectorWebsearchToTsQuery(col, val, overrideAlias) {
786
+ return new websearchTosQueryClause(col, val, {
787
+ tsVectorCol: true,
788
+ overrideAlias,
789
+ });
736
790
  }
737
791
  exports.TsVectorWebsearchToTsQuery = TsVectorWebsearchToTsQuery;
738
792
  // TODO would be nice to support this with building blocks but not supporting for now
@@ -759,28 +813,29 @@ exports.sensitiveValue = sensitiveValue;
759
813
  // https://www.postgresql.org/docs/12/functions-json.html#FUNCTIONS-JSON-OP-TABLE
760
814
  // see test in db_clause.test.ts
761
815
  // unclear best time to use this...
762
- function JSONObjectFieldKeyASJSON(col, field) {
816
+ function JSONObjectFieldKeyASJSON(col, field, overrideAlias) {
763
817
  // type as keyof T to make it easier to use in other queries
764
- return `${col}->'${field}'`;
818
+ return `${renderCol(col, overrideAlias)}->'${field}'`;
765
819
  }
766
820
  exports.JSONObjectFieldKeyASJSON = JSONObjectFieldKeyASJSON;
767
- function JSONObjectFieldKeyAsText(col, field) {
821
+ function JSONObjectFieldKeyAsText(col, field, overrideAlias) {
768
822
  // type as keyof T to make it easier to use in other queries
769
- return `${col}->>'${field}'`;
823
+ return `${renderCol(col, overrideAlias)}->>'${field}'`;
770
824
  }
771
825
  exports.JSONObjectFieldKeyAsText = JSONObjectFieldKeyAsText;
772
826
  class jSONPathValuePredicateClause {
773
- constructor(col, path, value, pred) {
827
+ constructor(col, path, value, pred, overrideAlias) {
774
828
  this.col = col;
775
829
  this.path = path;
776
830
  this.value = value;
777
831
  this.pred = pred;
832
+ this.overrideAlias = overrideAlias;
778
833
  }
779
834
  clause(idx, alias) {
780
835
  if (db_1.default.getDialect() !== db_1.Dialect.Postgres) {
781
836
  throw new Error(`not supported`);
782
837
  }
783
- return `${renderCol(this.col, alias)} @@ $${idx}`;
838
+ return `${renderCol(this.col, this.overrideAlias, alias)} @@ $${idx}`;
784
839
  }
785
840
  columns() {
786
841
  return [this.col];
@@ -801,22 +856,29 @@ class jSONPathValuePredicateClause {
801
856
  return [this.wrap(this.value)];
802
857
  }
803
858
  instanceKey() {
859
+ if (this.overrideAlias) {
860
+ return `${this.overrideAlias}.${this.col}${this.path}${rawValue(this.value)}${this.pred}`;
861
+ }
804
862
  return `${this.col}${this.path}${rawValue(this.value)}${this.pred}`;
805
863
  }
806
864
  }
807
865
  // https://www.postgresql.org/docs/12/functions-json.html#FUNCTIONS-JSON-OP-TABLE
808
- function JSONPathValuePredicate(dbCol, path, val, pred) {
809
- return new jSONPathValuePredicateClause(dbCol, path, val, pred);
866
+ function JSONPathValuePredicate(dbCol, path, val, pred, overrideAlias) {
867
+ return new jSONPathValuePredicateClause(dbCol, path, val, pred, overrideAlias);
810
868
  }
811
869
  exports.JSONPathValuePredicate = JSONPathValuePredicate;
812
- function JSONKeyExists(dbCol, val) {
813
- return new simpleClause(dbCol, val, "?", new isNullClause(dbCol));
870
+ function JSONKeyExists(dbCol, val, overrideAlias) {
871
+ return new simpleClause(dbCol, val, "?", {
872
+ // TODO ola: does isNullClause make sense here???
873
+ handleNull: new isNullClause(dbCol),
874
+ overrideAlias,
875
+ });
814
876
  }
815
877
  exports.JSONKeyExists = JSONKeyExists;
816
- function JSONBKeyInList(dbCol, jsonCol, val) {
878
+ function JSONBKeyInList(dbCol, jsonCol, val, overrideAlias) {
817
879
  const opts = {
818
880
  fields: ["1"],
819
- tableName: `jsonb_array_elements(${dbCol}) AS json_element`,
881
+ tableName: `jsonb_array_elements(${renderCol(dbCol, overrideAlias)}) AS json_element`,
820
882
  // @ts-ignore
821
883
  clause: And(JSONKeyExists("json_element", jsonCol),
822
884
  // @ts-ignore
@@ -825,10 +887,10 @@ function JSONBKeyInList(dbCol, jsonCol, val) {
825
887
  return new existsQueryClause(opts);
826
888
  }
827
889
  exports.JSONBKeyInList = JSONBKeyInList;
828
- function JSONKeyInList(dbCol, jsonCol, val) {
890
+ function JSONKeyInList(dbCol, jsonCol, val, overrideAlias) {
829
891
  const opts = {
830
892
  fields: ["1"],
831
- tableName: `json_array_elements(${dbCol}) AS json_element`,
893
+ tableName: `json_array_elements(${renderCol(dbCol, overrideAlias)}) AS json_element`,
832
894
  // @ts-ignore
833
895
  clause: And(JSONKeyExists("json_element", jsonCol),
834
896
  // @ts-ignore
@@ -840,22 +902,25 @@ exports.JSONKeyInList = JSONKeyInList;
840
902
  // TODO need a better name for this lol
841
903
  // this assumes we're doing the same direction twice which isn't necessarily accurate in the future...
842
904
  class paginationMultipleColumnsSubQueryClause {
843
- constructor(col, op, tableName, uniqueCol, val) {
905
+ constructor(col, op, tableName, uniqueCol, val, overrideAlias) {
844
906
  this.col = col;
845
907
  this.op = op;
846
908
  this.tableName = tableName;
847
909
  this.uniqueCol = uniqueCol;
848
910
  this.val = val;
911
+ this.overrideAlias = overrideAlias;
849
912
  }
850
913
  buildSimpleQuery(clause, idx, alias) {
851
- return `SELECT ${renderCol(this.col, alias)} FROM ${this.tableName} WHERE ${clause.clause(idx, alias)}`;
914
+ return `SELECT ${renderCol(this.col, this.overrideAlias, alias)} FROM ${this.tableName} WHERE ${clause.clause(idx, alias)}`;
852
915
  }
853
916
  clause(idx, alias) {
854
- const eq1 = this.buildSimpleQuery(Eq(this.uniqueCol, this.val), idx, alias);
855
- const eq2 = this.buildSimpleQuery(Eq(this.uniqueCol, this.val), idx + 1, alias);
856
- const op = new simpleClause(this.uniqueCol, this.val, this.op).clause(idx + 2, alias);
917
+ const eq1 = this.buildSimpleQuery(Eq(this.uniqueCol, this.val, this.overrideAlias), idx, alias);
918
+ const eq2 = this.buildSimpleQuery(Eq(this.uniqueCol, this.val, this.overrideAlias), idx + 1, alias);
919
+ const op = new simpleClause(this.uniqueCol, this.val, this.op, {
920
+ overrideAlias: this.overrideAlias,
921
+ }).clause(idx + 2, alias);
857
922
  // nest in () to make sure it's scoped correctly
858
- return `(${renderCol(this.col, alias)} ${this.op} (${eq1}) OR (${renderCol(this.col, alias)} = (${eq2}) AND ${op}))`;
923
+ return `(${renderCol(this.col, this.overrideAlias, alias)} ${this.op} (${eq1}) OR (${renderCol(this.col, this.overrideAlias, alias)} = (${eq2}) AND ${op}))`;
859
924
  }
860
925
  columns() {
861
926
  return [this.col];
@@ -868,32 +933,56 @@ class paginationMultipleColumnsSubQueryClause {
868
933
  return [log, log, log];
869
934
  }
870
935
  instanceKey() {
936
+ if (this.overrideAlias) {
937
+ return `${this.overrideAlias}.${this.col}-${this.op}-${this.tableName}-${this.overrideAlias}.${this.uniqueCol}-${this.val}`;
938
+ }
871
939
  return `${this.col}-${this.op}-${this.tableName}-${this.uniqueCol}-${this.val}`;
872
940
  }
873
941
  }
874
- function PaginationMultipleColsSubQuery(col, op, tableName, uniqueCol, val) {
875
- return new paginationMultipleColumnsSubQueryClause(col, op, tableName, uniqueCol, val);
942
+ function PaginationMultipleColsSubQuery(col, op, tableName, uniqueCol, val, overrideAlias) {
943
+ return new paginationMultipleColumnsSubQueryClause(col, op, tableName, uniqueCol, val, overrideAlias);
876
944
  }
877
945
  exports.PaginationMultipleColsSubQuery = PaginationMultipleColsSubQuery;
946
+ function PaginationMultipleColsQuery(sortCol, cursorCol, less, // if true, <, if false, >
947
+ sortValue, cursorValue, overrideAlias) {
948
+ const clauseFn = less ? Less : Greater;
949
+ return And(Or(clauseFn(sortCol, sortValue, overrideAlias), And(Eq(sortCol, sortValue, overrideAlias), clauseFn(cursorCol, cursorValue, overrideAlias))));
950
+ }
951
+ exports.PaginationMultipleColsQuery = PaginationMultipleColsQuery;
878
952
  // These 5 are used on the RHS of an expression
879
- function Add(col, value) {
880
- return new simpleClause(col, value, "+", new isNullClause(col));
953
+ function Add(col, value, overrideAlias) {
954
+ return new simpleClause(col, value, "+", {
955
+ handleNull: new isNullClause(col),
956
+ overrideAlias,
957
+ });
881
958
  }
882
959
  exports.Add = Add;
883
- function Subtract(col, value) {
884
- return new simpleClause(col, value, "-", new isNullClause(col));
960
+ function Subtract(col, value, overrideAlias) {
961
+ return new simpleClause(col, value, "-", {
962
+ handleNull: new isNullClause(col),
963
+ overrideAlias,
964
+ });
885
965
  }
886
966
  exports.Subtract = Subtract;
887
- function Multiply(col, value) {
888
- return new simpleClause(col, value, "*", new isNullClause(col));
967
+ function Multiply(col, value, overrideAlias) {
968
+ return new simpleClause(col, value, "*", {
969
+ handleNull: new isNullClause(col),
970
+ overrideAlias,
971
+ });
889
972
  }
890
973
  exports.Multiply = Multiply;
891
- function Divide(col, value) {
892
- return new simpleClause(col, value, "/", new isNullClause(col));
974
+ function Divide(col, value, overrideAlias) {
975
+ return new simpleClause(col, value, "/", {
976
+ handleNull: new isNullClause(col),
977
+ overrideAlias,
978
+ });
893
979
  }
894
980
  exports.Divide = Divide;
895
- function Modulo(col, value) {
896
- return new simpleClause(col, value, "%", new isNullClause(col));
981
+ function Modulo(col, value, overrideAlias) {
982
+ return new simpleClause(col, value, "%", {
983
+ handleNull: new isNullClause(col),
984
+ overrideAlias,
985
+ });
897
986
  }
898
987
  exports.Modulo = Modulo;
899
988
  function getCombinedClause(options, cls, checkIntersection = false) {