@snowtop/ent 0.1.0-alpha1 → 0.1.0-alpha10

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 (78) hide show
  1. package/action/action.d.ts +2 -0
  2. package/action/executor.d.ts +1 -1
  3. package/action/orchestrator.d.ts +10 -2
  4. package/action/orchestrator.js +128 -34
  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/index.d.ts +16 -1
  39. package/index.js +18 -5
  40. package/package.json +3 -3
  41. package/parse_schema/parse.d.ts +21 -4
  42. package/parse_schema/parse.js +79 -8
  43. package/schema/base_schema.d.ts +36 -1
  44. package/schema/base_schema.js +48 -2
  45. package/schema/field.d.ts +1 -1
  46. package/schema/field.js +11 -3
  47. package/schema/index.d.ts +4 -2
  48. package/schema/index.js +10 -1
  49. package/schema/schema.d.ts +56 -4
  50. package/schema/schema.js +126 -5
  51. package/schema/struct_field.d.ts +17 -0
  52. package/schema/struct_field.js +102 -0
  53. package/schema/union_field.d.ts +23 -0
  54. package/schema/union_field.js +79 -0
  55. package/scripts/custom_graphql.js +122 -15
  56. package/scripts/read_schema.js +15 -1
  57. package/scripts/transform_code.d.ts +1 -0
  58. package/scripts/transform_code.js +114 -0
  59. package/scripts/transform_schema.js +190 -121
  60. package/testutils/builder.d.ts +13 -9
  61. package/testutils/builder.js +64 -8
  62. package/testutils/context/test_context.d.ts +2 -2
  63. package/testutils/context/test_context.js +7 -1
  64. package/testutils/db/test_db.d.ts +2 -1
  65. package/testutils/db/test_db.js +13 -4
  66. package/testutils/ent-graphql-tests/index.d.ts +2 -0
  67. package/testutils/ent-graphql-tests/index.js +26 -17
  68. package/testutils/fake_data/fake_contact.d.ts +3 -7
  69. package/testutils/fake_data/fake_contact.js +14 -19
  70. package/testutils/fake_data/fake_event.d.ts +3 -7
  71. package/testutils/fake_data/fake_event.js +20 -25
  72. package/testutils/fake_data/fake_user.d.ts +3 -7
  73. package/testutils/fake_data/fake_user.js +22 -27
  74. package/testutils/fake_data/test_helpers.js +1 -1
  75. package/tsc/ast.d.ts +20 -0
  76. package/tsc/ast.js +131 -0
  77. package/tsc/compilerOptions.d.ts +5 -0
  78. package/tsc/compilerOptions.js +35 -1
@@ -27,51 +27,28 @@ const typescript_1 = __importDefault(require("typescript"));
27
27
  const fs = __importStar(require("fs"));
28
28
  const compilerOptions_1 = require("../tsc/compilerOptions");
29
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
- }
30
+ const path_1 = __importDefault(require("path"));
31
+ const ast_1 = require("../tsc/ast");
56
32
  async function main() {
57
33
  // this assumes this is being run from root of directory
58
34
  const options = (0, compilerOptions_1.readCompilerOptions)(".");
59
35
  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;
36
+ const target = (0, compilerOptions_1.getTarget)(options.target?.toString());
64
37
  // filter to only event.ts e.g. for comments and whitespace...
65
38
  // files = files.filter((f) => f.endsWith("event.ts"));
66
39
  files.forEach((file) => {
67
40
  // assume valid file since we do glob above
68
- const idx = file.lastIndexOf(".ts");
69
- const writeFile = file.substring(0, idx) + "2" + ".ts";
70
- let contents = fs.readFileSync(file).toString();
41
+ // const idx = file.lastIndexOf(".ts");
42
+ // const writeFile = file.substring(0, idx) + "2" + ".ts";
43
+ // console.debug(file);
44
+ const writeFile = "src/schema/" + path_1.default.basename(file).slice(0, -3) + "_schema.ts";
45
+ // const writeFile = file;
46
+ // console.debug(file, writeFile);
71
47
  // go through the file and print everything back if not starting immediately after other position
72
- const sourceFile = typescript_1.default.createSourceFile(file, contents, target, false, typescript_1.default.ScriptKind.TS);
48
+ let { contents, sourceFile } = (0, compilerOptions_1.createSourceFile)(target, file);
73
49
  const nodes = [];
74
50
  let updateImport = false;
51
+ let removeImports = [];
75
52
  const f = {
76
53
  trackNode: function (tni) {
77
54
  nodes.push({
@@ -79,6 +56,9 @@ async function main() {
79
56
  importNode: tni.node && typescript_1.default.isImportDeclaration(tni.node),
80
57
  rawString: tni.rawString,
81
58
  });
59
+ if (tni.removeImports) {
60
+ removeImports.push(...tni.removeImports);
61
+ }
82
62
  },
83
63
  flagUpdateImport() {
84
64
  updateImport = true;
@@ -91,7 +71,10 @@ async function main() {
91
71
  for (const node of nodes) {
92
72
  if (updateImport && node.importNode) {
93
73
  const importNode = node.node;
94
- const transformedImport = transformImport(importNode, sourceFile);
74
+ const transformedImport = (0, ast_1.transformImport)(contents, importNode, sourceFile, {
75
+ removeImports,
76
+ transform: transformSchema,
77
+ });
95
78
  if (transformedImport) {
96
79
  newContents += transformedImport;
97
80
  continue;
@@ -110,6 +93,7 @@ async function main() {
110
93
  // console.debug(newContents);
111
94
  // ideally there's a flag that indicates if we write
112
95
  fs.writeFileSync(writeFile, newContents);
96
+ fs.rmSync(file);
113
97
  });
114
98
  (0, child_process_1.execSync)("prettier src/schema/*.ts --write");
115
99
  }
@@ -129,62 +113,183 @@ function traverse(fileContents, sourceFile, f) {
129
113
  });
130
114
  return traversed;
131
115
  }
132
- // TODO need to replace class field member, print that and see what happens
133
116
  function traverseClass(fileContents, sourceFile, node, f) {
134
- let updated = false;
135
- // beginning of class...
136
- // including comment
137
- let klassContents = fileContents.substring(node.getFullStart(), node.members[0].getFullStart());
117
+ const ci = getTransformClassInfo(fileContents, sourceFile, node);
118
+ if (!ci) {
119
+ return false;
120
+ }
121
+ let klassContents = `${ci.comment}const ${ci.name} = new ${ci.class}({\n`;
122
+ let removeImports = [];
123
+ if (ci.implementsSchema) {
124
+ removeImports.push("Schema");
125
+ }
138
126
  for (let member of node.members) {
139
- if (!isFieldElement(member, sourceFile)) {
140
- klassContents += member.getFullText(sourceFile);
141
- continue;
142
- }
143
- // intentionally doesn't parse decorators since we don't need it
144
- let fieldMap = "";
145
- // fieldMapComment...
146
- const comment = getPreText(fileContents, member, sourceFile);
147
- if (comment) {
148
- fieldMap += comment;
127
+ const fInfo = getClassElementInfo(fileContents, member, sourceFile);
128
+ if (!fInfo) {
129
+ return false;
149
130
  }
150
- updated = true;
151
- // need to change to fields: FieldMap = {code: StringType()};
152
- const property = member;
153
- const initializer = property.initializer;
154
- fieldMap += "fields: FieldMap = {";
155
- for (const element of initializer.elements) {
156
- const parsed = parseFieldElement(element, sourceFile, fileContents);
157
- if (parsed === null) {
158
- return false;
159
- }
160
- const { callEx, name, nameComment, properties } = parsed;
161
- let property = "";
162
- const fieldComment = getPreText(fileContents, element, sourceFile).trim();
163
- if (fieldComment) {
164
- property += "\n" + fieldComment + "\n";
165
- }
166
- if (nameComment) {
167
- property += nameComment + "\n";
168
- }
169
- // e.g. UUIDType, StringType etc
170
- let call = callEx.expression.getText(sourceFile);
171
- let fnCall = "";
172
- if (properties.length) {
173
- fnCall = `{${properties.join(",")}}`;
174
- }
175
- property += `${name}:${call}(${fnCall}),`;
176
- fieldMap += property;
131
+ klassContents += `${fInfo.comment}${fInfo.key}:${fInfo.value},\n`;
132
+ if (fInfo.type) {
133
+ removeImports.push(fInfo.type);
177
134
  }
178
- fieldMap += "}";
179
- klassContents += fieldMap;
180
135
  }
181
- klassContents += "\n}";
136
+ klassContents += "\n})";
137
+ if (ci.export && ci.default) {
138
+ klassContents += `\n export default ${ci.name};`;
139
+ }
140
+ else if (ci.export) {
141
+ klassContents = "export " + klassContents;
142
+ }
182
143
  // console.debug(klassContents);
183
- if (!updated) {
184
- return updated;
144
+ f.trackNode({ rawString: klassContents, removeImports: removeImports });
145
+ return true;
146
+ }
147
+ function transformSchema(str) {
148
+ // only do known class names
149
+ if (str === "BaseEntSchema" || str === "BaseEntSchemaWithTZ") {
150
+ return str.substring(4);
151
+ }
152
+ return str;
153
+ }
154
+ // TODO need to generify this...
155
+ // and then have a schema specific version
156
+ // may make sense to just duplicate this logic...
157
+ function getTransformClassInfo(fileContents, sourceFile, node) {
158
+ const generic = (0, ast_1.getClassInfo)(fileContents, sourceFile, node);
159
+ if (!generic) {
160
+ return;
161
+ }
162
+ let className = generic.name;
163
+ if (!className?.endsWith("Schema")) {
164
+ className += "Schema";
165
+ }
166
+ let implementsSchema = generic.implements?.some((v) => v == "Schema");
167
+ let classExtends = generic.extends;
168
+ if (classExtends && classExtends === transformSchema(classExtends)) {
169
+ return undefined;
170
+ }
171
+ if (!className || !node.heritageClauses || !classExtends) {
172
+ return undefined;
173
+ }
174
+ let ci = {
175
+ ...generic,
176
+ name: className,
177
+ class: classExtends,
178
+ implementsSchema,
179
+ };
180
+ return ci;
181
+ }
182
+ // intentionally doesn't parse decorators since we don't need it
183
+ function getClassElementInfo(fileContents, member, sourceFile) {
184
+ if (isFieldElement(member, sourceFile)) {
185
+ return getFieldElementInfo(fileContents, member, sourceFile);
186
+ }
187
+ if (member.kind === typescript_1.default.SyntaxKind.Constructor) {
188
+ return getConstructorElementInfo(fileContents, member, sourceFile);
189
+ }
190
+ if (member.kind !== typescript_1.default.SyntaxKind.PropertyDeclaration) {
191
+ return;
192
+ }
193
+ // other properties
194
+ const property = member;
195
+ if (!property.initializer) {
196
+ return;
185
197
  }
186
- f.trackNode({ rawString: klassContents });
187
- return updated;
198
+ const token = property.name;
199
+ return {
200
+ key: token.escapedText.toString(),
201
+ value: property.initializer?.getFullText(sourceFile),
202
+ comment: (0, ast_1.getPreText)(fileContents, member, sourceFile),
203
+ type: getType(property, sourceFile),
204
+ };
205
+ }
206
+ function getType(property, sourceFile) {
207
+ let propertytype = property.type?.getText(sourceFile) || "";
208
+ let ends = ["| null", "[]"];
209
+ for (const end of ends) {
210
+ if (propertytype.endsWith(end)) {
211
+ propertytype = propertytype.slice(0, -1 * end.length);
212
+ }
213
+ }
214
+ return propertytype;
215
+ }
216
+ function getFieldElementInfo(fileContents, member, sourceFile) {
217
+ let fieldMap = "";
218
+ // need to change to fields: {code: StringType()};
219
+ const property = member;
220
+ const initializer = property.initializer;
221
+ fieldMap += "{";
222
+ for (const element of initializer.elements) {
223
+ const parsed = parseFieldElement(element, sourceFile, fileContents);
224
+ if (parsed === null) {
225
+ return;
226
+ }
227
+ const { callEx, name, nameComment, properties } = parsed;
228
+ let property = "";
229
+ const fieldComment = (0, ast_1.getPreText)(fileContents, element, sourceFile).trim();
230
+ if (fieldComment) {
231
+ property += "\n" + fieldComment + "\n";
232
+ }
233
+ if (nameComment) {
234
+ property += nameComment + "\n";
235
+ }
236
+ // e.g. UUIDType, StringType etc
237
+ let call = callEx.expression.getText(sourceFile);
238
+ let fnCall = "";
239
+ if (properties.length) {
240
+ fnCall = `{${properties.join(",")}}`;
241
+ }
242
+ property += `${name}:${call}(${fnCall}),`;
243
+ fieldMap += property;
244
+ }
245
+ fieldMap += "}";
246
+ return {
247
+ key: "fields",
248
+ value: fieldMap,
249
+ comment: (0, ast_1.getPreText)(fileContents, member, sourceFile),
250
+ type: getType(property, sourceFile),
251
+ };
252
+ }
253
+ function getConstructorElementInfo(fileContents, member, sourceFile) {
254
+ const c = member;
255
+ //remove {}
256
+ let fullText = c.body?.getFullText(sourceFile) || "";
257
+ fullText = fullText.trim().slice(1, -1).trim();
258
+ // convert something like
259
+ /*
260
+ constructor() {
261
+ super();
262
+ this.addPatterns(
263
+ new Feedback(),
264
+ new DayOfWeek(),
265
+ new Feedback(),
266
+ new DayOfWeek(),
267
+ );
268
+ }
269
+ */
270
+ // into this.addPatterns(new Feedback(),new DayOfWeek(),new Feedback(),new DayOfWeek(),)
271
+ const lines = fullText
272
+ .split("\n")
273
+ .map((line) => line.trim())
274
+ .join("")
275
+ .split(";")
276
+ .filter((f) => f != "super()" && f != "");
277
+ // at this point there should be only line for what we handle
278
+ if (lines.length != 1) {
279
+ return;
280
+ }
281
+ const line = lines[0];
282
+ const addPatterns = "this.addPatterns(";
283
+ if (!line.startsWith(addPatterns)) {
284
+ return;
285
+ }
286
+ return {
287
+ key: "patterns",
288
+ // remove this.addPatterns at the front, remove trailing ) at the end
289
+ // if there's a trailing comma, it'll be handled by prettier
290
+ value: `[${line.slice(addPatterns.length, -1)}]`,
291
+ comment: "",
292
+ };
188
293
  }
189
294
  function isFieldElement(member, sourceFile) {
190
295
  if (member.kind !== typescript_1.default.SyntaxKind.PropertyDeclaration) {
@@ -230,7 +335,7 @@ function parseFieldElement(element, sourceFile, fileContents) {
230
335
  if (p2.name.escapedText === "name") {
231
336
  name = p2.initializer.getText(sourceFile);
232
337
  // check for any comment associated with name: "fooo"
233
- propertyComment = getPreText(fileContents, p, sourceFile).trim();
338
+ propertyComment = (0, ast_1.getPreText)(fileContents, p, sourceFile).trim();
234
339
  }
235
340
  else {
236
341
  properties.push(p.getFullText(sourceFile));
@@ -249,40 +354,4 @@ function parseFieldElement(element, sourceFile, fileContents) {
249
354
  nameComment: propertyComment,
250
355
  };
251
356
  }
252
- function transformImport(importNode, sourceFile) {
253
- // remove quotes too
254
- const text = importNode.moduleSpecifier.getText(sourceFile).slice(1, -1);
255
- if (text !== "@snowtop/ent" &&
256
- text !== "@snowtop/ent/schema" &&
257
- text !== "@snowtop/ent/schema/") {
258
- return;
259
- }
260
- const importText = importNode.importClause?.getText(sourceFile) || "";
261
- const start = importText.indexOf("{");
262
- const end = importText.lastIndexOf("}");
263
- if (start === -1 || end === -1) {
264
- return;
265
- }
266
- const imports = importText
267
- .substring(start + 1, end)
268
- // .trim()
269
- .split(",");
270
- for (let i = 0; i < imports.length; i++) {
271
- const imp = imports[i].trim();
272
- if (imp === "Field") {
273
- imports[i] = "FieldMap";
274
- }
275
- }
276
- // TODO better to update node instead of doing this but this works for now
277
- return ("import " +
278
- importText.substring(0, start + 1) +
279
- imports.join(", ") +
280
- importText.substring(end) +
281
- ' from "' +
282
- text +
283
- '"');
284
- }
285
- function getPreText(fileContents, node, sourceFile) {
286
- return fileContents.substring(node.getFullStart(), node.getStart(sourceFile));
287
- }
288
357
  Promise.resolve(main());
@@ -1,14 +1,15 @@
1
- import { Ent, ID, Viewer, Data, EntConstructor } from "../core/base";
1
+ import { Ent, ID, Viewer, Data, EntConstructor, PrivacyPolicy } from "../core/base";
2
2
  import { Orchestrator } from "../action/orchestrator";
3
3
  import { Action, Builder, Changeset, WriteOperation, Validator, Trigger, Observer } from "../action";
4
- import { Schema } from "../schema";
4
+ import { FieldMap, Schema } from "../schema";
5
+ import { SchemaConfig } from "../schema/base_schema";
5
6
  export declare class User implements Ent {
6
7
  viewer: Viewer;
7
8
  data: Data;
8
9
  id: ID;
9
10
  accountID: string;
10
11
  nodeType: string;
11
- privacyPolicy: import("../core/base").PrivacyPolicy<Ent>;
12
+ getPrivacyPolicy(): PrivacyPolicy<this>;
12
13
  firstName: string;
13
14
  constructor(viewer: Viewer, data: Data);
14
15
  }
@@ -18,7 +19,7 @@ export declare class Event implements Ent {
18
19
  id: ID;
19
20
  accountID: string;
20
21
  nodeType: string;
21
- privacyPolicy: import("../core/base").PrivacyPolicy<Ent>;
22
+ getPrivacyPolicy(): PrivacyPolicy<this>;
22
23
  constructor(viewer: Viewer, data: Data);
23
24
  }
24
25
  export declare class Contact implements Ent {
@@ -27,7 +28,7 @@ export declare class Contact implements Ent {
27
28
  id: ID;
28
29
  accountID: string;
29
30
  nodeType: string;
30
- privacyPolicy: import("../core/base").PrivacyPolicy<Ent>;
31
+ getPrivacyPolicy(): PrivacyPolicy<this>;
31
32
  constructor(viewer: Viewer, data: Data);
32
33
  }
33
34
  export declare class Group implements Ent {
@@ -36,7 +37,7 @@ export declare class Group implements Ent {
36
37
  id: ID;
37
38
  accountID: string;
38
39
  nodeType: string;
39
- privacyPolicy: import("../core/base").PrivacyPolicy<Ent>;
40
+ getPrivacyPolicy(): PrivacyPolicy<this>;
40
41
  constructor(viewer: Viewer, data: Data);
41
42
  }
42
43
  export declare class Message implements Ent {
@@ -45,7 +46,7 @@ export declare class Message implements Ent {
45
46
  id: ID;
46
47
  accountID: string;
47
48
  nodeType: string;
48
- privacyPolicy: import("../core/base").PrivacyPolicy<Ent>;
49
+ getPrivacyPolicy(): PrivacyPolicy<this>;
49
50
  constructor(viewer: Viewer, data: Data);
50
51
  }
51
52
  export declare class Address implements Ent {
@@ -54,12 +55,15 @@ export declare class Address implements Ent {
54
55
  id: ID;
55
56
  accountID: string;
56
57
  nodeType: string;
57
- privacyPolicy: import("../core/base").PrivacyPolicy<Ent>;
58
+ getPrivacyPolicy(): PrivacyPolicy<this>;
58
59
  constructor(viewer: Viewer, data: Data);
59
60
  }
60
61
  export interface BuilderSchema<T extends Ent> extends Schema {
61
62
  ent: EntConstructor<T>;
62
63
  }
64
+ export declare function getBuilderSchema<T extends Ent>(cfg: SchemaConfig, ent: EntConstructor<T>): BuilderSchema<T>;
65
+ export declare function getBuilderSchemaFromFields<T extends Ent>(fields: FieldMap, ent: EntConstructor<T>): BuilderSchema<T>;
66
+ export declare function getBuilderSchemaTZFromFields<T extends Ent>(fields: FieldMap, ent: EntConstructor<T>): BuilderSchema<T>;
63
67
  export declare function getSchemaName(value: BuilderSchema<Ent>): string;
64
68
  export declare function getTableName(value: BuilderSchema<Ent>): string;
65
69
  export declare class SimpleBuilder<T extends Ent> implements Builder<T> {
@@ -93,7 +97,7 @@ export declare class SimpleAction<T extends Ent> implements Action<T, SimpleBuil
93
97
  observers: Observer<SimpleBuilder<T>, Data>[];
94
98
  viewerForEntLoad: viewerEntLoadFunc | undefined;
95
99
  constructor(viewer: Viewer, schema: BuilderSchema<T>, fields: Map<string, any>, operation?: WriteOperation, existingEnt?: T | undefined);
96
- getPrivacyPolicy(): import("../core/base").PrivacyPolicy<Ent>;
100
+ getPrivacyPolicy(): PrivacyPolicy<Ent>;
97
101
  getInput(): Data;
98
102
  changeset(): Promise<Changeset<T>>;
99
103
  valid(): Promise<boolean>;
@@ -3,7 +3,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
3
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.SimpleAction = exports.SimpleBuilder = exports.getTableName = exports.getSchemaName = exports.Address = exports.Message = exports.Group = exports.Contact = exports.Event = exports.User = void 0;
6
+ exports.SimpleAction = exports.SimpleBuilder = exports.getTableName = exports.getSchemaName = exports.getBuilderSchemaTZFromFields = exports.getBuilderSchemaFromFields = exports.getBuilderSchema = exports.Address = exports.Message = exports.Group = exports.Contact = exports.Event = exports.User = void 0;
7
7
  const privacy_1 = require("../core/privacy");
8
8
  const orchestrator_1 = require("../action/orchestrator");
9
9
  const action_1 = require("../action");
@@ -14,18 +14,21 @@ const snake_case_1 = require("snake-case");
14
14
  const loaders_1 = require("../core/loaders");
15
15
  const convert_1 = require("../core/convert");
16
16
  const camel_case_1 = require("camel-case");
17
+ const base_schema_1 = require("../schema/base_schema");
17
18
  class User {
18
19
  constructor(viewer, data) {
19
20
  this.viewer = viewer;
20
21
  this.data = data;
21
22
  this.accountID = "";
22
23
  this.nodeType = "User";
23
- this.privacyPolicy = privacy_1.AlwaysAllowPrivacyPolicy;
24
24
  this.data.created_at = (0, convert_1.convertDate)(data.created_at);
25
25
  this.data.updated_at = (0, convert_1.convertDate)(data.updated_at);
26
26
  this.id = data.id;
27
27
  this.firstName = data.first_name;
28
28
  }
29
+ getPrivacyPolicy() {
30
+ return privacy_1.AlwaysAllowPrivacyPolicy;
31
+ }
29
32
  }
30
33
  exports.User = User;
31
34
  class Event {
@@ -34,9 +37,11 @@ class Event {
34
37
  this.data = data;
35
38
  this.accountID = "";
36
39
  this.nodeType = "Event";
37
- this.privacyPolicy = privacy_1.AlwaysAllowPrivacyPolicy;
38
40
  this.id = data.id;
39
41
  }
42
+ getPrivacyPolicy() {
43
+ return privacy_1.AlwaysAllowPrivacyPolicy;
44
+ }
40
45
  }
41
46
  exports.Event = Event;
42
47
  class Contact {
@@ -45,11 +50,13 @@ class Contact {
45
50
  this.data = data;
46
51
  this.accountID = "";
47
52
  this.nodeType = "Contact";
48
- this.privacyPolicy = privacy_1.AlwaysAllowPrivacyPolicy;
49
53
  this.data.created_at = (0, convert_1.convertDate)(data.created_at);
50
54
  this.data.updated_at = (0, convert_1.convertDate)(data.updated_at);
51
55
  this.id = data.id;
52
56
  }
57
+ getPrivacyPolicy() {
58
+ return privacy_1.AlwaysAllowPrivacyPolicy;
59
+ }
53
60
  }
54
61
  exports.Contact = Contact;
55
62
  class Group {
@@ -58,9 +65,11 @@ class Group {
58
65
  this.data = data;
59
66
  this.accountID = "";
60
67
  this.nodeType = "Group";
61
- this.privacyPolicy = privacy_1.AlwaysAllowPrivacyPolicy;
62
68
  this.id = data.id;
63
69
  }
70
+ getPrivacyPolicy() {
71
+ return privacy_1.AlwaysAllowPrivacyPolicy;
72
+ }
64
73
  }
65
74
  exports.Group = Group;
66
75
  class Message {
@@ -69,9 +78,11 @@ class Message {
69
78
  this.data = data;
70
79
  this.accountID = "";
71
80
  this.nodeType = "Message";
72
- this.privacyPolicy = privacy_1.AlwaysAllowPrivacyPolicy;
73
81
  this.id = data.id;
74
82
  }
83
+ getPrivacyPolicy() {
84
+ return privacy_1.AlwaysAllowPrivacyPolicy;
85
+ }
75
86
  }
76
87
  exports.Message = Message;
77
88
  class Address {
@@ -80,11 +91,34 @@ class Address {
80
91
  this.data = data;
81
92
  this.accountID = "";
82
93
  this.nodeType = "Address";
83
- this.privacyPolicy = privacy_1.AlwaysAllowPrivacyPolicy;
84
94
  this.id = data.id;
85
95
  }
96
+ getPrivacyPolicy() {
97
+ return privacy_1.AlwaysAllowPrivacyPolicy;
98
+ }
86
99
  }
87
100
  exports.Address = Address;
101
+ function getBuilderSchema(cfg, ent) {
102
+ return {
103
+ ...new base_schema_1.EntSchema(cfg),
104
+ ent,
105
+ };
106
+ }
107
+ exports.getBuilderSchema = getBuilderSchema;
108
+ function getBuilderSchemaFromFields(fields, ent) {
109
+ return {
110
+ ...new base_schema_1.EntSchema({ fields }),
111
+ ent,
112
+ };
113
+ }
114
+ exports.getBuilderSchemaFromFields = getBuilderSchemaFromFields;
115
+ function getBuilderSchemaTZFromFields(fields, ent) {
116
+ return {
117
+ ...new base_schema_1.EntSchemaWithTZ({ fields }),
118
+ ent,
119
+ };
120
+ }
121
+ exports.getBuilderSchemaTZFromFields = getBuilderSchemaTZFromFields;
88
122
  function getSchemaName(value) {
89
123
  return value.ent.name;
90
124
  }
@@ -142,12 +176,34 @@ class SimpleBuilder {
142
176
  ent: schema.ent,
143
177
  tableName: tableName,
144
178
  fields: [],
179
+ fieldPrivacy: (0, schema_1.getFieldsWithPrivacy)(schema),
145
180
  },
146
181
  builder: this,
147
182
  action: action,
148
183
  schema: this.schema,
149
184
  editedFields: () => {
150
- return this.fields;
185
+ // to simulate what we do in generated builders where we return a new Map
186
+ const m = new Map();
187
+ for (const [k, v] of this.fields) {
188
+ m.set(k, v);
189
+ }
190
+ return m;
191
+ },
192
+ updateInput: (input) => {
193
+ const knownFields = (0, schema_1.getFields)(this.schema);
194
+ for (const k in input) {
195
+ if (knownFields.has(k)) {
196
+ this.fields.set(k, input[k]);
197
+ }
198
+ else {
199
+ // related to #510. we do camelCase to pass fields in here but fields may be snakeCase and we want that to pass in tests
200
+ // we do camelCase in
201
+ const sc = (0, snake_case_1.snakeCase)(k);
202
+ if (knownFields.has(sc)) {
203
+ this.fields.set(sc, input[k]);
204
+ }
205
+ }
206
+ }
151
207
  },
152
208
  });
153
209
  }
@@ -1,8 +1,8 @@
1
1
  import { Context, Viewer } from "../../core/base";
2
2
  import { ContextCache } from "../../core/context";
3
- import { LoggedOutViewer } from "../../core/viewer";
4
3
  export declare class TestContext implements Context {
4
+ constructor(viewer?: Viewer);
5
5
  cache: ContextCache;
6
- viewer: LoggedOutViewer;
6
+ viewer: Viewer;
7
7
  getViewer(): Viewer;
8
8
  }
@@ -4,9 +4,15 @@ exports.TestContext = void 0;
4
4
  const context_1 = require("../../core/context");
5
5
  const viewer_1 = require("../../core/viewer");
6
6
  class TestContext {
7
- constructor() {
7
+ constructor(viewer) {
8
8
  this.cache = new context_1.ContextCache();
9
9
  this.viewer = new viewer_1.LoggedOutViewer(this);
10
+ if (viewer) {
11
+ this.viewer = viewer;
12
+ if (viewer.setContext !== undefined) {
13
+ viewer.setContext(this);
14
+ }
15
+ }
10
16
  }
11
17
  getViewer() {
12
18
  return this.viewer;
@@ -71,10 +71,11 @@ export declare class TempDB {
71
71
  constructor(tables: CoreConcept[]);
72
72
  getDialect(): Dialect;
73
73
  getTables(): Map<string, CoreConcept>;
74
- beforeAll(): Promise<void>;
74
+ beforeAll(setupConnString?: boolean): Promise<void>;
75
75
  getSqliteClient(): SqliteDatabase;
76
76
  getPostgresClient(): PGClient;
77
77
  afterAll(): Promise<void>;
78
+ getDB(): string;
78
79
  dropAll(): Promise<void>;
79
80
  drop(...tables: string[]): Promise<void>;
80
81
  create(...tables: CoreConcept[]): Promise<void>;