@snowtop/ent 0.1.0-alpha2 → 0.1.0-alpha20

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 (103) hide show
  1. package/action/action.d.ts +28 -24
  2. package/action/executor.d.ts +4 -4
  3. package/action/executor.js +2 -2
  4. package/action/experimental_action.d.ts +21 -20
  5. package/action/experimental_action.js +11 -5
  6. package/action/orchestrator.d.ts +31 -16
  7. package/action/orchestrator.js +156 -43
  8. package/action/privacy.d.ts +2 -2
  9. package/core/base.d.ts +25 -21
  10. package/core/base.js +16 -0
  11. package/core/clause.d.ts +24 -3
  12. package/core/clause.js +246 -5
  13. package/core/config.d.ts +23 -0
  14. package/core/config.js +17 -0
  15. package/core/context.d.ts +2 -2
  16. package/core/db.d.ts +3 -3
  17. package/core/db.js +2 -0
  18. package/core/ent.d.ts +19 -21
  19. package/core/ent.js +76 -25
  20. package/core/loaders/assoc_count_loader.d.ts +2 -2
  21. package/core/loaders/assoc_edge_loader.d.ts +3 -3
  22. package/core/loaders/assoc_edge_loader.js +5 -4
  23. package/core/loaders/index_loader.js +1 -0
  24. package/core/loaders/object_loader.d.ts +10 -5
  25. package/core/loaders/object_loader.js +59 -4
  26. package/core/loaders/query_loader.d.ts +2 -2
  27. package/core/loaders/raw_count_loader.d.ts +2 -2
  28. package/core/privacy.d.ts +25 -25
  29. package/core/privacy.js +3 -0
  30. package/core/query/assoc_query.d.ts +6 -6
  31. package/core/query/custom_query.d.ts +5 -5
  32. package/core/query/query.d.ts +1 -1
  33. package/core/viewer.d.ts +4 -3
  34. package/core/viewer.js +4 -0
  35. package/graphql/builtins/connection.js +3 -3
  36. package/graphql/builtins/edge.js +2 -2
  37. package/graphql/builtins/node.js +1 -1
  38. package/graphql/graphql.d.ts +3 -2
  39. package/graphql/graphql.js +24 -23
  40. package/graphql/index.d.ts +1 -0
  41. package/graphql/index.js +3 -1
  42. package/graphql/mutations/union.d.ts +2 -0
  43. package/graphql/mutations/union.js +35 -0
  44. package/graphql/node_resolver.d.ts +0 -1
  45. package/graphql/query/connection_type.js +6 -6
  46. package/graphql/query/edge_connection.d.ts +9 -9
  47. package/graphql/query/page_info.d.ts +1 -1
  48. package/graphql/query/page_info.js +4 -4
  49. package/graphql/query/shared_assoc_test.js +2 -2
  50. package/graphql/scalars/time.d.ts +1 -1
  51. package/index.d.ts +16 -1
  52. package/index.js +18 -5
  53. package/package.json +3 -3
  54. package/parse_schema/parse.d.ts +23 -4
  55. package/parse_schema/parse.js +68 -6
  56. package/schema/base_schema.d.ts +36 -1
  57. package/schema/base_schema.js +51 -2
  58. package/schema/field.d.ts +1 -1
  59. package/schema/field.js +11 -3
  60. package/schema/index.d.ts +4 -2
  61. package/schema/index.js +10 -1
  62. package/schema/schema.d.ts +64 -2
  63. package/schema/schema.js +125 -5
  64. package/schema/struct_field.d.ts +17 -0
  65. package/schema/struct_field.js +102 -0
  66. package/schema/union_field.d.ts +23 -0
  67. package/schema/union_field.js +79 -0
  68. package/scripts/custom_graphql.js +122 -15
  69. package/scripts/{transform_schema.d.ts → migrate_v0.1.d.ts} +0 -0
  70. package/scripts/migrate_v0.1.js +43 -0
  71. package/scripts/read_schema.js +15 -1
  72. package/testutils/builder.d.ts +31 -21
  73. package/testutils/builder.js +98 -13
  74. package/testutils/context/test_context.d.ts +2 -2
  75. package/testutils/context/test_context.js +7 -1
  76. package/testutils/db/test_db.d.ts +2 -1
  77. package/testutils/db/test_db.js +13 -4
  78. package/testutils/ent-graphql-tests/index.d.ts +2 -0
  79. package/testutils/ent-graphql-tests/index.js +26 -17
  80. package/testutils/fake_data/fake_contact.d.ts +4 -8
  81. package/testutils/fake_data/fake_contact.js +15 -19
  82. package/testutils/fake_data/fake_event.d.ts +4 -8
  83. package/testutils/fake_data/fake_event.js +21 -25
  84. package/testutils/fake_data/fake_user.d.ts +5 -9
  85. package/testutils/fake_data/fake_user.js +23 -27
  86. package/testutils/fake_data/test_helpers.js +1 -1
  87. package/testutils/fake_data/user_query.d.ts +2 -2
  88. package/testutils/fake_log.d.ts +3 -3
  89. package/tsc/ast.d.ts +33 -0
  90. package/tsc/ast.js +191 -0
  91. package/tsc/compilerOptions.d.ts +6 -0
  92. package/tsc/compilerOptions.js +40 -1
  93. package/tsc/move_generated.d.ts +1 -0
  94. package/tsc/move_generated.js +175 -0
  95. package/tsc/transform.d.ts +21 -0
  96. package/tsc/transform.js +144 -0
  97. package/tsc/transform_action.d.ts +27 -0
  98. package/tsc/transform_action.js +204 -0
  99. package/tsc/transform_ent.d.ts +17 -0
  100. package/tsc/transform_ent.js +59 -0
  101. package/tsc/transform_schema.d.ts +21 -0
  102. package/tsc/transform_schema.js +341 -0
  103. package/scripts/transform_schema.js +0 -288
@@ -18,9 +18,11 @@ function processFields(src, patternName) {
18
18
  }
19
19
  for (const name in m) {
20
20
  const field = m[name];
21
+ //@ts-ignore type and other changed fields with different type in ProcessedField vs Field
21
22
  let f = { name, ...field };
22
23
  f.hasDefaultValueOnCreate = field.defaultValueOnCreate != undefined;
23
24
  f.hasDefaultValueOnEdit = field.defaultValueOnEdit != undefined;
25
+ f.hasFieldPrivacy = field.privacyPolicy !== undefined;
24
26
  if (field.polymorphic) {
25
27
  // convert boolean into object
26
28
  // we keep boolean as an option to keep API simple
@@ -45,13 +47,44 @@ function processFields(src, patternName) {
45
47
  if (patternName) {
46
48
  f.patternName = patternName;
47
49
  }
50
+ transformType(field.type);
48
51
  if (field.getDerivedFields) {
49
52
  f.derivedFields = processFields(field.getDerivedFields(name));
50
53
  }
54
+ if (field.type.subFields) {
55
+ f.type.subFields = processFields(field.type.subFields);
56
+ }
57
+ if (field.type.unionFields) {
58
+ f.type.unionFields = processFields(field.type.unionFields);
59
+ }
60
+ if (field.type.listElemType &&
61
+ field.type.listElemType.subFields &&
62
+ // check to avoid ts-ignore below. exists just for tsc
63
+ f.type.listElemType) {
64
+ f.type.listElemType.subFields = processFields(field.type.listElemType.subFields);
65
+ }
51
66
  ret.push(f);
52
67
  }
53
68
  return ret;
54
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
+ }
55
88
  function processEdges(src, patternName) {
56
89
  const ret = [];
57
90
  for (const edge of src) {
@@ -76,6 +109,9 @@ function processEdgeGroups(processedSchema, edgeGroups) {
76
109
  }
77
110
  }
78
111
  function processPattern(patterns, pattern, processedSchema) {
112
+ let ret = {
113
+ ...pattern,
114
+ };
79
115
  const name = pattern.name;
80
116
  const fields = processFields(pattern.fields, pattern.name);
81
117
  processedSchema.fields.push(...fields);
@@ -83,6 +119,10 @@ function processPattern(patterns, pattern, processedSchema) {
83
119
  const edges = processEdges(pattern.edges, pattern.name);
84
120
  processedSchema.assocEdges.push(...edges);
85
121
  }
122
+ // flag transformsSelect
123
+ if (pattern.transformRead) {
124
+ ret.transformsSelect = true;
125
+ }
86
126
  if (patterns[name] === undefined) {
87
127
  // intentionally processing separately and not passing pattern.name
88
128
  const edges = processEdges(pattern.edges || []);
@@ -90,12 +130,14 @@ function processPattern(patterns, pattern, processedSchema) {
90
130
  name: pattern.name,
91
131
  assocEdges: edges,
92
132
  fields: fields,
133
+ disableMixin: pattern.disableMixin,
93
134
  };
94
135
  }
95
136
  else {
96
137
  // TODO ideally we want to make sure that different patterns don't have the same name
97
138
  // can't do a deepEqual check because function calls and therefore different instances in fields
98
139
  }
140
+ return ret;
99
141
  }
100
142
  var NullableResult;
101
143
  (function (NullableResult) {
@@ -136,14 +178,19 @@ function parseSchema(potentialSchemas) {
136
178
  for (const key in potentialSchemas) {
137
179
  const value = potentialSchemas[key];
138
180
  let schema;
139
- if (value.constructor == Object) {
140
- schema = value;
141
- }
142
- else {
143
- schema = new value();
181
+ const name = value.constructor.name;
182
+ // named class e.g. new BaseEntSchema
183
+ switch (name) {
184
+ case "Function":
185
+ schema = new value();
186
+ break;
187
+ default:
188
+ // implicit schema or named class
189
+ schema = value;
144
190
  }
145
191
  let processedSchema = {
146
192
  fields: [],
193
+ schemaPath: schema.schemaPath,
147
194
  tableName: schema.tableName,
148
195
  enumTable: schema.enumTable,
149
196
  dbRows: schema.dbRows,
@@ -156,13 +203,28 @@ function parseSchema(potentialSchemas) {
156
203
  };
157
204
  // let's put patterns first just so we have id, created_at, updated_at first
158
205
  // ¯\_(ツ)_/¯
206
+ let patternNames = [];
159
207
  if (schema.patterns) {
160
208
  for (const pattern of schema.patterns) {
161
- processPattern(patterns, pattern, processedSchema);
209
+ const ret = processPattern(patterns, pattern, processedSchema);
210
+ patternNames.push(pattern.name);
211
+ if (ret.transformsSelect) {
212
+ if (processedSchema.transformsSelect) {
213
+ throw new Error(`can only have one pattern which transforms default querying behavior`);
214
+ }
215
+ processedSchema.transformsSelect = true;
216
+ }
217
+ if (ret.transformsDelete) {
218
+ if (processedSchema.transformsDelete) {
219
+ throw new Error(`can only have one pattern which transforms default deletion behavior`);
220
+ }
221
+ processedSchema.transformsDelete = true;
222
+ }
162
223
  }
163
224
  }
164
225
  const fields = processFields(schema.fields);
165
226
  processedSchema.fields.push(...fields);
227
+ processedSchema.patternNames = patternNames;
166
228
  if (schema.edges) {
167
229
  const edges = processEdges(schema.edges);
168
230
  processedSchema.assocEdges.push(...edges);
@@ -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 = {
@@ -66,9 +66,56 @@ let nodeFieldsWithTZ = {
66
66
  exports.Node = {
67
67
  name: "node",
68
68
  fields: nodeFields,
69
+ disableMixin: true,
69
70
  };
70
- // Base ent schema. has Node Pattern by default.
71
+ // Ent schema. has Node Pattern by default.
71
72
  // exists just to have less typing and easier for clients to implement
73
+ class EntSchema {
74
+ constructor(cfg) {
75
+ this.patterns = [exports.Node];
76
+ this.fields = cfg.fields;
77
+ this.tableName = cfg.tableName;
78
+ if (cfg.patterns) {
79
+ this.patterns.push(...cfg.patterns);
80
+ }
81
+ this.edges = cfg.edges;
82
+ this.edgeGroups = cfg.edgeGroups;
83
+ this.actions = cfg.actions;
84
+ this.enumTable = cfg.enumTable;
85
+ this.dbRows = cfg.dbRows;
86
+ this.constraints = cfg.constraints;
87
+ this.indices = cfg.indices;
88
+ this.hideFromGraphQL = cfg.hideFromGraphQL;
89
+ }
90
+ }
91
+ exports.EntSchema = EntSchema;
92
+ class EntSchemaWithTZ {
93
+ constructor(cfg) {
94
+ this.patterns = [
95
+ {
96
+ // default schema added
97
+ name: "nodeWithTZ",
98
+ fields: nodeFieldsWithTZ,
99
+ disableMixin: true,
100
+ },
101
+ ];
102
+ this.fields = cfg.fields;
103
+ this.tableName = cfg.tableName;
104
+ if (cfg.patterns) {
105
+ this.patterns.push(...cfg.patterns);
106
+ }
107
+ this.edges = cfg.edges;
108
+ this.edgeGroups = cfg.edgeGroups;
109
+ this.actions = cfg.actions;
110
+ this.enumTable = cfg.enumTable;
111
+ this.dbRows = cfg.dbRows;
112
+ this.constraints = cfg.constraints;
113
+ this.indices = cfg.indices;
114
+ this.hideFromGraphQL = cfg.hideFromGraphQL;
115
+ }
116
+ }
117
+ exports.EntSchemaWithTZ = EntSchemaWithTZ;
118
+ // @deprecated use EntSchema
72
119
  class BaseEntSchema {
73
120
  constructor() {
74
121
  this.patterns = [exports.Node];
@@ -78,6 +125,7 @@ class BaseEntSchema {
78
125
  }
79
126
  }
80
127
  exports.BaseEntSchema = BaseEntSchema;
128
+ // @deprecated use EntSchemaWithTZ
81
129
  class BaseEntSchemaWithTZ {
82
130
  constructor() {
83
131
  this.patterns = [
@@ -85,6 +133,7 @@ class BaseEntSchemaWithTZ {
85
133
  // default schema added
86
134
  name: "nodeWithTZ",
87
135
  fields: nodeFieldsWithTZ,
136
+ disableMixin: true,
88
137
  },
89
138
  ];
90
139
  }
package/schema/field.d.ts CHANGED
@@ -146,7 +146,7 @@ export declare class ListField extends BaseField {
146
146
  validate(validator: (val: any[]) => boolean): this;
147
147
  valid(val: any): Promise<boolean>;
148
148
  private postgresVal;
149
- format(val: any): any;
149
+ format(val: any, nested?: boolean): any;
150
150
  minLen(l: number): this;
151
151
  maxLen(l: number): this;
152
152
  length(l: number): this;
package/schema/field.js CHANGED
@@ -597,13 +597,17 @@ class ListField extends BaseField {
597
597
  }
598
598
  return JSON.stringify(val);
599
599
  }
600
- format(val) {
600
+ format(val, nested) {
601
601
  if (!Array.isArray(val)) {
602
602
  throw new Error(`need an array to format`);
603
603
  }
604
604
  const elemDBType = this.type.listElemType.dbType;
605
605
  const jsonType = elemDBType === "JSON" || elemDBType === "JSONB";
606
- const postgres = db_1.default.getDialect() === db_1.Dialect.Postgres;
606
+ // postgres ish doesn't apply when nested
607
+ const postgres = !nested && db_1.default.getDialect() === db_1.Dialect.Postgres;
608
+ if (nested && !this.field.format) {
609
+ return val;
610
+ }
607
611
  if (!postgres && !this.field.format) {
608
612
  return JSON.stringify(val);
609
613
  }
@@ -612,7 +616,7 @@ class ListField extends BaseField {
612
616
  for (let i = 0; i < val.length; i++) {
613
617
  let formatted = val[i];
614
618
  if (this.field.format) {
615
- formatted = this.field.format(val[i]);
619
+ formatted = this.field.format(val[i], nested);
616
620
  }
617
621
  // postgres supports arrays natively so we
618
622
  // structure it in the expected format
@@ -629,6 +633,10 @@ class ListField extends BaseField {
629
633
  if (postgres) {
630
634
  return postgresRet + "}";
631
635
  }
636
+ // don't JSON.stringify if nested
637
+ if (nested) {
638
+ return ret;
639
+ }
632
640
  return JSON.stringify(ret);
633
641
  }
634
642
  minLen(l) {
package/schema/index.d.ts CHANGED
@@ -1,6 +1,8 @@
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
+ export * from "./struct_field";
8
+ export * from "./union_field";
package/schema/index.js CHANGED
@@ -10,19 +10,28 @@ 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);
36
+ __exportStar(require("./struct_field"), exports);
37
+ __exportStar(require("./union_field"), exports);
@@ -1,8 +1,16 @@
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
  };
7
+ interface FieldInfo {
8
+ dbCol: string;
9
+ inputKey: string;
10
+ }
11
+ export declare type FieldInfoMap = {
12
+ [key: string]: FieldInfo;
13
+ };
6
14
  export default interface Schema {
7
15
  fields: FieldMap | Field[];
8
16
  tableName?: string;
@@ -61,7 +69,29 @@ export declare type Edge = AssocEdge;
61
69
  export interface Pattern {
62
70
  name: string;
63
71
  fields: FieldMap | Field[];
72
+ disableMixin?: boolean;
64
73
  edges?: Edge[];
74
+ transformRead?: () => Clause;
75
+ transformWrite?: <T extends Ent>(stmt: UpdateOperation<T>) => TransformedUpdateOperation<T> | null;
76
+ transformsDelete?: boolean;
77
+ transformsInsert?: boolean;
78
+ transformsUpdate?: boolean;
79
+ }
80
+ export declare enum SQLStatementOperation {
81
+ Insert = "insert",
82
+ Update = "update",
83
+ Delete = "delete"
84
+ }
85
+ export interface UpdateOperation<T extends Ent> {
86
+ op: SQLStatementOperation;
87
+ existingEnt: T | null;
88
+ viewer: Viewer;
89
+ data?: Map<string, any>;
90
+ }
91
+ export interface TransformedUpdateOperation<T extends Ent> {
92
+ op: SQLStatementOperation;
93
+ data?: Data;
94
+ existingEnt?: T | null;
65
95
  }
66
96
  export declare enum DBType {
67
97
  UUID = "UUID",
@@ -85,6 +115,7 @@ export declare enum DBType {
85
115
  export interface ImportType {
86
116
  path: string;
87
117
  type: string;
118
+ [x: string]: any;
88
119
  }
89
120
  declare type EnumMap = {
90
121
  [key: string]: string;
@@ -97,6 +128,8 @@ export interface Type {
97
128
  values?: string[];
98
129
  enumMap?: EnumMap;
99
130
  importType?: ImportType;
131
+ subFields?: FieldMap;
132
+ unionFields?: FieldMap;
100
133
  }
101
134
  export interface ForeignKey {
102
135
  schema: string;
@@ -133,11 +166,13 @@ export interface FieldOptions {
133
166
  fieldEdge?: FieldEdge;
134
167
  primaryKey?: boolean;
135
168
  disableUserEditable?: boolean;
169
+ disableUserGraphQLEditable?: boolean;
136
170
  defaultValueOnCreate?(builder: Builder<Ent>, input: Data): any;
137
171
  defaultToViewerOnCreate?: boolean;
138
172
  defaultValueOnEdit?(builder: Builder<Ent>, input: Data): any;
139
173
  derivedWhenEmbedded?: boolean;
140
174
  polymorphic?: boolean | PolymorphicOptions;
175
+ privacyPolicy?: PrivacyPolicy | (() => PrivacyPolicy);
141
176
  getDerivedFields?(name: string): FieldMap;
142
177
  [x: string]: any;
143
178
  }
@@ -149,14 +184,27 @@ export interface PolymorphicOptions {
149
184
  export interface Field extends FieldOptions {
150
185
  type: Type;
151
186
  valid?(val: any): Promise<boolean> | boolean;
152
- format?(val: any): any;
187
+ format?(val: any, nested?: boolean): any;
153
188
  logValue(val: any): any;
154
189
  }
155
190
  export interface SchemaConstructor {
156
191
  new (): Schema;
157
192
  }
158
193
  export declare type SchemaInputType = Schema | SchemaConstructor;
194
+ export declare function getSchema(value: SchemaInputType): Schema;
159
195
  export declare function getFields(value: SchemaInputType): Map<string, Field>;
196
+ /**
197
+ * @deprecated should only be used by tests
198
+ */
199
+ export declare function getStorageKey(field: Field, fieldName: string): string;
200
+ export declare function getFieldsWithPrivacy(value: SchemaInputType, fieldMap: FieldInfoMap): Map<string, PrivacyPolicy>;
201
+ export declare function getTransformedReadClause(value: SchemaInputType): Clause | undefined;
202
+ interface objectLoaderOptions {
203
+ clause?: () => Clause | undefined;
204
+ instanceKey?: string;
205
+ }
206
+ export declare function getObjectLoaderProperties(value: SchemaInputType, tableName: string): objectLoaderOptions | undefined;
207
+ export declare function getTransformedUpdateOp<T extends Ent>(value: SchemaInputType, stmt: UpdateOperation<T>): TransformedUpdateOperation<T> | null;
160
208
  export declare enum ActionOperation {
161
209
  Create = 1,
162
210
  Edit = 2,
@@ -199,10 +247,24 @@ export interface Constraint {
199
247
  fkey?: ForeignKeyInfo;
200
248
  condition?: string;
201
249
  }
250
+ export interface FullTextWeight {
251
+ A?: string[];
252
+ B?: string[];
253
+ C?: string[];
254
+ D?: string[];
255
+ }
256
+ export interface FullText {
257
+ generatedColumnName?: string;
258
+ language?: "english" | "french" | "german" | "simple";
259
+ languageColumn?: string;
260
+ indexType?: "gin" | "gist";
261
+ weights?: FullTextWeight;
262
+ }
202
263
  export interface Index {
203
264
  name: string;
204
265
  columns: string[];
205
266
  unique?: boolean;
267
+ fulltext?: FullText;
206
268
  }
207
269
  export interface ForeignKeyInfo {
208
270
  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,108 @@ function getFields(value) {
68
86
  return m;
69
87
  }
70
88
  exports.getFields = getFields;
89
+ /**
90
+ * @deprecated should only be used by tests
91
+ */
92
+ function getStorageKey(field, fieldName) {
93
+ return field.storageKey || (0, snake_case_1.snakeCase)(fieldName);
94
+ }
95
+ exports.getStorageKey = getStorageKey;
96
+ // returns a mapping of storage key to field privacy
97
+ function getFieldsWithPrivacy(value, fieldMap) {
98
+ const schema = getSchema(value);
99
+ function addFields(fields) {
100
+ if (Array.isArray(fields)) {
101
+ for (const field of fields) {
102
+ const name = field.name;
103
+ if (!field.name) {
104
+ throw new Error(`name required`);
105
+ }
106
+ if (field.getDerivedFields !== undefined) {
107
+ addFields(field.getDerivedFields(name));
108
+ }
109
+ if (field.privacyPolicy) {
110
+ let privacyPolicy;
111
+ if (typeof field.privacyPolicy === "function") {
112
+ privacyPolicy = field.privacyPolicy();
113
+ }
114
+ else {
115
+ privacyPolicy = field.privacyPolicy;
116
+ }
117
+ const info = fieldMap[name];
118
+ if (!info) {
119
+ throw new Error(`field with name ${name} not passed in fieldMap`);
120
+ }
121
+ m.set(info.dbCol, privacyPolicy);
122
+ }
123
+ }
124
+ }
125
+ for (const name in fields) {
126
+ const field = fields[name];
127
+ if (field.getDerivedFields !== undefined) {
128
+ addFields(field.getDerivedFields(name));
129
+ }
130
+ if (field.privacyPolicy) {
131
+ let privacyPolicy;
132
+ if (typeof field.privacyPolicy === "function") {
133
+ privacyPolicy = field.privacyPolicy();
134
+ }
135
+ else {
136
+ privacyPolicy = field.privacyPolicy;
137
+ }
138
+ const info = fieldMap[name];
139
+ if (!info) {
140
+ throw new Error(`field with name ${name} not passed in fieldMap`);
141
+ }
142
+ m.set(info.dbCol, privacyPolicy);
143
+ }
144
+ }
145
+ }
146
+ let m = new Map();
147
+ if (schema.patterns) {
148
+ for (const pattern of schema.patterns) {
149
+ addFields(pattern.fields);
150
+ }
151
+ }
152
+ addFields(schema.fields);
153
+ return m;
154
+ }
155
+ exports.getFieldsWithPrivacy = getFieldsWithPrivacy;
156
+ function getTransformedReadClause(value) {
157
+ const schema = getSchema(value);
158
+ if (!schema.patterns) {
159
+ return;
160
+ }
161
+ for (const p of schema.patterns) {
162
+ // e.g. discarded_at, deleted_at, etc
163
+ if (p.transformRead) {
164
+ // return clause.Eq('deleted_at', null);
165
+ return p.transformRead();
166
+ }
167
+ }
168
+ return;
169
+ }
170
+ exports.getTransformedReadClause = getTransformedReadClause;
171
+ function getObjectLoaderProperties(value, tableName) {
172
+ return {
173
+ clause: () => getTransformedReadClause(value),
174
+ instanceKey: `${tableName}:transformedReadClause`,
175
+ };
176
+ }
177
+ exports.getObjectLoaderProperties = getObjectLoaderProperties;
178
+ function getTransformedUpdateOp(value, stmt) {
179
+ const schema = getSchema(value);
180
+ if (!schema.patterns) {
181
+ return null;
182
+ }
183
+ for (const p of schema.patterns) {
184
+ if (p.transformWrite) {
185
+ return p.transformWrite(stmt);
186
+ }
187
+ }
188
+ return null;
189
+ }
190
+ exports.getTransformedUpdateOp = getTransformedUpdateOp;
71
191
  // this maps to ActionOperation in ent/action.go
72
192
  var ActionOperation;
73
193
  (function (ActionOperation) {
@@ -0,0 +1,17 @@
1
+ import { BaseField, ListField } from "./field";
2
+ import { FieldOptions, Field, Type, FieldMap } from "./schema";
3
+ export interface StructOptions extends FieldOptions {
4
+ tsType: string;
5
+ fields: FieldMap;
6
+ graphQLType?: string;
7
+ jsonNotJSONB?: boolean;
8
+ }
9
+ export declare class StructField extends BaseField implements Field {
10
+ private options;
11
+ type: Type;
12
+ constructor(options: StructOptions);
13
+ format(obj: any, nested?: boolean): string | Object;
14
+ valid(obj: any): Promise<boolean>;
15
+ }
16
+ export declare function StructType(options: StructOptions): StructField & StructOptions;
17
+ export declare function StructListType(options: StructOptions): ListField;