@snowtop/ent 0.1.0-alpha6 → 0.1.0-alpha9

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 (62) 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/builtins/connection.js +3 -3
  24. package/graphql/builtins/edge.js +2 -2
  25. package/graphql/builtins/node.js +1 -1
  26. package/graphql/graphql.d.ts +3 -2
  27. package/graphql/graphql.js +24 -23
  28. package/graphql/node_resolver.d.ts +0 -1
  29. package/graphql/query/connection_type.js +6 -6
  30. package/graphql/query/page_info.js +4 -4
  31. package/graphql/query/shared_assoc_test.js +2 -2
  32. package/graphql/scalars/time.d.ts +1 -1
  33. package/index.d.ts +16 -1
  34. package/index.js +18 -5
  35. package/package.json +3 -3
  36. package/parse_schema/parse.d.ts +16 -5
  37. package/parse_schema/parse.js +51 -6
  38. package/schema/base_schema.d.ts +36 -1
  39. package/schema/base_schema.js +48 -2
  40. package/schema/field.js +1 -1
  41. package/schema/index.d.ts +2 -2
  42. package/schema/index.js +8 -1
  43. package/schema/schema.d.ts +50 -1
  44. package/schema/schema.js +113 -5
  45. package/scripts/custom_graphql.js +122 -15
  46. package/scripts/read_schema.js +15 -1
  47. package/scripts/transform_schema.js +212 -55
  48. package/testutils/builder.d.ts +5 -1
  49. package/testutils/builder.js +46 -2
  50. package/testutils/context/test_context.d.ts +2 -2
  51. package/testutils/context/test_context.js +7 -1
  52. package/testutils/db/test_db.d.ts +2 -1
  53. package/testutils/db/test_db.js +13 -4
  54. package/testutils/ent-graphql-tests/index.d.ts +2 -0
  55. package/testutils/ent-graphql-tests/index.js +26 -17
  56. package/testutils/fake_data/fake_contact.d.ts +2 -6
  57. package/testutils/fake_data/fake_contact.js +9 -16
  58. package/testutils/fake_data/fake_event.d.ts +2 -6
  59. package/testutils/fake_data/fake_event.js +17 -24
  60. package/testutils/fake_data/fake_user.d.ts +2 -6
  61. package/testutils/fake_data/fake_user.js +10 -17
  62. package/testutils/fake_data/test_helpers.js +1 -1
@@ -22,6 +22,7 @@ function processFields(src, patternName) {
22
22
  let f = { name, ...field };
23
23
  f.hasDefaultValueOnCreate = field.defaultValueOnCreate != undefined;
24
24
  f.hasDefaultValueOnEdit = field.defaultValueOnEdit != undefined;
25
+ f.hasFieldPrivacy = field.privacyPolicy !== undefined;
25
26
  if (field.polymorphic) {
26
27
  // convert boolean into object
27
28
  // we keep boolean as an option to keep API simple
@@ -46,6 +47,7 @@ function processFields(src, patternName) {
46
47
  if (patternName) {
47
48
  f.patternName = patternName;
48
49
  }
50
+ transformType(field.type);
49
51
  if (field.getDerivedFields) {
50
52
  f.derivedFields = processFields(field.getDerivedFields(name));
51
53
  }
@@ -65,6 +67,24 @@ function processFields(src, patternName) {
65
67
  }
66
68
  return ret;
67
69
  }
70
+ function transformImportType(typ) {
71
+ if (!typ.importType) {
72
+ return;
73
+ }
74
+ typ.importType = {
75
+ ...typ.importType,
76
+ // these 2 needed for forwards compatibility with new go schema
77
+ importPath: typ.importType.path,
78
+ import: typ.importType.type,
79
+ };
80
+ }
81
+ function transformType(typ) {
82
+ if (!typ) {
83
+ return;
84
+ }
85
+ transformImportType(typ);
86
+ transformType(typ.listElemType);
87
+ }
68
88
  function processEdges(src, patternName) {
69
89
  const ret = [];
70
90
  for (const edge of src) {
@@ -89,6 +109,9 @@ function processEdgeGroups(processedSchema, edgeGroups) {
89
109
  }
90
110
  }
91
111
  function processPattern(patterns, pattern, processedSchema) {
112
+ let ret = {
113
+ ...pattern,
114
+ };
92
115
  const name = pattern.name;
93
116
  const fields = processFields(pattern.fields, pattern.name);
94
117
  processedSchema.fields.push(...fields);
@@ -96,6 +119,10 @@ function processPattern(patterns, pattern, processedSchema) {
96
119
  const edges = processEdges(pattern.edges, pattern.name);
97
120
  processedSchema.assocEdges.push(...edges);
98
121
  }
122
+ // flag transformsSelect
123
+ if (pattern.transformRead) {
124
+ ret.transformsSelect = true;
125
+ }
99
126
  if (patterns[name] === undefined) {
100
127
  // intentionally processing separately and not passing pattern.name
101
128
  const edges = processEdges(pattern.edges || []);
@@ -109,6 +136,7 @@ function processPattern(patterns, pattern, processedSchema) {
109
136
  // TODO ideally we want to make sure that different patterns don't have the same name
110
137
  // can't do a deepEqual check because function calls and therefore different instances in fields
111
138
  }
139
+ return ret;
112
140
  }
113
141
  var NullableResult;
114
142
  (function (NullableResult) {
@@ -149,14 +177,19 @@ function parseSchema(potentialSchemas) {
149
177
  for (const key in potentialSchemas) {
150
178
  const value = potentialSchemas[key];
151
179
  let schema;
152
- if (value.constructor == Object) {
153
- schema = value;
154
- }
155
- else {
156
- schema = new value();
180
+ const name = value.constructor.name;
181
+ // named class e.g. new BaseEntSchema
182
+ switch (name) {
183
+ case "Function":
184
+ schema = new value();
185
+ break;
186
+ default:
187
+ // implicit schema or named class
188
+ schema = value;
157
189
  }
158
190
  let processedSchema = {
159
191
  fields: [],
192
+ schemaPath: schema.schemaPath,
160
193
  tableName: schema.tableName,
161
194
  enumTable: schema.enumTable,
162
195
  dbRows: schema.dbRows,
@@ -171,7 +204,19 @@ function parseSchema(potentialSchemas) {
171
204
  // ¯\_(ツ)_/¯
172
205
  if (schema.patterns) {
173
206
  for (const pattern of schema.patterns) {
174
- processPattern(patterns, pattern, processedSchema);
207
+ const ret = processPattern(patterns, pattern, processedSchema);
208
+ if (ret.transformsSelect) {
209
+ if (processedSchema.transformsSelect) {
210
+ throw new Error(`can only have one pattern which transforms default querying behavior`);
211
+ }
212
+ processedSchema.transformsSelect = true;
213
+ }
214
+ if (ret.transformsDelete) {
215
+ if (processedSchema.transformsDelete) {
216
+ throw new Error(`can only have one pattern which transforms default deletion behavior`);
217
+ }
218
+ processedSchema.transformsDelete = true;
219
+ }
175
220
  }
176
221
  }
177
222
  const fields = processFields(schema.fields);
@@ -1,6 +1,41 @@
1
- import { Pattern } from "./schema";
1
+ import { Field, FieldMap, Pattern } from "./schema";
2
+ import { Action, AssocEdgeGroup, Constraint, Edge, Index, Schema } from ".";
2
3
  export declare const Timestamps: Pattern;
3
4
  export declare const Node: Pattern;
5
+ export interface SchemaConfig extends Schema {
6
+ }
7
+ export declare class EntSchema implements Schema {
8
+ fields: FieldMap | Field[];
9
+ tableName: string | undefined;
10
+ patterns: Pattern[];
11
+ edges: Edge[] | undefined;
12
+ edgeGroups: AssocEdgeGroup[] | undefined;
13
+ actions: Action[] | undefined;
14
+ enumTable: boolean | undefined;
15
+ dbRows: {
16
+ [key: string]: any;
17
+ }[] | undefined;
18
+ constraints: Constraint[] | undefined;
19
+ indices: Index[] | undefined;
20
+ hideFromGraphQL?: boolean;
21
+ constructor(cfg: SchemaConfig);
22
+ }
23
+ export declare class EntSchemaWithTZ implements Schema {
24
+ fields: FieldMap | Field[];
25
+ tableName: string | undefined;
26
+ patterns: Pattern[];
27
+ edges: Edge[] | undefined;
28
+ edgeGroups: AssocEdgeGroup[] | undefined;
29
+ actions: Action[] | undefined;
30
+ enumTable: boolean | undefined;
31
+ dbRows: {
32
+ [key: string]: any;
33
+ }[] | undefined;
34
+ constraints: Constraint[] | undefined;
35
+ indices: Index[] | undefined;
36
+ hideFromGraphQL?: boolean;
37
+ constructor(cfg: SchemaConfig);
38
+ }
4
39
  export declare abstract class BaseEntSchema {
5
40
  addPatterns(...patterns: Pattern[]): void;
6
41
  patterns: Pattern[];
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.BaseEntSchemaWithTZ = exports.BaseEntSchema = exports.Node = exports.Timestamps = void 0;
3
+ exports.BaseEntSchemaWithTZ = exports.BaseEntSchema = exports.EntSchemaWithTZ = exports.EntSchema = exports.Node = exports.Timestamps = void 0;
4
4
  const uuid_1 = require("uuid");
5
5
  const field_1 = require("./field");
6
6
  let tsFields = {
@@ -67,8 +67,53 @@ exports.Node = {
67
67
  name: "node",
68
68
  fields: nodeFields,
69
69
  };
70
- // Base ent schema. has Node Pattern by default.
70
+ // Ent schema. has Node Pattern by default.
71
71
  // exists just to have less typing and easier for clients to implement
72
+ class EntSchema {
73
+ constructor(cfg) {
74
+ this.patterns = [exports.Node];
75
+ this.fields = cfg.fields;
76
+ this.tableName = cfg.tableName;
77
+ if (cfg.patterns) {
78
+ this.patterns.push(...cfg.patterns);
79
+ }
80
+ this.edges = cfg.edges;
81
+ this.edgeGroups = cfg.edgeGroups;
82
+ this.actions = cfg.actions;
83
+ this.enumTable = cfg.enumTable;
84
+ this.dbRows = cfg.dbRows;
85
+ this.constraints = cfg.constraints;
86
+ this.indices = cfg.indices;
87
+ this.hideFromGraphQL = cfg.hideFromGraphQL;
88
+ }
89
+ }
90
+ exports.EntSchema = EntSchema;
91
+ class EntSchemaWithTZ {
92
+ constructor(cfg) {
93
+ this.patterns = [
94
+ {
95
+ // default schema added
96
+ name: "nodeWithTZ",
97
+ fields: nodeFieldsWithTZ,
98
+ },
99
+ ];
100
+ this.fields = cfg.fields;
101
+ this.tableName = cfg.tableName;
102
+ if (cfg.patterns) {
103
+ this.patterns.push(...cfg.patterns);
104
+ }
105
+ this.edges = cfg.edges;
106
+ this.edgeGroups = cfg.edgeGroups;
107
+ this.actions = cfg.actions;
108
+ this.enumTable = cfg.enumTable;
109
+ this.dbRows = cfg.dbRows;
110
+ this.constraints = cfg.constraints;
111
+ this.indices = cfg.indices;
112
+ this.hideFromGraphQL = cfg.hideFromGraphQL;
113
+ }
114
+ }
115
+ exports.EntSchemaWithTZ = EntSchemaWithTZ;
116
+ // @deprecated use EntSchema
72
117
  class BaseEntSchema {
73
118
  constructor() {
74
119
  this.patterns = [exports.Node];
@@ -78,6 +123,7 @@ class BaseEntSchema {
78
123
  }
79
124
  }
80
125
  exports.BaseEntSchema = BaseEntSchema;
126
+ // @deprecated use EntSchemaWithTZ
81
127
  class BaseEntSchemaWithTZ {
82
128
  constructor() {
83
129
  this.patterns = [
package/schema/field.js CHANGED
@@ -616,7 +616,7 @@ class ListField extends BaseField {
616
616
  for (let i = 0; i < val.length; i++) {
617
617
  let formatted = val[i];
618
618
  if (this.field.format) {
619
- formatted = this.field.format(val[i]);
619
+ formatted = this.field.format(val[i], nested);
620
620
  }
621
621
  // postgres supports arrays natively so we
622
622
  // structure it in the expected format
package/schema/index.d.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  import Schema from "./schema";
2
2
  export { Schema };
3
- export { Field, AssocEdge, AssocEdgeGroup, InverseAssocEdge, Edge, Pattern, DBType, Type, FieldOptions, SchemaConstructor, SchemaInputType, getFields, ActionOperation, Action, EdgeAction, NoFields, FieldMap, Constraint, Index, ConstraintType, ForeignKeyInfo, requiredField, optionalField, } from "./schema";
4
- export { Timestamps, Node, BaseEntSchema, BaseEntSchemaWithTZ, } from "./base_schema";
3
+ export { Field, AssocEdge, AssocEdgeGroup, InverseAssocEdge, Edge, Pattern, DBType, Type, FieldOptions, SchemaConstructor, SchemaInputType, getFields, getFieldsWithPrivacy, getStorageKey, ActionOperation, Action, EdgeAction, NoFields, FieldMap, Constraint, Index, ConstraintType, ForeignKeyInfo, requiredField, optionalField, UpdateOperation, TransformedUpdateOperation, SQLStatementOperation, getTransformedReadClause, getObjectLoaderProperties, } from "./schema";
4
+ export { Timestamps, Node, BaseEntSchema, BaseEntSchemaWithTZ, EntSchema, EntSchemaWithTZ, } from "./base_schema";
5
5
  export * from "./field";
6
6
  export * from "./json_field";
7
7
  export * from "./struct_field";
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) {