@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
@@ -0,0 +1,383 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = 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);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ var __importDefault = (this && this.__importDefault) || function (mod) {
26
+ return (mod && mod.__esModule) ? mod : { "default": mod };
27
+ };
28
+ Object.defineProperty(exports, "__esModule", { value: true });
29
+ exports.TransformSchema = void 0;
30
+ const typescript_1 = __importDefault(require("typescript"));
31
+ const fs = __importStar(require("fs"));
32
+ const path_1 = __importDefault(require("path"));
33
+ const ast_1 = require("../tsc/ast");
34
+ function traverseClass(fileContents, sourceFile, node, transformSchema) {
35
+ const ci = getTransformClassInfo(fileContents, sourceFile, node, transformSchema);
36
+ if (!ci) {
37
+ return;
38
+ }
39
+ let klassContents = `${ci.comment}const ${ci.name} = new ${transformSchema(ci.class)}({\n`;
40
+ let removeImports = [];
41
+ if (ci.implementsSchema) {
42
+ removeImports.push("Schema");
43
+ }
44
+ removeImports.push(ci.class);
45
+ let newImports = [transformSchema(ci.class)];
46
+ for (let member of node.members) {
47
+ const fInfo = getClassElementInfo(fileContents, member, sourceFile);
48
+ if (!fInfo) {
49
+ return;
50
+ }
51
+ klassContents += `${fInfo.comment}${fInfo.key}:${fInfo.value},\n`;
52
+ if (fInfo.type) {
53
+ removeImports.push(fInfo.type);
54
+ }
55
+ }
56
+ klassContents += "\n})";
57
+ if (ci.export && ci.default) {
58
+ klassContents += `\n export default ${ci.name};`;
59
+ }
60
+ else if (ci.export) {
61
+ klassContents = "export " + klassContents;
62
+ }
63
+ // console.debug(klassContents);
64
+ return {
65
+ rawString: klassContents,
66
+ removeImports: removeImports,
67
+ newImports,
68
+ };
69
+ }
70
+ function getTransformClassInfo(fileContents, sourceFile, node, transformSchema) {
71
+ const generic = (0, ast_1.getClassInfo)(fileContents, sourceFile, node);
72
+ if (!generic) {
73
+ return;
74
+ }
75
+ let className = generic.name;
76
+ if (!className?.endsWith("Schema")) {
77
+ className += "Schema";
78
+ }
79
+ let implementsSchema = generic.implements?.some((v) => v == "Schema");
80
+ let classExtends = generic.extends;
81
+ // nothing transformed here, so nothing to do here
82
+ if (classExtends && classExtends === transformSchema(classExtends)) {
83
+ return undefined;
84
+ }
85
+ if (!className || !node.heritageClauses || !classExtends) {
86
+ return undefined;
87
+ }
88
+ let ci = {
89
+ ...generic,
90
+ name: className,
91
+ class: classExtends,
92
+ implementsSchema,
93
+ };
94
+ return ci;
95
+ }
96
+ // intentionally doesn't parse decorators since we don't need it
97
+ function getClassElementInfo(fileContents, member, sourceFile) {
98
+ if (isFieldElement(fileContents, member, sourceFile)) {
99
+ return getFieldElementInfo(fileContents, member, sourceFile);
100
+ }
101
+ if (member.kind === typescript_1.default.SyntaxKind.Constructor) {
102
+ return getConstructorElementInfo(fileContents, member, sourceFile);
103
+ }
104
+ if (member.kind !== typescript_1.default.SyntaxKind.PropertyDeclaration) {
105
+ return;
106
+ }
107
+ // other properties
108
+ const property = member;
109
+ if (!property.initializer) {
110
+ return;
111
+ }
112
+ const token = property.name;
113
+ return {
114
+ key: token.escapedText.toString(),
115
+ value: property.initializer?.getFullText(sourceFile),
116
+ comment: (0, ast_1.getPreText)(fileContents, member, sourceFile),
117
+ type: getType(property, sourceFile),
118
+ };
119
+ }
120
+ function getType(property, sourceFile) {
121
+ let propertytype = property.type?.getText(sourceFile) || "";
122
+ let ends = ["| null", "[]"];
123
+ for (const end of ends) {
124
+ if (propertytype.endsWith(end)) {
125
+ propertytype = propertytype.slice(0, -1 * end.length);
126
+ }
127
+ }
128
+ return propertytype;
129
+ }
130
+ function getFieldElementInfo(fileContents, member, sourceFile) {
131
+ let fieldMap = "";
132
+ // need to change to fields: {code: StringType()};
133
+ const property = member;
134
+ const initializer = property.initializer;
135
+ fieldMap += "{";
136
+ for (const element of initializer.elements) {
137
+ const parsed = parseFieldElement(element, sourceFile, fileContents);
138
+ if (parsed === null) {
139
+ return;
140
+ }
141
+ const { callEx, name, nameComment, properties, suffix } = parsed;
142
+ let property = "";
143
+ const fieldComment = (0, ast_1.getPreText)(fileContents, element, sourceFile).trim();
144
+ if (fieldComment) {
145
+ property += "\n" + fieldComment + "\n";
146
+ }
147
+ if (nameComment) {
148
+ property += nameComment + "\n";
149
+ }
150
+ // e.g. UUIDType, StringType etc
151
+ let call = callEx.expression.getText(sourceFile);
152
+ let fnCall = "";
153
+ if (properties.length) {
154
+ fnCall = `{${properties.join(",")}}`;
155
+ }
156
+ property += `${name}:${call}(${fnCall})${suffix || ""},`;
157
+ fieldMap += property;
158
+ }
159
+ fieldMap += "}";
160
+ return {
161
+ key: "fields",
162
+ value: fieldMap,
163
+ comment: (0, ast_1.getPreText)(fileContents, member, sourceFile),
164
+ type: getType(property, sourceFile),
165
+ };
166
+ }
167
+ function getConstructorElementInfo(fileContents, member, sourceFile) {
168
+ const c = member;
169
+ //remove {}
170
+ let fullText = c.body?.getFullText(sourceFile) || "";
171
+ fullText = fullText.trim().slice(1, -1).trim();
172
+ // convert something like
173
+ /*
174
+ constructor() {
175
+ super();
176
+ this.addPatterns(
177
+ new Feedback(),
178
+ new DayOfWeek(),
179
+ new Feedback(),
180
+ new DayOfWeek(),
181
+ );
182
+ }
183
+ */
184
+ // into this.addPatterns(new Feedback(),new DayOfWeek(),new Feedback(),new DayOfWeek(),)
185
+ const lines = fullText
186
+ .split("\n")
187
+ .map((line) => line.trim())
188
+ .join("")
189
+ .split(";")
190
+ .filter((f) => f != "super()" && f != "");
191
+ // at this point there should be only line for what we handle
192
+ if (lines.length != 1) {
193
+ return;
194
+ }
195
+ const line = lines[0];
196
+ const addPatterns = "this.addPatterns(";
197
+ if (!line.startsWith(addPatterns)) {
198
+ return;
199
+ }
200
+ return {
201
+ key: "patterns",
202
+ // remove this.addPatterns at the front, remove trailing ) at the end
203
+ // if there's a trailing comma, it'll be handled by prettier
204
+ value: `[${line.slice(addPatterns.length, -1)}]`,
205
+ comment: "",
206
+ };
207
+ }
208
+ function isFieldElement(fileContents, member, sourceFile) {
209
+ if (member.kind !== typescript_1.default.SyntaxKind.PropertyDeclaration) {
210
+ return false;
211
+ }
212
+ const property = member;
213
+ const token = property.name;
214
+ if (token.escapedText !== "fields") {
215
+ return false;
216
+ }
217
+ const propertytype = property.type?.getText(sourceFile);
218
+ if (propertytype !== "Field[]") {
219
+ return false;
220
+ }
221
+ if (property.initializer?.kind !== typescript_1.default.SyntaxKind.ArrayLiteralExpression) {
222
+ throwErr(fileContents, member, "invalid array type");
223
+ return false;
224
+ }
225
+ return true;
226
+ }
227
+ // if there's an error transforming any of the schemas, we should stop...
228
+ function throwErr(fileContents, node, error) {
229
+ console.error(error);
230
+ throw new Error(`error transforming this field ${fileContents.substring(node.getFullStart(), node.getEnd())}`);
231
+ }
232
+ function parseFieldElement(element, sourceFile, fileContents, nested) {
233
+ if (element.kind !== typescript_1.default.SyntaxKind.CallExpression &&
234
+ element.kind !== typescript_1.default.SyntaxKind.PropertyAccessExpression) {
235
+ throwErr(fileContents, element, `skipped unknown (non-call|non-property) expression ${element.kind}`);
236
+ return null;
237
+ }
238
+ if (element.kind === typescript_1.default.SyntaxKind.PropertyAccessExpression) {
239
+ const ret = parseFieldElement(element.expression, sourceFile, fileContents, true);
240
+ if (ret !== null) {
241
+ if (!nested) {
242
+ ret.suffix = fileContents.substring(ret.callEx.getEnd(), element.getEnd());
243
+ }
244
+ return ret;
245
+ }
246
+ }
247
+ let callEx = element;
248
+ if (callEx.arguments.length !== 1) {
249
+ // have a situation like: StringType({ name: "canonicalName" }).trim().toLowerCase(),
250
+ // need to keep calling this until we find what we want and then get the suffix we should just add to the end of the transformed code
251
+ if (callEx.expression.kind === typescript_1.default.SyntaxKind.PropertyAccessExpression) {
252
+ const ret = parseFieldElement(callEx.expression.expression, sourceFile, fileContents, true);
253
+ if (ret !== null) {
254
+ if (!nested) {
255
+ ret.suffix = fileContents.substring(ret.callEx.getEnd(), callEx.getEnd());
256
+ }
257
+ return ret;
258
+ }
259
+ }
260
+ throwErr(fileContents, element, "callExpression with arguments not of length 1");
261
+ }
262
+ let arg = callEx.arguments[0];
263
+ if (arg.kind !== typescript_1.default.SyntaxKind.ObjectLiteralExpression) {
264
+ // this and the check above for PropertyAccessExpression are to handle things like
265
+ // FooType({
266
+ /// ...
267
+ // }).function(blah)
268
+ const ret = parseFieldElement(callEx.expression, sourceFile, fileContents, true);
269
+ if (ret !== null) {
270
+ if (!nested) {
271
+ ret.suffix = fileContents.substring(ret.callEx.getEnd(), callEx.getEnd());
272
+ }
273
+ return ret;
274
+ }
275
+ throwErr(fileContents, element, `not objectLiteralExpression. kind ${arg.kind}`);
276
+ return null;
277
+ }
278
+ let expr = arg;
279
+ let name = "";
280
+ let propertyComment;
281
+ let properties = [];
282
+ for (const p of expr.properties) {
283
+ const p2 = p;
284
+ // found name property
285
+ if (p2.name.escapedText === "name") {
286
+ name = p2.initializer.getText(sourceFile);
287
+ // check for any comment associated with name: "fooo"
288
+ propertyComment = (0, ast_1.getPreText)(fileContents, p, sourceFile).trim();
289
+ }
290
+ else {
291
+ properties.push(p.getFullText(sourceFile));
292
+ }
293
+ }
294
+ if (!name) {
295
+ throwErr(fileContents, element, `couldn't find name property`);
296
+ return null;
297
+ }
298
+ // remove quotes
299
+ name = name.slice(1, -1);
300
+ return {
301
+ callEx,
302
+ name,
303
+ properties,
304
+ nameComment: propertyComment,
305
+ };
306
+ }
307
+ // find which of these importPaths is being used and use that to replace
308
+ function findSchemaImportPath(sourceFile) {
309
+ const paths = {
310
+ "@snowtop/ent": true,
311
+ "@snowtop/ent/schema": true,
312
+ "@snowtop/ent/schema/": true,
313
+ };
314
+ // @ts-ignore
315
+ const importStatements = sourceFile.statements.filter((stmt) => typescript_1.default.isImportDeclaration(stmt));
316
+ for (const imp of importStatements) {
317
+ const impInfo = (0, ast_1.getImportInfo)(imp, sourceFile);
318
+ if (!impInfo) {
319
+ continue;
320
+ }
321
+ if (paths[impInfo.importPath] !== undefined) {
322
+ return impInfo.importPath;
323
+ }
324
+ }
325
+ }
326
+ class TransformSchema {
327
+ // we only end up doing this once because we change the schema representation
328
+ // so safe to run this multiple times
329
+ constructor(relativeImports, oldBaseClass, newSchemaClass, transformPath) {
330
+ this.relativeImports = relativeImports;
331
+ this.oldBaseClass = oldBaseClass;
332
+ this.newSchemaClass = newSchemaClass;
333
+ this.transformPath = transformPath;
334
+ this.glob = "src/schema/*.ts";
335
+ this.prettierGlob = "src/schema/*.ts";
336
+ }
337
+ transformSchema(className) {
338
+ if (className === "BaseEntSchema" || className === "BaseEntSchemaWithTZ") {
339
+ return className.substring(4);
340
+ }
341
+ if (className === this.oldBaseClass && this.newSchemaClass) {
342
+ return this.newSchemaClass;
343
+ }
344
+ return className;
345
+ }
346
+ traverseChild(sourceFile, contents, file, node) {
347
+ if (!typescript_1.default.isClassDeclaration(node)) {
348
+ return { node };
349
+ }
350
+ // TODO address implicit schema doesn't work here...
351
+ const ret = traverseClass(contents, sourceFile, node, this.transformSchema.bind(this));
352
+ if (ret === undefined) {
353
+ return;
354
+ }
355
+ let imports = new Map();
356
+ const imp = findSchemaImportPath(sourceFile);
357
+ if (imp) {
358
+ if (this.transformPath) {
359
+ imports.set(imp, []);
360
+ }
361
+ else {
362
+ imports.set(imp, ret.newImports);
363
+ }
364
+ }
365
+ if (this.transformPath) {
366
+ // add new imports to this path
367
+ imports.set((0, ast_1.transformRelative)(file, this.transformPath, this.relativeImports), ret.newImports);
368
+ }
369
+ return {
370
+ traversed: true,
371
+ rawString: ret.rawString,
372
+ removeImports: ret.removeImports,
373
+ imports: imports,
374
+ };
375
+ }
376
+ fileToWrite(file) {
377
+ return "src/schema/" + path_1.default.basename(file).slice(0, -3) + "_schema.ts";
378
+ }
379
+ postProcess(file) {
380
+ fs.rmSync(file);
381
+ }
382
+ }
383
+ exports.TransformSchema = TransformSchema;