@snowtop/ent 0.1.0-alpha120 → 0.1.0-alpha121

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.
package/core/ent.d.ts CHANGED
@@ -4,7 +4,6 @@ import { Executor } from "../action/action";
4
4
  import * as clause from "./clause";
5
5
  import { Builder } from "../action";
6
6
  import DataLoader from "dataloader";
7
- import { GlobalSchema } from "../schema/";
8
7
  export declare function getEntKey<TEnt extends Ent<TViewer>, TViewer extends Viewer>(viewer: TViewer, id: ID, options: LoadEntOptions<TEnt, TViewer>): string;
9
8
  export declare function loadEnt<TEnt extends Ent<TViewer>, TViewer extends Viewer>(viewer: TViewer, id: ID, options: LoadEntOptions<TEnt, TViewer>): Promise<TEnt | null>;
10
9
  export declare function loadEntViaKey<TEnt extends Ent<TViewer>, TViewer extends Viewer>(viewer: TViewer, key: any, options: LoadEntOptions<TEnt, TViewer>): Promise<TEnt | null>;
@@ -114,9 +113,6 @@ export declare class EditNodeOperation<T extends Ent> implements DataOperation {
114
113
  returnedRow(): Data | null;
115
114
  createdEnt(viewer: Viewer): T | null;
116
115
  }
117
- export declare function setGlobalSchema(val: GlobalSchema): void;
118
- export declare function clearGlobalSchema(): void;
119
- export declare function __hasGlobalSchema(): boolean;
120
116
  export declare class EdgeOperation implements DataOperation {
121
117
  private builder;
122
118
  edgeInput: AssocEdgeInput;
package/core/ent.js CHANGED
@@ -26,15 +26,16 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
26
26
  return (mod && mod.__esModule) ? mod : { "default": mod };
27
27
  };
28
28
  Object.defineProperty(exports, "__esModule", { value: true });
29
- exports.loadUniqueNode = exports.loadUniqueEdge = exports.loadCustomEdges = exports.getEdgeClauseAndFields = exports.loadEdges = exports.DefaultLimit = exports.loadEdgeDatas = exports.loadEdgeData = exports.assocEdgeLoader = exports.AssocEdgeData = exports.getCursor = exports.AssocEdge = exports.DeleteNodeOperation = exports.deleteRowsSync = exports.deleteRows = exports.editRowSync = exports.editRow = exports.buildUpdateQuery = exports.createRowSync = exports.createRow = exports.buildInsertQuery = exports.EdgeOperation = exports.__hasGlobalSchema = exports.clearGlobalSchema = exports.setGlobalSchema = exports.EditNodeOperation = exports.RawQueryOperation = exports.buildGroupQuery = exports.buildQuery = exports.loadRows = exports.performRawQuery = exports.___setLogQueryErrorWithError = exports.loadRow = exports.loadRowX = exports.logQuery = exports.loadDerivedEntX = exports.loadDerivedEnt = exports.loadCustomCount = exports.loadCustomData = exports.loadCustomEnts = exports.loadEntsFromClause = exports.loadEntsList = exports.loadEnts = exports.loadEntXFromClause = exports.loadEntFromClause = exports.loadEntXViaKey = exports.loadEntX = exports.loadEntViaKey = exports.loadEnt = exports.getEntKey = void 0;
30
- exports.getEdgeTypeInGroup = exports.applyPrivacyPolicyForRows = exports.applyPrivacyPolicyForRow = exports.loadNodesByEdge = exports.loadEdgeForID2 = exports.loadRawEdgeCountX = void 0;
29
+ exports.loadNodesByEdge = exports.loadEdgeForID2 = exports.loadRawEdgeCountX = exports.loadUniqueNode = exports.loadUniqueEdge = exports.loadCustomEdges = exports.getEdgeClauseAndFields = exports.loadEdges = exports.DefaultLimit = exports.loadEdgeDatas = exports.loadEdgeData = exports.assocEdgeLoader = exports.AssocEdgeData = exports.getCursor = exports.AssocEdge = exports.DeleteNodeOperation = exports.deleteRowsSync = exports.deleteRows = exports.editRowSync = exports.editRow = exports.buildUpdateQuery = exports.createRowSync = exports.createRow = exports.buildInsertQuery = exports.EdgeOperation = exports.EditNodeOperation = exports.RawQueryOperation = exports.buildGroupQuery = exports.buildQuery = exports.loadRows = exports.performRawQuery = exports.___setLogQueryErrorWithError = exports.loadRow = exports.loadRowX = exports.logQuery = exports.loadDerivedEntX = exports.loadDerivedEnt = exports.loadCustomCount = exports.loadCustomData = exports.loadCustomEnts = exports.loadEntsFromClause = exports.loadEntsList = exports.loadEnts = exports.loadEntXFromClause = exports.loadEntFromClause = exports.loadEntXViaKey = exports.loadEntX = exports.loadEntViaKey = exports.loadEnt = exports.getEntKey = void 0;
30
+ exports.getEdgeTypeInGroup = exports.applyPrivacyPolicyForRows = exports.applyPrivacyPolicyForRow = void 0;
31
31
  const db_1 = __importStar(require("./db"));
32
32
  const privacy_1 = require("./privacy");
33
33
  const clause = __importStar(require("./clause"));
34
34
  const action_1 = require("../action");
35
35
  const logger_1 = require("./logger");
36
36
  const dataloader_1 = __importDefault(require("dataloader"));
37
- const schema_1 = require("../schema/");
37
+ const schema_1 = require("../schema/schema");
38
+ const global_schema_1 = require("./global_schema");
38
39
  // TODO kill this and createDataLoader
39
40
  class cacheMap {
40
41
  constructor(options) {
@@ -816,20 +817,6 @@ class EditNodeOperation {
816
817
  }
817
818
  }
818
819
  exports.EditNodeOperation = EditNodeOperation;
819
- let globalSchema;
820
- function setGlobalSchema(val) {
821
- globalSchema = val;
822
- }
823
- exports.setGlobalSchema = setGlobalSchema;
824
- function clearGlobalSchema() {
825
- globalSchema = undefined;
826
- }
827
- exports.clearGlobalSchema = clearGlobalSchema;
828
- // used by tests. no guarantee will always exist
829
- function __hasGlobalSchema() {
830
- return globalSchema !== undefined;
831
- }
832
- exports.__hasGlobalSchema = __hasGlobalSchema;
833
820
  class EdgeOperation {
834
821
  constructor(builder, edgeInput, options) {
835
822
  this.builder = builder;
@@ -872,8 +859,9 @@ class EdgeOperation {
872
859
  let op = schema_1.SQLStatementOperation.Delete;
873
860
  let updateData = null;
874
861
  // TODO respect disableTransformations
875
- if (globalSchema?.transformEdgeWrite) {
876
- transformed = globalSchema.transformEdgeWrite({
862
+ const transformedEdgeWrite = (0, global_schema_1.__getGlobalSchema)()?.transformEdgeWrite;
863
+ if (transformedEdgeWrite) {
864
+ transformed = transformedEdgeWrite({
877
865
  op: schema_1.SQLStatementOperation.Delete,
878
866
  edge,
879
867
  });
@@ -951,9 +939,10 @@ class EdgeOperation {
951
939
  fields["time"] = new Date().toISOString();
952
940
  }
953
941
  const onConflictFields = ["data"];
954
- if (globalSchema?.extraEdgeFields) {
955
- for (const name in globalSchema.extraEdgeFields) {
956
- const f = globalSchema.extraEdgeFields[name];
942
+ const extraEdgeFields = (0, global_schema_1.__getGlobalSchema)()?.extraEdgeFields;
943
+ if (extraEdgeFields) {
944
+ for (const name in extraEdgeFields) {
945
+ const f = extraEdgeFields[name];
957
946
  if (f.defaultValueOnCreate) {
958
947
  const storageKey = (0, schema_1.getStorageKey)(f, name);
959
948
  fields[storageKey] = f.defaultValueOnCreate(this.builder, {});
@@ -965,8 +954,9 @@ class EdgeOperation {
965
954
  }
966
955
  // TODO respect disableTransformations
967
956
  let transformed = null;
968
- if (globalSchema?.transformEdgeWrite) {
969
- transformed = globalSchema.transformEdgeWrite({
957
+ const transformEdgeWrite = (0, global_schema_1.__getGlobalSchema)()?.transformEdgeWrite;
958
+ if (transformEdgeWrite) {
959
+ transformed = transformEdgeWrite({
970
960
  op: schema_1.SQLStatementOperation.Insert,
971
961
  edge,
972
962
  });
@@ -1483,8 +1473,9 @@ async function loadEdges(options) {
1483
1473
  exports.loadEdges = loadEdges;
1484
1474
  function getEdgeClauseAndFields(cls, options) {
1485
1475
  let fields = edgeFields;
1486
- if (globalSchema?.transformEdgeRead) {
1487
- const transformClause = globalSchema.transformEdgeRead();
1476
+ const transformEdgeRead = (0, global_schema_1.__getGlobalSchema)()?.transformEdgeRead;
1477
+ if (transformEdgeRead) {
1478
+ const transformClause = transformEdgeRead();
1488
1479
  if (!options.disableTransformations) {
1489
1480
  cls = clause.And(cls, transformClause);
1490
1481
  }
@@ -0,0 +1,7 @@
1
+ import { Field, GlobalSchema } from "../schema/schema";
2
+ export declare function setGlobalSchema(val: GlobalSchema): void;
3
+ export declare function clearGlobalSchema(): void;
4
+ export declare function __hasGlobalSchema(): boolean;
5
+ export declare function __getGlobalSchema(): GlobalSchema | undefined;
6
+ export declare function __getGlobalSchemaFields(): Map<string, Field>;
7
+ export declare function __getGlobalSchemaField(type: string): Field | undefined;
@@ -0,0 +1,51 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.__getGlobalSchemaField = exports.__getGlobalSchemaFields = exports.__getGlobalSchema = exports.__hasGlobalSchema = exports.clearGlobalSchema = exports.setGlobalSchema = void 0;
4
+ const schema_1 = require("../schema/schema");
5
+ let globalSchema;
6
+ let globalSchemaFields = new Map();
7
+ function isGlobalSchemaField(f) {
8
+ switch (f.type.dbType) {
9
+ case schema_1.DBType.Enum:
10
+ case schema_1.DBType.StringEnum:
11
+ case schema_1.DBType.IntEnum:
12
+ case schema_1.DBType.JSON:
13
+ case schema_1.DBType.JSONB:
14
+ return true;
15
+ }
16
+ return false;
17
+ }
18
+ function setGlobalSchema(val) {
19
+ globalSchema = val;
20
+ if (val.fields) {
21
+ for (const [k, v] of Object.entries(val.fields)) {
22
+ if (isGlobalSchemaField(v) && v.type.type) {
23
+ globalSchemaFields.set(v.type.type, v);
24
+ }
25
+ }
26
+ }
27
+ }
28
+ exports.setGlobalSchema = setGlobalSchema;
29
+ function clearGlobalSchema() {
30
+ globalSchema = undefined;
31
+ globalSchemaFields.clear();
32
+ }
33
+ exports.clearGlobalSchema = clearGlobalSchema;
34
+ // used by tests. no guarantee will always exist
35
+ function __hasGlobalSchema() {
36
+ return globalSchema !== undefined;
37
+ }
38
+ exports.__hasGlobalSchema = __hasGlobalSchema;
39
+ // used by tests. no guarantee will always exist
40
+ function __getGlobalSchema() {
41
+ return globalSchema;
42
+ }
43
+ exports.__getGlobalSchema = __getGlobalSchema;
44
+ function __getGlobalSchemaFields() {
45
+ return globalSchemaFields;
46
+ }
47
+ exports.__getGlobalSchemaFields = __getGlobalSchemaFields;
48
+ function __getGlobalSchemaField(type) {
49
+ return globalSchemaFields.get(type);
50
+ }
51
+ exports.__getGlobalSchemaField = __getGlobalSchemaField;
@@ -2,6 +2,7 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.commonTests = void 0;
4
4
  const ent_1 = require("../ent");
5
+ const global_schema_1 = require("../global_schema");
5
6
  const viewer_1 = require("../viewer");
6
7
  const index_1 = require("../../testutils/fake_data/index");
7
8
  const test_helpers_1 = require("../../testutils/fake_data/test_helpers");
@@ -289,7 +290,7 @@ const commonTests = (opts) => {
289
290
  return { verify, getCursor };
290
291
  }
291
292
  if (opts.globalSchema) {
292
- (0, ent_1.setGlobalSchema)(test_edge_global_schema_1.testEdgeGlobalSchema);
293
+ (0, global_schema_1.setGlobalSchema)(test_edge_global_schema_1.testEdgeGlobalSchema);
293
294
  }
294
295
  let tdb;
295
296
  if (opts.sqlite) {
package/index.d.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  export * from "./core/base";
2
- export { loadEnt, loadCustomData, loadCustomEnts, loadCustomCount, loadEntX, loadEnts, CustomQuery, loadDerivedEnt, loadDerivedEntX, loadEntViaKey, loadEntXViaKey, performRawQuery, loadRowX, loadRow, loadRows, DataOperation, EditNodeOptions, EditNodeOperation, RawQueryOperation, EdgeOperation, DeleteNodeOperation, AssocEdge, AssocEdgeInputOptions, AssocEdgeInput, AssocEdgeData, loadEdgeData, loadEdgeDatas, loadEdges, loadUniqueEdge, loadUniqueNode, loadRawEdgeCountX, loadEdgeForID2, loadNodesByEdge, getEdgeTypeInGroup, setGlobalSchema, } from "./core/ent";
2
+ export { loadEnt, loadCustomData, loadCustomEnts, loadCustomCount, loadEntX, loadEnts, CustomQuery, loadDerivedEnt, loadDerivedEntX, loadEntViaKey, loadEntXViaKey, performRawQuery, loadRowX, loadRow, loadRows, DataOperation, EditNodeOptions, EditNodeOperation, RawQueryOperation, EdgeOperation, DeleteNodeOperation, AssocEdge, AssocEdgeInputOptions, AssocEdgeInput, AssocEdgeData, loadEdgeData, loadEdgeDatas, loadEdges, loadUniqueEdge, loadUniqueNode, loadRawEdgeCountX, loadEdgeForID2, loadNodesByEdge, getEdgeTypeInGroup, } from "./core/ent";
3
+ export { setGlobalSchema } from "./core/global_schema";
3
4
  import DB from "./core/db";
4
5
  export * from "./core/loaders";
5
6
  export { DB };
package/index.js CHANGED
@@ -63,7 +63,8 @@ Object.defineProperty(exports, "loadRawEdgeCountX", { enumerable: true, get: fun
63
63
  Object.defineProperty(exports, "loadEdgeForID2", { enumerable: true, get: function () { return ent_1.loadEdgeForID2; } });
64
64
  Object.defineProperty(exports, "loadNodesByEdge", { enumerable: true, get: function () { return ent_1.loadNodesByEdge; } });
65
65
  Object.defineProperty(exports, "getEdgeTypeInGroup", { enumerable: true, get: function () { return ent_1.getEdgeTypeInGroup; } });
66
- Object.defineProperty(exports, "setGlobalSchema", { enumerable: true, get: function () { return ent_1.setGlobalSchema; } });
66
+ var global_schema_1 = require("./core/global_schema");
67
+ Object.defineProperty(exports, "setGlobalSchema", { enumerable: true, get: function () { return global_schema_1.setGlobalSchema; } });
67
68
  const db_1 = __importDefault(require("./core/db"));
68
69
  exports.DB = db_1.default;
69
70
  __exportStar(require("./core/loaders"), exports);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@snowtop/ent",
3
- "version": "0.1.0-alpha120",
3
+ "version": "0.1.0-alpha121",
4
4
  "description": "snowtop ent framework",
5
5
  "main": "index.js",
6
6
  "types": "index.d.ts",
@@ -84,5 +84,6 @@ interface ProcessedGlobalSchema {
84
84
  globalEdges: ProcessedAssocEdge[];
85
85
  extraEdgeFields: ProcessedField[];
86
86
  initForEdges?: boolean;
87
+ globalFields?: ProcessedField[];
87
88
  }
88
89
  export {};
@@ -3,6 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.parseSchema = void 0;
4
4
  const cosmiconfig_1 = require("cosmiconfig");
5
5
  const const_1 = require("../core/const");
6
+ const global_schema_1 = require("../core/global_schema");
6
7
  async function processFields(src, patternName) {
7
8
  const ret = [];
8
9
  let m = {};
@@ -236,6 +237,9 @@ async function parseSchema(potentialSchemas, globalSchema) {
236
237
  let parsedGlobalSchema;
237
238
  if (globalSchema) {
238
239
  parsedGlobalSchema = await parseGlobalSchema(globalSchema);
240
+ // set this so that we can use it, if we're trying to process server default or anything
241
+ // that ends up parsing,validating and formatting fields
242
+ (0, global_schema_1.setGlobalSchema)(globalSchema);
239
243
  }
240
244
  for (const key in potentialSchemas) {
241
245
  const value = potentialSchemas[key];
@@ -364,5 +368,8 @@ async function parseGlobalSchema(s) {
364
368
  if (s.edges) {
365
369
  ret.globalEdges = processEdges(s.edges);
366
370
  }
371
+ if (s.fields) {
372
+ ret.globalFields = await processFields(s.fields);
373
+ }
367
374
  return ret;
368
375
  }
package/schema/field.d.ts CHANGED
@@ -149,6 +149,7 @@ export interface EnumOptions extends FieldOptions {
149
149
  graphQLType?: string;
150
150
  createEnumType?: boolean;
151
151
  disableUnknownType?: boolean;
152
+ globalType?: string;
152
153
  }
153
154
  /**
154
155
  * @deprecated Use StringEnumField
@@ -158,7 +159,7 @@ export declare class EnumField extends BaseField implements Field {
158
159
  private values?;
159
160
  private map?;
160
161
  constructor(options: StringEnumOptions);
161
- valid(val: any): boolean;
162
+ valid(val: any): Promise<boolean>;
162
163
  format(val: any): any;
163
164
  }
164
165
  export declare class StringEnumField extends EnumField {
@@ -173,17 +174,18 @@ declare type IntEnumMap = {
173
174
  [key: string]: number;
174
175
  };
175
176
  export interface IntegerEnumOptions extends FieldOptions {
176
- map: IntEnumMap;
177
+ map?: IntEnumMap;
177
178
  deprecated?: IntEnumMap;
178
179
  tsType?: string;
179
180
  graphQLType?: string;
180
181
  disableUnknownType?: boolean;
182
+ globalType?: string;
181
183
  }
182
184
  export declare class IntegerEnumField extends BaseField implements Field {
183
185
  type: Type;
184
186
  private map;
185
187
  constructor(options: IntegerEnumOptions);
186
- valid(val: any): boolean;
188
+ valid(val: any): Promise<boolean>;
187
189
  format(val: any): any;
188
190
  }
189
191
  export declare function IntegerEnumType(options: IntegerEnumOptions): IntegerEnumField;
package/schema/field.js CHANGED
@@ -31,6 +31,7 @@ const uuid_1 = require("uuid");
31
31
  const base_1 = require("../core/base");
32
32
  const db_1 = __importStar(require("../core/db"));
33
33
  const schema_1 = require("./schema");
34
+ const global_schema_1 = require("../core/global_schema");
34
35
  class BaseField {
35
36
  logValue(val) {
36
37
  if (this.sensitive) {
@@ -525,10 +526,11 @@ class EnumField extends BaseField {
525
526
  type: options.tsType,
526
527
  graphQLType: options.graphQLType,
527
528
  disableUnknownType: options.disableUnknownType,
529
+ globalType: options.globalType,
528
530
  };
529
531
  if (!options.foreignKey) {
530
- if (!options.values && !options.map) {
531
- throw new Error("values or map required if not look up table enum. Look-up table enum indicated by foreignKey field");
532
+ if (!options.values && !options.map && !options.globalType) {
533
+ throw new Error("values, map or globalType required if not look up table enum. Look-up table enum indicated by foreignKey field");
532
534
  }
533
535
  if (options.values) {
534
536
  if (!options.values.length) {
@@ -547,8 +549,8 @@ class EnumField extends BaseField {
547
549
  }
548
550
  }
549
551
  else {
550
- if (options.values || options.map) {
551
- throw new Error("cannot specify values or map and foreign key for lookup table enum type");
552
+ if (options.values || options.map || options.globalType) {
553
+ throw new Error("cannot specify values, map or globalType and foreign key for lookup table enum type");
552
554
  }
553
555
  if (options.createEnumType) {
554
556
  throw new Error("cannot specify createEnumType without specifying values");
@@ -563,7 +565,14 @@ class EnumField extends BaseField {
563
565
  this.values = options.values;
564
566
  this.map = options.map;
565
567
  }
566
- valid(val) {
568
+ async valid(val) {
569
+ if (this.type.globalType) {
570
+ const f = (0, global_schema_1.__getGlobalSchemaField)(this.type.globalType);
571
+ if (f && f.valid) {
572
+ return f.valid(val);
573
+ }
574
+ return false;
575
+ }
567
576
  // lookup table enum and indicated via presence of foreignKey
568
577
  if (!this.values && !this.map) {
569
578
  return true;
@@ -581,6 +590,13 @@ class EnumField extends BaseField {
581
590
  return false;
582
591
  }
583
592
  format(val) {
593
+ if (this.type.globalType) {
594
+ const f = (0, global_schema_1.__getGlobalSchemaField)(this.type.globalType);
595
+ if (f && f.format) {
596
+ return f.format(val);
597
+ }
598
+ return val;
599
+ }
584
600
  return val;
585
601
  }
586
602
  }
@@ -603,21 +619,40 @@ class IntegerEnumField extends BaseField {
603
619
  graphQLType: options.graphQLType,
604
620
  deprecatedIntEnumMap: options.deprecated,
605
621
  disableUnknownType: options.disableUnknownType,
622
+ globalType: options.globalType,
606
623
  };
607
- let count = 0;
608
- for (const _ in options.map) {
609
- count++;
610
- break;
611
- }
612
- if (!count) {
613
- throw new Error("need at least one entry in enum map");
614
- }
615
624
  if (options.foreignKey) {
616
625
  throw new Error(`foreignKey on intEnum not supported`);
617
626
  }
618
- this.map = options.map;
627
+ if (options.globalType) {
628
+ if (options.map) {
629
+ throw new Error(`cannot specify map and globalType`);
630
+ }
631
+ this.map = {};
632
+ }
633
+ else {
634
+ let count = 0;
635
+ for (const _ in options.map) {
636
+ count++;
637
+ break;
638
+ }
639
+ if (!count) {
640
+ throw new Error("need at least one entry in enum map");
641
+ }
642
+ if (!options.map) {
643
+ throw new Error("map required if not globalType");
644
+ }
645
+ this.map = options.map;
646
+ }
619
647
  }
620
- valid(val) {
648
+ async valid(val) {
649
+ if (this.type?.globalType) {
650
+ const f = (0, global_schema_1.__getGlobalSchemaField)(this.type.globalType);
651
+ if (f && f.valid) {
652
+ return f.valid(val);
653
+ }
654
+ return false;
655
+ }
621
656
  // lookup table enum and indicated via presence of foreignKey
622
657
  for (const k in this.map) {
623
658
  const v = this.map[k];
@@ -628,6 +663,12 @@ class IntegerEnumField extends BaseField {
628
663
  return false;
629
664
  }
630
665
  format(val) {
666
+ if (this.type.globalType) {
667
+ const f = (0, global_schema_1.__getGlobalSchemaField)(this.type.globalType);
668
+ if (f && f.format) {
669
+ return f.format(val);
670
+ }
671
+ }
631
672
  return parseInt(val);
632
673
  }
633
674
  }
@@ -17,6 +17,7 @@ export interface GlobalSchema {
17
17
  extraEdgeFields?: FieldMap;
18
18
  transformEdgeRead?: () => Clause;
19
19
  transformEdgeWrite?: (stmt: EdgeUpdateOperation) => TransformedEdgeUpdateOperation | null;
20
+ fields?: FieldMap;
20
21
  }
21
22
  type FieldOverride = Pick<FieldOptions, "nullable" | "storageKey" | "serverDefault" | "unique" | "hideFromGraphQL" | "graphqlName" | "index">;
22
23
  export type FieldOverrideMap = {
@@ -171,6 +172,7 @@ export interface Type {
171
172
  intEnumMap?: IntEnumMap;
172
173
  deprecatedIntEnumMap?: IntEnumMap;
173
174
  disableUnknownType?: boolean;
175
+ globalType?: string;
174
176
  importType?: DeprecatedImportType;
175
177
  subFields?: FieldMap;
176
178
  unionFields?: FieldMap;
@@ -1,20 +1,22 @@
1
1
  import { BaseField, ListField } from "./field";
2
2
  import { FieldOptions, Field, Type, FieldMap } from "./schema";
3
- export interface StructOptions extends FieldOptions {
3
+ interface structFieldOptions extends FieldOptions {
4
4
  tsType: string;
5
5
  fields: FieldMap;
6
6
  graphQLType?: string;
7
7
  jsonNotJSONB?: boolean;
8
8
  }
9
- interface allStructOptions extends StructOptions {
10
- jsonAsList?: boolean;
9
+ interface GlobalStructOptions extends FieldOptions {
10
+ globalType: string;
11
11
  }
12
+ export type StructOptions = structFieldOptions | GlobalStructOptions;
12
13
  export declare class StructField extends BaseField implements Field {
13
14
  private options;
15
+ private jsonAsList?;
14
16
  type: Type;
15
- constructor(options: allStructOptions);
17
+ constructor(options: StructOptions, jsonAsList?: boolean | undefined);
16
18
  formatImpl(obj: any, nested?: boolean): string | Object;
17
- format(obj: any, nested?: boolean): string | Object;
19
+ format(obj: any, nested?: boolean): any;
18
20
  private validImpl;
19
21
  valid(obj: any): Promise<boolean>;
20
22
  }
@@ -23,5 +25,5 @@ export declare function StructType(options: StructOptions): StructField & Struct
23
25
  * @deprecated use StructTypeAsList
24
26
  */
25
27
  export declare function StructListType(options: StructOptions): ListField;
26
- export declare function StructTypeAsList(options: allStructOptions): StructField & allStructOptions;
28
+ export declare function StructTypeAsList(options: StructOptions): StructField & StructOptions;
27
29
  export {};
@@ -4,20 +4,23 @@ exports.StructTypeAsList = exports.StructListType = exports.StructType = exports
4
4
  const camel_case_1 = require("camel-case");
5
5
  const field_1 = require("./field");
6
6
  const schema_1 = require("./schema");
7
+ const global_schema_1 = require("../core/global_schema");
7
8
  class StructField extends field_1.BaseField {
8
- constructor(options) {
9
+ constructor(options, jsonAsList) {
9
10
  super();
10
11
  this.options = options;
12
+ this.jsonAsList = jsonAsList;
11
13
  this.type = {
12
14
  dbType: schema_1.DBType.JSONB,
13
15
  };
14
16
  this.type.subFields = options.fields;
15
17
  this.type.type = options.tsType;
16
18
  this.type.graphQLType = options.graphQLType || options.tsType;
19
+ this.type.globalType = this.options.globalType;
17
20
  if (options.jsonNotJSONB) {
18
21
  this.type.dbType = schema_1.DBType.JSON;
19
22
  }
20
- if (options?.jsonAsList) {
23
+ if (jsonAsList) {
21
24
  this.type.listElemType = {
22
25
  dbType: schema_1.DBType.JSONB,
23
26
  };
@@ -57,7 +60,13 @@ class StructField extends field_1.BaseField {
57
60
  return JSON.stringify(ret);
58
61
  }
59
62
  format(obj, nested) {
60
- if (Array.isArray(obj) && this.options.jsonAsList) {
63
+ if (this.type.globalType) {
64
+ const f = (0, global_schema_1.__getGlobalSchemaField)(this.type.globalType);
65
+ if (f && f.format) {
66
+ return f.format(obj);
67
+ }
68
+ }
69
+ if (Array.isArray(obj) && this.jsonAsList) {
61
70
  const ret = obj.map((v) => this.formatImpl(v, true));
62
71
  if (nested) {
63
72
  return ret;
@@ -102,7 +111,14 @@ class StructField extends field_1.BaseField {
102
111
  return ret.every((v) => v);
103
112
  }
104
113
  async valid(obj) {
105
- if (this.options.jsonAsList) {
114
+ if (this.type.globalType) {
115
+ const f = (0, global_schema_1.__getGlobalSchemaField)(this.type.globalType);
116
+ if (f && f.valid) {
117
+ return f.valid(obj);
118
+ }
119
+ return false;
120
+ }
121
+ if (this.jsonAsList) {
106
122
  if (!Array.isArray(obj)) {
107
123
  return false;
108
124
  }
@@ -129,10 +145,7 @@ function StructListType(options) {
129
145
  }
130
146
  exports.StructListType = StructListType;
131
147
  function StructTypeAsList(options) {
132
- let result = new StructField({
133
- ...options,
134
- jsonAsList: true,
135
- });
148
+ let result = new StructField(options, true);
136
149
  return Object.assign(result, options);
137
150
  }
138
151
  exports.StructTypeAsList = StructTypeAsList;
@@ -15,7 +15,7 @@ export declare class UnionField extends BaseField implements FieldOptions {
15
15
  type: Type;
16
16
  m: Map<Object, string>;
17
17
  constructor(options: UnionOptions);
18
- format(obj: any): string | Object;
18
+ format(obj: any): any;
19
19
  private validField;
20
20
  valid(obj: any): Promise<boolean>;
21
21
  }