@snowtop/ent 0.1.0-alpha9 → 0.1.0-alpha91

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.
Files changed (135) hide show
  1. package/action/action.d.ts +36 -31
  2. package/action/action.js +2 -6
  3. package/action/executor.d.ts +3 -3
  4. package/action/executor.js +2 -2
  5. package/action/experimental_action.d.ts +29 -22
  6. package/action/experimental_action.js +29 -6
  7. package/action/orchestrator.d.ts +38 -16
  8. package/action/orchestrator.js +223 -61
  9. package/action/privacy.d.ts +2 -2
  10. package/core/base.d.ts +45 -24
  11. package/core/base.js +7 -1
  12. package/core/clause.d.ts +83 -7
  13. package/core/clause.js +334 -63
  14. package/core/config.d.ts +8 -0
  15. package/core/config.js +5 -1
  16. package/core/context.d.ts +5 -3
  17. package/core/context.js +20 -2
  18. package/core/convert.d.ts +1 -1
  19. package/core/db.d.ts +2 -2
  20. package/core/db.js +6 -2
  21. package/core/ent.d.ts +79 -24
  22. package/core/ent.js +527 -176
  23. package/core/loaders/assoc_count_loader.d.ts +3 -2
  24. package/core/loaders/assoc_count_loader.js +14 -2
  25. package/core/loaders/assoc_edge_loader.d.ts +2 -2
  26. package/core/loaders/assoc_edge_loader.js +5 -1
  27. package/core/loaders/index.d.ts +1 -1
  28. package/core/loaders/index.js +1 -3
  29. package/core/loaders/index_loader.d.ts +2 -2
  30. package/core/loaders/loader.js +5 -5
  31. package/core/loaders/object_loader.d.ts +6 -5
  32. package/core/loaders/object_loader.js +67 -59
  33. package/core/loaders/query_loader.d.ts +6 -12
  34. package/core/loaders/query_loader.js +52 -11
  35. package/core/loaders/raw_count_loader.d.ts +2 -2
  36. package/core/loaders/raw_count_loader.js +5 -1
  37. package/core/logger.d.ts +1 -1
  38. package/core/logger.js +1 -0
  39. package/core/privacy.d.ts +26 -25
  40. package/core/privacy.js +21 -25
  41. package/core/query/assoc_query.d.ts +7 -6
  42. package/core/query/assoc_query.js +9 -1
  43. package/core/query/custom_clause_query.d.ts +26 -0
  44. package/core/query/custom_clause_query.js +78 -0
  45. package/core/query/custom_query.d.ts +20 -5
  46. package/core/query/custom_query.js +87 -12
  47. package/core/query/index.d.ts +1 -0
  48. package/core/query/index.js +3 -1
  49. package/core/query/query.d.ts +8 -4
  50. package/core/query/query.js +101 -53
  51. package/core/query/shared_assoc_test.d.ts +2 -1
  52. package/core/query/shared_assoc_test.js +34 -43
  53. package/core/query/shared_test.d.ts +8 -1
  54. package/core/query/shared_test.js +470 -236
  55. package/core/viewer.d.ts +3 -3
  56. package/core/viewer.js +1 -1
  57. package/graphql/graphql.js +16 -6
  58. package/graphql/query/edge_connection.d.ts +9 -9
  59. package/graphql/query/page_info.d.ts +1 -1
  60. package/graphql/query/shared_edge_connection.js +1 -15
  61. package/imports/index.js +5 -1
  62. package/index.d.ts +11 -5
  63. package/index.js +20 -7
  64. package/package.json +1 -1
  65. package/parse_schema/parse.d.ts +12 -3
  66. package/parse_schema/parse.js +70 -11
  67. package/schema/base_schema.js +3 -0
  68. package/schema/field.d.ts +44 -8
  69. package/schema/field.js +136 -10
  70. package/schema/index.d.ts +2 -2
  71. package/schema/index.js +5 -1
  72. package/schema/json_field.d.ts +13 -1
  73. package/schema/json_field.js +28 -1
  74. package/schema/schema.d.ts +66 -11
  75. package/schema/schema.js +18 -4
  76. package/schema/struct_field.d.ts +11 -1
  77. package/schema/struct_field.js +44 -5
  78. package/scripts/custom_compiler.js +10 -6
  79. package/scripts/custom_graphql.js +13 -4
  80. package/scripts/{transform_schema.d.ts → migrate_v0.1.d.ts} +0 -0
  81. package/scripts/migrate_v0.1.js +36 -0
  82. package/scripts/read_schema.js +20 -5
  83. package/testutils/builder.d.ts +31 -21
  84. package/testutils/builder.js +83 -29
  85. package/testutils/db/fixture.d.ts +10 -0
  86. package/testutils/db/fixture.js +26 -0
  87. package/testutils/db/{test_db.d.ts → temp_db.d.ts} +20 -7
  88. package/testutils/db/{test_db.js → temp_db.js} +102 -36
  89. package/testutils/db/value.d.ts +6 -0
  90. package/testutils/db/value.js +251 -0
  91. package/testutils/db_mock.js +3 -1
  92. package/testutils/db_time_zone.d.ts +4 -0
  93. package/testutils/db_time_zone.js +41 -0
  94. package/testutils/ent-graphql-tests/index.js +8 -1
  95. package/testutils/fake_data/const.d.ts +2 -1
  96. package/testutils/fake_data/const.js +3 -0
  97. package/testutils/fake_data/fake_contact.d.ts +7 -4
  98. package/testutils/fake_data/fake_contact.js +14 -6
  99. package/testutils/fake_data/fake_event.d.ts +5 -3
  100. package/testutils/fake_data/fake_event.js +8 -5
  101. package/testutils/fake_data/fake_tag.d.ts +35 -0
  102. package/testutils/fake_data/fake_tag.js +88 -0
  103. package/testutils/fake_data/fake_user.d.ts +6 -4
  104. package/testutils/fake_data/fake_user.js +16 -13
  105. package/testutils/fake_data/index.js +5 -1
  106. package/testutils/fake_data/internal.d.ts +2 -0
  107. package/testutils/fake_data/internal.js +7 -1
  108. package/testutils/fake_data/tag_query.d.ts +13 -0
  109. package/testutils/fake_data/tag_query.js +43 -0
  110. package/testutils/fake_data/test_helpers.d.ts +11 -4
  111. package/testutils/fake_data/test_helpers.js +28 -12
  112. package/testutils/fake_data/user_query.d.ts +13 -6
  113. package/testutils/fake_data/user_query.js +54 -22
  114. package/testutils/fake_log.d.ts +3 -3
  115. package/testutils/parse_sql.d.ts +6 -0
  116. package/testutils/parse_sql.js +16 -2
  117. package/testutils/test_edge_global_schema.d.ts +15 -0
  118. package/testutils/test_edge_global_schema.js +62 -0
  119. package/testutils/write.d.ts +2 -2
  120. package/testutils/write.js +33 -7
  121. package/tsc/ast.d.ts +44 -0
  122. package/tsc/ast.js +271 -0
  123. package/tsc/compilerOptions.d.ts +6 -0
  124. package/tsc/compilerOptions.js +45 -2
  125. package/tsc/move_generated.d.ts +1 -0
  126. package/tsc/move_generated.js +164 -0
  127. package/tsc/transform.d.ts +21 -0
  128. package/tsc/transform.js +171 -0
  129. package/tsc/transform_action.d.ts +22 -0
  130. package/tsc/transform_action.js +183 -0
  131. package/tsc/transform_ent.d.ts +17 -0
  132. package/tsc/transform_ent.js +59 -0
  133. package/tsc/transform_schema.d.ts +27 -0
  134. package/tsc/transform_schema.js +383 -0
  135. package/scripts/transform_schema.js +0 -445
package/core/clause.d.ts CHANGED
@@ -1,8 +1,10 @@
1
1
  export interface Clause {
2
2
  clause(idx: number): string;
3
+ columns(): string[];
3
4
  values(): any[];
4
5
  instanceKey(): string;
5
6
  logValues(): any[];
7
+ compositeOp?: string;
6
8
  }
7
9
  export interface SensitiveValue {
8
10
  value(): any;
@@ -12,10 +14,23 @@ declare class simpleClause implements Clause {
12
14
  protected col: string;
13
15
  private value;
14
16
  private op;
15
- private handleSqliteNull?;
16
- constructor(col: string, value: any, op: string, handleSqliteNull?: Clause | undefined);
17
+ private handleNull?;
18
+ constructor(col: string, value: any, op: string, handleNull?: Clause | undefined);
17
19
  clause(idx: number): string;
18
- private sqliteNull;
20
+ private nullClause;
21
+ columns(): string[];
22
+ values(): any[];
23
+ logValues(): any[];
24
+ instanceKey(): string;
25
+ }
26
+ export declare class inClause implements Clause {
27
+ private col;
28
+ private value;
29
+ private type;
30
+ static getPostgresInClauseValuesThreshold(): number;
31
+ constructor(col: string, value: any[], type?: string);
32
+ clause(idx: number): string;
33
+ columns(): string[];
19
34
  values(): any[];
20
35
  logValues(): any[];
21
36
  instanceKey(): string;
@@ -23,18 +38,58 @@ declare class simpleClause implements Clause {
23
38
  declare class compositeClause implements Clause {
24
39
  private clauses;
25
40
  private sep;
41
+ compositeOp: string;
26
42
  constructor(clauses: Clause[], sep: string);
27
43
  clause(idx: number): string;
44
+ columns(): string[];
28
45
  values(): any[];
29
46
  logValues(): any[];
30
47
  instanceKey(): string;
31
48
  }
49
+ /**
50
+ * creates a clause to determine if the given value is contained in the array stored in the column in the db
51
+ * only works with postgres gin indexes
52
+ * https://www.postgresql.org/docs/current/indexes-types.html#INDEXES-TYPES-GIN
53
+ */
54
+ export declare function PostgresArrayContainsValue(col: string, value: any): Clause;
55
+ /**
56
+ * creates a clause to determine if every item in the list is stored in the array stored in the column in the db
57
+ * only works with postgres gin indexes
58
+ * https://www.postgresql.org/docs/current/indexes-types.html#INDEXES-TYPES-GIN
59
+ */
60
+ export declare function PostgresArrayContains(col: string, value: any[]): Clause;
61
+ /**
62
+ * creates a clause to determine if the given value is NOT contained in the array stored in the column in the db
63
+ * only works with postgres gin indexes
64
+ * https://www.postgresql.org/docs/current/indexes-types.html#INDEXES-TYPES-GIN
65
+ */
66
+ export declare function PostgresArrayNotContainsValue(col: string, value: any): Clause;
67
+ /**
68
+ * creates a clause to determine if every item in the list is NOT stored in the array stored in the column in the db
69
+ * only works with postgres gin indexes
70
+ * https://www.postgresql.org/docs/current/indexes-types.html#INDEXES-TYPES-GIN
71
+ */
72
+ export declare function PostgresArrayNotContains(col: string, value: any[]): Clause;
73
+ /**
74
+ * creates a clause to determine if the arrays overlap, that is, do they have any elements in common
75
+ * only works with postgres gin indexes
76
+ * https://www.postgresql.org/docs/current/indexes-types.html#INDEXES-TYPES-GIN
77
+ */
78
+ export declare function PostgresArrayOverlaps(col: string, value: any[]): Clause;
79
+ /**
80
+ * creates a clause to determine if the arrays do not overlap, that is, do they have any elements in common
81
+ * only works with postgres gin indexes
82
+ * https://www.postgresql.org/docs/current/indexes-types.html#INDEXES-TYPES-GIN
83
+ */
84
+ export declare function PostgresArrayNotOverlaps(col: string, value: any[]): Clause;
85
+ /**
86
+ * @deprecated use PostgresArrayContainsValue
87
+ */
32
88
  export declare function ArrayEq(col: string, value: any): Clause;
89
+ /**
90
+ * @deprecated use PostgresNotArrayContains
91
+ */
33
92
  export declare function ArrayNotEq(col: string, value: any): Clause;
34
- export declare function ArrayGreater(col: string, value: any): Clause;
35
- export declare function ArrayLess(col: string, value: any): Clause;
36
- export declare function ArrayGreaterEq(col: string, value: any): Clause;
37
- export declare function ArrayLessEq(col: string, value: any): Clause;
38
93
  export declare function Eq(col: string, value: any): Clause;
39
94
  export declare function NotEq(col: string, value: any): Clause;
40
95
  export declare function Greater(col: string, value: any): simpleClause;
@@ -44,7 +99,9 @@ export declare function LessEq(col: string, value: any): simpleClause;
44
99
  export declare function And(...args: Clause[]): compositeClause;
45
100
  export declare function AndOptional(...args: (Clause | undefined)[]): Clause;
46
101
  export declare function Or(...args: Clause[]): compositeClause;
102
+ export declare function OrOptional(...args: (Clause | undefined)[]): Clause;
47
103
  export declare function In(col: string, ...values: any): Clause;
104
+ export declare function In(col: string, values: any[], type?: string): Clause;
48
105
  interface TsQuery {
49
106
  language: "english" | "french" | "german" | "simple";
50
107
  value: string;
@@ -58,4 +115,23 @@ export declare function TsVectorPlainToTsQuery(col: string, val: string | TsQuer
58
115
  export declare function TsVectorPhraseToTsQuery(col: string, val: string | TsQuery): Clause;
59
116
  export declare function TsVectorWebsearchToTsQuery(col: string, val: string | TsQuery): Clause;
60
117
  export declare function sensitiveValue(val: any): SensitiveValue;
118
+ export declare function JSONObjectFieldKeyASJSON(col: string, field: string): string;
119
+ export declare function JSONObjectFieldKeyAsText(col: string, field: string): string;
120
+ declare type predicate = "==" | ">" | "<" | "!=" | ">=" | "<=";
121
+ export declare function JSONPathValuePredicate(dbCol: string, path: string, val: any, pred: predicate): Clause;
122
+ declare class paginationMultipleColumnsSubQueryClause implements Clause {
123
+ private col;
124
+ private op;
125
+ private tableName;
126
+ private uniqueCol;
127
+ private val;
128
+ constructor(col: string, op: string, tableName: string, uniqueCol: string, val: any);
129
+ private buildSimpleQuery;
130
+ clause(idx: number): string;
131
+ columns(): string[];
132
+ values(): any[];
133
+ logValues(): any[];
134
+ instanceKey(): string;
135
+ }
136
+ export declare function PaginationMultipleColsSubQuery(col: string, op: string, tableName: string, uniqueCol: string, val: any): paginationMultipleColumnsSubQueryClause;
61
137
  export {};
package/core/clause.js CHANGED
@@ -1,7 +1,11 @@
1
1
  "use strict";
2
2
  var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
3
  if (k2 === undefined) k2 = k;
4
- Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
5
9
  }) : (function(o, m, k, k2) {
6
10
  if (k2 === undefined) k2 = k;
7
11
  o[k2] = m[k];
@@ -19,7 +23,7 @@ var __importStar = (this && this.__importStar) || function (mod) {
19
23
  return result;
20
24
  };
21
25
  Object.defineProperty(exports, "__esModule", { value: true });
22
- exports.sensitiveValue = exports.TsVectorWebsearchToTsQuery = exports.TsVectorPhraseToTsQuery = exports.TsVectorPlainToTsQuery = exports.TsVectorColTsQuery = exports.WebsearchToTsQuery = exports.PhraseToTsQuery = exports.PlainToTsQuery = exports.TsQuery = exports.In = exports.Or = exports.AndOptional = exports.And = exports.LessEq = exports.GreaterEq = exports.Less = exports.Greater = exports.NotEq = exports.Eq = exports.ArrayLessEq = exports.ArrayGreaterEq = exports.ArrayLess = exports.ArrayGreater = exports.ArrayNotEq = exports.ArrayEq = void 0;
26
+ exports.PaginationMultipleColsSubQuery = 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.In = exports.OrOptional = exports.Or = exports.AndOptional = exports.And = exports.LessEq = exports.GreaterEq = exports.Less = exports.Greater = exports.NotEq = exports.Eq = exports.ArrayNotEq = exports.ArrayEq = exports.PostgresArrayNotOverlaps = exports.PostgresArrayOverlaps = exports.PostgresArrayNotContains = exports.PostgresArrayNotContainsValue = exports.PostgresArrayContains = exports.PostgresArrayContainsValue = exports.inClause = void 0;
23
27
  const db_1 = __importStar(require("./db"));
24
28
  function isSensitive(val) {
25
29
  return (val !== null &&
@@ -33,35 +37,35 @@ function rawValue(val) {
33
37
  return val;
34
38
  }
35
39
  class simpleClause {
36
- constructor(col, value, op, handleSqliteNull) {
40
+ constructor(col, value, op, handleNull) {
37
41
  this.col = col;
38
42
  this.value = value;
39
43
  this.op = op;
40
- this.handleSqliteNull = handleSqliteNull;
44
+ this.handleNull = handleNull;
41
45
  }
42
46
  clause(idx) {
43
- const sqliteClause = this.sqliteNull();
44
- if (sqliteClause) {
45
- return sqliteClause.clause(idx);
47
+ const nullClause = this.nullClause();
48
+ if (nullClause) {
49
+ return nullClause.clause(idx);
46
50
  }
47
51
  if (db_1.default.getDialect() === db_1.Dialect.Postgres) {
48
52
  return `${this.col} ${this.op} $${idx}`;
49
53
  }
50
54
  return `${this.col} ${this.op} ?`;
51
55
  }
52
- sqliteNull() {
53
- if (!this.handleSqliteNull || this.value !== null) {
54
- return;
55
- }
56
- if (db_1.default.getDialect() !== db_1.Dialect.SQLite) {
56
+ nullClause() {
57
+ if (!this.handleNull || this.value !== null) {
57
58
  return;
58
59
  }
59
- return this.handleSqliteNull;
60
+ return this.handleNull;
61
+ }
62
+ columns() {
63
+ return [this.col];
60
64
  }
61
65
  values() {
62
- const sqliteClause = this.sqliteNull();
63
- if (sqliteClause) {
64
- return sqliteClause.values();
66
+ const nullClause = this.nullClause();
67
+ if (nullClause) {
68
+ return nullClause.values();
65
69
  }
66
70
  if (isSensitive(this.value)) {
67
71
  return [this.value.value()];
@@ -69,9 +73,9 @@ class simpleClause {
69
73
  return [this.value];
70
74
  }
71
75
  logValues() {
72
- const sqliteClause = this.sqliteNull();
73
- if (sqliteClause) {
74
- return sqliteClause.logValues();
76
+ const nullClause = this.nullClause();
77
+ if (nullClause) {
78
+ return nullClause.logValues();
75
79
  }
76
80
  if (isSensitive(this.value)) {
77
81
  return [this.value.logValue()];
@@ -79,9 +83,9 @@ class simpleClause {
79
83
  return [this.value];
80
84
  }
81
85
  instanceKey() {
82
- const sqliteClause = this.sqliteNull();
83
- if (sqliteClause) {
84
- return sqliteClause.instanceKey();
86
+ const nullClause = this.nullClause();
87
+ if (nullClause) {
88
+ return nullClause.instanceKey();
85
89
  }
86
90
  return `${this.col}${this.op}${rawValue(this.value)}`;
87
91
  }
@@ -93,6 +97,9 @@ class isNullClause {
93
97
  clause(idx) {
94
98
  return `${this.col} IS NULL`;
95
99
  }
100
+ columns() {
101
+ return [];
102
+ }
96
103
  values() {
97
104
  return [];
98
105
  }
@@ -110,6 +117,9 @@ class isNotNullClause {
110
117
  clause(idx) {
111
118
  return `${this.col} IS NOT NULL`;
112
119
  }
120
+ columns() {
121
+ return [];
122
+ }
113
123
  values() {
114
124
  return [];
115
125
  }
@@ -132,6 +142,9 @@ class arraySimpleClause {
132
142
  }
133
143
  return `${this.col} ${this.op} ?`;
134
144
  }
145
+ columns() {
146
+ return [this.col];
147
+ }
135
148
  values() {
136
149
  if (isSensitive(this.value)) {
137
150
  return [this.value.value()];
@@ -148,18 +161,105 @@ class arraySimpleClause {
148
161
  return `${this.col}${this.op}${rawValue(this.value)}`;
149
162
  }
150
163
  }
164
+ class postgresArrayOperator {
165
+ constructor(col, value, op, not) {
166
+ this.col = col;
167
+ this.value = value;
168
+ this.op = op;
169
+ this.not = not;
170
+ }
171
+ clause(idx) {
172
+ if (db_1.default.getDialect() === db_1.Dialect.Postgres) {
173
+ if (this.not) {
174
+ return `NOT ${this.col} ${this.op} $${idx}`;
175
+ }
176
+ return `${this.col} ${this.op} $${idx}`;
177
+ }
178
+ throw new Error(`not supported`);
179
+ }
180
+ columns() {
181
+ return [this.col];
182
+ }
183
+ values() {
184
+ if (isSensitive(this.value)) {
185
+ return [`{${this.value.value()}}`];
186
+ }
187
+ return [`{${this.value}}`];
188
+ }
189
+ logValues() {
190
+ if (isSensitive(this.value)) {
191
+ return [`{${this.value.logValue()}}`];
192
+ }
193
+ return [`{${this.value}}`];
194
+ }
195
+ instanceKey() {
196
+ if (this.not) {
197
+ return `NOT:${this.col}${this.op}${rawValue(this.value)}`;
198
+ }
199
+ return `${this.col}${this.op}${rawValue(this.value)}`;
200
+ }
201
+ }
202
+ class postgresArrayOperatorList extends postgresArrayOperator {
203
+ constructor(col, value, op, not) {
204
+ super(col, value, op, not);
205
+ }
206
+ values() {
207
+ return [
208
+ `{${this.value
209
+ .map((v) => {
210
+ if (isSensitive(v)) {
211
+ return v.value();
212
+ }
213
+ return v;
214
+ })
215
+ .join(", ")}}`,
216
+ ];
217
+ }
218
+ logValues() {
219
+ return [
220
+ `{${this.value
221
+ .map((v) => {
222
+ if (isSensitive(v)) {
223
+ return v.logValue();
224
+ }
225
+ return v;
226
+ })
227
+ .join(", ")}}`,
228
+ ];
229
+ }
230
+ }
151
231
  class inClause {
152
- constructor(col, value) {
232
+ constructor(col, value, type = "uuid") {
153
233
  this.col = col;
154
234
  this.value = value;
235
+ this.type = type;
236
+ }
237
+ static getPostgresInClauseValuesThreshold() {
238
+ return 70;
155
239
  }
156
240
  clause(idx) {
157
- const dialect = db_1.default.getDialect();
241
+ // do a simple = when only one item
242
+ if (this.value.length === 1) {
243
+ return new simpleClause(this.col, this.value[0], "=").clause(idx);
244
+ }
245
+ const postgres = db_1.default.getDialect() === db_1.Dialect.Postgres;
246
+ const postgresValuesList = postgres &&
247
+ this.value.length >= inClause.getPostgresInClauseValuesThreshold();
158
248
  let indices;
159
- if (dialect === db_1.Dialect.Postgres) {
249
+ if (postgres) {
160
250
  indices = [];
161
251
  for (let i = 0; i < this.value.length; i++) {
162
- indices.push(`$${idx}`);
252
+ if (postgresValuesList) {
253
+ if (i === 0) {
254
+ indices.push(`($${idx}::${this.type})`);
255
+ }
256
+ else {
257
+ indices.push(`($${idx})`);
258
+ }
259
+ }
260
+ else {
261
+ indices.push(`$${idx}`);
262
+ }
163
263
  idx++;
164
264
  }
165
265
  }
@@ -167,34 +267,31 @@ class inClause {
167
267
  indices = new Array(this.value.length);
168
268
  indices.fill("?", 0);
169
269
  }
170
- const inValue = indices.join(", ");
270
+ let inValue = indices.join(", ");
271
+ // wrap in VALUES list for postgres...
272
+ if (postgresValuesList) {
273
+ inValue = `VALUES${inValue}`;
274
+ }
171
275
  return `${this.col} IN (${inValue})`;
172
276
  // TODO we need to return idx at end to query builder...
173
277
  // or anything that's doing a composite query so next clause knows where to start
174
278
  // or change to a sqlx.Rebind format
175
279
  // here's what sqlx does: https://play.golang.org/p/vPzvYqeAcP0
176
280
  }
281
+ columns() {
282
+ return [this.col];
283
+ }
177
284
  values() {
178
285
  const result = [];
179
- for (const value of this.value) {
180
- if (isSensitive(value)) {
181
- result.push(value.value());
182
- }
183
- else {
184
- result.push(value);
185
- }
286
+ for (let value of this.value) {
287
+ result.push(rawValue(value));
186
288
  }
187
289
  return result;
188
290
  }
189
291
  logValues() {
190
292
  const result = [];
191
- for (const value of this.value) {
192
- if (isSensitive(value)) {
193
- result.push(value.logValue());
194
- }
195
- else {
196
- result.push(value);
197
- }
293
+ for (let value of this.value) {
294
+ result.push(isSensitive(value) ? value.logValue() : value);
198
295
  }
199
296
  return result;
200
297
  }
@@ -202,19 +299,33 @@ class inClause {
202
299
  return `in:${this.col}:${this.values().join(",")}`;
203
300
  }
204
301
  }
302
+ exports.inClause = inClause;
205
303
  class compositeClause {
206
304
  constructor(clauses, sep) {
207
305
  this.clauses = clauses;
208
306
  this.sep = sep;
307
+ this.compositeOp = this.sep;
209
308
  }
210
309
  clause(idx) {
211
310
  let clauses = [];
212
311
  for (const clause of this.clauses) {
213
- clauses.push(clause.clause(idx));
312
+ let cls = clause.clause(idx);
313
+ // if composite clause and a different op, add parens so that we enforce order of precedence
314
+ if (clause.compositeOp && clause.compositeOp !== this.sep) {
315
+ cls = `(${cls})`;
316
+ }
317
+ clauses.push(cls);
214
318
  idx = idx + clause.values().length;
215
319
  }
216
320
  return clauses.join(this.sep);
217
321
  }
322
+ columns() {
323
+ const ret = [];
324
+ for (const cls of this.clauses) {
325
+ ret.push(...cls.columns());
326
+ }
327
+ return ret;
328
+ }
218
329
  values() {
219
330
  let result = [];
220
331
  for (const clause of this.clauses) {
@@ -231,7 +342,14 @@ class compositeClause {
231
342
  }
232
343
  instanceKey() {
233
344
  let keys = [];
234
- this.clauses.forEach((clause) => keys.push(clause.instanceKey()));
345
+ this.clauses.forEach((clause) => {
346
+ if (clause.compositeOp && clause.compositeOp != this.sep) {
347
+ keys.push(`(${clause.instanceKey()})`);
348
+ }
349
+ else {
350
+ keys.push(clause.instanceKey());
351
+ }
352
+ });
235
353
  return keys.join(this.sep);
236
354
  }
237
355
  }
@@ -264,6 +382,9 @@ class tsQueryClause {
264
382
  // FYI this doesn't actually work for sqlite since different
265
383
  return `${this.col} @@ ${this.getFunction()}('${language}', ?)`;
266
384
  }
385
+ columns() {
386
+ return [this.col];
387
+ }
267
388
  values() {
268
389
  const { value } = this.getInfo();
269
390
  return [value];
@@ -298,31 +419,76 @@ class websearchTosQueryClause extends tsQueryClause {
298
419
  return "websearch_to_tsquery";
299
420
  }
300
421
  }
301
- // TODO we need to check sqlite version...
422
+ // postgres array operators
423
+ // https://www.postgresql.org/docs/current/functions-array.html
424
+ /**
425
+ * creates a clause to determine if the given value is contained in the array stored in the column in the db
426
+ * only works with postgres gin indexes
427
+ * https://www.postgresql.org/docs/current/indexes-types.html#INDEXES-TYPES-GIN
428
+ */
429
+ function PostgresArrayContainsValue(col, value) {
430
+ return new postgresArrayOperator(col, value, "@>");
431
+ }
432
+ exports.PostgresArrayContainsValue = PostgresArrayContainsValue;
433
+ /**
434
+ * creates a clause to determine if every item in the list is stored in the array stored in the column in the db
435
+ * only works with postgres gin indexes
436
+ * https://www.postgresql.org/docs/current/indexes-types.html#INDEXES-TYPES-GIN
437
+ */
438
+ function PostgresArrayContains(col, value) {
439
+ return new postgresArrayOperatorList(col, value, "@>");
440
+ }
441
+ exports.PostgresArrayContains = PostgresArrayContains;
442
+ /**
443
+ * creates a clause to determine if the given value is NOT contained in the array stored in the column in the db
444
+ * only works with postgres gin indexes
445
+ * https://www.postgresql.org/docs/current/indexes-types.html#INDEXES-TYPES-GIN
446
+ */
447
+ function PostgresArrayNotContainsValue(col, value) {
448
+ return new postgresArrayOperator(col, value, "@>", true);
449
+ }
450
+ exports.PostgresArrayNotContainsValue = PostgresArrayNotContainsValue;
451
+ /**
452
+ * creates a clause to determine if every item in the list is NOT stored in the array stored in the column in the db
453
+ * only works with postgres gin indexes
454
+ * https://www.postgresql.org/docs/current/indexes-types.html#INDEXES-TYPES-GIN
455
+ */
456
+ function PostgresArrayNotContains(col, value) {
457
+ return new postgresArrayOperatorList(col, value, "@>", true);
458
+ }
459
+ exports.PostgresArrayNotContains = PostgresArrayNotContains;
460
+ /**
461
+ * creates a clause to determine if the arrays overlap, that is, do they have any elements in common
462
+ * only works with postgres gin indexes
463
+ * https://www.postgresql.org/docs/current/indexes-types.html#INDEXES-TYPES-GIN
464
+ */
465
+ function PostgresArrayOverlaps(col, value) {
466
+ return new postgresArrayOperatorList(col, value, "&&");
467
+ }
468
+ exports.PostgresArrayOverlaps = PostgresArrayOverlaps;
469
+ /**
470
+ * creates a clause to determine if the arrays do not overlap, that is, do they have any elements in common
471
+ * only works with postgres gin indexes
472
+ * https://www.postgresql.org/docs/current/indexes-types.html#INDEXES-TYPES-GIN
473
+ */
474
+ function PostgresArrayNotOverlaps(col, value) {
475
+ return new postgresArrayOperatorList(col, value, "&&", true);
476
+ }
477
+ exports.PostgresArrayNotOverlaps = PostgresArrayNotOverlaps;
478
+ /**
479
+ * @deprecated use PostgresArrayContainsValue
480
+ */
302
481
  function ArrayEq(col, value) {
303
482
  return new arraySimpleClause(col, value, "=");
304
483
  }
305
484
  exports.ArrayEq = ArrayEq;
485
+ /**
486
+ * @deprecated use PostgresNotArrayContains
487
+ */
306
488
  function ArrayNotEq(col, value) {
307
489
  return new arraySimpleClause(col, value, "!=");
308
490
  }
309
491
  exports.ArrayNotEq = ArrayNotEq;
310
- function ArrayGreater(col, value) {
311
- return new arraySimpleClause(col, value, ">");
312
- }
313
- exports.ArrayGreater = ArrayGreater;
314
- function ArrayLess(col, value) {
315
- return new arraySimpleClause(col, value, "<");
316
- }
317
- exports.ArrayLess = ArrayLess;
318
- function ArrayGreaterEq(col, value) {
319
- return new arraySimpleClause(col, value, ">=");
320
- }
321
- exports.ArrayGreaterEq = ArrayGreaterEq;
322
- function ArrayLessEq(col, value) {
323
- return new arraySimpleClause(col, value, "<=");
324
- }
325
- exports.ArrayLessEq = ArrayLessEq;
326
492
  function Eq(col, value) {
327
493
  return new simpleClause(col, value, "=", new isNullClause(col));
328
494
  }
@@ -364,9 +530,24 @@ function Or(...args) {
364
530
  return new compositeClause(args, " OR ");
365
531
  }
366
532
  exports.Or = Or;
367
- // TODO this breaks if values.length ===1 and array. todo fix
368
- function In(col, ...values) {
369
- return new inClause(col, values);
533
+ function OrOptional(...args) {
534
+ // @ts-ignore
535
+ let filtered = args.filter((v) => v !== undefined);
536
+ if (filtered.length === 1) {
537
+ return filtered[0];
538
+ }
539
+ return Or(...filtered);
540
+ }
541
+ exports.OrOptional = OrOptional;
542
+ function In(...args) {
543
+ if (args.length < 2) {
544
+ throw new Error(`invalid args passed to In`);
545
+ }
546
+ // 2nd overload
547
+ if (Array.isArray(args[1])) {
548
+ return new inClause(args[0], args[1], args[2]);
549
+ }
550
+ return new inClause(args[0], args.slice(1));
370
551
  }
371
552
  exports.In = In;
372
553
  // if string defaults to english
@@ -437,3 +618,93 @@ function sensitiveValue(val) {
437
618
  };
438
619
  }
439
620
  exports.sensitiveValue = sensitiveValue;
621
+ // These don't return Clauses but return helpful things that can be passed to clauses
622
+ // https://www.postgresql.org/docs/12/functions-json.html#FUNCTIONS-JSON-OP-TABLE
623
+ // see test in db_clause.test.ts
624
+ // unclear best time to use this...
625
+ function JSONObjectFieldKeyASJSON(col, field) {
626
+ return `${col}->'${field}'`;
627
+ }
628
+ exports.JSONObjectFieldKeyASJSON = JSONObjectFieldKeyASJSON;
629
+ function JSONObjectFieldKeyAsText(col, field) {
630
+ return `${col}->>'${field}'`;
631
+ }
632
+ exports.JSONObjectFieldKeyAsText = JSONObjectFieldKeyAsText;
633
+ class jSONPathValuePredicateClause {
634
+ constructor(col, path, value, pred) {
635
+ this.col = col;
636
+ this.path = path;
637
+ this.value = value;
638
+ this.pred = pred;
639
+ }
640
+ clause(idx) {
641
+ if (db_1.default.getDialect() !== db_1.Dialect.Postgres) {
642
+ throw new Error(`not supported`);
643
+ }
644
+ return `${this.col} @@ $${idx}`;
645
+ }
646
+ columns() {
647
+ return [this.col];
648
+ }
649
+ wrap(val) {
650
+ return `${this.path} ${this.pred} ${JSON.stringify(val)}`;
651
+ }
652
+ values() {
653
+ if (isSensitive(this.value)) {
654
+ return [this.wrap(this.value.value())];
655
+ }
656
+ return [this.wrap(this.value)];
657
+ }
658
+ logValues() {
659
+ if (isSensitive(this.value)) {
660
+ return [this.wrap(this.value.logValue())];
661
+ }
662
+ return [this.wrap(this.value)];
663
+ }
664
+ instanceKey() {
665
+ return `${this.col}${this.path}${rawValue(this.value)}${this.pred}`;
666
+ }
667
+ }
668
+ // https://www.postgresql.org/docs/12/functions-json.html#FUNCTIONS-JSON-OP-TABLE
669
+ function JSONPathValuePredicate(dbCol, path, val, pred) {
670
+ return new jSONPathValuePredicateClause(dbCol, path, val, pred);
671
+ }
672
+ exports.JSONPathValuePredicate = JSONPathValuePredicate;
673
+ // TODO need a better name for this lol
674
+ // this assumes we're doing the same direction twice which isn't necessarily accurate in the future...
675
+ class paginationMultipleColumnsSubQueryClause {
676
+ constructor(col, op, tableName, uniqueCol, val) {
677
+ this.col = col;
678
+ this.op = op;
679
+ this.tableName = tableName;
680
+ this.uniqueCol = uniqueCol;
681
+ this.val = val;
682
+ }
683
+ buildSimpleQuery(clause, idx) {
684
+ return `SELECT ${this.col} FROM ${this.tableName} WHERE ${clause.clause(idx)}`;
685
+ }
686
+ clause(idx) {
687
+ const eq1 = this.buildSimpleQuery(Eq(this.uniqueCol, this.val), idx);
688
+ const eq2 = this.buildSimpleQuery(Eq(this.uniqueCol, this.val), idx + 1);
689
+ const op = new simpleClause(this.uniqueCol, this.val, this.op).clause(idx + 2);
690
+ // nest in () to make sure it's scoped correctly
691
+ return `(${this.col} ${this.op} (${eq1}) OR (${this.col} = (${eq2}) AND ${op}))`;
692
+ }
693
+ columns() {
694
+ return [this.col];
695
+ }
696
+ values() {
697
+ return [this.val, this.val, this.val];
698
+ }
699
+ logValues() {
700
+ const log = isSensitive(this.val) ? this.val.logValue() : this.val;
701
+ return [log, log, log];
702
+ }
703
+ instanceKey() {
704
+ return `${this.col}-${this.op}-${this.tableName}-${this.uniqueCol}-${this.val}`;
705
+ }
706
+ }
707
+ function PaginationMultipleColsSubQuery(col, op, tableName, uniqueCol, val) {
708
+ return new paginationMultipleColumnsSubQueryClause(col, op, tableName, uniqueCol, val);
709
+ }
710
+ exports.PaginationMultipleColsSubQuery = PaginationMultipleColsSubQuery;