@snowtop/ent 0.1.0-alpha → 0.1.0-alpha12

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 (86) hide show
  1. package/action/action.d.ts +2 -0
  2. package/action/executor.d.ts +1 -1
  3. package/action/orchestrator.d.ts +16 -3
  4. package/action/orchestrator.js +157 -47
  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/imports/index.d.ts +0 -1
  39. package/imports/index.js +3 -36
  40. package/index.d.ts +16 -1
  41. package/index.js +18 -5
  42. package/package.json +3 -3
  43. package/parse_schema/parse.d.ts +23 -4
  44. package/parse_schema/parse.js +87 -8
  45. package/schema/base_schema.d.ts +36 -1
  46. package/schema/base_schema.js +63 -17
  47. package/schema/field.d.ts +25 -25
  48. package/schema/field.js +42 -33
  49. package/schema/index.d.ts +4 -2
  50. package/schema/index.js +10 -1
  51. package/schema/json_field.d.ts +6 -6
  52. package/schema/json_field.js +2 -2
  53. package/schema/schema.d.ts +70 -6
  54. package/schema/schema.js +142 -10
  55. package/schema/struct_field.d.ts +17 -0
  56. package/schema/struct_field.js +102 -0
  57. package/schema/union_field.d.ts +23 -0
  58. package/schema/union_field.js +79 -0
  59. package/scripts/custom_compiler.js +2 -19
  60. package/scripts/custom_graphql.js +122 -15
  61. package/scripts/move_generated.d.ts +1 -0
  62. package/scripts/move_generated.js +142 -0
  63. package/scripts/read_schema.js +15 -1
  64. package/scripts/transform_code.d.ts +1 -0
  65. package/scripts/transform_code.js +113 -0
  66. package/scripts/transform_schema.d.ts +1 -0
  67. package/scripts/transform_schema.js +355 -0
  68. package/testutils/builder.d.ts +16 -9
  69. package/testutils/builder.js +80 -8
  70. package/testutils/context/test_context.d.ts +2 -2
  71. package/testutils/context/test_context.js +7 -1
  72. package/testutils/db/test_db.d.ts +2 -1
  73. package/testutils/db/test_db.js +22 -13
  74. package/testutils/ent-graphql-tests/index.d.ts +2 -0
  75. package/testutils/ent-graphql-tests/index.js +26 -17
  76. package/testutils/fake_data/fake_contact.d.ts +3 -7
  77. package/testutils/fake_data/fake_contact.js +14 -26
  78. package/testutils/fake_data/fake_event.d.ts +3 -7
  79. package/testutils/fake_data/fake_event.js +20 -33
  80. package/testutils/fake_data/fake_user.d.ts +3 -7
  81. package/testutils/fake_data/fake_user.js +22 -36
  82. package/testutils/fake_data/test_helpers.js +1 -1
  83. package/tsc/ast.d.ts +21 -0
  84. package/tsc/ast.js +154 -0
  85. package/tsc/compilerOptions.d.ts +8 -0
  86. package/tsc/compilerOptions.js +100 -0
package/schema/field.js CHANGED
@@ -40,19 +40,25 @@ class UUIDField extends BaseField {
40
40
  super();
41
41
  this.options = options;
42
42
  this.type = { dbType: schema_1.DBType.UUID };
43
- const polymorphic = options.polymorphic;
43
+ if (options?.fieldEdge?.enforceSchema &&
44
+ !options.fieldEdge.getLoaderInfoFromSchema) {
45
+ throw new Error(`cannot enforceSchema if getLoaderInfoFromSchema wasn't passed in`);
46
+ }
47
+ }
48
+ getDerivedFields(fieldName) {
49
+ const polymorphic = this.options?.polymorphic;
44
50
  if (polymorphic) {
45
51
  let name = "";
46
- if (options.name.endsWith("_id")) {
47
- let idx = options.name.indexOf("_id");
48
- name = options.name.substring(0, idx) + "_type";
52
+ if (fieldName.endsWith("_id")) {
53
+ let idx = fieldName.indexOf("_id");
54
+ name = fieldName.substring(0, idx) + "_type";
49
55
  }
50
- else if (options.name.endsWith("ID")) {
51
- let idx = options.name.indexOf("ID");
52
- name = options.name.substring(0, idx) + "Type";
56
+ else if (fieldName.endsWith("ID")) {
57
+ let idx = fieldName.indexOf("ID");
58
+ name = fieldName.substring(0, idx) + "Type";
53
59
  }
54
60
  else {
55
- throw new Error(`unsupported id polymorhpic type ${options.name}`);
61
+ throw new Error(`unsupported id polymorhpic type ${fieldName}`);
56
62
  }
57
63
  // polymorphic field automatically hidden from GraphQL
58
64
  // can be made visible with custom fields if user wants to change this behavior
@@ -60,38 +66,33 @@ class UUIDField extends BaseField {
60
66
  // intentionally not made private as it doesn't seem like it needs to be hidden
61
67
  if (typeof polymorphic === "object" && polymorphic.types) {
62
68
  // an enum with types validated here
63
- this.derivedFields = [
64
- EnumType({
65
- name,
69
+ return {
70
+ [name]: EnumType({
66
71
  values: polymorphic.types,
67
72
  hideFromGraphQL: true,
68
73
  derivedWhenEmbedded: true,
69
- nullable: options.nullable,
74
+ nullable: this.options?.nullable,
70
75
  }),
71
- ];
76
+ };
72
77
  }
73
78
  else {
74
79
  // just a string field...
75
- this.derivedFields = [
76
- StringType({
77
- name,
80
+ return {
81
+ [name]: StringType({
78
82
  hideFromGraphQL: true,
79
83
  derivedWhenEmbedded: true,
80
- nullable: options.nullable,
84
+ nullable: this.options?.nullable,
81
85
  }),
82
- ];
86
+ };
83
87
  }
84
88
  }
85
- if (options.fieldEdge?.enforceSchema &&
86
- !options.fieldEdge.getLoaderInfoFromSchema) {
87
- throw new Error(`cannot enforceSchema if getLoaderInfoFromSchema wasn't passed in`);
88
- }
89
+ return {};
89
90
  }
90
91
  isBuilder(val) {
91
92
  return val.placeholderID !== undefined;
92
93
  }
93
94
  async valid(val) {
94
- if (!this.options.fieldEdge?.enforceSchema) {
95
+ if (!this.options?.fieldEdge?.enforceSchema) {
95
96
  return true;
96
97
  }
97
98
  const getLoaderInfo = this.options.fieldEdge.getLoaderInfoFromSchema;
@@ -119,7 +120,7 @@ class IntegerField extends BaseField {
119
120
  super();
120
121
  this.type = { dbType: schema_1.DBType.Int };
121
122
  this.validators = [];
122
- this.options = { name: "field" };
123
+ this.options = {};
123
124
  // for legacy callers
124
125
  this.handleOptions(options || this.options);
125
126
  }
@@ -206,9 +207,9 @@ class StringField extends BaseField {
206
207
  this.type = { dbType: schema_1.DBType.String };
207
208
  this.validators = [];
208
209
  this.formatters = [];
209
- this.options = { name: "field" };
210
+ this.options = {};
210
211
  // for legacy callers
211
- this.handleOptions(options || { name: "field" });
212
+ this.handleOptions(options || {});
212
213
  }
213
214
  getOptions() {
214
215
  return this.options;
@@ -347,7 +348,7 @@ class TimestampField extends BaseField {
347
348
  }
348
349
  exports.TimestampField = TimestampField;
349
350
  function TimestampType(options) {
350
- let result = new TimestampField(options);
351
+ let result = new TimestampField({ ...options });
351
352
  return Object.assign(result, options);
352
353
  }
353
354
  exports.TimestampType = TimestampType;
@@ -374,7 +375,7 @@ class TimeField extends BaseField {
374
375
  constructor(options) {
375
376
  super();
376
377
  this.type = { dbType: schema_1.DBType.Time };
377
- if (options.withTimezone) {
378
+ if (options?.withTimezone) {
378
379
  this.type = {
379
380
  dbType: schema_1.DBType.Timetz,
380
381
  };
@@ -451,8 +452,8 @@ class EnumField extends BaseField {
451
452
  dbType: options.createEnumType ? schema_1.DBType.Enum : schema_1.DBType.StringEnum,
452
453
  values: options.values,
453
454
  enumMap: options.map,
454
- type: options.tsType || options.name,
455
- graphQLType: options.graphQLType || options.name,
455
+ type: options.tsType,
456
+ graphQLType: options.graphQLType,
456
457
  };
457
458
  if (!options.foreignKey) {
458
459
  if (!options.values && !options.map) {
@@ -596,13 +597,17 @@ class ListField extends BaseField {
596
597
  }
597
598
  return JSON.stringify(val);
598
599
  }
599
- format(val) {
600
+ format(val, nested) {
600
601
  if (!Array.isArray(val)) {
601
602
  throw new Error(`need an array to format`);
602
603
  }
603
604
  const elemDBType = this.type.listElemType.dbType;
604
605
  const jsonType = elemDBType === "JSON" || elemDBType === "JSONB";
605
- 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
+ }
606
611
  if (!postgres && !this.field.format) {
607
612
  return JSON.stringify(val);
608
613
  }
@@ -611,7 +616,7 @@ class ListField extends BaseField {
611
616
  for (let i = 0; i < val.length; i++) {
612
617
  let formatted = val[i];
613
618
  if (this.field.format) {
614
- formatted = this.field.format(val[i]);
619
+ formatted = this.field.format(val[i], nested);
615
620
  }
616
621
  // postgres supports arrays natively so we
617
622
  // structure it in the expected format
@@ -628,6 +633,10 @@ class ListField extends BaseField {
628
633
  if (postgres) {
629
634
  return postgresRet + "}";
630
635
  }
636
+ // don't JSON.stringify if nested
637
+ if (nested) {
638
+ return ret;
639
+ }
631
640
  return JSON.stringify(ret);
632
641
  }
633
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, 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);
@@ -5,13 +5,13 @@ export interface JSONOptions extends FieldOptions {
5
5
  importType?: ImportType;
6
6
  }
7
7
  export declare class JSONField extends BaseField implements Field {
8
- private options;
8
+ private options?;
9
9
  type: Type;
10
- constructor(jsonb: boolean, options: JSONOptions);
10
+ constructor(jsonb: boolean, options?: JSONOptions | undefined);
11
11
  format(val: any): string;
12
12
  valid(val: any): boolean;
13
13
  }
14
- export declare function JSONType(options: JSONOptions): JSONField;
15
- export declare function JSONBType(options: JSONOptions): JSONField;
16
- export declare function JSONBListType(options: JSONOptions): ListField;
17
- export declare function JSONListType(options: JSONOptions): ListField;
14
+ export declare function JSONType(options?: JSONOptions): JSONField;
15
+ export declare function JSONBType(options?: JSONOptions): JSONField;
16
+ export declare function JSONBListType(options?: JSONOptions): ListField;
17
+ export declare function JSONListType(options?: JSONOptions): ListField;
@@ -13,7 +13,7 @@ class JSONField extends field_1.BaseField {
13
13
  if (jsonb) {
14
14
  this.type.dbType = schema_1.DBType.JSONB;
15
15
  }
16
- if (options.importType) {
16
+ if (options?.importType) {
17
17
  this.type.importType = options.importType;
18
18
  }
19
19
  }
@@ -21,7 +21,7 @@ class JSONField extends field_1.BaseField {
21
21
  return JSON.stringify(val);
22
22
  }
23
23
  valid(val) {
24
- if (this.options.validator) {
24
+ if (this.options?.validator) {
25
25
  return this.options.validator(val);
26
26
  }
27
27
  return true;
@@ -1,7 +1,18 @@
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";
4
+ export declare type FieldMap = {
5
+ [key: string]: Field;
6
+ };
7
+ interface FieldInfo {
8
+ dbCol: string;
9
+ inputKey: string;
10
+ }
11
+ export declare type FieldInfoMap = {
12
+ [key: string]: FieldInfo;
13
+ };
3
14
  export default interface Schema {
4
- fields: Field[];
15
+ fields: FieldMap | Field[];
5
16
  tableName?: string;
6
17
  patterns?: Pattern[];
7
18
  edges?: Edge[];
@@ -57,8 +68,29 @@ export interface AssocEdgeGroup {
57
68
  export declare type Edge = AssocEdge;
58
69
  export interface Pattern {
59
70
  name: string;
60
- fields: Field[];
71
+ fields: FieldMap | Field[];
61
72
  edges?: Edge[];
73
+ transformRead?: () => Clause;
74
+ transformWrite?: <T extends Ent>(stmt: UpdateOperation<T>) => TransformedUpdateOperation<T> | undefined;
75
+ transformsDelete?: boolean;
76
+ transformsInsert?: boolean;
77
+ transformsUpdate?: boolean;
78
+ }
79
+ export declare enum SQLStatementOperation {
80
+ Insert = "insert",
81
+ Update = "update",
82
+ Delete = "delete"
83
+ }
84
+ export interface UpdateOperation<T extends Ent> {
85
+ op: SQLStatementOperation;
86
+ existingEnt?: T;
87
+ viewer: Viewer;
88
+ data?: Map<string, any>;
89
+ }
90
+ export interface TransformedUpdateOperation<T extends Ent> {
91
+ op: SQLStatementOperation;
92
+ data?: Data;
93
+ existingEnt?: T;
62
94
  }
63
95
  export declare enum DBType {
64
96
  UUID = "UUID",
@@ -82,6 +114,7 @@ export declare enum DBType {
82
114
  export interface ImportType {
83
115
  path: string;
84
116
  type: string;
117
+ [x: string]: any;
85
118
  }
86
119
  declare type EnumMap = {
87
120
  [key: string]: string;
@@ -94,6 +127,8 @@ export interface Type {
94
127
  values?: string[];
95
128
  enumMap?: EnumMap;
96
129
  importType?: ImportType;
130
+ subFields?: FieldMap;
131
+ unionFields?: FieldMap;
97
132
  }
98
133
  export interface ForeignKey {
99
134
  schema: string;
@@ -117,7 +152,6 @@ export interface FieldEdge {
117
152
  disableBuilderType?: boolean;
118
153
  }
119
154
  export interface FieldOptions {
120
- name: string;
121
155
  nullable?: boolean;
122
156
  storageKey?: string;
123
157
  serverDefault?: any;
@@ -131,12 +165,15 @@ export interface FieldOptions {
131
165
  fieldEdge?: FieldEdge;
132
166
  primaryKey?: boolean;
133
167
  disableUserEditable?: boolean;
168
+ disableUserGraphQLEditable?: boolean;
134
169
  defaultValueOnCreate?(builder: Builder<Ent>, input: Data): any;
135
170
  defaultToViewerOnCreate?: boolean;
136
171
  defaultValueOnEdit?(builder: Builder<Ent>, input: Data): any;
137
172
  derivedWhenEmbedded?: boolean;
138
173
  polymorphic?: boolean | PolymorphicOptions;
139
- derivedFields?: Field[];
174
+ privacyPolicy?: PrivacyPolicy | (() => PrivacyPolicy);
175
+ getDerivedFields?(name: string): FieldMap;
176
+ [x: string]: any;
140
177
  }
141
178
  export interface PolymorphicOptions {
142
179
  types?: string[];
@@ -146,14 +183,27 @@ export interface PolymorphicOptions {
146
183
  export interface Field extends FieldOptions {
147
184
  type: Type;
148
185
  valid?(val: any): Promise<boolean> | boolean;
149
- format?(val: any): any;
186
+ format?(val: any, nested?: boolean): any;
150
187
  logValue(val: any): any;
151
188
  }
152
189
  export interface SchemaConstructor {
153
190
  new (): Schema;
154
191
  }
155
192
  export declare type SchemaInputType = Schema | SchemaConstructor;
193
+ export declare function getSchema(value: SchemaInputType): Schema;
156
194
  export declare function getFields(value: SchemaInputType): Map<string, Field>;
195
+ /**
196
+ * @deprecated should only be used by tests
197
+ */
198
+ export declare function getStorageKey(field: Field, fieldName: string): string;
199
+ export declare function getFieldsWithPrivacy(value: SchemaInputType, fieldMap: FieldInfoMap): Map<string, PrivacyPolicy>;
200
+ export declare function getTransformedReadClause(value: SchemaInputType): Clause | undefined;
201
+ interface objectLoaderOptions {
202
+ clause?: () => Clause | undefined;
203
+ instanceKey?: string;
204
+ }
205
+ export declare function getObjectLoaderProperties(value: SchemaInputType, tableName: string): objectLoaderOptions | undefined;
206
+ export declare function getTransformedUpdateOp<T extends Ent>(value: SchemaInputType, stmt: UpdateOperation<T>): TransformedUpdateOperation<T> | undefined;
157
207
  export declare enum ActionOperation {
158
208
  Create = 1,
159
209
  Edit = 2,
@@ -196,10 +246,24 @@ export interface Constraint {
196
246
  fkey?: ForeignKeyInfo;
197
247
  condition?: string;
198
248
  }
249
+ export interface FullTextWeight {
250
+ A?: string[];
251
+ B?: string[];
252
+ C?: string[];
253
+ D?: string[];
254
+ }
255
+ export interface FullText {
256
+ generatedColumnName?: string;
257
+ language?: "english" | "french" | "german" | "simple";
258
+ languageColumn?: string;
259
+ indexType?: "gin" | "gist";
260
+ weights?: FullTextWeight;
261
+ }
199
262
  export interface Index {
200
263
  name: string;
201
264
  columns: string[];
202
265
  unique?: boolean;
266
+ fulltext?: FullText;
203
267
  }
204
268
  export interface ForeignKeyInfo {
205
269
  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,21 +43,37 @@ 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
- for (const field of fields) {
41
- const derivedFields = field.derivedFields;
42
- if (derivedFields !== undefined) {
43
- addFields(derivedFields);
58
+ if (Array.isArray(fields)) {
59
+ for (const field of fields) {
60
+ const name = field.name;
61
+ if (!name) {
62
+ throw new Error(`name required`);
63
+ }
64
+ if (field.getDerivedFields !== undefined) {
65
+ addFields(field.getDerivedFields(name));
66
+ }
67
+ m.set(name, field);
68
+ }
69
+ return;
70
+ }
71
+ for (const name in fields) {
72
+ const field = fields[name];
73
+ if (field.getDerivedFields !== undefined) {
74
+ addFields(field.getDerivedFields(name));
44
75
  }
45
- m.set(field.name, field);
76
+ m.set(name, field);
46
77
  }
47
78
  }
48
79
  let m = new Map();
@@ -55,6 +86,107 @@ function getFields(value) {
55
86
  return m;
56
87
  }
57
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;
182
+ }
183
+ for (const p of schema.patterns) {
184
+ if (p.transformWrite) {
185
+ return p.transformWrite(stmt);
186
+ }
187
+ }
188
+ }
189
+ exports.getTransformedUpdateOp = getTransformedUpdateOp;
58
190
  // this maps to ActionOperation in ent/action.go
59
191
  var ActionOperation;
60
192
  (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;