@snowtop/ent 0.1.0-alpha9 → 0.1.0-alpha90

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