@snowtop/ent 0.0.31 → 0.0.32-alpha

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.
@@ -380,16 +380,21 @@ class Orchestrator {
380
380
  }
381
381
  }
382
382
  else if (this.isBuilder(value)) {
383
- let builder = value;
383
+ if (field.valid) {
384
+ const valid = await Promise.resolve(field.valid(value));
385
+ if (!valid) {
386
+ throw new Error(`invalid field ${field.name} with value ${value}`);
387
+ }
388
+ }
384
389
  // keep track of dependencies to resolve
385
- this.dependencies.set(builder.placeholderID, builder);
390
+ this.dependencies.set(value.placeholderID, value);
386
391
  // keep track of fields to resolve
387
392
  this.fieldsToResolve.push(dbKey);
388
393
  }
389
394
  else {
390
395
  if (field.valid) {
391
396
  // TODO this could be async. handle this better
392
- let valid = await Promise.resolve(field.valid(value));
397
+ const valid = await Promise.resolve(field.valid(value));
393
398
  if (!valid) {
394
399
  throw new Error(`invalid field ${field.name} with value ${value}`);
395
400
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@snowtop/ent",
3
- "version": "0.0.31",
3
+ "version": "0.0.32-alpha",
4
4
  "description": "snowtop ent framework",
5
5
  "main": "index.js",
6
6
  "types": "index.d.ts",
@@ -37,7 +37,7 @@ let nodeField = (0, field_1.UUIDType)({
37
37
  return (0, uuid_1.v4)();
38
38
  },
39
39
  });
40
- let nodeFields = [nodeField].concat(tsFields);
40
+ let nodeFields = [nodeField, ...tsFields];
41
41
  let nodeFieldsWithTZ = [
42
42
  nodeField,
43
43
  (0, field_1.TimestampType)({
package/schema/field.d.ts CHANGED
@@ -17,8 +17,11 @@ export declare abstract class BaseField {
17
17
  logValue(val: any): any;
18
18
  }
19
19
  export declare class UUIDField extends BaseField implements Field {
20
+ private options;
20
21
  type: Type;
21
22
  constructor(options: FieldOptions);
23
+ private isBuilder;
24
+ valid(val: any): Promise<boolean>;
22
25
  }
23
26
  export declare function UUIDType(options: FieldOptions): UUIDField;
24
27
  export interface IntegerOptions extends FieldOptions {
package/schema/field.js CHANGED
@@ -37,6 +37,7 @@ exports.BaseField = BaseField;
37
37
  class UUIDField extends BaseField {
38
38
  constructor(options) {
39
39
  super();
40
+ this.options = options;
40
41
  this.type = { dbType: schema_1.DBType.UUID };
41
42
  const polymorphic = options.polymorphic;
42
43
  if (polymorphic) {
@@ -80,6 +81,26 @@ class UUIDField extends BaseField {
80
81
  ];
81
82
  }
82
83
  }
84
+ if (options.fieldEdge?.enforceSchema && !options.fieldEdge.loadRowByType) {
85
+ throw new Error(`cannot enforceSchema if loadRowByType wasn't passed in`);
86
+ }
87
+ }
88
+ isBuilder(val) {
89
+ return val.placeholderID !== undefined;
90
+ }
91
+ async valid(val) {
92
+ if (!this.options.fieldEdge?.enforceSchema) {
93
+ return true;
94
+ }
95
+ const loadRowByType = this.options.fieldEdge.loadRowByType;
96
+ const loadRowOptions = loadRowByType(this.options.fieldEdge.schema);
97
+ if (this.isBuilder(val)) {
98
+ // if builder, the ent type of the builder and the ent type returned by the load constructor should match
99
+ return val.ent === loadRowOptions.ent;
100
+ }
101
+ // TODO we need context here to make sure that we hit local cache
102
+ const row = await loadRowOptions.loaderFactory.createLoader().load(val);
103
+ return row !== null;
83
104
  }
84
105
  }
85
106
  exports.UUIDField = UUIDField;
@@ -1,4 +1,4 @@
1
- import { Data, Ent } from "../core/base";
1
+ import { Data, Ent, LoadEntOptions } from "../core/base";
2
2
  import { Builder } from "../action/action";
3
3
  export default interface Schema {
4
4
  fields: Field[];
@@ -101,9 +101,12 @@ export interface ForeignKey {
101
101
  name?: string;
102
102
  disableIndex?: boolean;
103
103
  }
104
+ declare type loadRowFn = (type: string) => LoadEntOptions<Ent>;
104
105
  export interface FieldEdge {
105
106
  schema: string;
106
- inverseEdge: string;
107
+ inverseEdge?: string;
108
+ enforceSchema?: boolean;
109
+ loadRowByType?: loadRowFn;
107
110
  }
108
111
  export interface FieldOptions {
109
112
  name: string;
@@ -133,7 +136,7 @@ export interface PolymorphicOptions {
133
136
  }
134
137
  export interface Field extends FieldOptions {
135
138
  type: Type;
136
- valid?(val: any): boolean;
139
+ valid?(val: any): Promise<boolean> | boolean;
137
140
  format?(val: any): any;
138
141
  logValue(val: any): any;
139
142
  }