@snowtop/ent 0.1.0-alpha8 → 0.1.0-alpha80

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 (112) hide show
  1. package/action/action.d.ts +27 -25
  2. package/action/executor.d.ts +3 -3
  3. package/action/executor.js +2 -2
  4. package/action/experimental_action.d.ts +29 -22
  5. package/action/experimental_action.js +29 -6
  6. package/action/orchestrator.d.ts +37 -16
  7. package/action/orchestrator.js +184 -54
  8. package/action/privacy.d.ts +2 -2
  9. package/core/base.d.ts +38 -22
  10. package/core/clause.d.ts +65 -7
  11. package/core/clause.js +261 -54
  12. package/core/config.d.ts +8 -0
  13. package/core/context.d.ts +5 -3
  14. package/core/context.js +7 -2
  15. package/core/convert.d.ts +1 -1
  16. package/core/db.d.ts +1 -2
  17. package/core/db.js +1 -1
  18. package/core/ent.d.ts +76 -24
  19. package/core/ent.js +459 -143
  20. package/core/loaders/assoc_count_loader.d.ts +2 -2
  21. package/core/loaders/assoc_count_loader.js +6 -1
  22. package/core/loaders/assoc_edge_loader.d.ts +2 -2
  23. package/core/loaders/loader.js +5 -5
  24. package/core/loaders/object_loader.d.ts +4 -3
  25. package/core/loaders/object_loader.js +22 -5
  26. package/core/loaders/query_loader.d.ts +2 -2
  27. package/core/loaders/raw_count_loader.d.ts +2 -2
  28. package/core/logger.d.ts +1 -1
  29. package/core/logger.js +1 -0
  30. package/core/privacy.d.ts +26 -25
  31. package/core/privacy.js +21 -25
  32. package/core/query/assoc_query.d.ts +6 -6
  33. package/core/query/custom_query.d.ts +5 -5
  34. package/core/query/query.d.ts +1 -1
  35. package/core/query/shared_assoc_test.d.ts +1 -1
  36. package/core/query/shared_assoc_test.js +17 -5
  37. package/core/query/shared_test.d.ts +3 -0
  38. package/core/query/shared_test.js +95 -17
  39. package/core/viewer.d.ts +3 -3
  40. package/core/viewer.js +1 -1
  41. package/graphql/builtins/connection.js +3 -3
  42. package/graphql/builtins/edge.js +2 -2
  43. package/graphql/builtins/node.js +1 -1
  44. package/graphql/graphql.js +8 -2
  45. package/graphql/query/connection_type.js +6 -6
  46. package/graphql/query/edge_connection.d.ts +9 -9
  47. package/graphql/query/page_info.d.ts +1 -1
  48. package/graphql/query/page_info.js +4 -4
  49. package/graphql/query/shared_assoc_test.js +2 -2
  50. package/graphql/scalars/time.d.ts +1 -1
  51. package/index.d.ts +10 -5
  52. package/index.js +13 -6
  53. package/package.json +2 -2
  54. package/parse_schema/parse.d.ts +14 -2
  55. package/parse_schema/parse.js +40 -2
  56. package/schema/base_schema.d.ts +1 -1
  57. package/schema/base_schema.js +3 -0
  58. package/schema/field.d.ts +36 -7
  59. package/schema/field.js +70 -2
  60. package/schema/index.d.ts +2 -2
  61. package/schema/json_field.d.ts +13 -1
  62. package/schema/json_field.js +28 -1
  63. package/schema/schema.d.ts +61 -10
  64. package/schema/schema.js +18 -4
  65. package/schema/struct_field.d.ts +11 -1
  66. package/schema/struct_field.js +43 -4
  67. package/scripts/custom_graphql.js +5 -1
  68. package/scripts/{transform_schema.d.ts → migrate_v0.1.d.ts} +0 -0
  69. package/scripts/migrate_v0.1.js +36 -0
  70. package/scripts/read_schema.js +25 -2
  71. package/testutils/builder.d.ts +31 -21
  72. package/testutils/builder.js +82 -29
  73. package/testutils/db/fixture.d.ts +10 -0
  74. package/testutils/db/fixture.js +26 -0
  75. package/testutils/db/{test_db.d.ts → temp_db.d.ts} +15 -3
  76. package/testutils/db/{test_db.js → temp_db.js} +63 -15
  77. package/testutils/db/value.d.ts +6 -0
  78. package/testutils/db/value.js +251 -0
  79. package/testutils/db_time_zone.d.ts +4 -0
  80. package/testutils/db_time_zone.js +41 -0
  81. package/testutils/ent-graphql-tests/index.js +19 -12
  82. package/testutils/fake_data/fake_contact.d.ts +3 -3
  83. package/testutils/fake_data/fake_contact.js +9 -6
  84. package/testutils/fake_data/fake_event.d.ts +3 -3
  85. package/testutils/fake_data/fake_event.js +8 -5
  86. package/testutils/fake_data/fake_user.d.ts +4 -4
  87. package/testutils/fake_data/fake_user.js +16 -13
  88. package/testutils/fake_data/test_helpers.d.ts +2 -2
  89. package/testutils/fake_data/test_helpers.js +5 -5
  90. package/testutils/fake_data/user_query.d.ts +2 -2
  91. package/testutils/fake_log.d.ts +3 -3
  92. package/testutils/parse_sql.d.ts +6 -0
  93. package/testutils/parse_sql.js +16 -2
  94. package/testutils/test_edge_global_schema.d.ts +15 -0
  95. package/testutils/test_edge_global_schema.js +58 -0
  96. package/testutils/write.d.ts +2 -2
  97. package/testutils/write.js +29 -7
  98. package/tsc/ast.d.ts +44 -0
  99. package/tsc/ast.js +267 -0
  100. package/tsc/compilerOptions.d.ts +6 -0
  101. package/tsc/compilerOptions.js +40 -1
  102. package/tsc/move_generated.d.ts +1 -0
  103. package/tsc/move_generated.js +160 -0
  104. package/tsc/transform.d.ts +21 -0
  105. package/tsc/transform.js +167 -0
  106. package/tsc/transform_action.d.ts +22 -0
  107. package/tsc/transform_action.js +179 -0
  108. package/tsc/transform_ent.d.ts +17 -0
  109. package/tsc/transform_ent.js +59 -0
  110. package/tsc/transform_schema.d.ts +27 -0
  111. package/tsc/transform_schema.js +379 -0
  112. package/scripts/transform_schema.js +0 -437
@@ -1,437 +0,0 @@
1
- "use strict";
2
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
- if (k2 === undefined) k2 = k;
4
- Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
5
- }) : (function(o, m, k, k2) {
6
- if (k2 === undefined) k2 = k;
7
- o[k2] = m[k];
8
- }));
9
- var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
10
- Object.defineProperty(o, "default", { enumerable: true, value: v });
11
- }) : function(o, v) {
12
- o["default"] = v;
13
- });
14
- var __importStar = (this && this.__importStar) || function (mod) {
15
- if (mod && mod.__esModule) return mod;
16
- var result = {};
17
- if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
18
- __setModuleDefault(result, mod);
19
- return result;
20
- };
21
- var __importDefault = (this && this.__importDefault) || function (mod) {
22
- return (mod && mod.__esModule) ? mod : { "default": mod };
23
- };
24
- Object.defineProperty(exports, "__esModule", { value: true });
25
- const glob_1 = require("glob");
26
- const typescript_1 = __importDefault(require("typescript"));
27
- const fs = __importStar(require("fs"));
28
- const compilerOptions_1 = require("../tsc/compilerOptions");
29
- const child_process_1 = require("child_process");
30
- function getTarget(target) {
31
- switch (target.toLowerCase()) {
32
- case "es2015":
33
- return typescript_1.default.ScriptTarget.ES2015;
34
- case "es2016":
35
- return typescript_1.default.ScriptTarget.ES2016;
36
- case "es2017":
37
- return typescript_1.default.ScriptTarget.ES2017;
38
- case "es2018":
39
- return typescript_1.default.ScriptTarget.ES2018;
40
- case "es2019":
41
- return typescript_1.default.ScriptTarget.ES2019;
42
- case "es2020":
43
- return typescript_1.default.ScriptTarget.ES2020;
44
- case "es2021":
45
- return typescript_1.default.ScriptTarget.ES2021;
46
- case "es3":
47
- return typescript_1.default.ScriptTarget.ES3;
48
- case "es5":
49
- return typescript_1.default.ScriptTarget.ES5;
50
- case "esnext":
51
- return typescript_1.default.ScriptTarget.ESNext;
52
- default:
53
- return typescript_1.default.ScriptTarget.ESNext;
54
- }
55
- }
56
- async function main() {
57
- // this assumes this is being run from root of directory
58
- const options = (0, compilerOptions_1.readCompilerOptions)(".");
59
- let files = glob_1.glob.sync("src/schema/*.ts");
60
- const target = options.target
61
- ? // @ts-ignore
62
- getTarget(options.target)
63
- : typescript_1.default.ScriptTarget.ESNext;
64
- // filter to only event.ts e.g. for comments and whitespace...
65
- // files = files.filter((f) => f.endsWith("user.ts"));
66
- files.forEach((file) => {
67
- // assume valid file since we do glob above
68
- // const idx = file.lastIndexOf(".ts");
69
- // const writeFile = file.substring(0, idx) + "2" + ".ts";
70
- const writeFile = file;
71
- let contents = fs.readFileSync(file).toString();
72
- // go through the file and print everything back if not starting immediately after other position
73
- const sourceFile = typescript_1.default.createSourceFile(file, contents, target, false, typescript_1.default.ScriptKind.TS);
74
- const nodes = [];
75
- let updateImport = false;
76
- let removeImports = [];
77
- const f = {
78
- trackNode: function (tni) {
79
- nodes.push({
80
- node: tni.node,
81
- importNode: tni.node && typescript_1.default.isImportDeclaration(tni.node),
82
- rawString: tni.rawString,
83
- });
84
- if (tni.removeImports) {
85
- removeImports.push(...tni.removeImports);
86
- }
87
- },
88
- flagUpdateImport() {
89
- updateImport = true;
90
- },
91
- };
92
- if (!traverse(contents, sourceFile, f)) {
93
- return;
94
- }
95
- let newContents = "";
96
- for (const node of nodes) {
97
- if (updateImport && node.importNode) {
98
- const importNode = node.node;
99
- const transformedImport = transformImport(importNode, sourceFile, removeImports);
100
- if (transformedImport) {
101
- newContents += transformedImport;
102
- continue;
103
- }
104
- }
105
- if (node.node) {
106
- newContents += node.node.getFullText(sourceFile);
107
- }
108
- else if (node.rawString) {
109
- newContents += node.rawString;
110
- }
111
- else {
112
- console.error("invalid node");
113
- }
114
- }
115
- // console.debug(newContents);
116
- // ideally there's a flag that indicates if we write
117
- fs.writeFileSync(writeFile, newContents);
118
- });
119
- (0, child_process_1.execSync)("prettier src/schema/*.ts --write");
120
- }
121
- function traverse(fileContents, sourceFile, f) {
122
- let traversed = false;
123
- typescript_1.default.forEachChild(sourceFile, function (node) {
124
- if (typescript_1.default.isClassDeclaration(node)) {
125
- traversed = true;
126
- // TODO address implicit schema doesn't work here...
127
- // console.debug(sourceFile.fileName, node.kind);
128
- if (traverseClass(fileContents, sourceFile, node, f)) {
129
- f.flagUpdateImport();
130
- return;
131
- }
132
- }
133
- f.trackNode({ node });
134
- });
135
- return traversed;
136
- }
137
- function traverseClass(fileContents, sourceFile, node, f) {
138
- const ci = getClassInfo(fileContents, sourceFile, node);
139
- if (!ci) {
140
- return false;
141
- }
142
- let klassContents = `${ci.comment}const ${ci.name} = new ${ci.class}({\n`;
143
- let removeImports = [];
144
- if (ci.implementsSchema) {
145
- removeImports.push("Schema");
146
- }
147
- for (let member of node.members) {
148
- const fInfo = getClassElementInfo(fileContents, member, sourceFile);
149
- if (!fInfo) {
150
- return false;
151
- }
152
- klassContents += `${fInfo.comment}${fInfo.key}:${fInfo.value},\n`;
153
- if (fInfo.type) {
154
- removeImports.push(fInfo.type);
155
- }
156
- }
157
- klassContents += "\n})";
158
- if (ci.export && ci.default) {
159
- klassContents += `\n export default ${ci.name};`;
160
- }
161
- else if (ci.export) {
162
- klassContents = "export " + klassContents;
163
- }
164
- // console.debug(klassContents);
165
- f.trackNode({ rawString: klassContents, removeImports: removeImports });
166
- return true;
167
- }
168
- function transformSchema(str) {
169
- // only do known class names
170
- if (str === "BaseEntSchema" || str === "BaseEntSchemaWithTZ") {
171
- return str.substring(4);
172
- }
173
- return str;
174
- }
175
- function getClassInfo(fileContents, sourceFile, node) {
176
- const className = node.name?.text;
177
- let classExtends;
178
- let implementsSchema = false;
179
- if (node.heritageClauses) {
180
- for (const hc of node.heritageClauses) {
181
- if (hc.token === typescript_1.default.SyntaxKind.ImplementsKeyword) {
182
- for (const type of hc.types) {
183
- if (type.expression.getText(sourceFile) === "Schema") {
184
- implementsSchema = true;
185
- }
186
- }
187
- continue;
188
- }
189
- // ts.SyntaxKind.ExtendsKeyword
190
- // can only extend one class
191
- for (const type of hc.types) {
192
- const text = type.expression.getText(sourceFile);
193
- const transformed = transformSchema(text);
194
- if (transformed === text) {
195
- return undefined;
196
- }
197
- classExtends = transformed;
198
- }
199
- }
200
- }
201
- if (!className || !node.heritageClauses || !classExtends) {
202
- return undefined;
203
- }
204
- let ci = {
205
- name: className,
206
- class: classExtends,
207
- comment: getPreText(fileContents, node, sourceFile),
208
- implementsSchema,
209
- };
210
- if (node.modifiers) {
211
- for (const mod of node.modifiers) {
212
- const text = mod.getText(sourceFile);
213
- if (text === "export") {
214
- ci.export = true;
215
- }
216
- else if (text === "default") {
217
- ci.default = true;
218
- }
219
- }
220
- }
221
- return ci;
222
- }
223
- // intentionally doesn't parse decorators since we don't need it
224
- function getClassElementInfo(fileContents, member, sourceFile) {
225
- if (isFieldElement(member, sourceFile)) {
226
- return getFieldElementInfo(fileContents, member, sourceFile);
227
- }
228
- if (member.kind === typescript_1.default.SyntaxKind.Constructor) {
229
- return getConstructorElementInfo(fileContents, member, sourceFile);
230
- }
231
- if (member.kind !== typescript_1.default.SyntaxKind.PropertyDeclaration) {
232
- return;
233
- }
234
- // other properties
235
- const property = member;
236
- if (!property.initializer) {
237
- return;
238
- }
239
- const token = property.name;
240
- return {
241
- key: token.escapedText.toString(),
242
- value: property.initializer?.getFullText(sourceFile),
243
- comment: getPreText(fileContents, member, sourceFile),
244
- type: getType(property, sourceFile),
245
- };
246
- }
247
- function getType(property, sourceFile) {
248
- let propertytype = property.type?.getText(sourceFile) || "";
249
- let ends = ["| null", "[]"];
250
- for (const end of ends) {
251
- if (propertytype.endsWith(end)) {
252
- propertytype = propertytype.slice(0, -1 * end.length);
253
- }
254
- }
255
- return propertytype;
256
- }
257
- function getFieldElementInfo(fileContents, member, sourceFile) {
258
- let fieldMap = "";
259
- // need to change to fields: {code: StringType()};
260
- const property = member;
261
- const initializer = property.initializer;
262
- fieldMap += "{";
263
- for (const element of initializer.elements) {
264
- const parsed = parseFieldElement(element, sourceFile, fileContents);
265
- if (parsed === null) {
266
- return;
267
- }
268
- const { callEx, name, nameComment, properties } = parsed;
269
- let property = "";
270
- const fieldComment = getPreText(fileContents, element, sourceFile).trim();
271
- if (fieldComment) {
272
- property += "\n" + fieldComment + "\n";
273
- }
274
- if (nameComment) {
275
- property += nameComment + "\n";
276
- }
277
- // e.g. UUIDType, StringType etc
278
- let call = callEx.expression.getText(sourceFile);
279
- let fnCall = "";
280
- if (properties.length) {
281
- fnCall = `{${properties.join(",")}}`;
282
- }
283
- property += `${name}:${call}(${fnCall}),`;
284
- fieldMap += property;
285
- }
286
- fieldMap += "}";
287
- return {
288
- key: "fields",
289
- value: fieldMap,
290
- comment: getPreText(fileContents, member, sourceFile),
291
- type: getType(property, sourceFile),
292
- };
293
- }
294
- function getConstructorElementInfo(fileContents, member, sourceFile) {
295
- const c = member;
296
- //remove {}
297
- let fullText = c.body?.getFullText(sourceFile) || "";
298
- fullText = fullText.trim().slice(1, -1).trim();
299
- // convert something like
300
- /*
301
- constructor() {
302
- super();
303
- this.addPatterns(
304
- new Feedback(),
305
- new DayOfWeek(),
306
- new Feedback(),
307
- new DayOfWeek(),
308
- );
309
- }
310
- */
311
- // into this.addPatterns(new Feedback(),new DayOfWeek(),new Feedback(),new DayOfWeek(),)
312
- const lines = fullText
313
- .split("\n")
314
- .map((line) => line.trim())
315
- .join("")
316
- .split(";")
317
- .filter((f) => f != "super()" && f != "");
318
- // at this point there should be only line for what we handle
319
- if (lines.length != 1) {
320
- return;
321
- }
322
- const line = lines[0];
323
- const addPatterns = "this.addPatterns(";
324
- if (!line.startsWith(addPatterns)) {
325
- return;
326
- }
327
- return {
328
- key: "patterns",
329
- // remove this.addPatterns at the front, remove trailing ) at the end
330
- // if there's a trailing comma, it'll be handled by prettier
331
- value: `[${line.slice(addPatterns.length, -1)}]`,
332
- comment: "",
333
- };
334
- }
335
- function isFieldElement(member, sourceFile) {
336
- if (member.kind !== typescript_1.default.SyntaxKind.PropertyDeclaration) {
337
- return false;
338
- }
339
- const property = member;
340
- const token = property.name;
341
- if (token.escapedText !== "fields") {
342
- return false;
343
- }
344
- const propertytype = property.type?.getText(sourceFile);
345
- if (propertytype !== "Field[]") {
346
- return false;
347
- }
348
- if (property.initializer?.kind !== typescript_1.default.SyntaxKind.ArrayLiteralExpression) {
349
- console.error("invalid array type");
350
- return false;
351
- }
352
- return true;
353
- }
354
- function parseFieldElement(element, sourceFile, fileContents) {
355
- if (element.kind !== typescript_1.default.SyntaxKind.CallExpression) {
356
- console.error("skipped non-call expression");
357
- return null;
358
- }
359
- let callEx = element;
360
- if (callEx.arguments.length !== 1) {
361
- console.error("callExpression with arguments not of length 1");
362
- return null;
363
- }
364
- let arg = callEx.arguments[0];
365
- if (arg.kind !== typescript_1.default.SyntaxKind.ObjectLiteralExpression) {
366
- console.error("not objectLiteralExpression");
367
- return null;
368
- }
369
- let expr = arg;
370
- let name = "";
371
- let propertyComment;
372
- let properties = [];
373
- for (const p of expr.properties) {
374
- const p2 = p;
375
- // found name property
376
- if (p2.name.escapedText === "name") {
377
- name = p2.initializer.getText(sourceFile);
378
- // check for any comment associated with name: "fooo"
379
- propertyComment = getPreText(fileContents, p, sourceFile).trim();
380
- }
381
- else {
382
- properties.push(p.getFullText(sourceFile));
383
- }
384
- }
385
- if (!name) {
386
- console.error(`couldn't find name property`);
387
- return null;
388
- }
389
- // remove quotes
390
- name = name.slice(1, -1);
391
- return {
392
- callEx,
393
- name,
394
- properties,
395
- nameComment: propertyComment,
396
- };
397
- }
398
- function transformImport(importNode, sourceFile, removeImports) {
399
- // remove quotes too
400
- const text = importNode.moduleSpecifier.getText(sourceFile).slice(1, -1);
401
- if (text !== "@snowtop/ent" &&
402
- text !== "@snowtop/ent/schema" &&
403
- text !== "@snowtop/ent/schema/") {
404
- return;
405
- }
406
- const importText = importNode.importClause?.getText(sourceFile) || "";
407
- const start = importText.indexOf("{");
408
- const end = importText.lastIndexOf("}");
409
- if (start === -1 || end === -1) {
410
- return;
411
- }
412
- const imports = importText
413
- .substring(start + 1, end)
414
- // .trim()
415
- .split(",");
416
- let removeImportsMap = {};
417
- removeImports.forEach((imp) => (removeImportsMap[imp] = true));
418
- let newImports = [];
419
- for (let i = 0; i < imports.length; i++) {
420
- let imp = transformSchema(imports[i].trim());
421
- if (removeImportsMap[imp]) {
422
- continue;
423
- }
424
- newImports.push(imp);
425
- }
426
- return ("import " +
427
- importText.substring(0, start + 1) +
428
- newImports.join(", ") +
429
- importText.substring(end) +
430
- ' from "' +
431
- text +
432
- '"');
433
- }
434
- function getPreText(fileContents, node, sourceFile) {
435
- return fileContents.substring(node.getFullStart(), node.getStart(sourceFile));
436
- }
437
- Promise.resolve(main());