@snowtop/ent 0.1.0-alpha → 0.1.0-alpha12

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 (86) hide show
  1. package/action/action.d.ts +2 -0
  2. package/action/executor.d.ts +1 -1
  3. package/action/orchestrator.d.ts +16 -3
  4. package/action/orchestrator.js +157 -47
  5. package/core/base.d.ts +6 -2
  6. package/core/base.js +16 -0
  7. package/core/clause.d.ts +24 -3
  8. package/core/clause.js +246 -5
  9. package/core/config.d.ts +18 -0
  10. package/core/config.js +17 -0
  11. package/core/db.d.ts +3 -3
  12. package/core/db.js +2 -0
  13. package/core/ent.d.ts +2 -4
  14. package/core/ent.js +72 -25
  15. package/core/loaders/assoc_edge_loader.d.ts +1 -1
  16. package/core/loaders/assoc_edge_loader.js +5 -4
  17. package/core/loaders/index_loader.js +1 -0
  18. package/core/loaders/object_loader.d.ts +7 -2
  19. package/core/loaders/object_loader.js +59 -4
  20. package/core/privacy.d.ts +1 -1
  21. package/core/privacy.js +3 -0
  22. package/core/viewer.d.ts +1 -0
  23. package/core/viewer.js +4 -0
  24. package/graphql/builtins/connection.js +3 -3
  25. package/graphql/builtins/edge.js +2 -2
  26. package/graphql/builtins/node.js +1 -1
  27. package/graphql/graphql.d.ts +3 -2
  28. package/graphql/graphql.js +24 -23
  29. package/graphql/index.d.ts +1 -0
  30. package/graphql/index.js +3 -1
  31. package/graphql/mutations/union.d.ts +2 -0
  32. package/graphql/mutations/union.js +35 -0
  33. package/graphql/node_resolver.d.ts +0 -1
  34. package/graphql/query/connection_type.js +6 -6
  35. package/graphql/query/page_info.js +4 -4
  36. package/graphql/query/shared_assoc_test.js +2 -2
  37. package/graphql/scalars/time.d.ts +1 -1
  38. package/imports/index.d.ts +0 -1
  39. package/imports/index.js +3 -36
  40. package/index.d.ts +16 -1
  41. package/index.js +18 -5
  42. package/package.json +3 -3
  43. package/parse_schema/parse.d.ts +23 -4
  44. package/parse_schema/parse.js +87 -8
  45. package/schema/base_schema.d.ts +36 -1
  46. package/schema/base_schema.js +63 -17
  47. package/schema/field.d.ts +25 -25
  48. package/schema/field.js +42 -33
  49. package/schema/index.d.ts +4 -2
  50. package/schema/index.js +10 -1
  51. package/schema/json_field.d.ts +6 -6
  52. package/schema/json_field.js +2 -2
  53. package/schema/schema.d.ts +70 -6
  54. package/schema/schema.js +142 -10
  55. package/schema/struct_field.d.ts +17 -0
  56. package/schema/struct_field.js +102 -0
  57. package/schema/union_field.d.ts +23 -0
  58. package/schema/union_field.js +79 -0
  59. package/scripts/custom_compiler.js +2 -19
  60. package/scripts/custom_graphql.js +122 -15
  61. package/scripts/move_generated.d.ts +1 -0
  62. package/scripts/move_generated.js +142 -0
  63. package/scripts/read_schema.js +15 -1
  64. package/scripts/transform_code.d.ts +1 -0
  65. package/scripts/transform_code.js +113 -0
  66. package/scripts/transform_schema.d.ts +1 -0
  67. package/scripts/transform_schema.js +355 -0
  68. package/testutils/builder.d.ts +16 -9
  69. package/testutils/builder.js +80 -8
  70. package/testutils/context/test_context.d.ts +2 -2
  71. package/testutils/context/test_context.js +7 -1
  72. package/testutils/db/test_db.d.ts +2 -1
  73. package/testutils/db/test_db.js +22 -13
  74. package/testutils/ent-graphql-tests/index.d.ts +2 -0
  75. package/testutils/ent-graphql-tests/index.js +26 -17
  76. package/testutils/fake_data/fake_contact.d.ts +3 -7
  77. package/testutils/fake_data/fake_contact.js +14 -26
  78. package/testutils/fake_data/fake_event.d.ts +3 -7
  79. package/testutils/fake_data/fake_event.js +20 -33
  80. package/testutils/fake_data/fake_user.d.ts +3 -7
  81. package/testutils/fake_data/fake_user.js +22 -36
  82. package/testutils/fake_data/test_helpers.js +1 -1
  83. package/tsc/ast.d.ts +21 -0
  84. package/tsc/ast.js +154 -0
  85. package/tsc/compilerOptions.d.ts +8 -0
  86. package/tsc/compilerOptions.js +100 -0
@@ -26,7 +26,18 @@ class FakeUser {
26
26
  constructor(viewer, data) {
27
27
  this.viewer = viewer;
28
28
  this.nodeType = const_1.NodeType.FakeUser;
29
- this.privacyPolicy = {
29
+ this.data = data;
30
+ this.id = data.id;
31
+ this.createdAt = (0, convert_1.convertDate)(data.created_at);
32
+ this.updatedAt = (0, convert_1.convertDate)(data.updated_at);
33
+ this.firstName = data.first_name;
34
+ this.lastName = data.last_name;
35
+ this.emailAddress = data.email_address;
36
+ this.phoneNumber = data.phone_number;
37
+ this.password = data.password;
38
+ }
39
+ getPrivacyPolicy() {
40
+ return {
30
41
  rules: [
31
42
  privacy_1.AllowIfViewerRule,
32
43
  //can view user if friends
@@ -48,15 +59,6 @@ class FakeUser {
48
59
  privacy_1.AlwaysDenyRule,
49
60
  ],
50
61
  };
51
- this.data = data;
52
- this.id = data.id;
53
- this.createdAt = (0, convert_1.convertDate)(data.created_at);
54
- this.updatedAt = (0, convert_1.convertDate)(data.updated_at);
55
- this.firstName = data.first_name;
56
- this.lastName = data.last_name;
57
- this.emailAddress = data.email_address;
58
- this.phoneNumber = data.phone_number;
59
- this.password = data.password;
60
62
  }
61
63
  static getFields() {
62
64
  return [
@@ -89,31 +91,15 @@ class FakeUser {
89
91
  }
90
92
  }
91
93
  exports.FakeUser = FakeUser;
92
- class FakeUserSchema extends schema_1.BaseEntSchema {
93
- constructor() {
94
- super(...arguments);
95
- this.ent = FakeUser;
96
- this.fields = [
97
- (0, schema_1.StringType)({
98
- name: "firstName",
99
- }),
100
- (0, schema_1.StringType)({
101
- name: "lastName",
102
- }),
103
- (0, schema_1.StringType)({
104
- name: "emailAddress",
105
- }),
106
- (0, schema_1.StringType)({
107
- name: "phoneNumber",
108
- }),
109
- (0, schema_1.StringType)({
110
- name: "password",
111
- nullable: true,
112
- }),
113
- ];
114
- }
115
- }
116
- exports.FakeUserSchema = FakeUserSchema;
94
+ exports.FakeUserSchema = (0, builder_1.getBuilderSchemaFromFields)({
95
+ firstName: (0, schema_1.StringType)(),
96
+ lastName: (0, schema_1.StringType)(),
97
+ emailAddress: (0, schema_1.StringType)(),
98
+ phoneNumber: (0, schema_1.StringType)(),
99
+ password: (0, schema_1.StringType)({
100
+ nullable: true,
101
+ }),
102
+ }, FakeUser);
117
103
  function getUserBuilder(viewer, input) {
118
104
  const action = getUserAction(viewer, input);
119
105
  return action.builder;
@@ -124,7 +110,7 @@ function getUserAction(viewer, input) {
124
110
  for (const key in input) {
125
111
  m.set(key, input[key]);
126
112
  }
127
- const action = new builder_1.SimpleAction(viewer, new FakeUserSchema(), m);
113
+ const action = new builder_1.SimpleAction(viewer, exports.FakeUserSchema, m);
128
114
  action.viewerForEntLoad = (data) => {
129
115
  // load the created ent using a VC of the newly created user.
130
116
  return new viewer_1.IDViewer(data.id);
@@ -107,7 +107,7 @@ async function createUserPlusFriendRequests(input, slice) {
107
107
  return createTestUser(input);
108
108
  }));
109
109
  expect(friendRequests.length).toBe(userInputs.length);
110
- await addEdge(user, new _1.FakeUserSchema(), _1.EdgeType.UserToFriendRequests, true, ...friendRequests);
110
+ await addEdge(user, _1.FakeUserSchema, _1.EdgeType.UserToFriendRequests, true, ...friendRequests);
111
111
  return [user, friendRequests];
112
112
  }
113
113
  exports.createUserPlusFriendRequests = createUserPlusFriendRequests;
package/tsc/ast.d.ts ADDED
@@ -0,0 +1,21 @@
1
+ import ts from "typescript";
2
+ export declare function getPreText(fileContents: string, node: ts.Node, sourceFile: ts.SourceFile): string;
3
+ export interface ClassInfo {
4
+ extends?: string;
5
+ comment: string;
6
+ name: string;
7
+ export?: boolean;
8
+ default?: boolean;
9
+ implements?: string[];
10
+ wrapClassContents(inner: string): string;
11
+ }
12
+ export declare function getClassInfo(fileContents: string, sourceFile: ts.SourceFile, node: ts.ClassDeclaration): ClassInfo | undefined;
13
+ declare type transformImportFn = (imp: string) => string;
14
+ interface transformOpts {
15
+ removeImports?: string[];
16
+ newImports?: string[];
17
+ transform?: transformImportFn;
18
+ }
19
+ export declare function transformImport(fileContents: string, importNode: ts.ImportDeclaration, sourceFile: ts.SourceFile, opts?: transformOpts): string | undefined;
20
+ export declare function updateImportPath(fileContents: string, importNode: ts.ImportDeclaration, sourceFile: ts.SourceFile, newPath: string): string | undefined;
21
+ export {};
package/tsc/ast.js ADDED
@@ -0,0 +1,154 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.updateImportPath = exports.transformImport = exports.getClassInfo = exports.getPreText = void 0;
7
+ const typescript_1 = __importDefault(require("typescript"));
8
+ function getPreText(fileContents, node, sourceFile) {
9
+ return fileContents.substring(node.getFullStart(), node.getStart(sourceFile));
10
+ }
11
+ exports.getPreText = getPreText;
12
+ function getClassInfo(fileContents, sourceFile, node) {
13
+ let className = node.name?.text;
14
+ let classExtends;
15
+ let impl = [];
16
+ if (node.heritageClauses) {
17
+ for (const hc of node.heritageClauses) {
18
+ switch (hc.token) {
19
+ case typescript_1.default.SyntaxKind.ImplementsKeyword:
20
+ for (const type of hc.types) {
21
+ impl.push(type.expression.getText(sourceFile));
22
+ }
23
+ break;
24
+ case typescript_1.default.SyntaxKind.ExtendsKeyword:
25
+ // can only extend one class
26
+ for (const type of hc.types) {
27
+ const text = type.expression.getText(sourceFile);
28
+ classExtends = text;
29
+ }
30
+ break;
31
+ }
32
+ }
33
+ }
34
+ // we probably still don't need all of this...
35
+ if (!className || !node.heritageClauses || !classExtends) {
36
+ return undefined;
37
+ }
38
+ let hasExport = false;
39
+ let hasDefault = false;
40
+ let comment = getPreText(fileContents, node, sourceFile);
41
+ const wrapClassContents = (inner) => {
42
+ let ret = `${comment}`;
43
+ if (hasExport) {
44
+ ret += "export ";
45
+ }
46
+ if (hasDefault) {
47
+ ret += "default ";
48
+ }
49
+ ret += `class ${className} `;
50
+ if (classExtends) {
51
+ ret += `extends ${classExtends} `;
52
+ }
53
+ if (impl.length) {
54
+ ret += `implements ${impl.join(", ")}`;
55
+ }
56
+ return `${ret}{
57
+ ${inner}
58
+ }`;
59
+ };
60
+ if (node.modifiers) {
61
+ for (const mod of node.modifiers) {
62
+ const text = mod.getText(sourceFile);
63
+ if (text === "export") {
64
+ hasExport = true;
65
+ }
66
+ else if (text === "default") {
67
+ hasDefault = true;
68
+ }
69
+ }
70
+ }
71
+ return {
72
+ name: className,
73
+ extends: classExtends,
74
+ comment,
75
+ implements: impl,
76
+ wrapClassContents,
77
+ export: hasExport,
78
+ default: hasDefault,
79
+ };
80
+ }
81
+ exports.getClassInfo = getClassInfo;
82
+ function transformImport(fileContents, importNode, sourceFile, opts) {
83
+ // remove quotes too
84
+ const text = importNode.moduleSpecifier.getText(sourceFile).slice(1, -1);
85
+ if (text !== "@snowtop/ent" &&
86
+ text !== "@snowtop/ent/schema" &&
87
+ text !== "@snowtop/ent/schema/") {
88
+ return;
89
+ }
90
+ const importText = importNode.importClause?.getText(sourceFile) || "";
91
+ const start = importText.indexOf("{");
92
+ const end = importText.lastIndexOf("}");
93
+ if (start === -1 || end === -1) {
94
+ return;
95
+ }
96
+ const imports = importText
97
+ .substring(start + 1, end)
98
+ // .trim()
99
+ .split(",");
100
+ let removeImportsMap = {};
101
+ if (opts?.removeImports) {
102
+ opts.removeImports.forEach((imp) => (removeImportsMap[imp] = true));
103
+ }
104
+ let finalImports = new Set();
105
+ for (let i = 0; i < imports.length; i++) {
106
+ let imp = imports[i].trim();
107
+ if (imp === "") {
108
+ continue;
109
+ }
110
+ if (opts?.transform) {
111
+ imp = opts.transform(imp);
112
+ }
113
+ if (removeImportsMap[imp]) {
114
+ continue;
115
+ }
116
+ finalImports.add(imp);
117
+ }
118
+ if (opts?.newImports) {
119
+ opts.newImports.forEach((imp) => finalImports.add(imp));
120
+ }
121
+ const comment = getPreText(fileContents, importNode, sourceFile);
122
+ return (comment +
123
+ "import " +
124
+ importText.substring(0, start + 1) +
125
+ Array.from(finalImports).join(", ") +
126
+ importText.substring(end) +
127
+ ' from "' +
128
+ text +
129
+ '";');
130
+ }
131
+ exports.transformImport = transformImport;
132
+ function updateImportPath(fileContents, importNode, sourceFile, newPath) {
133
+ const comment = getPreText(fileContents, importNode, sourceFile);
134
+ // all this copied from above...
135
+ const importText = importNode.importClause?.getText(sourceFile) || "";
136
+ const start = importText.indexOf("{");
137
+ const end = importText.lastIndexOf("}");
138
+ if (start === -1 || end === -1) {
139
+ return;
140
+ }
141
+ const imports = importText
142
+ .substring(start + 1, end)
143
+ // .trim()
144
+ .split(",");
145
+ return (comment +
146
+ "import " +
147
+ importText.substring(0, start + 1) +
148
+ Array.from(imports).join(", ") +
149
+ importText.substring(end) +
150
+ ' from "' +
151
+ newPath +
152
+ '";');
153
+ }
154
+ exports.updateImportPath = updateImportPath;
@@ -0,0 +1,8 @@
1
+ import ts from "typescript";
2
+ export declare function readCompilerOptions(filePath: string): ts.CompilerOptions;
3
+ export declare function getTarget(target?: string): ts.ScriptTarget;
4
+ export declare function getTargetFromCurrentDir(): ts.ScriptTarget;
5
+ export declare function createSourceFile(target: ts.ScriptTarget, file: string): {
6
+ contents: string;
7
+ sourceFile: ts.SourceFile;
8
+ };
@@ -0,0 +1,100 @@
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.createSourceFile = exports.getTargetFromCurrentDir = exports.getTarget = exports.readCompilerOptions = void 0;
26
+ const fs = __importStar(require("fs"));
27
+ const json5_1 = __importDefault(require("json5"));
28
+ const typescript_1 = __importDefault(require("typescript"));
29
+ const path = __importStar(require("path"));
30
+ function findTSConfigFile(filePath) {
31
+ while (filePath != "/") {
32
+ let configPath = `${filePath}/tsconfig.json`;
33
+ if (fs.existsSync(configPath)) {
34
+ return configPath;
35
+ }
36
+ filePath = path.join(filePath, "..");
37
+ }
38
+ return null;
39
+ }
40
+ function readCompilerOptions(filePath) {
41
+ let configPath = findTSConfigFile(filePath);
42
+ if (!configPath) {
43
+ return {};
44
+ }
45
+ let json = {};
46
+ try {
47
+ json = json5_1.default.parse(fs.readFileSync(configPath, {
48
+ encoding: "utf8",
49
+ }));
50
+ }
51
+ catch (e) {
52
+ console.error("couldn't read tsconfig.json file");
53
+ }
54
+ let options = json["compilerOptions"] || {};
55
+ // @ts-ignore
56
+ if (options.moduleResolution === "node") {
57
+ options.moduleResolution = typescript_1.default.ModuleResolutionKind.NodeJs;
58
+ }
59
+ return options;
60
+ }
61
+ exports.readCompilerOptions = readCompilerOptions;
62
+ function getTarget(target) {
63
+ switch (target?.toLowerCase()) {
64
+ case "es2015":
65
+ return typescript_1.default.ScriptTarget.ES2015;
66
+ case "es2016":
67
+ return typescript_1.default.ScriptTarget.ES2016;
68
+ case "es2017":
69
+ return typescript_1.default.ScriptTarget.ES2017;
70
+ case "es2018":
71
+ return typescript_1.default.ScriptTarget.ES2018;
72
+ case "es2019":
73
+ return typescript_1.default.ScriptTarget.ES2019;
74
+ case "es2020":
75
+ return typescript_1.default.ScriptTarget.ES2020;
76
+ case "es2021":
77
+ return typescript_1.default.ScriptTarget.ES2021;
78
+ case "es3":
79
+ return typescript_1.default.ScriptTarget.ES3;
80
+ case "es5":
81
+ return typescript_1.default.ScriptTarget.ES5;
82
+ case "esnext":
83
+ return typescript_1.default.ScriptTarget.ESNext;
84
+ default:
85
+ return typescript_1.default.ScriptTarget.ESNext;
86
+ }
87
+ }
88
+ exports.getTarget = getTarget;
89
+ function getTargetFromCurrentDir() {
90
+ const options = readCompilerOptions(".");
91
+ return getTarget(options.target?.toString());
92
+ }
93
+ exports.getTargetFromCurrentDir = getTargetFromCurrentDir;
94
+ function createSourceFile(target, file) {
95
+ let contents = fs.readFileSync(file).toString();
96
+ // go through the file and print everything back if not starting immediately after other position
97
+ const sourceFile = typescript_1.default.createSourceFile(file, contents, target, false, typescript_1.default.ScriptKind.TS);
98
+ return { contents, sourceFile };
99
+ }
100
+ exports.createSourceFile = createSourceFile;