@snowtop/ent 0.1.0-alpha124 → 0.1.0-alpha125

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.
@@ -26,17 +26,29 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
26
26
  return (mod && mod.__esModule) ? mod : { "default": mod };
27
27
  };
28
28
  Object.defineProperty(exports, "__esModule", { value: true });
29
- exports.ObjectLoaderFactory = exports.ObjectCountLoader = exports.ObjectLoader = void 0;
29
+ exports.ObjectLoaderFactory = exports.ObjectLoader = void 0;
30
30
  const dataloader_1 = __importDefault(require("dataloader"));
31
31
  const ent_1 = require("../ent");
32
32
  const clause = __importStar(require("../clause"));
33
33
  const logger_1 = require("../logger");
34
- const clause_1 = require("../clause");
35
34
  const loader_1 = require("./loader");
36
35
  const memoizee_1 = __importDefault(require("memoizee"));
37
- async function loadRowsForIDLoader(options, ids, context) {
36
+ async function loadRowsForLoader(options, ids, context) {
38
37
  let col = options.key;
39
- const cls = (0, clause_1.getCombinedClause)(options, clause.In(col, ...ids));
38
+ let cls = clause.In(col, ...ids);
39
+ if (options.clause) {
40
+ let optionClause;
41
+ if (typeof options.clause === "function") {
42
+ optionClause = options.clause();
43
+ }
44
+ else {
45
+ optionClause = options.clause;
46
+ }
47
+ if (optionClause) {
48
+ // @ts-expect-error id/string mismatch
49
+ cls = clause.And(cls, optionClause);
50
+ }
51
+ }
40
52
  const rowOptions = {
41
53
  ...options,
42
54
  clause: cls,
@@ -63,29 +75,6 @@ async function loadRowsForIDLoader(options, ids, context) {
63
75
  }
64
76
  return result;
65
77
  }
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
- }
89
78
  // optional clause...
90
79
  // so ObjectLoaderFactory and createDataLoader need to take a new optional field which is a clause that's always added here
91
80
  // and we need a disableTransform which skips loader completely and uses loadRow...
@@ -100,66 +89,9 @@ function createDataLoader(options) {
100
89
  return [];
101
90
  }
102
91
  // context not needed because we're creating a loader which has its own cache which is being used here
103
- return loadRowsForIDLoader(options, ids);
92
+ return loadRowsForLoader(options, ids);
104
93
  }, loaderOptions);
105
94
  }
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
- });
120
- }
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 [];
137
- }
138
- const ret = [];
139
- for await (const clause of clauses) {
140
- const data = await loadRowsForClauseLoader(options, clause);
141
- ret.push(data);
142
- }
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
- });
162
- }
163
95
  class ObjectLoader {
164
96
  constructor(options, context, toPrime) {
165
97
  this.options = options;
@@ -169,8 +101,7 @@ class ObjectLoader {
169
101
  console.trace();
170
102
  }
171
103
  if (context) {
172
- this.idLoader = createDataLoader(options);
173
- this.clauseLoader = createClauseDataLoder(options);
104
+ this.loader = createDataLoader(options);
174
105
  }
175
106
  this.memoizedInitPrime = (0, memoizee_1.default)(this.initPrime.bind(this));
176
107
  }
@@ -192,17 +123,11 @@ class ObjectLoader {
192
123
  this.primedLoaders = primedLoaders;
193
124
  }
194
125
  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) {
201
126
  // simple case. we get parallelization etc
202
- if (this.idLoader) {
127
+ if (this.loader) {
203
128
  this.memoizedInitPrime();
204
129
  // prime the result if we got primable loaders
205
- const result = await this.idLoader.load(key);
130
+ const result = await this.loader.load(key);
206
131
  if (result && this.primedLoaders) {
207
132
  for (const [key, loader] of this.primedLoaders) {
208
133
  const value = result[key];
@@ -213,7 +138,19 @@ class ObjectLoader {
213
138
  }
214
139
  return result;
215
140
  }
216
- const cls = (0, clause_1.getCombinedClause)(this.options, clause.Eq(this.options.key, key));
141
+ let cls = clause.Eq(this.options.key, key);
142
+ if (this.options.clause) {
143
+ let optionClause;
144
+ if (typeof this.options.clause === "function") {
145
+ optionClause = this.options.clause();
146
+ }
147
+ else {
148
+ optionClause = this.options.clause;
149
+ }
150
+ if (optionClause) {
151
+ cls = clause.And(cls, optionClause);
152
+ }
153
+ }
217
154
  const rowOptions = {
218
155
  ...this.options,
219
156
  clause: cls,
@@ -221,50 +158,22 @@ class ObjectLoader {
221
158
  };
222
159
  return (0, ent_1.loadRow)(rowOptions);
223
160
  }
224
- async loadClause(key) {
225
- if (this.clauseLoader) {
226
- return this.clauseLoader.load(key);
227
- }
228
- return loadRowsForClauseLoader(this.options, key);
229
- }
230
161
  clearAll() {
231
- this.idLoader && this.idLoader.clearAll();
232
- this.clauseLoader && this.clauseLoader.clearAll();
162
+ this.loader && this.loader.clearAll();
233
163
  }
234
164
  async loadMany(keys) {
235
- if (!keys.length) {
236
- return [];
237
- }
238
- if (typeof keys[0] === "string" || typeof keys[0] === "number") {
239
- return this.loadIDMany(keys);
240
- }
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) {
165
+ if (this.loader) {
252
166
  // @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);
167
+ return this.loader.loadMany(keys);
259
168
  }
260
- return res;
169
+ return loadRowsForLoader(this.options, keys, this.context);
261
170
  }
262
171
  prime(data) {
263
172
  // we have this data from somewhere else, prime it in the c
264
- if (this.idLoader) {
173
+ if (this.loader) {
265
174
  const col = this.options.key;
266
175
  const key = data[col];
267
- this.idLoader.prime(key, data);
176
+ this.loader.prime(key, data);
268
177
  }
269
178
  }
270
179
  // prime this loader and any other loaders it's aware of
@@ -281,43 +190,6 @@ class ObjectLoader {
281
190
  }
282
191
  }
283
192
  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
193
  // NOTE: if not querying for all columns
322
194
  // have to query for the id field as one of the fields
323
195
  // because it's used to maintain sort order of the queried ids
@@ -341,15 +213,6 @@ class ObjectLoaderFactory {
341
213
  return new ObjectLoader(this.options, context, this.toPrime);
342
214
  }, context);
343
215
  }
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
- }
353
216
  // keep track of loaders to prime. needs to be done not in the constructor
354
217
  // because there's usually self references here
355
218
  addToPrime(factory) {
@@ -8,7 +8,7 @@ declare class QueryDirectLoader<K extends any> implements Loader<K, Data[]> {
8
8
  context?: Context<import("../base").Viewer<import("../base").Ent<any> | null, ID | null>> | undefined;
9
9
  private memoizedInitPrime;
10
10
  private primedLoaders;
11
- constructor(options: QueryOptions, queryOptions?: Partial<Pick<import("../base").QueryableDataOptions, "clause" | "limit" | "orderby">> | undefined, context?: Context<import("../base").Viewer<import("../base").Ent<any> | null, ID | null>> | undefined);
11
+ constructor(options: QueryOptions, queryOptions?: Partial<Pick<import("../base").QueryableDataOptions, "limit" | "orderby" | "clause">> | undefined, context?: Context<import("../base").Viewer<import("../base").Ent<any> | null, ID | null>> | undefined);
12
12
  private initPrime;
13
13
  load(id: K): Promise<Data[]>;
14
14
  clearAll(): void;
@@ -2,7 +2,6 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.commonTests = void 0;
4
4
  const ent_1 = require("../ent");
5
- const global_schema_1 = require("../global_schema");
6
5
  const viewer_1 = require("../viewer");
7
6
  const index_1 = require("../../testutils/fake_data/index");
8
7
  const test_helpers_1 = require("../../testutils/fake_data/test_helpers");
@@ -290,7 +289,7 @@ const commonTests = (opts) => {
290
289
  return { verify, getCursor };
291
290
  }
292
291
  if (opts.globalSchema) {
293
- (0, global_schema_1.setGlobalSchema)(test_edge_global_schema_1.testEdgeGlobalSchema);
292
+ (0, ent_1.setGlobalSchema)(test_edge_global_schema_1.testEdgeGlobalSchema);
294
293
  }
295
294
  let tdb;
296
295
  if (opts.sqlite) {
@@ -1,4 +1,3 @@
1
- import "reflect-metadata";
2
1
  import { GraphQLScalarType } from "graphql";
3
2
  interface ClassType<T = any> {
4
3
  new (...args: any[]): T;
@@ -19,17 +18,29 @@ type Type = GraphQLScalarType | ClassType | string | CustomType;
19
18
  export type GraphQLConnection<T> = {
20
19
  node: T;
21
20
  };
22
- export interface gqlFieldOptions {
21
+ interface gqlFieldOptionsBase {
23
22
  name?: string;
24
23
  nullable?: boolean | NullableListOptions;
25
24
  description?: string;
26
25
  type?: Type | Array<Type> | GraphQLConnection<Type>;
27
26
  }
27
+ interface gqlFieldArg extends gqlFieldOptionsBase {
28
+ isContextArg?: boolean;
29
+ }
30
+ export interface gqlFieldOptions extends gqlFieldOptionsBase {
31
+ nodeName: string;
32
+ args?: gqlFieldArg[];
33
+ async?: boolean;
34
+ type: NonNullable<gqlFieldOptionsBase["type"]>;
35
+ }
28
36
  export interface gqlObjectOptions {
29
37
  name?: string;
30
38
  description?: string;
31
39
  }
32
- type gqlTopLevelOptions = Exclude<gqlFieldOptions, "nullable">;
40
+ type gqlMutationOptions = Omit<gqlFieldOptions, "nullable" | "type"> & {
41
+ type?: gqlFieldOptionsBase["type"];
42
+ };
43
+ type gqlQueryOptions = Omit<gqlFieldOptions, "nullable">;
33
44
  export declare enum CustomFieldType {
34
45
  Accessor = "ACCESSOR",
35
46
  Field = "FIELD",
@@ -118,24 +129,20 @@ export declare class GQLCapture {
118
129
  static getProcessedCustomMutations(): ProcessedCustomField[];
119
130
  static getProcessedCustomQueries(): ProcessedCustomField[];
120
131
  private static getProcessedCustomFieldsImpl;
121
- private static getResultFromMetadata;
122
- static gqlField(options?: gqlFieldOptions): any;
132
+ private static getField;
133
+ static gqlField(options: gqlFieldOptions): any;
123
134
  private static getCustomField;
124
- private static argMap;
125
- private static argImpl;
126
- static gqlArg(name: string, options?: gqlFieldOptions): any;
127
- static gqlContextType(): any;
135
+ static gqlContextType(): gqlFieldArg;
128
136
  static gqlArgType(options?: gqlObjectOptions): any;
129
137
  static gqlInputObjectType(options?: gqlObjectOptions): any;
130
138
  static gqlObjectType(options?: gqlObjectOptions): any;
131
139
  private static customGQLObject;
132
- static gqlQuery(options?: gqlTopLevelOptions): any;
133
- static gqlMutation(options?: gqlTopLevelOptions): any;
140
+ static gqlQuery(options: gqlQueryOptions): any;
141
+ static gqlMutation(options: gqlMutationOptions): any;
134
142
  static gqlConnection(type: Type): any;
135
143
  static resolve(objects: string[]): void;
136
144
  }
137
145
  export declare const gqlField: typeof GQLCapture.gqlField;
138
- export declare const gqlArg: typeof GQLCapture.gqlArg;
139
146
  export declare const gqlArgType: typeof GQLCapture.gqlArgType;
140
147
  export declare const gqlInputObjectType: typeof GQLCapture.gqlInputObjectType;
141
148
  export declare const gqlObjectType: typeof GQLCapture.gqlObjectType;