@snowtop/ent 0.1.0-alpha9 → 0.1.0-alpha91

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 (135) hide show
  1. package/action/action.d.ts +36 -31
  2. package/action/action.js +2 -6
  3. package/action/executor.d.ts +3 -3
  4. package/action/executor.js +2 -2
  5. package/action/experimental_action.d.ts +29 -22
  6. package/action/experimental_action.js +29 -6
  7. package/action/orchestrator.d.ts +38 -16
  8. package/action/orchestrator.js +223 -61
  9. package/action/privacy.d.ts +2 -2
  10. package/core/base.d.ts +45 -24
  11. package/core/base.js +7 -1
  12. package/core/clause.d.ts +83 -7
  13. package/core/clause.js +334 -63
  14. package/core/config.d.ts +8 -0
  15. package/core/config.js +5 -1
  16. package/core/context.d.ts +5 -3
  17. package/core/context.js +20 -2
  18. package/core/convert.d.ts +1 -1
  19. package/core/db.d.ts +2 -2
  20. package/core/db.js +6 -2
  21. package/core/ent.d.ts +79 -24
  22. package/core/ent.js +527 -176
  23. package/core/loaders/assoc_count_loader.d.ts +3 -2
  24. package/core/loaders/assoc_count_loader.js +14 -2
  25. package/core/loaders/assoc_edge_loader.d.ts +2 -2
  26. package/core/loaders/assoc_edge_loader.js +5 -1
  27. package/core/loaders/index.d.ts +1 -1
  28. package/core/loaders/index.js +1 -3
  29. package/core/loaders/index_loader.d.ts +2 -2
  30. package/core/loaders/loader.js +5 -5
  31. package/core/loaders/object_loader.d.ts +6 -5
  32. package/core/loaders/object_loader.js +67 -59
  33. package/core/loaders/query_loader.d.ts +6 -12
  34. package/core/loaders/query_loader.js +52 -11
  35. package/core/loaders/raw_count_loader.d.ts +2 -2
  36. package/core/loaders/raw_count_loader.js +5 -1
  37. package/core/logger.d.ts +1 -1
  38. package/core/logger.js +1 -0
  39. package/core/privacy.d.ts +26 -25
  40. package/core/privacy.js +21 -25
  41. package/core/query/assoc_query.d.ts +7 -6
  42. package/core/query/assoc_query.js +9 -1
  43. package/core/query/custom_clause_query.d.ts +26 -0
  44. package/core/query/custom_clause_query.js +78 -0
  45. package/core/query/custom_query.d.ts +20 -5
  46. package/core/query/custom_query.js +87 -12
  47. package/core/query/index.d.ts +1 -0
  48. package/core/query/index.js +3 -1
  49. package/core/query/query.d.ts +8 -4
  50. package/core/query/query.js +101 -53
  51. package/core/query/shared_assoc_test.d.ts +2 -1
  52. package/core/query/shared_assoc_test.js +34 -43
  53. package/core/query/shared_test.d.ts +8 -1
  54. package/core/query/shared_test.js +470 -236
  55. package/core/viewer.d.ts +3 -3
  56. package/core/viewer.js +1 -1
  57. package/graphql/graphql.js +16 -6
  58. package/graphql/query/edge_connection.d.ts +9 -9
  59. package/graphql/query/page_info.d.ts +1 -1
  60. package/graphql/query/shared_edge_connection.js +1 -15
  61. package/imports/index.js +5 -1
  62. package/index.d.ts +11 -5
  63. package/index.js +20 -7
  64. package/package.json +1 -1
  65. package/parse_schema/parse.d.ts +12 -3
  66. package/parse_schema/parse.js +70 -11
  67. package/schema/base_schema.js +3 -0
  68. package/schema/field.d.ts +44 -8
  69. package/schema/field.js +136 -10
  70. package/schema/index.d.ts +2 -2
  71. package/schema/index.js +5 -1
  72. package/schema/json_field.d.ts +13 -1
  73. package/schema/json_field.js +28 -1
  74. package/schema/schema.d.ts +66 -11
  75. package/schema/schema.js +18 -4
  76. package/schema/struct_field.d.ts +11 -1
  77. package/schema/struct_field.js +44 -5
  78. package/scripts/custom_compiler.js +10 -6
  79. package/scripts/custom_graphql.js +13 -4
  80. package/scripts/{transform_schema.d.ts → migrate_v0.1.d.ts} +0 -0
  81. package/scripts/migrate_v0.1.js +36 -0
  82. package/scripts/read_schema.js +20 -5
  83. package/testutils/builder.d.ts +31 -21
  84. package/testutils/builder.js +83 -29
  85. package/testutils/db/fixture.d.ts +10 -0
  86. package/testutils/db/fixture.js +26 -0
  87. package/testutils/db/{test_db.d.ts → temp_db.d.ts} +20 -7
  88. package/testutils/db/{test_db.js → temp_db.js} +102 -36
  89. package/testutils/db/value.d.ts +6 -0
  90. package/testutils/db/value.js +251 -0
  91. package/testutils/db_mock.js +3 -1
  92. package/testutils/db_time_zone.d.ts +4 -0
  93. package/testutils/db_time_zone.js +41 -0
  94. package/testutils/ent-graphql-tests/index.js +8 -1
  95. package/testutils/fake_data/const.d.ts +2 -1
  96. package/testutils/fake_data/const.js +3 -0
  97. package/testutils/fake_data/fake_contact.d.ts +7 -4
  98. package/testutils/fake_data/fake_contact.js +14 -6
  99. package/testutils/fake_data/fake_event.d.ts +5 -3
  100. package/testutils/fake_data/fake_event.js +8 -5
  101. package/testutils/fake_data/fake_tag.d.ts +35 -0
  102. package/testutils/fake_data/fake_tag.js +88 -0
  103. package/testutils/fake_data/fake_user.d.ts +6 -4
  104. package/testutils/fake_data/fake_user.js +16 -13
  105. package/testutils/fake_data/index.js +5 -1
  106. package/testutils/fake_data/internal.d.ts +2 -0
  107. package/testutils/fake_data/internal.js +7 -1
  108. package/testutils/fake_data/tag_query.d.ts +13 -0
  109. package/testutils/fake_data/tag_query.js +43 -0
  110. package/testutils/fake_data/test_helpers.d.ts +11 -4
  111. package/testutils/fake_data/test_helpers.js +28 -12
  112. package/testutils/fake_data/user_query.d.ts +13 -6
  113. package/testutils/fake_data/user_query.js +54 -22
  114. package/testutils/fake_log.d.ts +3 -3
  115. package/testutils/parse_sql.d.ts +6 -0
  116. package/testutils/parse_sql.js +16 -2
  117. package/testutils/test_edge_global_schema.d.ts +15 -0
  118. package/testutils/test_edge_global_schema.js +62 -0
  119. package/testutils/write.d.ts +2 -2
  120. package/testutils/write.js +33 -7
  121. package/tsc/ast.d.ts +44 -0
  122. package/tsc/ast.js +271 -0
  123. package/tsc/compilerOptions.d.ts +6 -0
  124. package/tsc/compilerOptions.js +45 -2
  125. package/tsc/move_generated.d.ts +1 -0
  126. package/tsc/move_generated.js +164 -0
  127. package/tsc/transform.d.ts +21 -0
  128. package/tsc/transform.js +171 -0
  129. package/tsc/transform_action.d.ts +22 -0
  130. package/tsc/transform_action.js +183 -0
  131. package/tsc/transform_ent.d.ts +17 -0
  132. package/tsc/transform_ent.js +59 -0
  133. package/tsc/transform_schema.d.ts +27 -0
  134. package/tsc/transform_schema.js +383 -0
  135. package/scripts/transform_schema.js +0 -445
package/schema/field.js CHANGED
@@ -1,7 +1,11 @@
1
1
  "use strict";
2
2
  var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
3
  if (k2 === undefined) k2 = k;
4
- Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
5
9
  }) : (function(o, m, k, k2) {
6
10
  if (k2 === undefined) k2 = k;
7
11
  o[k2] = m[k];
@@ -19,12 +23,14 @@ var __importStar = (this && this.__importStar) || function (mod) {
19
23
  return result;
20
24
  };
21
25
  Object.defineProperty(exports, "__esModule", { value: true });
22
- exports.UUIDListType = exports.EnumListType = exports.DateListType = exports.TimetzListType = exports.TimeListType = exports.TimestamptzListType = exports.TimestampListType = exports.BooleanListType = exports.BigIntegerListType = exports.FloatListType = exports.IntegerListType = exports.IntListType = exports.StringListType = exports.ListField = exports.EnumType = exports.EnumField = exports.DateType = exports.DateField = exports.TimetzType = exports.TimeType = exports.TimeField = exports.leftPad = exports.TimestamptzType = exports.TimestampType = exports.TimestampField = exports.StringType = exports.StringField = exports.BooleanType = exports.BooleanField = exports.FloatType = exports.FloatField = exports.BigIntegerType = exports.BigIntegerField = exports.IntegerType = exports.IntegerField = exports.UUIDType = exports.UUIDField = exports.BaseField = void 0;
26
+ exports.UUIDListType = exports.IntegerEnumListType = exports.EnumListType = exports.DateListType = exports.TimetzListType = exports.TimeListType = exports.TimestamptzListType = exports.TimestampListType = exports.BooleanListType = exports.BigIntegerListType = exports.FloatListType = exports.IntegerListType = exports.IntListType = exports.StringListType = exports.ListField = exports.IntegerEnumType = exports.IntegerEnumField = exports.EnumType = exports.StringEnumField = exports.EnumField = exports.DateType = exports.DateField = exports.TimetzType = exports.TimeType = exports.TimeField = exports.leftPad = exports.TimestamptzType = exports.TimestampType = exports.TimestampField = exports.StringType = exports.StringField = exports.BooleanType = exports.BooleanField = exports.FloatType = exports.FloatField = exports.BigIntegerType = exports.BigIntegerField = exports.IntegerType = exports.IntegerField = exports.UUIDType = exports.UUIDField = exports.BaseField = void 0;
23
27
  const luxon_1 = require("luxon");
24
28
  const snake_case_1 = require("snake-case");
29
+ const util_1 = require("util");
30
+ const uuid_1 = require("uuid");
31
+ const base_1 = require("../core/base");
25
32
  const db_1 = __importStar(require("../core/db"));
26
33
  const schema_1 = require("./schema");
27
- const util_1 = require("util");
28
34
  class BaseField {
29
35
  logValue(val) {
30
36
  if (this.sensitive) {
@@ -67,21 +73,28 @@ class UUIDField extends BaseField {
67
73
  if (typeof polymorphic === "object" && polymorphic.types) {
68
74
  // an enum with types validated here
69
75
  return {
70
- [name]: EnumType({
76
+ [name]: PolymorphicStringEnumType({
71
77
  values: polymorphic.types,
72
78
  hideFromGraphQL: true,
73
79
  derivedWhenEmbedded: true,
74
80
  nullable: this.options?.nullable,
81
+ parentFieldToValidate: fieldName,
82
+ serverDefault: polymorphic.serverDefault,
75
83
  }),
76
84
  };
77
85
  }
78
86
  else {
79
- // just a string field...
87
+ let serverDefault = undefined;
88
+ if (typeof polymorphic === "object") {
89
+ serverDefault = polymorphic.serverDefault;
90
+ }
80
91
  return {
81
- [name]: StringType({
92
+ [name]: PolymorphicStringType({
82
93
  hideFromGraphQL: true,
83
94
  derivedWhenEmbedded: true,
84
95
  nullable: this.options?.nullable,
96
+ parentFieldToValidate: fieldName,
97
+ serverDefault: serverDefault,
85
98
  }),
86
99
  };
87
100
  }
@@ -92,6 +105,9 @@ class UUIDField extends BaseField {
92
105
  return val.placeholderID !== undefined;
93
106
  }
94
107
  async valid(val) {
108
+ if (typeof val === "string" && !(0, uuid_1.validate)(val)) {
109
+ return false;
110
+ }
95
111
  if (!this.options?.fieldEdge?.enforceSchema) {
96
112
  return true;
97
113
  }
@@ -307,6 +323,33 @@ class StringField extends BaseField {
307
323
  }
308
324
  }
309
325
  exports.StringField = StringField;
326
+ function validatePolymorphicTypeWithFullData(val, b, field) {
327
+ const input = b.getInput();
328
+ const inputKey = b.orchestrator.__getOptions().fieldInfo[field].inputKey;
329
+ const v = input[inputKey];
330
+ if (val === null) {
331
+ // if this is being set to null, ok if v is also null
332
+ return v === null;
333
+ }
334
+ // if this is not being set, ok if v is not being set
335
+ if (val === undefined && b.operation === base_1.WriteOperation.Insert) {
336
+ return v === undefined;
337
+ }
338
+ return true;
339
+ }
340
+ class PolymorphicStringField extends StringField {
341
+ constructor(opts) {
342
+ super(opts);
343
+ this.opts = opts;
344
+ }
345
+ validateWithFullData(val, b) {
346
+ return validatePolymorphicTypeWithFullData(val, b, this.opts.parentFieldToValidate);
347
+ }
348
+ }
349
+ function PolymorphicStringType(opts) {
350
+ let result = new PolymorphicStringField(opts);
351
+ return Object.assign(result, opts);
352
+ }
310
353
  function StringType(options) {
311
354
  let result = new StringField(options);
312
355
  const options2 = { ...options };
@@ -426,9 +469,10 @@ class DateField extends BaseField {
426
469
  this.type = { dbType: schema_1.DBType.Date };
427
470
  }
428
471
  format(val) {
429
- if (!(val instanceof Date)) {
472
+ if (typeof val === "string") {
430
473
  return val;
431
474
  }
475
+ val = new Date(val);
432
476
  let yy = (0, exports.leftPad)(val.getFullYear());
433
477
  // lol this API
434
478
  // for some reason this is 0-index
@@ -444,6 +488,9 @@ function DateType(options) {
444
488
  return Object.assign(result, options);
445
489
  }
446
490
  exports.DateType = DateType;
491
+ /**
492
+ * @deprecated Use StringEnumField
493
+ */
447
494
  class EnumField extends BaseField {
448
495
  constructor(options) {
449
496
  super();
@@ -546,15 +593,75 @@ class EnumField extends BaseField {
546
593
  }
547
594
  }
548
595
  exports.EnumField = EnumField;
596
+ class StringEnumField extends EnumField {
597
+ }
598
+ exports.StringEnumField = StringEnumField;
599
+ class PolymorphicStringEnumField extends StringEnumField {
600
+ constructor(opts) {
601
+ super(opts);
602
+ this.opts = opts;
603
+ }
604
+ validateWithFullData(val, b) {
605
+ return validatePolymorphicTypeWithFullData(val, b, this.opts.parentFieldToValidate);
606
+ }
607
+ }
608
+ function PolymorphicStringEnumType(options) {
609
+ let result = new PolymorphicStringEnumField(options);
610
+ return Object.assign(result, options);
611
+ }
549
612
  function EnumType(options) {
550
- let result = new EnumField(options);
613
+ let result = new StringEnumField(options);
551
614
  return Object.assign(result, options);
552
615
  }
553
616
  exports.EnumType = EnumType;
617
+ class IntegerEnumField extends BaseField {
618
+ constructor(options) {
619
+ super();
620
+ this.type = {
621
+ dbType: schema_1.DBType.IntEnum,
622
+ intEnumMap: options.map,
623
+ type: options.tsType,
624
+ graphQLType: options.graphQLType,
625
+ deprecatedIntEnumMap: options.deprecated,
626
+ };
627
+ let count = 0;
628
+ for (const k in options.map) {
629
+ count++;
630
+ break;
631
+ }
632
+ if (!count) {
633
+ throw new Error("need at least one entry in enum map");
634
+ }
635
+ if (options.foreignKey) {
636
+ throw new Error(`foreignKey on intEnum not supported`);
637
+ }
638
+ this.map = options.map;
639
+ }
640
+ valid(val) {
641
+ // lookup table enum and indicated via presence of foreignKey
642
+ for (const k in this.map) {
643
+ const v = this.map[k];
644
+ if (v === val || v === parseInt(val)) {
645
+ return true;
646
+ }
647
+ }
648
+ return false;
649
+ }
650
+ format(val) {
651
+ return parseInt(val);
652
+ }
653
+ }
654
+ exports.IntegerEnumField = IntegerEnumField;
655
+ function IntegerEnumType(options) {
656
+ let result = new IntegerEnumField(options);
657
+ return Object.assign(result, options);
658
+ }
659
+ exports.IntegerEnumType = IntegerEnumType;
554
660
  class ListField extends BaseField {
555
661
  constructor(field, options) {
556
662
  super();
557
663
  this.field = field;
664
+ this.options = options;
558
665
  this.validators = [];
559
666
  if (field.type.dbType === schema_1.DBType.List) {
560
667
  throw new Error(`nested lists not currently supported`);
@@ -565,6 +672,9 @@ class ListField extends BaseField {
565
672
  };
566
673
  Object.assign(this, options);
567
674
  }
675
+ __getElemField() {
676
+ return this.field;
677
+ }
568
678
  validate(validator) {
569
679
  this.validators.push(validator);
570
680
  return this;
@@ -592,7 +702,12 @@ class ListField extends BaseField {
592
702
  return result;
593
703
  }
594
704
  postgresVal(val, jsonType) {
595
- if (!jsonType) {
705
+ if (!jsonType && val === "") {
706
+ // support empty strings in list
707
+ val = '"' + val + '"';
708
+ return val;
709
+ }
710
+ if (this.options?.disableJSONStringify) {
596
711
  return val;
597
712
  }
598
713
  return JSON.stringify(val);
@@ -720,7 +835,18 @@ function EnumListType(options) {
720
835
  return new ListField(EnumType(options), options);
721
836
  }
722
837
  exports.EnumListType = EnumListType;
838
+ function IntegerEnumListType(options) {
839
+ // not all of these will make sense in a list...
840
+ // can make it work eventually but involves work we're not currently trying to do
841
+ // developer can try to work around it by calling below on their own.
842
+ // unclear what the behavior is
843
+ return new ListField(IntegerEnumType(options), options);
844
+ }
845
+ exports.IntegerEnumListType = IntegerEnumListType;
723
846
  function UUIDListType(options) {
724
- return new ListField(UUIDType(options), options);
847
+ return new ListField(UUIDType(options), {
848
+ ...options,
849
+ disableJSONStringify: true,
850
+ });
725
851
  }
726
852
  exports.UUIDListType = UUIDListType;
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, 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";
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, EdgeUpdateOperation, TransformedEdgeUpdateOperation, getTransformedReadClause, getObjectLoaderProperties, GlobalSchema, } from "./schema";
4
+ export { Timestamps, Node, BaseEntSchema, BaseEntSchemaWithTZ, EntSchema, EntSchemaWithTZ, SchemaConfig, } 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
@@ -1,7 +1,11 @@
1
1
  "use strict";
2
2
  var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
3
  if (k2 === undefined) k2 = k;
4
- Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
5
9
  }) : (function(o, m, k, k2) {
6
10
  if (k2 === undefined) k2 = k;
7
11
  o[k2] = m[k];
@@ -4,14 +4,26 @@ export interface JSONOptions extends FieldOptions {
4
4
  validator?: (val: any) => boolean;
5
5
  importType?: ImportType;
6
6
  }
7
+ interface allJSONOptions extends JSONOptions {
8
+ jsonAsList?: boolean;
9
+ }
7
10
  export declare class JSONField extends BaseField implements Field {
8
11
  private options?;
9
12
  type: Type;
10
- constructor(jsonb: boolean, options?: JSONOptions | undefined);
13
+ constructor(jsonb: boolean, options?: allJSONOptions | undefined);
11
14
  format(val: any): string;
12
15
  valid(val: any): boolean;
13
16
  }
14
17
  export declare function JSONType(options?: JSONOptions): JSONField;
15
18
  export declare function JSONBType(options?: JSONOptions): JSONField;
19
+ /**
20
+ * @deprecated use JSONBTypeAsList
21
+ */
16
22
  export declare function JSONBListType(options?: JSONOptions): ListField;
23
+ /**
24
+ * @deprecated use JSONTypeAsList
25
+ */
17
26
  export declare function JSONListType(options?: JSONOptions): ListField;
27
+ export declare function JSONBTypeAsList(options?: JSONOptions): JSONField & JSONOptions;
28
+ export declare function JSONTypeAsList(options?: JSONOptions): JSONField & JSONOptions;
29
+ export {};
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.JSONListType = exports.JSONBListType = exports.JSONBType = exports.JSONType = exports.JSONField = void 0;
3
+ exports.JSONTypeAsList = exports.JSONBTypeAsList = exports.JSONListType = exports.JSONBListType = exports.JSONBType = exports.JSONType = exports.JSONField = void 0;
4
4
  const schema_1 = require("./schema");
5
5
  const field_1 = require("./field");
6
6
  class JSONField extends field_1.BaseField {
@@ -16,6 +16,11 @@ class JSONField extends field_1.BaseField {
16
16
  if (options?.importType) {
17
17
  this.type.importType = options.importType;
18
18
  }
19
+ if (options?.jsonAsList) {
20
+ this.type.listElemType = {
21
+ dbType: schema_1.DBType.JSONB,
22
+ };
23
+ }
19
24
  }
20
25
  format(val) {
21
26
  return JSON.stringify(val);
@@ -38,11 +43,33 @@ function JSONBType(options) {
38
43
  return Object.assign(result, options);
39
44
  }
40
45
  exports.JSONBType = JSONBType;
46
+ /**
47
+ * @deprecated use JSONBTypeAsList
48
+ */
41
49
  function JSONBListType(options) {
42
50
  return new field_1.ListField(JSONBType(options), options);
43
51
  }
44
52
  exports.JSONBListType = JSONBListType;
53
+ /**
54
+ * @deprecated use JSONTypeAsList
55
+ */
45
56
  function JSONListType(options) {
46
57
  return new field_1.ListField(JSONType(options), options);
47
58
  }
48
59
  exports.JSONListType = JSONListType;
60
+ function JSONBTypeAsList(options) {
61
+ let result = new JSONField(true, {
62
+ ...options,
63
+ jsonAsList: true,
64
+ });
65
+ return Object.assign(result, options);
66
+ }
67
+ exports.JSONBTypeAsList = JSONBTypeAsList;
68
+ function JSONTypeAsList(options) {
69
+ let result = new JSONField(false, {
70
+ ...options,
71
+ jsonAsList: true,
72
+ });
73
+ return Object.assign(result, options);
74
+ }
75
+ exports.JSONTypeAsList = JSONTypeAsList;
@@ -1,9 +1,23 @@
1
1
  import { Data, Ent, LoaderInfo, PrivacyPolicy, Viewer } from "../core/base";
2
- import { Builder } from "../action/action";
2
+ import { Builder, Changeset } from "../action/action";
3
3
  import { Clause } from "../core/clause";
4
+ import { AssocEdgeInput } from "../core/ent";
4
5
  export declare type FieldMap = {
5
6
  [key: string]: Field;
6
7
  };
8
+ interface FieldInfo {
9
+ dbCol: string;
10
+ inputKey: string;
11
+ }
12
+ export declare type FieldInfoMap = {
13
+ [key: string]: FieldInfo;
14
+ };
15
+ export interface GlobalSchema {
16
+ edges?: Edge[];
17
+ extraEdgeFields?: FieldMap;
18
+ transformEdgeRead?: () => Clause;
19
+ transformEdgeWrite?: (stmt: EdgeUpdateOperation) => TransformedEdgeUpdateOperation | null;
20
+ }
7
21
  export default interface Schema {
8
22
  fields: FieldMap | Field[];
9
23
  tableName?: string;
@@ -54,7 +68,8 @@ export interface AssocEdgeGroup {
54
68
  tableName?: string;
55
69
  assocEdges: AssocEdge[];
56
70
  statusEnums?: string[];
57
- nullStates: string | string[];
71
+ viewerBased?: boolean;
72
+ nullStates?: string | string[];
58
73
  nullStateFn?: string;
59
74
  edgeAction?: EdgeGroupAction;
60
75
  }
@@ -62,9 +77,10 @@ export declare type Edge = AssocEdge;
62
77
  export interface Pattern {
63
78
  name: string;
64
79
  fields: FieldMap | Field[];
80
+ disableMixin?: boolean;
65
81
  edges?: Edge[];
66
82
  transformRead?: () => Clause;
67
- transformWrite?: <T extends Ent>(stmt: UpdateOperation<T>) => TransformedUpdateOperation<T> | undefined;
83
+ transformWrite?: <T extends Ent<TViewer>, TViewer extends Viewer = Viewer>(stmt: UpdateOperation<T, TViewer>) => TransformedUpdateOperation<T, TViewer> | null;
68
84
  transformsDelete?: boolean;
69
85
  transformsInsert?: boolean;
70
86
  transformsUpdate?: boolean;
@@ -74,16 +90,25 @@ export declare enum SQLStatementOperation {
74
90
  Update = "update",
75
91
  Delete = "delete"
76
92
  }
77
- export interface UpdateOperation<T extends Ent> {
93
+ export interface EdgeUpdateOperation {
94
+ op: SQLStatementOperation;
95
+ edge: AssocEdgeInput;
96
+ }
97
+ export interface TransformedEdgeUpdateOperation {
78
98
  op: SQLStatementOperation;
79
- existingEnt?: T;
80
- viewer: Viewer;
99
+ data?: Data;
100
+ }
101
+ export interface UpdateOperation<TEnt extends Ent<TViewer>, TViewer extends Viewer = Viewer> {
102
+ op: SQLStatementOperation;
103
+ builder: Builder<TEnt, TViewer, any>;
104
+ input: Data;
81
105
  data?: Map<string, any>;
82
106
  }
83
- export interface TransformedUpdateOperation<T extends Ent> {
107
+ export interface TransformedUpdateOperation<T extends Ent<TViewer>, TViewer extends Viewer = Viewer> {
84
108
  op: SQLStatementOperation;
85
109
  data?: Data;
86
- existingEnt?: T;
110
+ existingEnt?: T | null;
111
+ changeset?(): Promise<Changeset> | Changeset;
87
112
  }
88
113
  export declare enum DBType {
89
114
  UUID = "UUID",
@@ -99,6 +124,7 @@ export declare enum DBType {
99
124
  JSONB = "JSONB",
100
125
  Enum = "Enum",
101
126
  StringEnum = "StringEnum",
127
+ IntEnum = "IntEnum",
102
128
  Date = "Date",
103
129
  Time = "Time",
104
130
  Timetz = "Timetz",
@@ -109,9 +135,16 @@ export interface ImportType {
109
135
  type: string;
110
136
  [x: string]: any;
111
137
  }
138
+ export interface ConvertType {
139
+ path: string;
140
+ function: string;
141
+ }
112
142
  declare type EnumMap = {
113
143
  [key: string]: string;
114
144
  };
145
+ declare type IntEnumMap = {
146
+ [key: string]: number;
147
+ };
115
148
  export interface Type {
116
149
  dbType: DBType;
117
150
  listElemType?: Type;
@@ -119,9 +152,12 @@ export interface Type {
119
152
  graphQLType?: string;
120
153
  values?: string[];
121
154
  enumMap?: EnumMap;
155
+ intEnumMap?: IntEnumMap;
156
+ deprecatedIntEnumMap?: IntEnumMap;
122
157
  importType?: ImportType;
123
158
  subFields?: FieldMap;
124
159
  unionFields?: FieldMap;
160
+ [x: string]: any;
125
161
  }
126
162
  export interface ForeignKey {
127
163
  schema: string;
@@ -129,6 +165,7 @@ export interface ForeignKey {
129
165
  name?: string;
130
166
  disableIndex?: boolean;
131
167
  disableBuilderType?: boolean;
168
+ [x: string]: any;
132
169
  }
133
170
  declare type getLoaderInfoFn = (type: string) => LoaderInfo;
134
171
  export interface InverseFieldEdge {
@@ -144,13 +181,16 @@ export interface FieldEdge {
144
181
  getLoaderInfoFromSchema?: getLoaderInfoFn;
145
182
  disableBuilderType?: boolean;
146
183
  }
184
+ interface PrivateOptions {
185
+ exposeToActions?: boolean;
186
+ }
147
187
  export interface FieldOptions {
148
188
  nullable?: boolean;
149
189
  storageKey?: string;
150
190
  serverDefault?: any;
151
191
  unique?: boolean;
152
192
  hideFromGraphQL?: boolean;
153
- private?: boolean;
193
+ private?: boolean | PrivateOptions;
154
194
  sensitive?: boolean;
155
195
  graphqlName?: string;
156
196
  index?: boolean;
@@ -166,16 +206,21 @@ export interface FieldOptions {
166
206
  polymorphic?: boolean | PolymorphicOptions;
167
207
  privacyPolicy?: PrivacyPolicy | (() => PrivacyPolicy);
168
208
  getDerivedFields?(name: string): FieldMap;
209
+ convert?: ConvertType;
210
+ fetchOnDemand?: boolean;
169
211
  [x: string]: any;
170
212
  }
171
213
  export interface PolymorphicOptions {
214
+ name?: string;
172
215
  types?: string[];
173
216
  hideFromInverseGraphQL?: boolean;
174
217
  disableBuilderType?: boolean;
218
+ serverDefault?: any;
175
219
  }
176
220
  export interface Field extends FieldOptions {
177
221
  type: Type;
178
222
  valid?(val: any): Promise<boolean> | boolean;
223
+ validateWithFullData?(val: any, builder: Builder<any>): boolean | Promise<boolean>;
179
224
  format?(val: any, nested?: boolean): any;
180
225
  logValue(val: any): any;
181
226
  }
@@ -185,15 +230,18 @@ export interface SchemaConstructor {
185
230
  export declare type SchemaInputType = Schema | SchemaConstructor;
186
231
  export declare function getSchema(value: SchemaInputType): Schema;
187
232
  export declare function getFields(value: SchemaInputType): Map<string, Field>;
233
+ /**
234
+ * @deprecated should only be used by tests
235
+ */
188
236
  export declare function getStorageKey(field: Field, fieldName: string): string;
189
- export declare function getFieldsWithPrivacy(value: SchemaInputType): Map<string, PrivacyPolicy>;
237
+ export declare function getFieldsWithPrivacy(value: SchemaInputType, fieldMap: FieldInfoMap): Map<string, PrivacyPolicy>;
190
238
  export declare function getTransformedReadClause(value: SchemaInputType): Clause | undefined;
191
239
  interface objectLoaderOptions {
192
240
  clause?: () => Clause | undefined;
193
241
  instanceKey?: string;
194
242
  }
195
243
  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;
244
+ export declare function getTransformedUpdateOp<TEnt extends Ent<TViewer>, TViewer extends Viewer>(value: SchemaInputType, stmt: UpdateOperation<TEnt, TViewer>): TransformedUpdateOperation<TEnt> | null;
197
245
  export declare enum ActionOperation {
198
246
  Create = 1,
199
247
  Edit = 2,
@@ -212,6 +260,7 @@ export interface ActionField {
212
260
  list?: boolean;
213
261
  actionName?: string;
214
262
  excludedFields?: string[];
263
+ [x: string]: any;
215
264
  }
216
265
  export interface Action {
217
266
  operation: ActionOperation;
@@ -225,6 +274,7 @@ export interface Action {
225
274
  optionalFields?: string[];
226
275
  requiredFields?: string[];
227
276
  noFields?: boolean;
277
+ [x: string]: any;
228
278
  }
229
279
  export declare const NoFields = "__NO_FIELDS__";
230
280
  export declare function requiredField(field: string): string;
@@ -235,6 +285,7 @@ export interface Constraint {
235
285
  columns: string[];
236
286
  fkey?: ForeignKeyInfo;
237
287
  condition?: string;
288
+ [x: string]: any;
238
289
  }
239
290
  export interface FullTextWeight {
240
291
  A?: string[];
@@ -248,17 +299,21 @@ export interface FullText {
248
299
  languageColumn?: string;
249
300
  indexType?: "gin" | "gist";
250
301
  weights?: FullTextWeight;
302
+ [x: string]: any;
251
303
  }
252
304
  export interface Index {
253
305
  name: string;
254
306
  columns: string[];
255
307
  unique?: boolean;
256
308
  fulltext?: FullText;
309
+ indexType?: "gin" | "btree";
310
+ [x: string]: any;
257
311
  }
258
312
  export interface ForeignKeyInfo {
259
313
  tableName: string;
260
314
  ondelete?: "RESTRICT" | "CASCADE" | "SET NULL" | "SET DEFAULT" | "NO ACTION";
261
315
  columns: string[];
316
+ [x: string]: any;
262
317
  }
263
318
  export declare enum ConstraintType {
264
319
  PrimaryKey = "primary",
package/schema/schema.js CHANGED
@@ -35,6 +35,7 @@ var DBType;
35
35
  DBType["JSONB"] = "JSONB";
36
36
  DBType["Enum"] = "Enum";
37
37
  DBType["StringEnum"] = "StringEnum";
38
+ DBType["IntEnum"] = "IntEnum";
38
39
  DBType["Date"] = "Date";
39
40
  DBType["Time"] = "Time";
40
41
  DBType["Timetz"] = "Timetz";
@@ -86,12 +87,15 @@ function getFields(value) {
86
87
  return m;
87
88
  }
88
89
  exports.getFields = getFields;
90
+ /**
91
+ * @deprecated should only be used by tests
92
+ */
89
93
  function getStorageKey(field, fieldName) {
90
94
  return field.storageKey || (0, snake_case_1.snakeCase)(fieldName);
91
95
  }
92
96
  exports.getStorageKey = getStorageKey;
93
97
  // returns a mapping of storage key to field privacy
94
- function getFieldsWithPrivacy(value) {
98
+ function getFieldsWithPrivacy(value, fieldMap) {
95
99
  const schema = getSchema(value);
96
100
  function addFields(fields) {
97
101
  if (Array.isArray(fields)) {
@@ -111,9 +115,14 @@ function getFieldsWithPrivacy(value) {
111
115
  else {
112
116
  privacyPolicy = field.privacyPolicy;
113
117
  }
114
- m.set(getStorageKey(field, name), privacyPolicy);
118
+ const info = fieldMap[name];
119
+ if (!info) {
120
+ throw new Error(`field with name ${name} not passed in fieldMap`);
121
+ }
122
+ m.set(info.dbCol, privacyPolicy);
115
123
  }
116
124
  }
125
+ return;
117
126
  }
118
127
  for (const name in fields) {
119
128
  const field = fields[name];
@@ -128,7 +137,11 @@ function getFieldsWithPrivacy(value) {
128
137
  else {
129
138
  privacyPolicy = field.privacyPolicy;
130
139
  }
131
- m.set(getStorageKey(field, name), privacyPolicy);
140
+ const info = fieldMap[name];
141
+ if (!info) {
142
+ throw new Error(`field with name ${name} not passed in fieldMap`);
143
+ }
144
+ m.set(info.dbCol, privacyPolicy);
132
145
  }
133
146
  }
134
147
  }
@@ -167,13 +180,14 @@ exports.getObjectLoaderProperties = getObjectLoaderProperties;
167
180
  function getTransformedUpdateOp(value, stmt) {
168
181
  const schema = getSchema(value);
169
182
  if (!schema.patterns) {
170
- return;
183
+ return null;
171
184
  }
172
185
  for (const p of schema.patterns) {
173
186
  if (p.transformWrite) {
174
187
  return p.transformWrite(stmt);
175
188
  }
176
189
  }
190
+ return null;
177
191
  }
178
192
  exports.getTransformedUpdateOp = getTransformedUpdateOp;
179
193
  // this maps to ActionOperation in ent/action.go
@@ -6,12 +6,22 @@ export interface StructOptions extends FieldOptions {
6
6
  graphQLType?: string;
7
7
  jsonNotJSONB?: boolean;
8
8
  }
9
+ interface allStructOptions extends StructOptions {
10
+ jsonAsList?: boolean;
11
+ }
9
12
  export declare class StructField extends BaseField implements Field {
10
13
  private options;
11
14
  type: Type;
12
- constructor(options: StructOptions);
15
+ constructor(options: allStructOptions);
16
+ formatImpl(obj: any, nested?: boolean): string | Object;
13
17
  format(obj: any, nested?: boolean): string | Object;
18
+ private validImpl;
14
19
  valid(obj: any): Promise<boolean>;
15
20
  }
16
21
  export declare function StructType(options: StructOptions): StructField & StructOptions;
22
+ /**
23
+ * @deprecated use StructTypeAsList
24
+ */
17
25
  export declare function StructListType(options: StructOptions): ListField;
26
+ export declare function StructTypeAsList(options: allStructOptions): StructField & allStructOptions;
27
+ export {};