@snowtop/ent 0.1.0-alpha15 → 0.1.0-alpha150

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 (170) hide show
  1. package/action/action.d.ts +27 -16
  2. package/action/action.js +22 -7
  3. package/action/executor.d.ts +16 -3
  4. package/action/executor.js +90 -23
  5. package/action/experimental_action.d.ts +25 -16
  6. package/action/experimental_action.js +35 -9
  7. package/action/index.d.ts +3 -1
  8. package/action/index.js +7 -1
  9. package/action/operations.d.ts +125 -0
  10. package/action/operations.js +684 -0
  11. package/action/orchestrator.d.ts +38 -12
  12. package/action/orchestrator.js +427 -102
  13. package/action/relative_value.d.ts +47 -0
  14. package/action/relative_value.js +125 -0
  15. package/action/transaction.d.ts +10 -0
  16. package/action/transaction.js +23 -0
  17. package/auth/auth.d.ts +1 -1
  18. package/core/base.d.ts +51 -21
  19. package/core/base.js +7 -1
  20. package/core/clause.d.ts +85 -40
  21. package/core/clause.js +375 -64
  22. package/core/config.d.ts +15 -1
  23. package/core/config.js +10 -1
  24. package/core/const.d.ts +3 -0
  25. package/core/const.js +6 -0
  26. package/core/context.d.ts +4 -2
  27. package/core/context.js +20 -2
  28. package/core/convert.d.ts +1 -1
  29. package/core/date.js +1 -5
  30. package/core/db.d.ts +12 -8
  31. package/core/db.js +18 -8
  32. package/core/ent.d.ts +68 -94
  33. package/core/ent.js +535 -586
  34. package/core/global_schema.d.ts +7 -0
  35. package/core/global_schema.js +51 -0
  36. package/core/loaders/assoc_count_loader.d.ts +1 -0
  37. package/core/loaders/assoc_count_loader.js +10 -2
  38. package/core/loaders/assoc_edge_loader.d.ts +1 -1
  39. package/core/loaders/assoc_edge_loader.js +10 -13
  40. package/core/loaders/index.d.ts +1 -1
  41. package/core/loaders/index.js +1 -3
  42. package/core/loaders/index_loader.d.ts +3 -3
  43. package/core/loaders/loader.d.ts +2 -2
  44. package/core/loaders/loader.js +5 -5
  45. package/core/loaders/object_loader.d.ts +30 -9
  46. package/core/loaders/object_loader.js +225 -78
  47. package/core/loaders/query_loader.d.ts +6 -12
  48. package/core/loaders/query_loader.js +54 -13
  49. package/core/loaders/raw_count_loader.js +5 -1
  50. package/core/logger.d.ts +1 -1
  51. package/core/logger.js +1 -0
  52. package/core/privacy.d.ts +7 -6
  53. package/core/privacy.js +21 -25
  54. package/core/query/assoc_query.d.ts +3 -2
  55. package/core/query/assoc_query.js +9 -1
  56. package/core/query/custom_clause_query.d.ts +27 -0
  57. package/core/query/custom_clause_query.js +88 -0
  58. package/core/query/custom_query.d.ts +17 -2
  59. package/core/query/custom_query.js +88 -13
  60. package/core/query/index.d.ts +1 -0
  61. package/core/query/index.js +3 -1
  62. package/core/query/query.d.ts +15 -3
  63. package/core/query/query.js +128 -53
  64. package/core/query/shared_assoc_test.d.ts +2 -1
  65. package/core/query/shared_assoc_test.js +44 -54
  66. package/core/query/shared_test.d.ts +8 -1
  67. package/core/query/shared_test.js +532 -236
  68. package/core/viewer.d.ts +2 -0
  69. package/core/viewer.js +3 -1
  70. package/graphql/graphql.d.ts +52 -19
  71. package/graphql/graphql.js +174 -136
  72. package/graphql/graphql_field_helpers.d.ts +7 -1
  73. package/graphql/graphql_field_helpers.js +21 -1
  74. package/graphql/index.d.ts +2 -2
  75. package/graphql/index.js +3 -5
  76. package/graphql/query/connection_type.d.ts +9 -9
  77. package/graphql/query/shared_assoc_test.js +1 -1
  78. package/graphql/query/shared_edge_connection.js +1 -19
  79. package/graphql/scalars/orderby_direction.d.ts +2 -0
  80. package/graphql/scalars/orderby_direction.js +15 -0
  81. package/imports/dataz/example1/_auth.js +128 -47
  82. package/imports/dataz/example1/_viewer.js +87 -39
  83. package/imports/index.d.ts +7 -2
  84. package/imports/index.js +20 -5
  85. package/index.d.ts +14 -5
  86. package/index.js +26 -10
  87. package/package.json +18 -17
  88. package/parse_schema/parse.d.ts +31 -9
  89. package/parse_schema/parse.js +179 -32
  90. package/schema/base_schema.d.ts +13 -3
  91. package/schema/base_schema.js +13 -0
  92. package/schema/field.d.ts +78 -21
  93. package/schema/field.js +231 -71
  94. package/schema/index.d.ts +2 -2
  95. package/schema/index.js +7 -2
  96. package/schema/json_field.d.ts +16 -4
  97. package/schema/json_field.js +32 -2
  98. package/schema/schema.d.ts +109 -20
  99. package/schema/schema.js +42 -53
  100. package/schema/struct_field.d.ts +15 -3
  101. package/schema/struct_field.js +117 -22
  102. package/schema/union_field.d.ts +1 -1
  103. package/scripts/custom_compiler.js +12 -8
  104. package/scripts/custom_graphql.js +145 -34
  105. package/scripts/migrate_v0.1.js +36 -0
  106. package/scripts/move_types.js +120 -0
  107. package/scripts/read_schema.js +22 -7
  108. package/testutils/action/complex_schemas.d.ts +69 -0
  109. package/testutils/action/complex_schemas.js +405 -0
  110. package/testutils/builder.d.ts +39 -43
  111. package/testutils/builder.js +75 -49
  112. package/testutils/db/fixture.d.ts +10 -0
  113. package/testutils/db/fixture.js +26 -0
  114. package/testutils/db/{test_db.d.ts → temp_db.d.ts} +32 -8
  115. package/testutils/db/{test_db.js → temp_db.js} +244 -48
  116. package/testutils/db/value.d.ts +7 -0
  117. package/testutils/db/value.js +251 -0
  118. package/testutils/db_mock.d.ts +16 -4
  119. package/testutils/db_mock.js +52 -9
  120. package/testutils/db_time_zone.d.ts +4 -0
  121. package/testutils/db_time_zone.js +41 -0
  122. package/testutils/ent-graphql-tests/index.d.ts +7 -1
  123. package/testutils/ent-graphql-tests/index.js +56 -26
  124. package/testutils/fake_comms.js +1 -1
  125. package/testutils/fake_data/const.d.ts +2 -1
  126. package/testutils/fake_data/const.js +3 -0
  127. package/testutils/fake_data/fake_contact.d.ts +7 -3
  128. package/testutils/fake_data/fake_contact.js +13 -7
  129. package/testutils/fake_data/fake_event.d.ts +4 -1
  130. package/testutils/fake_data/fake_event.js +7 -6
  131. package/testutils/fake_data/fake_tag.d.ts +36 -0
  132. package/testutils/fake_data/fake_tag.js +89 -0
  133. package/testutils/fake_data/fake_user.d.ts +8 -5
  134. package/testutils/fake_data/fake_user.js +16 -15
  135. package/testutils/fake_data/index.js +5 -1
  136. package/testutils/fake_data/internal.d.ts +2 -0
  137. package/testutils/fake_data/internal.js +7 -1
  138. package/testutils/fake_data/tag_query.d.ts +13 -0
  139. package/testutils/fake_data/tag_query.js +43 -0
  140. package/testutils/fake_data/test_helpers.d.ts +11 -4
  141. package/testutils/fake_data/test_helpers.js +29 -13
  142. package/testutils/fake_data/user_query.d.ts +11 -4
  143. package/testutils/fake_data/user_query.js +54 -22
  144. package/testutils/fake_log.js +1 -1
  145. package/testutils/parse_sql.d.ts +6 -0
  146. package/testutils/parse_sql.js +16 -2
  147. package/testutils/test_edge_global_schema.d.ts +15 -0
  148. package/testutils/test_edge_global_schema.js +62 -0
  149. package/testutils/write.d.ts +2 -2
  150. package/testutils/write.js +33 -7
  151. package/tsc/ast.d.ts +25 -2
  152. package/tsc/ast.js +141 -17
  153. package/tsc/compilerOptions.js +5 -1
  154. package/tsc/move_generated.d.ts +1 -0
  155. package/tsc/move_generated.js +164 -0
  156. package/tsc/transform.d.ts +22 -0
  157. package/tsc/transform.js +182 -0
  158. package/tsc/transform_action.d.ts +22 -0
  159. package/tsc/transform_action.js +183 -0
  160. package/tsc/transform_ent.d.ts +17 -0
  161. package/tsc/transform_ent.js +60 -0
  162. package/tsc/transform_schema.d.ts +27 -0
  163. package/{scripts → tsc}/transform_schema.js +146 -117
  164. package/graphql/enums.d.ts +0 -3
  165. package/graphql/enums.js +0 -25
  166. package/scripts/move_generated.js +0 -142
  167. package/scripts/transform_code.js +0 -113
  168. package/scripts/transform_schema.d.ts +0 -1
  169. /package/scripts/{move_generated.d.ts → migrate_v0.1.d.ts} +0 -0
  170. /package/scripts/{transform_code.d.ts → move_types.d.ts} +0 -0
package/schema/field.d.ts CHANGED
@@ -1,3 +1,4 @@
1
+ import { Builder } from "../action/action";
1
2
  import { Field, FieldMap, FieldOptions, ForeignKey, PolymorphicOptions, Type } from "./schema";
2
3
  export declare abstract class BaseField {
3
4
  name: string;
@@ -24,31 +25,36 @@ export declare class UUIDField extends BaseField implements Field {
24
25
  valid(val: any): Promise<boolean>;
25
26
  }
26
27
  export declare function UUIDType(options?: FieldOptions): UUIDField;
27
- export interface IntegerOptions extends FieldOptions {
28
- min?: number;
29
- max?: number;
28
+ export interface IntegerOptions extends NumberOptions<number> {
30
29
  }
31
- export declare class IntegerField extends BaseField implements Field {
30
+ export interface NumberOptions<T> extends FieldOptions {
31
+ min?: T;
32
+ max?: T;
33
+ }
34
+ export declare class NumberField<T> extends BaseField {
32
35
  type: Type;
33
36
  private validators;
34
37
  private options;
35
- constructor(options?: IntegerOptions);
36
- getOptions(): IntegerOptions;
38
+ constructor(options?: NumberOptions<T>);
39
+ getOptions(): NumberOptions<T>;
37
40
  private handleOptions;
38
- min(l: number): this;
39
- max(l: number): this;
41
+ min(l: T): this;
42
+ max(l: T): this;
40
43
  valid(val: any): boolean;
41
44
  validate(validator: (str: number) => boolean): this;
42
45
  }
46
+ export declare class IntegerField extends NumberField<number> implements Field {
47
+ type: Type;
48
+ }
43
49
  export declare function IntegerType(options?: IntegerOptions): IntegerField;
44
- export declare class BigIntegerField extends BaseField implements Field {
50
+ export declare class BigIntegerField extends NumberField<BigInt> implements Field {
45
51
  type: Type;
46
52
  }
47
- export declare function BigIntegerType(options: FieldOptions): BigIntegerField;
48
- export declare class FloatField extends BaseField implements Field {
53
+ export declare function BigIntegerType(options?: NumberOptions<BigInt>): BigIntegerField;
54
+ export declare class FloatField extends NumberField<number> implements Field {
49
55
  type: Type;
50
56
  }
51
- export declare function FloatType(options?: FieldOptions): FloatField;
57
+ export declare function FloatType(options?: NumberOptions<number>): FloatField;
52
58
  export declare class BooleanField extends BaseField implements Field {
53
59
  type: Type;
54
60
  }
@@ -88,6 +94,18 @@ export declare class StringField extends BaseField implements Field {
88
94
  trimLeft(): this;
89
95
  trimRight(): this;
90
96
  }
97
+ interface PolymorphicStringOptions extends StringOptions {
98
+ parentFieldToValidate: string;
99
+ types?: string[];
100
+ }
101
+ export declare class PolymorphicStringField extends StringField {
102
+ private opts;
103
+ private camelCaseVals;
104
+ constructor(opts: PolymorphicStringOptions);
105
+ validateWithFullData(val: any, b: Builder<any>): boolean;
106
+ valid(val: any): boolean;
107
+ format(val: any): any;
108
+ }
91
109
  export declare function StringType(options?: StringOptions): StringField;
92
110
  export interface TimestampOptions extends FieldOptions {
93
111
  withTimezone?: boolean;
@@ -118,31 +136,69 @@ export declare class DateField extends BaseField implements Field {
118
136
  format(val: any): any;
119
137
  }
120
138
  export declare function DateType(options?: FieldOptions): DateField;
121
- declare type EnumMap = {
139
+ declare type StringEnumMap = {
122
140
  [key: string]: string;
123
141
  };
142
+ /**
143
+ * @deprecated use StringEnumOptions
144
+ */
124
145
  export interface EnumOptions extends FieldOptions {
125
146
  values?: string[];
126
- map?: EnumMap;
147
+ map?: StringEnumMap;
127
148
  tsType?: string;
128
149
  graphQLType?: string;
129
150
  createEnumType?: boolean;
151
+ disableUnknownType?: boolean;
152
+ globalType?: string;
130
153
  }
154
+ /**
155
+ * @deprecated Use StringEnumField
156
+ */
131
157
  export declare class EnumField extends BaseField implements Field {
132
158
  type: Type;
133
159
  private values?;
134
160
  private map?;
135
- constructor(options: EnumOptions);
136
- convertForGQL(value: string): string;
137
- valid(val: any): boolean;
161
+ constructor(options: StringEnumOptions);
162
+ valid(val: any): Promise<boolean>;
163
+ format(val: any): any;
164
+ }
165
+ export declare class StringEnumField extends EnumField {
166
+ }
167
+ export interface PolymorphicStringEnumOptions extends EnumOptions {
168
+ parentFieldToValidate: string;
169
+ }
170
+ export interface StringEnumOptions extends EnumOptions {
171
+ }
172
+ export declare function EnumType(options: StringEnumOptions): EnumField;
173
+ declare type IntEnumMap = {
174
+ [key: string]: number;
175
+ };
176
+ export interface IntegerEnumOptions extends FieldOptions {
177
+ map?: IntEnumMap;
178
+ deprecated?: IntEnumMap;
179
+ tsType?: string;
180
+ graphQLType?: string;
181
+ disableUnknownType?: boolean;
182
+ globalType?: string;
183
+ }
184
+ export declare class IntegerEnumField extends BaseField implements Field {
185
+ type: Type;
186
+ private map;
187
+ constructor(options: IntegerEnumOptions);
188
+ valid(val: any): Promise<boolean>;
138
189
  format(val: any): any;
139
190
  }
140
- export declare function EnumType(options: EnumOptions): EnumField;
191
+ export declare function IntegerEnumType(options: IntegerEnumOptions): IntegerEnumField;
192
+ interface ListOptions extends FieldOptions {
193
+ disableJSONStringify?: boolean;
194
+ }
141
195
  export declare class ListField extends BaseField {
142
196
  private field;
197
+ private options?;
143
198
  type: Type;
144
199
  private validators;
145
- constructor(field: Field, options?: FieldOptions);
200
+ constructor(field: Field, options?: ListOptions | undefined);
201
+ __getElemField(): Field;
146
202
  validate(validator: (val: any[]) => boolean): this;
147
203
  valid(val: any): Promise<boolean>;
148
204
  private postgresVal;
@@ -153,7 +209,7 @@ export declare class ListField extends BaseField {
153
209
  range(start: any, stop: any): this;
154
210
  }
155
211
  export declare function StringListType(options?: StringOptions): ListField;
156
- export declare function IntListType(options: FieldOptions): ListField;
212
+ export declare function IntListType(options?: FieldOptions): ListField;
157
213
  export declare function IntegerListType(options?: FieldOptions): ListField;
158
214
  export declare function FloatListType(options?: FieldOptions): ListField;
159
215
  export declare function BigIntegerListType(options: FieldOptions): ListField;
@@ -163,6 +219,7 @@ export declare function TimestamptzListType(options?: TimestampOptions): ListFie
163
219
  export declare function TimeListType(options?: TimeOptions): ListField;
164
220
  export declare function TimetzListType(options: TimeOptions): ListField;
165
221
  export declare function DateListType(options?: FieldOptions): ListField;
166
- export declare function EnumListType(options: EnumOptions): ListField;
222
+ export declare function EnumListType(options: StringEnumOptions): ListField;
223
+ export declare function IntegerEnumListType(options: IntegerEnumOptions): ListField;
167
224
  export declare function UUIDListType(options?: FieldOptions): ListField;
168
225
  export {};
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,16 @@ 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.PolymorphicStringField = exports.StringField = exports.BooleanType = exports.BooleanField = exports.FloatType = exports.FloatField = exports.BigIntegerType = exports.BigIntegerField = exports.IntegerType = exports.IntegerField = exports.NumberField = exports.UUIDType = exports.UUIDField = exports.BaseField = void 0;
23
27
  const luxon_1 = require("luxon");
24
- const snake_case_1 = require("snake-case");
28
+ const camel_case_1 = require("camel-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");
34
+ const global_schema_1 = require("../core/global_schema");
35
+ const logger_1 = require("../core/logger");
28
36
  class BaseField {
29
37
  logValue(val) {
30
38
  if (this.sensitive) {
@@ -60,31 +68,26 @@ class UUIDField extends BaseField {
60
68
  else {
61
69
  throw new Error(`unsupported id polymorhpic type ${fieldName}`);
62
70
  }
71
+ let types;
72
+ let serverDefault = undefined;
73
+ if (typeof polymorphic === "object") {
74
+ serverDefault = polymorphic.serverDefault;
75
+ types = polymorphic.types;
76
+ }
63
77
  // polymorphic field automatically hidden from GraphQL
64
78
  // can be made visible with custom fields if user wants to change this behavior
65
79
  // can't be foreignKey so need to make other changes to the field
66
80
  // intentionally not made private as it doesn't seem like it needs to be hidden
67
- if (typeof polymorphic === "object" && polymorphic.types) {
68
- // an enum with types validated here
69
- return {
70
- [name]: EnumType({
71
- values: polymorphic.types,
72
- hideFromGraphQL: true,
73
- derivedWhenEmbedded: true,
74
- nullable: this.options?.nullable,
75
- }),
76
- };
77
- }
78
- else {
79
- // just a string field...
80
- return {
81
- [name]: StringType({
82
- hideFromGraphQL: true,
83
- derivedWhenEmbedded: true,
84
- nullable: this.options?.nullable,
85
- }),
86
- };
87
- }
81
+ return {
82
+ [name]: PolymorphicStringType({
83
+ types: types,
84
+ hideFromGraphQL: true,
85
+ derivedWhenEmbedded: true,
86
+ nullable: this.options?.nullable,
87
+ parentFieldToValidate: fieldName,
88
+ serverDefault: serverDefault,
89
+ }),
90
+ };
88
91
  }
89
92
  return {};
90
93
  }
@@ -92,6 +95,9 @@ class UUIDField extends BaseField {
92
95
  return val.placeholderID !== undefined;
93
96
  }
94
97
  async valid(val) {
98
+ if (typeof val === "string" && !(0, uuid_1.validate)(val)) {
99
+ return false;
100
+ }
95
101
  if (!this.options?.fieldEdge?.enforceSchema) {
96
102
  return true;
97
103
  }
@@ -115,9 +121,10 @@ function UUIDType(options) {
115
121
  return Object.assign(result, options);
116
122
  }
117
123
  exports.UUIDType = UUIDType;
118
- class IntegerField extends BaseField {
124
+ class NumberField extends BaseField {
119
125
  constructor(options) {
120
126
  super();
127
+ // to be overriden as needed
121
128
  this.type = { dbType: schema_1.DBType.Int };
122
129
  this.validators = [];
123
130
  this.options = {};
@@ -141,9 +148,11 @@ class IntegerField extends BaseField {
141
148
  this.options = options;
142
149
  }
143
150
  min(l) {
151
+ // @ts-ignore Operator '>=' cannot be applied to types 'number' and 'T'.
144
152
  return this.validate((val) => val >= l);
145
153
  }
146
154
  max(l) {
155
+ // @ts-ignore Operator '<=' cannot be applied to types 'number' and 'T'.
147
156
  return this.validate((val) => val <= l);
148
157
  }
149
158
  valid(val) {
@@ -159,13 +168,20 @@ class IntegerField extends BaseField {
159
168
  return this;
160
169
  }
161
170
  }
171
+ exports.NumberField = NumberField;
172
+ class IntegerField extends NumberField {
173
+ constructor() {
174
+ super(...arguments);
175
+ this.type = { dbType: schema_1.DBType.Int };
176
+ }
177
+ }
162
178
  exports.IntegerField = IntegerField;
163
179
  function IntegerType(options) {
164
180
  let result = new IntegerField(options);
165
181
  return Object.assign(result, options);
166
182
  }
167
183
  exports.IntegerType = IntegerType;
168
- class BigIntegerField extends BaseField {
184
+ class BigIntegerField extends NumberField {
169
185
  constructor() {
170
186
  super(...arguments);
171
187
  this.type = { dbType: schema_1.DBType.BigInt };
@@ -173,11 +189,11 @@ class BigIntegerField extends BaseField {
173
189
  }
174
190
  exports.BigIntegerField = BigIntegerField;
175
191
  function BigIntegerType(options) {
176
- let result = new BigIntegerField();
192
+ let result = new BigIntegerField(options);
177
193
  return Object.assign(result, options);
178
194
  }
179
195
  exports.BigIntegerType = BigIntegerType;
180
- class FloatField extends BaseField {
196
+ class FloatField extends NumberField {
181
197
  constructor() {
182
198
  super(...arguments);
183
199
  this.type = { dbType: schema_1.DBType.Float };
@@ -185,7 +201,7 @@ class FloatField extends BaseField {
185
201
  }
186
202
  exports.FloatField = FloatField;
187
203
  function FloatType(options) {
188
- let result = new FloatField();
204
+ let result = new FloatField(options);
189
205
  return Object.assign(result, options);
190
206
  }
191
207
  exports.FloatType = FloatType;
@@ -307,6 +323,58 @@ class StringField extends BaseField {
307
323
  }
308
324
  }
309
325
  exports.StringField = StringField;
326
+ class PolymorphicStringField extends StringField {
327
+ constructor(opts) {
328
+ super(opts);
329
+ this.opts = opts;
330
+ if (opts.types) {
331
+ this.camelCaseVals = opts.types.map((v) => (0, camel_case_1.camelCase)(v));
332
+ }
333
+ }
334
+ validateWithFullData(val, b) {
335
+ const input = b.getInput();
336
+ const inputKey = b.orchestrator.__getOptions().fieldInfo[this.opts.parentFieldToValidate]
337
+ .inputKey;
338
+ const v = input[inputKey];
339
+ if (val === null) {
340
+ // if this is being set to null, ok if v is also null
341
+ return v === null;
342
+ }
343
+ // if this is not being set, ok if v is not being set
344
+ if (val === undefined && b.operation === base_1.WriteOperation.Insert) {
345
+ return v === undefined;
346
+ }
347
+ return true;
348
+ }
349
+ valid(val) {
350
+ if (!this.camelCaseVals) {
351
+ return true;
352
+ }
353
+ let str = (0, camel_case_1.camelCase)(String(val));
354
+ // allow different cases because it could be coming from different clients who don't have strong typing
355
+ return this.camelCaseVals.some((value) => value === str);
356
+ }
357
+ format(val) {
358
+ if (!this.camelCaseVals) {
359
+ return val;
360
+ }
361
+ const converted = (0, camel_case_1.camelCase)(String(val));
362
+ for (const v of this.camelCaseVals) {
363
+ if (v === val) {
364
+ return val;
365
+ }
366
+ if (converted === v) {
367
+ return converted;
368
+ }
369
+ }
370
+ return val;
371
+ }
372
+ }
373
+ exports.PolymorphicStringField = PolymorphicStringField;
374
+ function PolymorphicStringType(opts) {
375
+ let result = new PolymorphicStringField(opts);
376
+ return Object.assign(result, opts);
377
+ }
310
378
  function StringType(options) {
311
379
  let result = new StringField(options);
312
380
  const options2 = { ...options };
@@ -426,9 +494,10 @@ class DateField extends BaseField {
426
494
  this.type = { dbType: schema_1.DBType.Date };
427
495
  }
428
496
  format(val) {
429
- if (!(val instanceof Date)) {
497
+ if (typeof val === "string") {
430
498
  return val;
431
499
  }
500
+ val = new Date(val);
432
501
  let yy = (0, exports.leftPad)(val.getFullYear());
433
502
  // lol this API
434
503
  // for some reason this is 0-index
@@ -444,6 +513,9 @@ function DateType(options) {
444
513
  return Object.assign(result, options);
445
514
  }
446
515
  exports.DateType = DateType;
516
+ /**
517
+ * @deprecated Use StringEnumField
518
+ */
447
519
  class EnumField extends BaseField {
448
520
  constructor(options) {
449
521
  super();
@@ -454,10 +526,12 @@ class EnumField extends BaseField {
454
526
  enumMap: options.map,
455
527
  type: options.tsType,
456
528
  graphQLType: options.graphQLType,
529
+ disableUnknownType: options.disableUnknownType,
530
+ globalType: options.globalType,
457
531
  };
458
532
  if (!options.foreignKey) {
459
- if (!options.values && !options.map) {
460
- throw new Error("values or map required if not look up table enum. Look-up table enum indicated by foreignKey field");
533
+ if (!options.values && !options.map && !options.globalType) {
534
+ throw new Error("values, map or globalType required if not look up table enum. Look-up table enum indicated by foreignKey field");
461
535
  }
462
536
  if (options.values) {
463
537
  if (!options.values.length) {
@@ -466,7 +540,7 @@ class EnumField extends BaseField {
466
540
  }
467
541
  if (options.map) {
468
542
  let count = 0;
469
- for (const k in options.map) {
543
+ for (const _ in options.map) {
470
544
  count++;
471
545
  break;
472
546
  }
@@ -476,8 +550,8 @@ class EnumField extends BaseField {
476
550
  }
477
551
  }
478
552
  else {
479
- if (options.values || options.map) {
480
- throw new Error("cannot specify values or map and foreign key for lookup table enum type");
553
+ if (options.values || options.map || options.globalType) {
554
+ throw new Error("cannot specify values, map or globalType and foreign key for lookup table enum type");
481
555
  }
482
556
  if (options.createEnumType) {
483
557
  throw new Error("cannot specify createEnumType without specifying values");
@@ -492,69 +566,136 @@ class EnumField extends BaseField {
492
566
  this.values = options.values;
493
567
  this.map = options.map;
494
568
  }
495
- // TODO need to update this for map
496
- convertForGQL(value) {
497
- return (0, snake_case_1.snakeCase)(value).toUpperCase();
498
- }
499
- valid(val) {
569
+ async valid(val) {
570
+ if (this.type.globalType) {
571
+ const f = (0, global_schema_1.__getGlobalSchemaField)(this.type.globalType);
572
+ if (f) {
573
+ if (f.valid) {
574
+ return f.valid(val);
575
+ }
576
+ return true;
577
+ }
578
+ else {
579
+ (0, logger_1.log)("error", `globalType ${this.type.globalType} not found in global schema`);
580
+ return false;
581
+ }
582
+ }
500
583
  // lookup table enum and indicated via presence of foreignKey
501
584
  if (!this.values && !this.map) {
502
585
  return true;
503
586
  }
504
587
  if (this.values) {
505
588
  let str = String(val);
506
- return this.values.some((value) => value === str || this.convertForGQL(value) === str);
589
+ return this.values.some((value) => value === str);
507
590
  }
508
591
  for (const k in this.map) {
509
592
  const v = this.map[k];
510
- if (v === val || this.convertForGQL(k) === val) {
511
- // TODO decide on behavior for GQL since GQL only supports one type
593
+ if (v === val) {
512
594
  return true;
513
595
  }
514
596
  }
515
597
  return false;
516
598
  }
517
599
  format(val) {
518
- // TODO need to format correctly for graphql purposes...
519
- // how to best get the values in the db...
520
- if (!this.values && !this.map) {
600
+ if (this.type.globalType) {
601
+ const f = (0, global_schema_1.__getGlobalSchemaField)(this.type.globalType);
602
+ if (f && f.format) {
603
+ return f.format(val);
604
+ }
521
605
  return val;
522
606
  }
523
- let str = String(val);
524
- if (this.values) {
525
- for (let i = 0; i < this.values.length; i++) {
526
- let value = this.values[i];
527
- // store the format that maps to the given value in the db instead of saving the upper case value
528
- if (str === value || str === this.convertForGQL(value)) {
529
- return value;
530
- }
607
+ return val;
608
+ }
609
+ }
610
+ exports.EnumField = EnumField;
611
+ class StringEnumField extends EnumField {
612
+ }
613
+ exports.StringEnumField = StringEnumField;
614
+ function EnumType(options) {
615
+ let result = new StringEnumField(options);
616
+ return Object.assign(result, options);
617
+ }
618
+ exports.EnumType = EnumType;
619
+ class IntegerEnumField extends BaseField {
620
+ constructor(options) {
621
+ super();
622
+ this.type = {
623
+ dbType: schema_1.DBType.IntEnum,
624
+ intEnumMap: options.map,
625
+ type: options.tsType,
626
+ graphQLType: options.graphQLType,
627
+ deprecatedIntEnumMap: options.deprecated,
628
+ disableUnknownType: options.disableUnknownType,
629
+ globalType: options.globalType,
630
+ };
631
+ if (options.foreignKey) {
632
+ throw new Error(`foreignKey on intEnum not supported`);
633
+ }
634
+ if (options.globalType) {
635
+ if (options.map) {
636
+ throw new Error(`cannot specify map and globalType`);
531
637
  }
638
+ this.map = {};
532
639
  }
533
- if (this.map) {
534
- for (const k in this.map) {
535
- const v = this.map[k];
536
- if (str === v) {
537
- return v;
538
- }
539
- if (str === this.convertForGQL(k)) {
540
- return v;
640
+ else {
641
+ let count = 0;
642
+ for (const _ in options.map) {
643
+ count++;
644
+ break;
645
+ }
646
+ if (!count) {
647
+ throw new Error("need at least one entry in enum map");
648
+ }
649
+ if (!options.map) {
650
+ throw new Error("map required if not globalType");
651
+ }
652
+ this.map = options.map;
653
+ }
654
+ }
655
+ async valid(val) {
656
+ if (this.type?.globalType) {
657
+ const f = (0, global_schema_1.__getGlobalSchemaField)(this.type.globalType);
658
+ if (f) {
659
+ if (f.valid) {
660
+ return f.valid(val);
541
661
  }
662
+ return true;
663
+ }
664
+ else {
665
+ (0, logger_1.log)("error", `globalType ${this.type.globalType} not found in global schema`);
666
+ return false;
542
667
  }
543
668
  }
544
- // whelp, just return what's passed
545
- return val;
669
+ // lookup table enum and indicated via presence of foreignKey
670
+ for (const k in this.map) {
671
+ const v = this.map[k];
672
+ if (v === val || v === parseInt(val)) {
673
+ return true;
674
+ }
675
+ }
676
+ return false;
677
+ }
678
+ format(val) {
679
+ if (this.type.globalType) {
680
+ const f = (0, global_schema_1.__getGlobalSchemaField)(this.type.globalType);
681
+ if (f && f.format) {
682
+ return f.format(val);
683
+ }
684
+ }
685
+ return parseInt(val);
546
686
  }
547
687
  }
548
- exports.EnumField = EnumField;
549
- function EnumType(options) {
550
- let result = new EnumField(options);
688
+ exports.IntegerEnumField = IntegerEnumField;
689
+ function IntegerEnumType(options) {
690
+ let result = new IntegerEnumField(options);
551
691
  return Object.assign(result, options);
552
692
  }
553
- exports.EnumType = EnumType;
693
+ exports.IntegerEnumType = IntegerEnumType;
554
694
  class ListField extends BaseField {
555
695
  constructor(field, options) {
556
696
  super();
557
697
  this.field = field;
698
+ this.options = options;
558
699
  this.validators = [];
559
700
  if (field.type.dbType === schema_1.DBType.List) {
560
701
  throw new Error(`nested lists not currently supported`);
@@ -565,6 +706,9 @@ class ListField extends BaseField {
565
706
  };
566
707
  Object.assign(this, options);
567
708
  }
709
+ __getElemField() {
710
+ return this.field;
711
+ }
568
712
  validate(validator) {
569
713
  this.validators.push(validator);
570
714
  return this;
@@ -592,7 +736,12 @@ class ListField extends BaseField {
592
736
  return result;
593
737
  }
594
738
  postgresVal(val, jsonType) {
595
- if (!jsonType) {
739
+ if (!jsonType && val === "") {
740
+ // support empty strings in list
741
+ val = '"' + val + '"';
742
+ return val;
743
+ }
744
+ if (this.options?.disableJSONStringify) {
596
745
  return val;
597
746
  }
598
747
  return JSON.stringify(val);
@@ -720,7 +869,18 @@ function EnumListType(options) {
720
869
  return new ListField(EnumType(options), options);
721
870
  }
722
871
  exports.EnumListType = EnumListType;
872
+ function IntegerEnumListType(options) {
873
+ // not all of these will make sense in a list...
874
+ // can make it work eventually but involves work we're not currently trying to do
875
+ // developer can try to work around it by calling below on their own.
876
+ // unclear what the behavior is
877
+ return new ListField(IntegerEnumType(options), options);
878
+ }
879
+ exports.IntegerEnumListType = IntegerEnumListType;
723
880
  function UUIDListType(options) {
724
- return new ListField(UUIDType(options), options);
881
+ return new ListField(UUIDType(options), {
882
+ ...options,
883
+ disableJSONStringify: true,
884
+ });
725
885
  }
726
886
  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, getFieldsWithEditPrivacy, getStorageKey, ActionOperation, Action, EdgeAction, NoFields, FieldMap, Constraint, Index, ConstraintType, ForeignKeyInfo, requiredField, optionalField, UpdateOperation, TransformedUpdateOperation, SQLStatementOperation, EdgeUpdateOperation, TransformedEdgeUpdateOperation, getTransformedReadClause, getObjectLoaderProperties, GlobalSchema, ActionField, } 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";