@snowtop/ent 0.1.0-alpha7 → 0.1.0-alpha8

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 (53) 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 +5 -1
  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 +70 -23
  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.js +3 -0
  21. package/core/viewer.d.ts +1 -0
  22. package/core/viewer.js +4 -0
  23. package/graphql/graphql.d.ts +3 -2
  24. package/graphql/graphql.js +22 -21
  25. package/graphql/node_resolver.d.ts +0 -1
  26. package/index.d.ts +16 -1
  27. package/index.js +18 -5
  28. package/package.json +2 -2
  29. package/parse_schema/parse.d.ts +11 -4
  30. package/parse_schema/parse.js +50 -6
  31. package/schema/base_schema.d.ts +36 -1
  32. package/schema/base_schema.js +48 -2
  33. package/schema/index.d.ts +2 -2
  34. package/schema/index.js +8 -1
  35. package/schema/schema.d.ts +50 -1
  36. package/schema/schema.js +113 -5
  37. package/scripts/custom_graphql.js +122 -15
  38. package/scripts/transform_schema.js +204 -55
  39. package/testutils/builder.d.ts +5 -1
  40. package/testutils/builder.js +46 -2
  41. package/testutils/context/test_context.d.ts +2 -2
  42. package/testutils/context/test_context.js +7 -1
  43. package/testutils/db/test_db.d.ts +2 -1
  44. package/testutils/db/test_db.js +13 -4
  45. package/testutils/ent-graphql-tests/index.d.ts +2 -0
  46. package/testutils/ent-graphql-tests/index.js +7 -5
  47. package/testutils/fake_data/fake_contact.d.ts +2 -6
  48. package/testutils/fake_data/fake_contact.js +9 -16
  49. package/testutils/fake_data/fake_event.d.ts +2 -6
  50. package/testutils/fake_data/fake_event.js +17 -24
  51. package/testutils/fake_data/fake_user.d.ts +2 -6
  52. package/testutils/fake_data/fake_user.js +10 -17
  53. package/testutils/fake_data/test_helpers.js +1 -1
package/schema/index.js CHANGED
@@ -10,20 +10,27 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
10
10
  for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
11
11
  };
12
12
  Object.defineProperty(exports, "__esModule", { value: true });
13
- exports.BaseEntSchemaWithTZ = exports.BaseEntSchema = exports.Node = exports.Timestamps = exports.optionalField = exports.requiredField = exports.ConstraintType = exports.NoFields = exports.ActionOperation = exports.getFields = exports.DBType = void 0;
13
+ exports.EntSchemaWithTZ = exports.EntSchema = exports.BaseEntSchemaWithTZ = exports.BaseEntSchema = exports.Node = exports.Timestamps = exports.getObjectLoaderProperties = exports.getTransformedReadClause = exports.SQLStatementOperation = exports.optionalField = exports.requiredField = exports.ConstraintType = exports.NoFields = exports.ActionOperation = exports.getStorageKey = exports.getFieldsWithPrivacy = exports.getFields = exports.DBType = void 0;
14
14
  var schema_1 = require("./schema");
15
15
  Object.defineProperty(exports, "DBType", { enumerable: true, get: function () { return schema_1.DBType; } });
16
16
  Object.defineProperty(exports, "getFields", { enumerable: true, get: function () { return schema_1.getFields; } });
17
+ Object.defineProperty(exports, "getFieldsWithPrivacy", { enumerable: true, get: function () { return schema_1.getFieldsWithPrivacy; } });
18
+ Object.defineProperty(exports, "getStorageKey", { enumerable: true, get: function () { return schema_1.getStorageKey; } });
17
19
  Object.defineProperty(exports, "ActionOperation", { enumerable: true, get: function () { return schema_1.ActionOperation; } });
18
20
  Object.defineProperty(exports, "NoFields", { enumerable: true, get: function () { return schema_1.NoFields; } });
19
21
  Object.defineProperty(exports, "ConstraintType", { enumerable: true, get: function () { return schema_1.ConstraintType; } });
20
22
  Object.defineProperty(exports, "requiredField", { enumerable: true, get: function () { return schema_1.requiredField; } });
21
23
  Object.defineProperty(exports, "optionalField", { enumerable: true, get: function () { return schema_1.optionalField; } });
24
+ Object.defineProperty(exports, "SQLStatementOperation", { enumerable: true, get: function () { return schema_1.SQLStatementOperation; } });
25
+ Object.defineProperty(exports, "getTransformedReadClause", { enumerable: true, get: function () { return schema_1.getTransformedReadClause; } });
26
+ Object.defineProperty(exports, "getObjectLoaderProperties", { enumerable: true, get: function () { return schema_1.getObjectLoaderProperties; } });
22
27
  var base_schema_1 = require("./base_schema");
23
28
  Object.defineProperty(exports, "Timestamps", { enumerable: true, get: function () { return base_schema_1.Timestamps; } });
24
29
  Object.defineProperty(exports, "Node", { enumerable: true, get: function () { return base_schema_1.Node; } });
25
30
  Object.defineProperty(exports, "BaseEntSchema", { enumerable: true, get: function () { return base_schema_1.BaseEntSchema; } });
26
31
  Object.defineProperty(exports, "BaseEntSchemaWithTZ", { enumerable: true, get: function () { return base_schema_1.BaseEntSchemaWithTZ; } });
32
+ Object.defineProperty(exports, "EntSchema", { enumerable: true, get: function () { return base_schema_1.EntSchema; } });
33
+ Object.defineProperty(exports, "EntSchemaWithTZ", { enumerable: true, get: function () { return base_schema_1.EntSchemaWithTZ; } });
27
34
  __exportStar(require("./field"), exports);
28
35
  __exportStar(require("./json_field"), exports);
29
36
  __exportStar(require("./struct_field"), exports);
@@ -1,5 +1,6 @@
1
- import { Data, Ent, LoaderInfo } from "../core/base";
1
+ import { Data, Ent, LoaderInfo, PrivacyPolicy, Viewer } from "../core/base";
2
2
  import { Builder } from "../action/action";
3
+ import { Clause } from "../core/clause";
3
4
  export declare type FieldMap = {
4
5
  [key: string]: Field;
5
6
  };
@@ -62,6 +63,27 @@ export interface Pattern {
62
63
  name: string;
63
64
  fields: FieldMap | Field[];
64
65
  edges?: Edge[];
66
+ transformRead?: () => Clause;
67
+ transformWrite?: <T extends Ent>(stmt: UpdateOperation<T>) => TransformedUpdateOperation<T> | undefined;
68
+ transformsDelete?: boolean;
69
+ transformsInsert?: boolean;
70
+ transformsUpdate?: boolean;
71
+ }
72
+ export declare enum SQLStatementOperation {
73
+ Insert = "insert",
74
+ Update = "update",
75
+ Delete = "delete"
76
+ }
77
+ export interface UpdateOperation<T extends Ent> {
78
+ op: SQLStatementOperation;
79
+ existingEnt?: T;
80
+ viewer: Viewer;
81
+ data?: Map<string, any>;
82
+ }
83
+ export interface TransformedUpdateOperation<T extends Ent> {
84
+ op: SQLStatementOperation;
85
+ data?: Data;
86
+ existingEnt?: T;
65
87
  }
66
88
  export declare enum DBType {
67
89
  UUID = "UUID",
@@ -85,6 +107,7 @@ export declare enum DBType {
85
107
  export interface ImportType {
86
108
  path: string;
87
109
  type: string;
110
+ [x: string]: any;
88
111
  }
89
112
  declare type EnumMap = {
90
113
  [key: string]: string;
@@ -135,11 +158,13 @@ export interface FieldOptions {
135
158
  fieldEdge?: FieldEdge;
136
159
  primaryKey?: boolean;
137
160
  disableUserEditable?: boolean;
161
+ disableUserGraphQLEditable?: boolean;
138
162
  defaultValueOnCreate?(builder: Builder<Ent>, input: Data): any;
139
163
  defaultToViewerOnCreate?: boolean;
140
164
  defaultValueOnEdit?(builder: Builder<Ent>, input: Data): any;
141
165
  derivedWhenEmbedded?: boolean;
142
166
  polymorphic?: boolean | PolymorphicOptions;
167
+ privacyPolicy?: PrivacyPolicy | (() => PrivacyPolicy);
143
168
  getDerivedFields?(name: string): FieldMap;
144
169
  [x: string]: any;
145
170
  }
@@ -158,7 +183,17 @@ export interface SchemaConstructor {
158
183
  new (): Schema;
159
184
  }
160
185
  export declare type SchemaInputType = Schema | SchemaConstructor;
186
+ export declare function getSchema(value: SchemaInputType): Schema;
161
187
  export declare function getFields(value: SchemaInputType): Map<string, Field>;
188
+ export declare function getStorageKey(field: Field, fieldName: string): string;
189
+ export declare function getFieldsWithPrivacy(value: SchemaInputType): Map<string, PrivacyPolicy>;
190
+ export declare function getTransformedReadClause(value: SchemaInputType): Clause | undefined;
191
+ interface objectLoaderOptions {
192
+ clause?: () => Clause | undefined;
193
+ instanceKey?: string;
194
+ }
195
+ export declare function getObjectLoaderProperties(value: SchemaInputType, tableName: string): objectLoaderOptions | undefined;
196
+ export declare function getTransformedUpdateOp<T extends Ent>(value: SchemaInputType, stmt: UpdateOperation<T>): TransformedUpdateOperation<T> | undefined;
162
197
  export declare enum ActionOperation {
163
198
  Create = 1,
164
199
  Edit = 2,
@@ -201,10 +236,24 @@ export interface Constraint {
201
236
  fkey?: ForeignKeyInfo;
202
237
  condition?: string;
203
238
  }
239
+ export interface FullTextWeight {
240
+ A?: string[];
241
+ B?: string[];
242
+ C?: string[];
243
+ D?: string[];
244
+ }
245
+ export interface FullText {
246
+ generatedColumnName?: string;
247
+ language?: "english" | "french" | "german" | "simple";
248
+ languageColumn?: string;
249
+ indexType?: "gin" | "gist";
250
+ weights?: FullTextWeight;
251
+ }
204
252
  export interface Index {
205
253
  name: string;
206
254
  columns: string[];
207
255
  unique?: boolean;
256
+ fulltext?: FullText;
208
257
  }
209
258
  export interface ForeignKeyInfo {
210
259
  tableName: string;
package/schema/schema.js CHANGED
@@ -1,6 +1,21 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.ConstraintType = exports.optionalField = exports.requiredField = exports.NoFields = exports.ActionOperation = exports.getFields = exports.DBType = void 0;
3
+ exports.ConstraintType = exports.optionalField = exports.requiredField = exports.NoFields = exports.ActionOperation = exports.getTransformedUpdateOp = exports.getObjectLoaderProperties = exports.getTransformedReadClause = exports.getFieldsWithPrivacy = exports.getStorageKey = exports.getFields = exports.getSchema = exports.DBType = exports.SQLStatementOperation = void 0;
4
+ const snake_case_1 = require("snake-case");
5
+ // we also want this transformation to exist on a per-action basis
6
+ // if it exists on an action, we don't do the global schema transformation
7
+ var SQLStatementOperation;
8
+ (function (SQLStatementOperation) {
9
+ // transform insert e.g. to an update based on whatever logic
10
+ SQLStatementOperation["Insert"] = "insert";
11
+ // // transform select e.g. deleted_at. can't change from select to different query type
12
+ // // but can change the query
13
+ // Select = "select",
14
+ // e.g. change updated value
15
+ SQLStatementOperation["Update"] = "update";
16
+ // delete -> update theoretically e.g. deleted_at
17
+ SQLStatementOperation["Delete"] = "delete";
18
+ })(SQLStatementOperation = exports.SQLStatementOperation || (exports.SQLStatementOperation = {}));
4
19
  // we want --strictNullChecks flag so nullable is used to type graphql, ts, db
5
20
  // should eventually generate (boolean | null) etc
6
21
  // supported db types
@@ -28,14 +43,17 @@ var DBType;
28
43
  function isSchema(value) {
29
44
  return value.fields !== undefined;
30
45
  }
31
- function getFields(value) {
32
- let schema;
46
+ function getSchema(value) {
33
47
  if (isSchema(value)) {
34
- schema = value;
48
+ return value;
35
49
  }
36
50
  else {
37
- schema = new value();
51
+ return new value();
38
52
  }
53
+ }
54
+ exports.getSchema = getSchema;
55
+ function getFields(value) {
56
+ const schema = getSchema(value);
39
57
  function addFields(fields) {
40
58
  if (Array.isArray(fields)) {
41
59
  for (const field of fields) {
@@ -68,6 +86,96 @@ function getFields(value) {
68
86
  return m;
69
87
  }
70
88
  exports.getFields = getFields;
89
+ function getStorageKey(field, fieldName) {
90
+ return field.storageKey || (0, snake_case_1.snakeCase)(fieldName);
91
+ }
92
+ exports.getStorageKey = getStorageKey;
93
+ // returns a mapping of storage key to field privacy
94
+ function getFieldsWithPrivacy(value) {
95
+ const schema = getSchema(value);
96
+ function addFields(fields) {
97
+ if (Array.isArray(fields)) {
98
+ for (const field of fields) {
99
+ const name = field.name;
100
+ if (!field.name) {
101
+ throw new Error(`name required`);
102
+ }
103
+ if (field.getDerivedFields !== undefined) {
104
+ addFields(field.getDerivedFields(name));
105
+ }
106
+ if (field.privacyPolicy) {
107
+ let privacyPolicy;
108
+ if (typeof field.privacyPolicy === "function") {
109
+ privacyPolicy = field.privacyPolicy();
110
+ }
111
+ else {
112
+ privacyPolicy = field.privacyPolicy;
113
+ }
114
+ m.set(getStorageKey(field, name), privacyPolicy);
115
+ }
116
+ }
117
+ }
118
+ for (const name in fields) {
119
+ const field = fields[name];
120
+ if (field.getDerivedFields !== undefined) {
121
+ addFields(field.getDerivedFields(name));
122
+ }
123
+ if (field.privacyPolicy) {
124
+ let privacyPolicy;
125
+ if (typeof field.privacyPolicy === "function") {
126
+ privacyPolicy = field.privacyPolicy();
127
+ }
128
+ else {
129
+ privacyPolicy = field.privacyPolicy;
130
+ }
131
+ m.set(getStorageKey(field, name), privacyPolicy);
132
+ }
133
+ }
134
+ }
135
+ let m = new Map();
136
+ if (schema.patterns) {
137
+ for (const pattern of schema.patterns) {
138
+ addFields(pattern.fields);
139
+ }
140
+ }
141
+ addFields(schema.fields);
142
+ return m;
143
+ }
144
+ exports.getFieldsWithPrivacy = getFieldsWithPrivacy;
145
+ function getTransformedReadClause(value) {
146
+ const schema = getSchema(value);
147
+ if (!schema.patterns) {
148
+ return;
149
+ }
150
+ for (const p of schema.patterns) {
151
+ // e.g. discarded_at, deleted_at, etc
152
+ if (p.transformRead) {
153
+ // return clause.Eq('deleted_at', null);
154
+ return p.transformRead();
155
+ }
156
+ }
157
+ return;
158
+ }
159
+ exports.getTransformedReadClause = getTransformedReadClause;
160
+ function getObjectLoaderProperties(value, tableName) {
161
+ return {
162
+ clause: () => getTransformedReadClause(value),
163
+ instanceKey: `${tableName}:transformedReadClause`,
164
+ };
165
+ }
166
+ exports.getObjectLoaderProperties = getObjectLoaderProperties;
167
+ function getTransformedUpdateOp(value, stmt) {
168
+ const schema = getSchema(value);
169
+ if (!schema.patterns) {
170
+ return;
171
+ }
172
+ for (const p of schema.patterns) {
173
+ if (p.transformWrite) {
174
+ return p.transformWrite(stmt);
175
+ }
176
+ }
177
+ }
178
+ exports.getTransformedUpdateOp = getTransformedUpdateOp;
71
179
  // this maps to ActionOperation in ent/action.go
72
180
  var ActionOperation;
73
181
  (function (ActionOperation) {
@@ -24,6 +24,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
24
24
  };
25
25
  Object.defineProperty(exports, "__esModule", { value: true });
26
26
  const glob_1 = __importDefault(require("glob"));
27
+ const json5_1 = __importDefault(require("json5"));
27
28
  const minimist_1 = __importDefault(require("minimist"));
28
29
  const graphql_1 = require("../graphql/graphql");
29
30
  const readline = __importStar(require("readline"));
@@ -53,7 +54,103 @@ async function readInputs() {
53
54
  });
54
55
  });
55
56
  }
56
- async function captureCustom(filePath, filesCsv) {
57
+ function processCustomObjects(l, gqlCapture, input) {
58
+ let m;
59
+ if (input) {
60
+ m = gqlCapture.getCustomInputObjects();
61
+ }
62
+ else {
63
+ m = gqlCapture.getCustomObjects();
64
+ }
65
+ for (const input of l) {
66
+ m.set(input.name, {
67
+ nodeName: input.graphQLName || input.name,
68
+ className: input.name,
69
+ });
70
+ if (input.fields) {
71
+ processCustomFields(input.fields, gqlCapture, input.name);
72
+ }
73
+ }
74
+ }
75
+ function transformArgs(f) {
76
+ return (f.args || []).map((v) => {
77
+ const ret = {
78
+ ...v,
79
+ };
80
+ // duplicated from getType in graphql.ts
81
+ if ((0, graphql_1.isCustomType)(ret.type)) {
82
+ ret.type = v.type.type;
83
+ (0, graphql_1.addCustomType)(v.type);
84
+ }
85
+ // scalar types not supported for now
86
+ ret.tsType = graphql_1.knownAllowedNames.get(v.type);
87
+ return ret;
88
+ });
89
+ }
90
+ function transformResultType(f) {
91
+ return f.resultType
92
+ ? [
93
+ {
94
+ name: "",
95
+ type: f.resultType,
96
+ tsType: graphql_1.knownAllowedNames.get(f.resultType),
97
+ list: f.list,
98
+ nullable: f.nullable,
99
+ },
100
+ ]
101
+ : [];
102
+ }
103
+ function processTopLevel(l, l2) {
104
+ for (const custom of l) {
105
+ l2.push({
106
+ nodeName: custom.class,
107
+ functionName: custom.functionName || custom.name,
108
+ gqlName: custom.graphQLName || custom.name,
109
+ fieldType: custom.fieldType,
110
+ args: transformArgs(custom),
111
+ results: transformResultType(custom),
112
+ });
113
+ }
114
+ }
115
+ function processCustomFields(fields, gqlCapture, nodeName) {
116
+ const m = gqlCapture.getCustomFields();
117
+ let results = [];
118
+ for (const f of fields) {
119
+ results.push({
120
+ nodeName: nodeName,
121
+ gqlName: f.graphQLName || f.name,
122
+ functionName: f.functionName || f.name,
123
+ fieldType: f.fieldType,
124
+ args: transformArgs(f),
125
+ results: transformResultType(f),
126
+ });
127
+ }
128
+ m.set(nodeName, results);
129
+ }
130
+ async function captureCustom(filePath, filesCsv, jsonPath, gqlCapture) {
131
+ if (jsonPath !== undefined) {
132
+ let json = json5_1.default.parse(fs.readFileSync(jsonPath, {
133
+ encoding: "utf8",
134
+ }));
135
+ if (json.fields) {
136
+ for (const k in json.fields) {
137
+ processCustomFields(json.fields[k], gqlCapture, k);
138
+ }
139
+ }
140
+ if (json.inputs) {
141
+ processCustomObjects(json.inputs, gqlCapture, true);
142
+ }
143
+ if (json.objects) {
144
+ processCustomObjects(json.objects, gqlCapture);
145
+ }
146
+ if (json.queries) {
147
+ processTopLevel(json.queries, gqlCapture.getCustomQueries());
148
+ }
149
+ if (json.mutations) {
150
+ processTopLevel(json.mutations, gqlCapture.getCustomMutations());
151
+ }
152
+ return;
153
+ }
57
154
  if (filesCsv !== undefined) {
58
155
  let files = filesCsv.split(",");
59
156
  for (let i = 0; i < files.length; i++) {
@@ -153,15 +250,25 @@ async function main() {
153
250
  if (!gqlPath) {
154
251
  throw new Error("could not find graphql path");
155
252
  }
156
- const r = require(gqlPath);
157
- if (!r.GQLCapture) {
158
- throw new Error("could not find GQLCapture in module");
253
+ // use different variable so that we use the correct GQLCapture as needed
254
+ // for local dev, get the one from the file system. otherwise, get the one
255
+ // from node_modules
256
+ let gqlCapture;
257
+ if (process.env.LOCAL_SCRIPT_PATH) {
258
+ const r = require("../graphql/graphql");
259
+ gqlCapture = r.GQLCapture;
260
+ }
261
+ else {
262
+ const r = require(gqlPath);
263
+ if (!r.GQLCapture) {
264
+ throw new Error("could not find GQLCapture in module");
265
+ }
266
+ gqlCapture = r.GQLCapture;
267
+ gqlCapture.enable(true);
159
268
  }
160
- const GQLCapture = r.GQLCapture;
161
- GQLCapture.enable(true);
162
269
  const [inputsRead, _, imports] = await Promise.all([
163
270
  readInputs(),
164
- captureCustom(options.path, options.files),
271
+ captureCustom(options.path, options.files, options.json_path, gqlCapture),
165
272
  parseImports(options.path),
166
273
  ]);
167
274
  const { nodes, nodesMap } = inputsRead;
@@ -172,14 +279,14 @@ async function main() {
172
279
  }
173
280
  return result;
174
281
  }
175
- GQLCapture.resolve(nodes);
176
- let args = fromMap(GQLCapture.getCustomArgs());
177
- let inputs = fromMap(GQLCapture.getCustomInputObjects());
178
- let fields = GQLCapture.getProcessedCustomFields();
179
- let queries = GQLCapture.getProcessedCustomQueries();
180
- let mutations = GQLCapture.getProcessedCustomMutations();
181
- let objects = fromMap(GQLCapture.getCustomObjects());
182
- let customTypes = fromMap(GQLCapture.getCustomTypes());
282
+ gqlCapture.resolve(nodes);
283
+ let args = fromMap(gqlCapture.getCustomArgs());
284
+ let inputs = fromMap(gqlCapture.getCustomInputObjects());
285
+ let fields = gqlCapture.getProcessedCustomFields();
286
+ let queries = gqlCapture.getProcessedCustomQueries();
287
+ let mutations = gqlCapture.getProcessedCustomMutations();
288
+ let objects = fromMap(gqlCapture.getCustomObjects());
289
+ let customTypes = fromMap(gqlCapture.getCustomTypes());
183
290
  let classes = {};
184
291
  let allFiles = {};
185
292
  const buildClasses2 = (args) => {