@snowtop/ent 0.1.0-alpha1 → 0.1.0-alpha13

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 (83) hide show
  1. package/action/action.d.ts +17 -13
  2. package/action/executor.d.ts +2 -2
  3. package/action/experimental_action.d.ts +4 -4
  4. package/action/experimental_action.js +1 -1
  5. package/action/orchestrator.d.ts +24 -9
  6. package/action/orchestrator.js +150 -39
  7. package/action/privacy.d.ts +2 -2
  8. package/core/base.d.ts +6 -2
  9. package/core/base.js +16 -0
  10. package/core/clause.d.ts +24 -3
  11. package/core/clause.js +246 -5
  12. package/core/config.d.ts +18 -0
  13. package/core/config.js +17 -0
  14. package/core/db.d.ts +3 -3
  15. package/core/db.js +2 -0
  16. package/core/ent.d.ts +2 -4
  17. package/core/ent.js +72 -25
  18. package/core/loaders/assoc_edge_loader.d.ts +1 -1
  19. package/core/loaders/assoc_edge_loader.js +5 -4
  20. package/core/loaders/index_loader.js +1 -0
  21. package/core/loaders/object_loader.d.ts +7 -2
  22. package/core/loaders/object_loader.js +59 -4
  23. package/core/privacy.d.ts +1 -1
  24. package/core/privacy.js +3 -0
  25. package/core/viewer.d.ts +1 -0
  26. package/core/viewer.js +4 -0
  27. package/graphql/builtins/connection.js +3 -3
  28. package/graphql/builtins/edge.js +2 -2
  29. package/graphql/builtins/node.js +1 -1
  30. package/graphql/graphql.d.ts +3 -2
  31. package/graphql/graphql.js +24 -23
  32. package/graphql/index.d.ts +1 -0
  33. package/graphql/index.js +3 -1
  34. package/graphql/mutations/union.d.ts +2 -0
  35. package/graphql/mutations/union.js +35 -0
  36. package/graphql/node_resolver.d.ts +0 -1
  37. package/graphql/query/connection_type.js +6 -6
  38. package/graphql/query/page_info.js +4 -4
  39. package/graphql/query/shared_assoc_test.js +2 -2
  40. package/graphql/scalars/time.d.ts +1 -1
  41. package/index.d.ts +16 -1
  42. package/index.js +18 -5
  43. package/package.json +3 -3
  44. package/parse_schema/parse.d.ts +21 -4
  45. package/parse_schema/parse.js +79 -8
  46. package/schema/base_schema.d.ts +36 -1
  47. package/schema/base_schema.js +48 -2
  48. package/schema/field.d.ts +1 -1
  49. package/schema/field.js +11 -3
  50. package/schema/index.d.ts +4 -2
  51. package/schema/index.js +10 -1
  52. package/schema/schema.d.ts +66 -4
  53. package/schema/schema.js +138 -5
  54. package/schema/struct_field.d.ts +17 -0
  55. package/schema/struct_field.js +102 -0
  56. package/schema/union_field.d.ts +23 -0
  57. package/schema/union_field.js +79 -0
  58. package/scripts/custom_graphql.js +122 -15
  59. package/scripts/move_generated.d.ts +1 -0
  60. package/scripts/move_generated.js +142 -0
  61. package/scripts/read_schema.js +15 -1
  62. package/scripts/transform_code.d.ts +1 -0
  63. package/scripts/transform_code.js +113 -0
  64. package/scripts/transform_schema.js +190 -123
  65. package/testutils/builder.d.ts +27 -18
  66. package/testutils/builder.js +82 -10
  67. package/testutils/context/test_context.d.ts +2 -2
  68. package/testutils/context/test_context.js +7 -1
  69. package/testutils/db/test_db.d.ts +2 -1
  70. package/testutils/db/test_db.js +13 -4
  71. package/testutils/ent-graphql-tests/index.d.ts +2 -0
  72. package/testutils/ent-graphql-tests/index.js +26 -17
  73. package/testutils/fake_data/fake_contact.d.ts +4 -8
  74. package/testutils/fake_data/fake_contact.js +15 -19
  75. package/testutils/fake_data/fake_event.d.ts +4 -8
  76. package/testutils/fake_data/fake_event.js +21 -25
  77. package/testutils/fake_data/fake_user.d.ts +5 -9
  78. package/testutils/fake_data/fake_user.js +23 -27
  79. package/testutils/fake_data/test_helpers.js +1 -1
  80. package/tsc/ast.d.ts +21 -0
  81. package/tsc/ast.js +154 -0
  82. package/tsc/compilerOptions.d.ts +6 -0
  83. package/tsc/compilerOptions.js +40 -1
@@ -9,11 +9,11 @@ const const_1 = require("./const");
9
9
  const test_db_1 = require("../db/test_db");
10
10
  const loaders_1 = require("../../core/loaders");
11
11
  const convert_1 = require("../../core/convert");
12
+ const action_1 = require("../../action");
12
13
  class FakeEvent {
13
14
  constructor(viewer, data) {
14
15
  this.viewer = viewer;
15
16
  this.nodeType = const_1.NodeType.FakeEvent;
16
- this.privacyPolicy = privacy_1.AlwaysAllowPrivacyPolicy;
17
17
  this.data = data;
18
18
  this.id = data.id;
19
19
  this.createdAt = (0, convert_1.convertDate)(data.created_at);
@@ -25,6 +25,9 @@ class FakeEvent {
25
25
  this.description = data.description;
26
26
  this.userID = data.user_id;
27
27
  }
28
+ getPrivacyPolicy() {
29
+ return privacy_1.AlwaysAllowPrivacyPolicy;
30
+ }
28
31
  static getFields() {
29
32
  return [
30
33
  "id",
@@ -63,35 +66,28 @@ class FakeEvent {
63
66
  }
64
67
  }
65
68
  exports.FakeEvent = FakeEvent;
66
- class FakeEventSchema extends schema_1.BaseEntSchema {
67
- constructor() {
68
- super(...arguments);
69
- this.ent = FakeEvent;
70
- this.fields = {
71
- startTime: (0, schema_1.TimestampType)({
72
- index: true,
73
- }),
74
- endTime: (0, schema_1.TimestampType)({
75
- nullable: true,
76
- }),
77
- title: (0, schema_1.StringType)(),
78
- location: (0, schema_1.StringType)(),
79
- description: (0, schema_1.StringType)({
80
- nullable: true,
81
- }),
82
- userID: (0, schema_1.UUIDType)({
83
- foreignKey: { schema: "User", column: "ID" },
84
- }),
85
- };
86
- }
87
- }
88
- exports.FakeEventSchema = FakeEventSchema;
69
+ exports.FakeEventSchema = (0, builder_1.getBuilderSchemaFromFields)({
70
+ startTime: (0, schema_1.TimestampType)({
71
+ index: true,
72
+ }),
73
+ endTime: (0, schema_1.TimestampType)({
74
+ nullable: true,
75
+ }),
76
+ title: (0, schema_1.StringType)(),
77
+ location: (0, schema_1.StringType)(),
78
+ description: (0, schema_1.StringType)({
79
+ nullable: true,
80
+ }),
81
+ userID: (0, schema_1.UUIDType)({
82
+ foreignKey: { schema: "User", column: "ID" },
83
+ }),
84
+ }, FakeEvent);
89
85
  function getEventBuilder(viewer, input) {
90
86
  const m = new Map();
91
87
  for (const key in input) {
92
88
  m.set(key, input[key]);
93
89
  }
94
- return new builder_1.SimpleBuilder(viewer, new FakeEventSchema(), m);
90
+ return new builder_1.SimpleBuilder(viewer, exports.FakeEventSchema, m, action_1.WriteOperation.Insert, null);
95
91
  }
96
92
  exports.getEventBuilder = getEventBuilder;
97
93
  async function createEvent(viewer, input) {
@@ -1,6 +1,5 @@
1
1
  import { ID, Ent, Viewer, Data, LoadEntOptions, PrivacyPolicy } from "../../core/base";
2
- import { BuilderSchema, SimpleAction } from "../builder";
3
- import { BaseEntSchema, FieldMap } from "../../schema";
2
+ import { SimpleAction } from "../builder";
4
3
  import { NodeType } from "./const";
5
4
  import { IDViewer, IDViewerOptions } from "../../core/viewer";
6
5
  import { ObjectLoaderFactory } from "../../core/loaders";
@@ -24,7 +23,7 @@ export declare class FakeUser implements Ent {
24
23
  readonly emailAddress: string;
25
24
  readonly phoneNumber: string | null;
26
25
  protected readonly password: string | null;
27
- privacyPolicy: PrivacyPolicy;
26
+ getPrivacyPolicy(): PrivacyPolicy<this>;
28
27
  constructor(viewer: Viewer, data: Data);
29
28
  static getFields(): string[];
30
29
  static getTestTable(): import("../db/test_db").Table;
@@ -32,10 +31,7 @@ export declare class FakeUser implements Ent {
32
31
  static load(v: Viewer, id: ID): Promise<FakeUser | null>;
33
32
  static loadX(v: Viewer, id: ID): Promise<FakeUser>;
34
33
  }
35
- export declare class FakeUserSchema extends BaseEntSchema implements BuilderSchema<FakeUser> {
36
- ent: typeof FakeUser;
37
- fields: FieldMap;
38
- }
34
+ export declare const FakeUserSchema: import("../builder").BuilderSchema<FakeUser>;
39
35
  export interface UserCreateInput {
40
36
  firstName: string;
41
37
  lastName: string;
@@ -44,8 +40,8 @@ export interface UserCreateInput {
44
40
  password: string | null;
45
41
  }
46
42
  export declare type UserEditInput = Partial<UserCreateInput>;
47
- export declare function getUserBuilder(viewer: Viewer, input: UserCreateInput): import("../builder").SimpleBuilder<FakeUser>;
48
- export declare function getUserAction(viewer: Viewer, input: UserCreateInput): SimpleAction<FakeUser>;
43
+ export declare function getUserBuilder(viewer: Viewer, input: UserCreateInput): import("../builder").SimpleBuilder<FakeUser, null>;
44
+ export declare function getUserAction(viewer: Viewer, input: UserCreateInput): SimpleAction<FakeUser, null>;
49
45
  export declare function createUser(viewer: Viewer, input: UserCreateInput): Promise<FakeUser>;
50
46
  export declare const userLoader: ObjectLoaderFactory<unknown>;
51
47
  export declare const userEmailLoader: ObjectLoaderFactory<unknown>;
@@ -11,6 +11,7 @@ const viewer_1 = require("../../core/viewer");
11
11
  const test_db_1 = require("../db/test_db");
12
12
  const loaders_1 = require("../../core/loaders");
13
13
  const convert_1 = require("../../core/convert");
14
+ const action_1 = require("../../action");
14
15
  class ViewerWithAccessToken extends viewer_1.IDViewer {
15
16
  constructor(viewerID, opts) {
16
17
  super(viewerID, opts);
@@ -26,7 +27,18 @@ class FakeUser {
26
27
  constructor(viewer, data) {
27
28
  this.viewer = viewer;
28
29
  this.nodeType = const_1.NodeType.FakeUser;
29
- this.privacyPolicy = {
30
+ this.data = data;
31
+ this.id = data.id;
32
+ this.createdAt = (0, convert_1.convertDate)(data.created_at);
33
+ this.updatedAt = (0, convert_1.convertDate)(data.updated_at);
34
+ this.firstName = data.first_name;
35
+ this.lastName = data.last_name;
36
+ this.emailAddress = data.email_address;
37
+ this.phoneNumber = data.phone_number;
38
+ this.password = data.password;
39
+ }
40
+ getPrivacyPolicy() {
41
+ return {
30
42
  rules: [
31
43
  privacy_1.AllowIfViewerRule,
32
44
  //can view user if friends
@@ -48,15 +60,6 @@ class FakeUser {
48
60
  privacy_1.AlwaysDenyRule,
49
61
  ],
50
62
  };
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
63
  }
61
64
  static getFields() {
62
65
  return [
@@ -89,22 +92,15 @@ class FakeUser {
89
92
  }
90
93
  }
91
94
  exports.FakeUser = FakeUser;
92
- class FakeUserSchema extends schema_1.BaseEntSchema {
93
- constructor() {
94
- super(...arguments);
95
- this.ent = FakeUser;
96
- this.fields = {
97
- firstName: (0, schema_1.StringType)(),
98
- lastName: (0, schema_1.StringType)(),
99
- emailAddress: (0, schema_1.StringType)(),
100
- phoneNumber: (0, schema_1.StringType)(),
101
- password: (0, schema_1.StringType)({
102
- nullable: true,
103
- }),
104
- };
105
- }
106
- }
107
- exports.FakeUserSchema = FakeUserSchema;
95
+ exports.FakeUserSchema = (0, builder_1.getBuilderSchemaFromFields)({
96
+ firstName: (0, schema_1.StringType)(),
97
+ lastName: (0, schema_1.StringType)(),
98
+ emailAddress: (0, schema_1.StringType)(),
99
+ phoneNumber: (0, schema_1.StringType)(),
100
+ password: (0, schema_1.StringType)({
101
+ nullable: true,
102
+ }),
103
+ }, FakeUser);
108
104
  function getUserBuilder(viewer, input) {
109
105
  const action = getUserAction(viewer, input);
110
106
  return action.builder;
@@ -115,7 +111,7 @@ function getUserAction(viewer, input) {
115
111
  for (const key in input) {
116
112
  m.set(key, input[key]);
117
113
  }
118
- const action = new builder_1.SimpleAction(viewer, new FakeUserSchema(), m);
114
+ const action = new builder_1.SimpleAction(viewer, exports.FakeUserSchema, m, action_1.WriteOperation.Insert, null);
119
115
  action.viewerForEntLoad = (data) => {
120
116
  // load the created ent using a VC of the newly created user.
121
117
  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;
@@ -1,2 +1,8 @@
1
1
  import ts from "typescript";
2
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
+ };
@@ -22,7 +22,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
22
22
  return (mod && mod.__esModule) ? mod : { "default": mod };
23
23
  };
24
24
  Object.defineProperty(exports, "__esModule", { value: true });
25
- exports.readCompilerOptions = void 0;
25
+ exports.createSourceFile = exports.getTargetFromCurrentDir = exports.getTarget = exports.readCompilerOptions = void 0;
26
26
  const fs = __importStar(require("fs"));
27
27
  const json5_1 = __importDefault(require("json5"));
28
28
  const typescript_1 = __importDefault(require("typescript"));
@@ -59,3 +59,42 @@ function readCompilerOptions(filePath) {
59
59
  return options;
60
60
  }
61
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;