@snowtop/ent 0.1.0-alpha16 → 0.1.0-alpha160-test1

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 (176) hide show
  1. package/action/action.d.ts +25 -14
  2. package/action/action.js +22 -7
  3. package/action/executor.d.ts +16 -3
  4. package/action/executor.js +89 -28
  5. package/action/experimental_action.d.ts +25 -16
  6. package/action/experimental_action.js +34 -14
  7. package/action/index.d.ts +4 -1
  8. package/action/index.js +7 -1
  9. package/action/operations.d.ts +126 -0
  10. package/action/operations.js +686 -0
  11. package/action/orchestrator.d.ts +43 -12
  12. package/action/orchestrator.js +461 -101
  13. package/action/relative_value.d.ts +47 -0
  14. package/action/relative_value.js +125 -0
  15. package/action/transaction.d.ts +10 -0
  16. package/action/transaction.js +23 -0
  17. package/auth/auth.d.ts +1 -1
  18. package/core/base.d.ts +56 -23
  19. package/core/base.js +7 -1
  20. package/core/clause.d.ts +103 -39
  21. package/core/clause.js +430 -66
  22. package/core/config.d.ts +13 -3
  23. package/core/config.js +10 -1
  24. package/core/const.d.ts +3 -0
  25. package/core/const.js +6 -0
  26. package/core/context.d.ts +6 -3
  27. package/core/context.js +22 -3
  28. package/core/convert.d.ts +1 -1
  29. package/core/date.js +1 -5
  30. package/core/db.d.ts +12 -8
  31. package/core/db.js +21 -9
  32. package/core/ent.d.ts +99 -95
  33. package/core/ent.js +550 -602
  34. package/core/global_schema.d.ts +7 -0
  35. package/core/global_schema.js +51 -0
  36. package/core/loaders/assoc_count_loader.d.ts +5 -2
  37. package/core/loaders/assoc_count_loader.js +19 -3
  38. package/core/loaders/assoc_edge_loader.d.ts +2 -3
  39. package/core/loaders/assoc_edge_loader.js +23 -17
  40. package/core/loaders/index.d.ts +1 -2
  41. package/core/loaders/index.js +1 -5
  42. package/core/loaders/loader.d.ts +3 -3
  43. package/core/loaders/loader.js +4 -21
  44. package/core/loaders/object_loader.d.ts +30 -9
  45. package/core/loaders/object_loader.js +226 -79
  46. package/core/loaders/query_loader.d.ts +7 -13
  47. package/core/loaders/query_loader.js +60 -24
  48. package/core/loaders/raw_count_loader.d.ts +1 -0
  49. package/core/loaders/raw_count_loader.js +8 -3
  50. package/core/logger.d.ts +1 -1
  51. package/core/logger.js +1 -0
  52. package/core/privacy.d.ts +26 -16
  53. package/core/privacy.js +68 -51
  54. package/core/query/assoc_query.d.ts +3 -2
  55. package/core/query/assoc_query.js +10 -2
  56. package/core/query/custom_clause_query.d.ts +29 -0
  57. package/core/query/custom_clause_query.js +105 -0
  58. package/core/query/custom_query.d.ts +19 -2
  59. package/core/query/custom_query.js +111 -13
  60. package/core/query/index.d.ts +1 -0
  61. package/core/query/index.js +3 -1
  62. package/core/query/query.d.ts +18 -4
  63. package/core/query/query.js +135 -58
  64. package/core/query/shared_assoc_test.d.ts +2 -1
  65. package/core/query/shared_assoc_test.js +186 -55
  66. package/core/query/shared_test.d.ts +9 -2
  67. package/core/query/shared_test.js +529 -236
  68. package/core/query_impl.d.ts +8 -0
  69. package/core/query_impl.js +28 -0
  70. package/core/viewer.d.ts +2 -0
  71. package/core/viewer.js +3 -1
  72. package/graphql/graphql.d.ts +108 -22
  73. package/graphql/graphql.js +183 -137
  74. package/graphql/graphql_field_helpers.d.ts +9 -3
  75. package/graphql/graphql_field_helpers.js +22 -2
  76. package/graphql/index.d.ts +2 -2
  77. package/graphql/index.js +5 -5
  78. package/graphql/query/connection_type.d.ts +9 -9
  79. package/graphql/query/shared_assoc_test.js +1 -1
  80. package/graphql/query/shared_edge_connection.js +1 -19
  81. package/graphql/scalars/orderby_direction.d.ts +2 -0
  82. package/graphql/scalars/orderby_direction.js +15 -0
  83. package/imports/dataz/example1/_auth.js +128 -47
  84. package/imports/dataz/example1/_viewer.js +87 -39
  85. package/imports/index.d.ts +7 -2
  86. package/imports/index.js +20 -5
  87. package/index.d.ts +23 -5
  88. package/index.js +35 -10
  89. package/package.json +19 -19
  90. package/parse_schema/parse.d.ts +33 -9
  91. package/parse_schema/parse.js +182 -33
  92. package/schema/base_schema.d.ts +13 -3
  93. package/schema/base_schema.js +13 -0
  94. package/schema/field.d.ts +78 -21
  95. package/schema/field.js +232 -72
  96. package/schema/index.d.ts +2 -2
  97. package/schema/index.js +7 -2
  98. package/schema/json_field.d.ts +16 -4
  99. package/schema/json_field.js +32 -2
  100. package/schema/schema.d.ts +109 -20
  101. package/schema/schema.js +42 -53
  102. package/schema/struct_field.d.ts +15 -3
  103. package/schema/struct_field.js +117 -22
  104. package/schema/union_field.d.ts +1 -1
  105. package/scripts/custom_compiler.js +12 -8
  106. package/scripts/custom_graphql.js +171 -64
  107. package/scripts/migrate_v0.1.js +36 -0
  108. package/scripts/move_types.js +120 -0
  109. package/scripts/read_schema.js +22 -7
  110. package/testutils/action/complex_schemas.d.ts +69 -0
  111. package/testutils/action/complex_schemas.js +405 -0
  112. package/testutils/builder.d.ts +37 -41
  113. package/testutils/builder.js +66 -46
  114. package/testutils/db/fixture.d.ts +10 -0
  115. package/testutils/db/fixture.js +26 -0
  116. package/testutils/db/{test_db.d.ts → temp_db.d.ts} +32 -8
  117. package/testutils/db/{test_db.js → temp_db.js} +251 -48
  118. package/testutils/db/value.d.ts +7 -0
  119. package/testutils/db/value.js +251 -0
  120. package/testutils/db_mock.d.ts +16 -4
  121. package/testutils/db_mock.js +52 -9
  122. package/testutils/db_time_zone.d.ts +4 -0
  123. package/testutils/db_time_zone.js +41 -0
  124. package/testutils/ent-graphql-tests/index.d.ts +7 -1
  125. package/testutils/ent-graphql-tests/index.js +56 -26
  126. package/testutils/fake_comms.js +1 -1
  127. package/testutils/fake_data/const.d.ts +2 -1
  128. package/testutils/fake_data/const.js +3 -0
  129. package/testutils/fake_data/fake_contact.d.ts +7 -3
  130. package/testutils/fake_data/fake_contact.js +13 -7
  131. package/testutils/fake_data/fake_event.d.ts +4 -1
  132. package/testutils/fake_data/fake_event.js +7 -6
  133. package/testutils/fake_data/fake_tag.d.ts +36 -0
  134. package/testutils/fake_data/fake_tag.js +89 -0
  135. package/testutils/fake_data/fake_user.d.ts +8 -5
  136. package/testutils/fake_data/fake_user.js +31 -19
  137. package/testutils/fake_data/index.js +5 -1
  138. package/testutils/fake_data/internal.d.ts +2 -0
  139. package/testutils/fake_data/internal.js +7 -1
  140. package/testutils/fake_data/tag_query.d.ts +13 -0
  141. package/testutils/fake_data/tag_query.js +48 -0
  142. package/testutils/fake_data/test_helpers.d.ts +14 -6
  143. package/testutils/fake_data/test_helpers.js +31 -15
  144. package/testutils/fake_data/user_query.d.ts +16 -6
  145. package/testutils/fake_data/user_query.js +72 -23
  146. package/testutils/fake_log.js +1 -1
  147. package/testutils/parse_sql.d.ts +6 -0
  148. package/testutils/parse_sql.js +16 -2
  149. package/testutils/test_edge_global_schema.d.ts +15 -0
  150. package/testutils/test_edge_global_schema.js +62 -0
  151. package/testutils/write.d.ts +2 -2
  152. package/testutils/write.js +33 -7
  153. package/tsc/ast.d.ts +15 -3
  154. package/tsc/ast.js +114 -23
  155. package/tsc/compilerOptions.js +5 -1
  156. package/tsc/move_generated.d.ts +1 -0
  157. package/tsc/move_generated.js +164 -0
  158. package/tsc/transform.d.ts +22 -0
  159. package/tsc/transform.js +182 -0
  160. package/tsc/transform_action.d.ts +22 -0
  161. package/tsc/transform_action.js +183 -0
  162. package/tsc/transform_ent.d.ts +17 -0
  163. package/tsc/transform_ent.js +60 -0
  164. package/tsc/transform_schema.d.ts +27 -0
  165. package/{scripts → tsc}/transform_schema.js +146 -117
  166. package/core/loaders/index_loader.d.ts +0 -14
  167. package/core/loaders/index_loader.js +0 -27
  168. package/graphql/enums.d.ts +0 -3
  169. package/graphql/enums.js +0 -25
  170. package/scripts/move_generated.js +0 -141
  171. package/scripts/transform_actions.js +0 -266
  172. package/scripts/transform_code.d.ts +0 -1
  173. package/scripts/transform_code.js +0 -111
  174. package/scripts/transform_schema.d.ts +0 -1
  175. /package/scripts/{move_generated.d.ts → migrate_v0.1.d.ts} +0 -0
  176. /package/scripts/{transform_actions.d.ts → move_types.d.ts} +0 -0
@@ -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];
@@ -22,13 +26,66 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
22
26
  return (mod && mod.__esModule) ? mod : { "default": mod };
23
27
  };
24
28
  Object.defineProperty(exports, "__esModule", { value: true });
25
- exports.ObjectLoaderFactory = exports.ObjectLoader = void 0;
29
+ exports.ObjectLoaderFactory = exports.ObjectCountLoader = exports.ObjectLoader = void 0;
26
30
  const dataloader_1 = __importDefault(require("dataloader"));
27
31
  const ent_1 = require("../ent");
28
32
  const clause = __importStar(require("../clause"));
29
33
  const logger_1 = require("../logger");
34
+ const clause_1 = require("../clause");
30
35
  const loader_1 = require("./loader");
31
36
  const memoizee_1 = __importDefault(require("memoizee"));
37
+ async function loadRowsForIDLoader(options, ids, context) {
38
+ let col = options.key;
39
+ const cls = (0, clause_1.getCombinedClause)(options, clause.DBTypeIn(col, ids, options.keyType || "uuid"));
40
+ const rowOptions = {
41
+ ...options,
42
+ clause: cls,
43
+ context,
44
+ };
45
+ let m = new Map();
46
+ let result = [];
47
+ for (let i = 0; i < ids.length; i++) {
48
+ result.push(null);
49
+ // store the index....
50
+ m.set(ids[i], i);
51
+ }
52
+ const rows = (await (0, ent_1.loadRows)(rowOptions));
53
+ for (const row of rows) {
54
+ const id = row[col];
55
+ if (id === undefined) {
56
+ throw new Error(`need to query for column ${col} when using an object loader because the query may not be sorted and we need the id to maintain sort order`);
57
+ }
58
+ const idx = m.get(id);
59
+ if (idx === undefined) {
60
+ throw new Error(`malformed query. got ${id} back but didn't query for it`);
61
+ }
62
+ result[idx] = row;
63
+ }
64
+ return result;
65
+ }
66
+ async function loadRowsForClauseLoader(options, clause) {
67
+ const rowOptions = {
68
+ ...options,
69
+ // @ts-expect-error clause in LoadRowOptions doesn't take templatized version of Clause
70
+ clause: (0, clause_1.getCombinedClause)(options, clause),
71
+ };
72
+ return (await (0, ent_1.loadRows)(rowOptions));
73
+ }
74
+ async function loadCountForClauseLoader(options, clause) {
75
+ const rowOptions = {
76
+ ...options,
77
+ // @ts-expect-error clause in LoadRowOptions doesn't take templatized version of Clause
78
+ clause: (0, clause_1.getCombinedClause)(options, clause),
79
+ };
80
+ const row = await (0, ent_1.loadRow)({
81
+ ...rowOptions,
82
+ fields: ["count(*) as count"],
83
+ });
84
+ if (!row) {
85
+ return 0;
86
+ }
87
+ return parseInt(row.count, 10);
88
+ }
32
89
  // optional clause...
33
90
  // so ObjectLoaderFactory and createDataLoader need to take a new optional field which is a clause that's always added here
34
91
  // and we need a disableTransform which skips loader completely and uses loadRow...
@@ -36,49 +93,72 @@ function createDataLoader(options) {
36
93
  const loaderOptions = {};
37
94
  // if query logging is enabled, we should log what's happening with loader
38
95
  if ((0, logger_1.logEnabled)("query")) {
39
- loaderOptions.cacheMap = new loader_1.cacheMap(options);
96
+ loaderOptions.cacheMap = new loader_1.CacheMap(options);
40
97
  }
41
98
  return new dataloader_1.default(async (ids) => {
42
99
  if (!ids.length) {
43
100
  return [];
44
101
  }
45
- let col = options.key;
46
- let cls = clause.In(col, ...ids);
47
- if (options.clause) {
48
- let optionClause;
49
- if (typeof options.clause === "function") {
50
- optionClause = options.clause();
51
- }
52
- else {
53
- optionClause = options.clause;
54
- }
55
- if (optionClause) {
56
- cls = clause.And(optionClause, cls);
57
- }
102
+ // context not needed because we're creating a loader which has its own cache which is being used here
103
+ return loadRowsForIDLoader(options, ids);
104
+ }, loaderOptions);
105
+ }
106
+ class clauseCacheMap {
107
+ constructor(options, count) {
108
+ this.options = options;
109
+ this.count = count;
110
+ this.m = new Map();
111
+ }
112
+ get(key) {
113
+ const key2 = key.instanceKey();
114
+ const ret = this.m.get(key2);
115
+ if (ret) {
116
+ (0, logger_1.log)("cache", {
117
+ "dataloader-cache-hit": key2 + (this.count ? ":count" : ""),
118
+ "tableName": this.options.tableName,
119
+ });
58
120
  }
59
- const rowOptions = {
60
- ...options,
61
- clause: cls,
62
- };
63
- let m = new Map();
64
- let result = [];
65
- for (let i = 0; i < ids.length; i++) {
66
- result.push(null);
67
- // store the index....
68
- m.set(ids[i], i);
121
+ return ret;
122
+ }
123
+ set(key, value) {
124
+ return this.m.set(key.instanceKey(), value);
125
+ }
126
+ delete(key) {
127
+ return this.m.delete(key.instanceKey());
128
+ }
129
+ clear() {
130
+ return this.m.clear();
131
+ }
132
+ }
133
+ function createClauseDataLoder(options) {
134
+ return new dataloader_1.default(async (clauses) => {
135
+ if (!clauses.length) {
136
+ return [];
69
137
  }
70
- // context not needed because we're creating a loader which has its own cache which is being used here
71
- const rows = await (0, ent_1.loadRows)(rowOptions);
72
- for (const row of rows) {
73
- const id = row[col];
74
- const idx = m.get(id);
75
- if (idx === undefined) {
76
- throw new Error(`malformed query. got ${id} back but didn't query for it`);
77
- }
78
- result[idx] = row;
138
+ const ret = [];
139
+ for await (const clause of clauses) {
140
+ const data = await loadRowsForClauseLoader(options, clause);
141
+ ret.push(data);
79
142
  }
80
- return result;
81
- }, loaderOptions);
143
+ return ret;
144
+ }, {
145
+ cacheMap: new clauseCacheMap(options),
146
+ });
147
+ }
148
+ function createClauseCountDataLoader(options) {
149
+ return new dataloader_1.default(async (clauses) => {
150
+ if (!clauses.length) {
151
+ return [];
152
+ }
153
+ const ret = [];
154
+ for await (const clause of clauses) {
155
+ const data = await loadCountForClauseLoader(options, clause);
156
+ ret.push(data);
157
+ }
158
+ return ret;
159
+ }, {
160
+ cacheMap: new clauseCacheMap(options, true),
161
+ });
82
162
  }
83
163
  class ObjectLoader {
84
164
  constructor(options, context, toPrime) {
@@ -89,7 +169,8 @@ class ObjectLoader {
89
169
  console.trace();
90
170
  }
91
171
  if (context) {
92
- this.loader = createDataLoader(options);
172
+ this.idLoader = createDataLoader(options);
173
+ this.clauseLoader = createClauseDataLoder(options);
93
174
  }
94
175
  this.memoizedInitPrime = (0, memoizee_1.default)(this.initPrime.bind(this));
95
176
  }
@@ -111,11 +192,17 @@ class ObjectLoader {
111
192
  this.primedLoaders = primedLoaders;
112
193
  }
113
194
  async load(key) {
195
+ if (typeof key === "string" || typeof key === "number") {
196
+ return this.loadID(key);
197
+ }
198
+ return this.loadClause(key);
199
+ }
200
+ async loadID(key) {
114
201
  // simple case. we get parallelization etc
115
- if (this.loader) {
202
+ if (this.idLoader) {
116
203
  this.memoizedInitPrime();
117
204
  // prime the result if we got primable loaders
118
- const result = await this.loader.load(key);
205
+ const result = await this.idLoader.load(key);
119
206
  if (result && this.primedLoaders) {
120
207
  for (const [key, loader] of this.primedLoaders) {
121
208
  const value = result[key];
@@ -126,69 +213,119 @@ class ObjectLoader {
126
213
  }
127
214
  return result;
128
215
  }
129
- let cls = clause.Eq(this.options.key, key);
130
- if (this.options.clause) {
131
- let optionClause;
132
- if (typeof this.options.clause === "function") {
133
- optionClause = this.options.clause();
134
- }
135
- else {
136
- optionClause = this.options.clause;
137
- }
138
- if (optionClause) {
139
- cls = clause.And(optionClause, cls);
140
- }
141
- }
216
+ const cls = (0, clause_1.getCombinedClause)(this.options, clause.Eq(this.options.key, key));
142
217
  const rowOptions = {
143
218
  ...this.options,
144
219
  clause: cls,
145
220
  context: this.context,
146
221
  };
147
- return await (0, ent_1.loadRow)(rowOptions);
222
+ return (0, ent_1.loadRow)(rowOptions);
223
+ }
224
+ async loadClause(key) {
225
+ if (this.clauseLoader) {
226
+ return this.clauseLoader.load(key);
227
+ }
228
+ return loadRowsForClauseLoader(this.options, key);
148
229
  }
149
230
  clearAll() {
150
- this.loader && this.loader.clearAll();
231
+ this.idLoader && this.idLoader.clearAll();
232
+ this.clauseLoader && this.clauseLoader.clearAll();
151
233
  }
152
234
  async loadMany(keys) {
153
- if (this.loader) {
154
- return await this.loader.loadMany(keys);
235
+ if (!keys.length) {
236
+ return [];
155
237
  }
156
- let cls = clause.In(this.options.key, ...keys);
157
- if (this.options.clause) {
158
- let optionClause;
159
- if (typeof this.options.clause === "function") {
160
- optionClause = this.options.clause();
161
- }
162
- else {
163
- optionClause = this.options.clause;
164
- }
165
- if (optionClause) {
166
- cls = clause.And(optionClause, cls);
167
- }
238
+ if (typeof keys[0] === "string" || typeof keys[0] === "number") {
239
+ return this.loadIDMany(keys);
168
240
  }
169
- const rowOptions = {
170
- ...this.options,
171
- clause: cls,
172
- context: this.context,
173
- };
174
- return await (0, ent_1.loadRows)(rowOptions);
241
+ return this.loadClauseMany(keys);
242
+ }
243
+ loadIDMany(keys) {
244
+ if (this.idLoader) {
245
+ // @ts-expect-error TODO?
246
+ return this.idLoader.loadMany(keys);
247
+ }
248
+ return loadRowsForIDLoader(this.options, keys, this.context);
249
+ }
250
+ async loadClauseMany(keys) {
251
+ if (this.clauseLoader) {
252
+ // @ts-expect-error TODO?
253
+ return this.clauseLoader.loadMany(keys);
254
+ }
255
+ const res = [];
256
+ for await (const key of keys) {
257
+ const rows = await loadRowsForClauseLoader(this.options, key);
258
+ res.push(rows);
259
+ }
260
+ return res;
175
261
  }
176
262
  prime(data) {
177
263
  // we have this data from somewhere else, prime it in the c
178
- if (this.loader) {
264
+ if (this.idLoader) {
179
265
  const col = this.options.key;
180
266
  const key = data[col];
181
- this.loader.prime(key, data);
267
+ this.idLoader.prime(key, data);
268
+ }
269
+ }
270
+ // prime this loader and any other loaders it's aware of
271
+ primeAll(data) {
272
+ this.prime(data);
273
+ if (this.primedLoaders) {
274
+ for (const [key, loader] of this.primedLoaders) {
275
+ const value = data[key];
276
+ if (value !== undefined) {
277
+ loader.prime(data);
278
+ }
279
+ }
182
280
  }
183
281
  }
184
282
  }
185
283
  exports.ObjectLoader = ObjectLoader;
284
+ class ObjectCountLoader {
285
+ constructor(options, context) {
286
+ this.options = options;
287
+ this.context = context;
288
+ if (context) {
289
+ this.loader = createClauseCountDataLoader(options);
290
+ }
291
+ }
292
+ getOptions() {
293
+ return this.options;
294
+ }
295
+ async load(key) {
296
+ if (this.loader) {
297
+ return this.loader.load(key);
298
+ }
299
+ return loadCountForClauseLoader(this.options, key);
300
+ }
301
+ clearAll() {
302
+ this.loader && this.loader.clearAll();
303
+ }
304
+ async loadMany(keys) {
305
+ if (!keys.length) {
306
+ return [];
307
+ }
308
+ if (this.loader) {
309
+ // @ts-expect-error
310
+ return this.loader.loadMany(keys);
311
+ }
312
+ const res = [];
313
+ for await (const key of keys) {
314
+ const r = await loadCountForClauseLoader(this.options, key);
315
+ res.push(r);
316
+ }
317
+ return res;
318
+ }
319
+ }
320
+ exports.ObjectCountLoader = ObjectCountLoader;
321
+ // NOTE: if not querying for all columns
322
+ // have to query for the id field as one of the fields
323
+ // because it's used to maintain sort order of the queried ids
186
324
  class ObjectLoaderFactory {
187
325
  constructor(options) {
188
326
  this.options = options;
189
327
  this.toPrime = [];
190
- // we don't wanna do it here because we want it to be delayed
191
- let instanceKey = "";
328
+ let instanceKey = options.instanceKey || "";
192
329
  if (typeof this.options.clause === "function") {
193
330
  if (!options.instanceKey) {
194
331
  throw new Error(`need to pass an instanceKey to ObjectLoader if clause is a function`);
@@ -204,10 +341,20 @@ class ObjectLoaderFactory {
204
341
  return new ObjectLoader(this.options, context, this.toPrime);
205
342
  }, context);
206
343
  }
344
+ createTypedLoader(context) {
345
+ const loader = this.createLoader(context);
346
+ return loader;
347
+ }
348
+ createCountLoader(context) {
349
+ return (0, loader_1.getCustomLoader)(`${this.name}:count_loader`, () => {
350
+ return new ObjectCountLoader(this.options, context);
351
+ }, context);
352
+ }
207
353
  // keep track of loaders to prime. needs to be done not in the constructor
208
354
  // because there's usually self references here
209
355
  addToPrime(factory) {
210
356
  this.toPrime.push(factory);
357
+ return this;
211
358
  }
212
359
  }
213
360
  exports.ObjectLoaderFactory = ObjectLoaderFactory;
@@ -1,21 +1,14 @@
1
1
  import { Context, ID, EdgeQueryableDataOptions, Loader, LoaderFactory, Data } from "../base";
2
2
  import * as clause from "../clause";
3
3
  import { ObjectLoaderFactory } from "./object_loader";
4
- export declare class QueryDirectLoader<K extends any> implements Loader<K, Data[]> {
4
+ import { OrderBy } from "../query_impl";
5
+ declare class QueryDirectLoader<K extends any> implements Loader<K, Data[]> {
5
6
  private options;
6
7
  private queryOptions?;
7
- constructor(options: QueryOptions, queryOptions?: Partial<Pick<import("../base").QueryableDataOptions, "limit" | "orderby" | "clause">> | undefined);
8
- load(id: K): Promise<Data[]>;
9
- clearAll(): void;
10
- }
11
- export declare class QueryLoader<K extends any> implements Loader<K, Data[]> {
12
- private options;
13
8
  context?: Context<import("../base").Viewer<import("../base").Ent<any> | null, ID | null>> | undefined;
14
- private queryOptions?;
15
- private loader;
16
- private primedLoaders;
17
9
  private memoizedInitPrime;
18
- constructor(options: QueryOptions, context?: Context<import("../base").Viewer<import("../base").Ent<any> | null, ID | null>> | undefined, queryOptions?: Partial<Pick<import("../base").QueryableDataOptions, "limit" | "orderby" | "clause">> | undefined);
10
+ private primedLoaders;
11
+ constructor(options: QueryOptions, queryOptions?: Partial<Pick<import("../base").QueryableDataOptions, "clause" | "limit" | "orderby" | "disableTransformations">> | undefined, context?: Context<import("../base").Viewer<import("../base").Ent<any> | null, ID | null>> | undefined);
19
12
  private initPrime;
20
13
  load(id: K): Promise<Data[]>;
21
14
  clearAll(): void;
@@ -25,8 +18,8 @@ interface QueryOptions {
25
18
  tableName: string;
26
19
  groupCol?: string;
27
20
  clause?: clause.Clause;
28
- sortColumn?: string;
29
- toPrime?: ObjectLoaderFactory<ID>[];
21
+ orderby?: OrderBy;
22
+ toPrime?: ObjectLoaderFactory<Data>[];
30
23
  }
31
24
  export declare class QueryLoaderFactory<K extends any> implements LoaderFactory<K, Data[]> {
32
25
  private options;
@@ -34,5 +27,6 @@ export declare class QueryLoaderFactory<K extends any> implements LoaderFactory<
34
27
  constructor(options: QueryOptions);
35
28
  createLoader(context?: Context): any;
36
29
  createConfigurableLoader(options: EdgeQueryableDataOptions, context?: Context): Loader<unknown, Data[]> | QueryDirectLoader<unknown>;
30
+ static createConfigurableLoader(name: string, queryOptions: QueryOptions, options: EdgeQueryableDataOptions, context?: Context): Loader<unknown, Data[]> | QueryDirectLoader<unknown>;
37
31
  }
38
32
  export {};
@@ -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];
@@ -22,20 +26,21 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
22
26
  return (mod && mod.__esModule) ? mod : { "default": mod };
23
27
  };
24
28
  Object.defineProperty(exports, "__esModule", { value: true });
25
- exports.QueryLoaderFactory = exports.QueryLoader = exports.QueryDirectLoader = void 0;
29
+ exports.QueryLoaderFactory = void 0;
26
30
  const dataloader_1 = __importDefault(require("dataloader"));
27
31
  const ent_1 = require("../ent");
28
32
  const clause = __importStar(require("../clause"));
29
33
  const logger_1 = require("../logger");
30
34
  const loader_1 = require("./loader");
31
35
  const memoizee_1 = __importDefault(require("memoizee"));
32
- function getOrderBy(sortCol, orderby) {
33
- let sortColLower = sortCol.toLowerCase();
34
- let orderbyDirection = " DESC";
35
- if (sortColLower.endsWith("asc") || sortCol.endsWith("desc")) {
36
- orderbyDirection = "";
37
- }
38
- return orderby || `${sortCol}${orderbyDirection}`;
36
+ function getOrderByLocal(options, queryOptions) {
37
+ return (options.orderby ??
38
+ queryOptions?.orderby ?? [
39
+ {
40
+ column: "created_at",
41
+ direction: "DESC",
42
+ },
43
+ ]);
39
44
  }
40
45
  async function simpleCase(options, id, queryOptions) {
41
46
  let cls;
@@ -54,20 +59,18 @@ async function simpleCase(options, id, queryOptions) {
54
59
  if (queryOptions?.clause) {
55
60
  cls = clause.And(cls, queryOptions.clause);
56
61
  }
57
- let sortCol = options.sortColumn || "created_at";
58
62
  return await (0, ent_1.loadRows)({
59
63
  ...options,
60
64
  clause: cls,
61
- orderby: getOrderBy(sortCol, queryOptions?.orderby),
62
- limit: queryOptions?.limit || ent_1.DefaultLimit,
65
+ orderby: getOrderByLocal(options, queryOptions),
66
+ limit: queryOptions?.limit || (0, ent_1.getDefaultLimit)(),
63
67
  });
64
68
  }
65
69
  function createLoader(options, queryOptions) {
66
- let sortCol = options.sortColumn || "created_at";
67
70
  const loaderOptions = {};
68
71
  // if query logging is enabled, we should log what's happening with loader
69
72
  if ((0, logger_1.logEnabled)("query")) {
70
- loaderOptions.cacheMap = new loader_1.cacheMap(options);
73
+ loaderOptions.cacheMap = new loader_1.CacheMap(options);
71
74
  }
72
75
  return new dataloader_1.default(async (keys) => {
73
76
  if (!keys.length) {
@@ -101,8 +104,8 @@ function createLoader(options, queryOptions) {
101
104
  tableName: options.tableName,
102
105
  fields: options.fields,
103
106
  values: keys,
104
- orderby: getOrderBy(sortCol, queryOptions?.orderby),
105
- limit: queryOptions?.limit || ent_1.DefaultLimit,
107
+ orderby: getOrderByLocal(options, queryOptions),
108
+ limit: queryOptions?.limit || (0, ent_1.getDefaultLimit)(),
106
109
  groupColumn: col,
107
110
  clause: extraClause,
108
111
  });
@@ -120,16 +123,47 @@ function createLoader(options, queryOptions) {
120
123
  }, loaderOptions);
121
124
  }
122
125
  class QueryDirectLoader {
123
- constructor(options, queryOptions) {
126
+ constructor(options, queryOptions, context) {
124
127
  this.options = options;
125
128
  this.queryOptions = queryOptions;
129
+ this.context = context;
130
+ this.memoizedInitPrime = (0, memoizee_1.default)(this.initPrime.bind(this));
131
+ }
132
+ initPrime() {
133
+ if (!this.context || !this.options?.toPrime) {
134
+ return;
135
+ }
136
+ let primedLoaders = new Map();
137
+ this.options.toPrime.forEach((prime) => {
138
+ const l2 = prime.createLoader(this.context);
139
+ if (l2.prime === undefined) {
140
+ return;
141
+ }
142
+ primedLoaders.set(prime.options.key, l2);
143
+ });
144
+ this.primedLoaders = primedLoaders;
126
145
  }
127
146
  async load(id) {
128
- return simpleCase(this.options, id, this.queryOptions);
147
+ const rows = await simpleCase(this.options, id, this.queryOptions);
148
+ if (this.context) {
149
+ this.memoizedInitPrime();
150
+ if (this.primedLoaders) {
151
+ for (const row of rows) {
152
+ for (const [key, loader] of this.primedLoaders) {
153
+ const value = row[key];
154
+ if (value !== undefined) {
155
+ loader.prime(row);
156
+ }
157
+ }
158
+ }
159
+ }
160
+ }
161
+ return rows;
129
162
  }
130
163
  clearAll() { }
131
164
  }
132
- exports.QueryDirectLoader = QueryDirectLoader;
165
+ // note, you should never call this directly
166
+ // there's scenarios where QueryDirectLoader is needed instead of this...
133
167
  class QueryLoader {
134
168
  constructor(options, context, queryOptions) {
135
169
  this.options = options;
@@ -176,7 +210,6 @@ class QueryLoader {
176
210
  this.loader && this.loader.clearAll();
177
211
  }
178
212
  }
179
- exports.QueryLoader = QueryLoader;
180
213
  class QueryLoaderFactory {
181
214
  constructor(options) {
182
215
  this.options = options;
@@ -194,11 +227,14 @@ class QueryLoaderFactory {
194
227
  return (0, loader_1.getLoader)(this, () => new QueryLoader(this.options, context), context);
195
228
  }
196
229
  createConfigurableLoader(options, context) {
197
- if (options?.clause || !context) {
198
- return new QueryDirectLoader(this.options, options);
230
+ return QueryLoaderFactory.createConfigurableLoader(this.name, this.options, options, context);
231
+ }
232
+ static createConfigurableLoader(name, queryOptions, options, context) {
233
+ if (options.clause || !context) {
234
+ return new QueryDirectLoader(queryOptions, options, context);
199
235
  }
200
- const key = `${this.name}:limit:${options.limit}:orderby:${options.orderby}`;
201
- return (0, loader_1.getCustomLoader)(key, () => new QueryLoader(this.options, context, options), context);
236
+ const key = `${name}:limit:${options.limit}:orderby:${options.orderby}`;
237
+ return (0, loader_1.getCustomLoader)(key, () => new QueryLoader(queryOptions, context, options), context);
202
238
  }
203
239
  }
204
240
  exports.QueryLoaderFactory = QueryLoaderFactory;
@@ -4,6 +4,7 @@ import * as clause from "../clause";
4
4
  interface QueryCountOptions {
5
5
  tableName: string;
6
6
  groupCol?: string;
7
+ groupColType?: string;
7
8
  clause?: clause.Clause;
8
9
  }
9
10
  export declare function createCountDataLoader<K extends any>(options: QueryCountOptions): DataLoader<K, number, K>;
@@ -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];
@@ -55,7 +59,7 @@ function createCountDataLoader(options) {
55
59
  const loaderOptions = {};
56
60
  // if query logging is enabled, we should log what's happening with loader
57
61
  if ((0, logger_1.logEnabled)("query")) {
58
- loaderOptions.cacheMap = new loader_1.cacheMap(options);
62
+ loaderOptions.cacheMap = new loader_1.CacheMap(options);
59
63
  }
60
64
  return new dataloader_1.default(async (keys) => {
61
65
  if (!keys.length) {
@@ -65,7 +69,8 @@ function createCountDataLoader(options) {
65
69
  if (keys.length == 1 || !options.groupCol) {
66
70
  return simpleCase(options, keys[0]);
67
71
  }
68
- let cls = clause.In(options.groupCol, ...keys);
72
+ let typ = options.groupColType || "uuid";
73
+ let cls = clause.DBTypeIn(options.groupCol, keys, typ);
69
74
  if (options.clause) {
70
75
  cls = clause.And(cls, options.clause);
71
76
  }
package/core/logger.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- declare type logType = "query" | "warn" | "info" | "error" | "debug";
1
+ type logType = "query" | "warn" | "info" | "error" | "debug" | "cache";
2
2
  export declare function setLogLevels(levels: logType | logType[]): void;
3
3
  export declare function clearLogLevels(): void;
4
4
  export declare function log(level: logType, msg: any): void;
package/core/logger.js CHANGED
@@ -7,6 +7,7 @@ var m = {
7
7
  info: "log",
8
8
  error: "error",
9
9
  debug: "debug",
10
+ cache: "log",
10
11
  };
11
12
  var logLevels = new Map();
12
13
  function setLogLevels(levels) {