@snowtop/ent 0.1.0-alpha89-16dc3410-33c4-11ed-9fc4-5d4a927887be → 0.1.0-alpha90

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.
@@ -356,6 +356,10 @@ class Orchestrator {
356
356
  privacyError = err;
357
357
  }
358
358
  }
359
+ // privacyError should return first since it's less confusing
360
+ if (privacyError !== null) {
361
+ return [privacyError];
362
+ }
359
363
  // have to run triggers which update fields first before field and other validators
360
364
  // so running this first to build things up
361
365
  if (action?.getTriggers) {
@@ -372,9 +376,6 @@ class Orchestrator {
372
376
  this.formatAndValidateFields(schemaFields, editedFields2),
373
377
  this.validators(validators, action, builder),
374
378
  ]);
375
- if (privacyError !== null) {
376
- errors.unshift(privacyError);
377
- }
378
379
  errors.push(...errs2);
379
380
  return errors;
380
381
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@snowtop/ent",
3
- "version": "0.1.0-alpha89-16dc3410-33c4-11ed-9fc4-5d4a927887be",
3
+ "version": "0.1.0-alpha90",
4
4
  "description": "snowtop ent framework",
5
5
  "main": "index.js",
6
6
  "types": "index.d.ts",
@@ -46,7 +46,7 @@ declare type ProcessedType = Omit<Type, "subFields" | "listElemType" | "unionFie
46
46
  listElemType?: ProcessedType;
47
47
  unionFields?: ProcessedField[];
48
48
  };
49
- declare type ProcessedField = Omit<Field, "defaultValueOnEdit" | "defaultValueOnCreate" | "privacyPolicy" | "type"> & {
49
+ declare type ProcessedField = Omit<Field, "defaultValueOnEdit" | "defaultValueOnCreate" | "privacyPolicy" | "type" | "serverDefault"> & {
50
50
  name: string;
51
51
  hasDefaultValueOnCreate?: boolean;
52
52
  hasDefaultValueOnEdit?: boolean;
@@ -54,6 +54,7 @@ declare type ProcessedField = Omit<Field, "defaultValueOnEdit" | "defaultValueOn
54
54
  hasFieldPrivacy?: boolean;
55
55
  derivedFields?: ProcessedField[];
56
56
  type: ProcessedType;
57
+ serverDefault?: string;
57
58
  };
58
59
  interface patternsDict {
59
60
  [key: string]: ProcessedPattern;
@@ -66,7 +67,7 @@ interface Result {
66
67
  declare type PotentialSchemas = {
67
68
  [key: string]: any;
68
69
  };
69
- export declare function parseSchema(potentialSchemas: PotentialSchemas, globalSchema?: GlobalSchema): Result;
70
+ export declare function parseSchema(potentialSchemas: PotentialSchemas, globalSchema?: GlobalSchema): Promise<Result>;
70
71
  interface ProcessedGlobalSchema {
71
72
  globalEdges: ProcessedAssocEdge[];
72
73
  extraEdgeFields: ProcessedField[];
@@ -1,7 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.parseSchema = void 0;
4
- function processFields(src, patternName) {
4
+ async function processFields(src, patternName) {
5
5
  const ret = [];
6
6
  let m = {};
7
7
  if (Array.isArray(src)) {
@@ -60,26 +60,48 @@ function processFields(src, patternName) {
60
60
  if (patternName) {
61
61
  f.patternName = patternName;
62
62
  }
63
+ if (field.serverDefault !== undefined) {
64
+ f.serverDefault = await transformServerDefault(name, field, field.serverDefault);
65
+ }
63
66
  transformType(field.type);
64
67
  if (field.getDerivedFields) {
65
- f.derivedFields = processFields(field.getDerivedFields(name));
68
+ f.derivedFields = await processFields(field.getDerivedFields(name));
66
69
  }
67
70
  if (field.type.subFields) {
68
- f.type.subFields = processFields(field.type.subFields);
71
+ f.type.subFields = await processFields(field.type.subFields);
69
72
  }
70
73
  if (field.type.unionFields) {
71
- f.type.unionFields = processFields(field.type.unionFields);
74
+ f.type.unionFields = await processFields(field.type.unionFields);
72
75
  }
73
76
  if (field.type.listElemType &&
74
77
  field.type.listElemType.subFields &&
75
78
  // check to avoid ts-ignore below. exists just for tsc
76
79
  f.type.listElemType) {
77
- f.type.listElemType.subFields = processFields(field.type.listElemType.subFields);
80
+ f.type.listElemType.subFields = await processFields(field.type.listElemType.subFields);
78
81
  }
79
82
  ret.push(f);
80
83
  }
81
84
  return ret;
82
85
  }
86
+ async function transformServerDefault(name, f, value) {
87
+ if (f.valid) {
88
+ if (!(await f.valid(value))) {
89
+ throw new Error(`invalid value ${value} passed to field ${name}`);
90
+ }
91
+ }
92
+ if (f.format) {
93
+ value = await f.format(value);
94
+ }
95
+ switch (typeof value) {
96
+ case "boolean":
97
+ case "number":
98
+ case "bigint":
99
+ case "string":
100
+ return `${value}`;
101
+ default:
102
+ throw new Error(`invalid value ${value} passed to field ${name}`);
103
+ }
104
+ }
83
105
  function transformImportType(typ) {
84
106
  if (!typ.importType) {
85
107
  return;
@@ -121,12 +143,12 @@ function processEdgeGroups(processedSchema, edgeGroups) {
121
143
  processedSchema.assocEdgeGroups.push(group2);
122
144
  }
123
145
  }
124
- function processPattern(patterns, pattern, processedSchema) {
146
+ async function processPattern(patterns, pattern, processedSchema) {
125
147
  let ret = {
126
148
  ...pattern,
127
149
  };
128
150
  const name = pattern.name;
129
- const fields = processFields(pattern.fields, pattern.name);
151
+ const fields = await processFields(pattern.fields, pattern.name);
130
152
  processedSchema.fields.push(...fields);
131
153
  if (pattern.edges) {
132
154
  const edges = processEdges(pattern.edges, pattern.name);
@@ -185,12 +207,12 @@ function processAction(action) {
185
207
  ret.actionOnlyFields = actionOnlyFields;
186
208
  return ret;
187
209
  }
188
- function parseSchema(potentialSchemas, globalSchema) {
210
+ async function parseSchema(potentialSchemas, globalSchema) {
189
211
  let schemas = {};
190
212
  let patterns = {};
191
213
  let parsedGlobalSchema;
192
214
  if (globalSchema) {
193
- parsedGlobalSchema = parseGlobalSchema(globalSchema);
215
+ parsedGlobalSchema = await parseGlobalSchema(globalSchema);
194
216
  }
195
217
  for (const key in potentialSchemas) {
196
218
  const value = potentialSchemas[key];
@@ -223,7 +245,7 @@ function parseSchema(potentialSchemas, globalSchema) {
223
245
  let patternNames = [];
224
246
  if (schema.patterns) {
225
247
  for (const pattern of schema.patterns) {
226
- const ret = processPattern(patterns, pattern, processedSchema);
248
+ const ret = await processPattern(patterns, pattern, processedSchema);
227
249
  patternNames.push(pattern.name);
228
250
  if (ret.transformsSelect) {
229
251
  if (processedSchema.transformsSelect) {
@@ -239,7 +261,7 @@ function parseSchema(potentialSchemas, globalSchema) {
239
261
  }
240
262
  }
241
263
  }
242
- const fields = processFields(schema.fields);
264
+ const fields = await processFields(schema.fields);
243
265
  processedSchema.fields.push(...fields);
244
266
  processedSchema.patternNames = patternNames;
245
267
  if (schema.edges) {
@@ -254,7 +276,7 @@ function parseSchema(potentialSchemas, globalSchema) {
254
276
  return { schemas, patterns, globalSchema: parsedGlobalSchema };
255
277
  }
256
278
  exports.parseSchema = parseSchema;
257
- function parseGlobalSchema(s) {
279
+ async function parseGlobalSchema(s) {
258
280
  const ret = {
259
281
  globalEdges: [],
260
282
  extraEdgeFields: [],
@@ -263,7 +285,7 @@ function parseGlobalSchema(s) {
263
285
  s.transformEdgeWrite !== undefined,
264
286
  };
265
287
  if (s.extraEdgeFields) {
266
- ret.extraEdgeFields = processFields(s.extraEdgeFields);
288
+ ret.extraEdgeFields = await processFields(s.extraEdgeFields);
267
289
  }
268
290
  if (s.edges) {
269
291
  ret.globalEdges = processEdges(s.edges);
package/schema/field.js CHANGED
@@ -459,9 +459,10 @@ class DateField extends BaseField {
459
459
  this.type = { dbType: schema_1.DBType.Date };
460
460
  }
461
461
  format(val) {
462
- if (!(val instanceof Date)) {
462
+ if (typeof val === "string") {
463
463
  return val;
464
464
  }
465
+ val = new Date(val);
465
466
  let yy = (0, exports.leftPad)(val.getFullYear());
466
467
  // lol this API
467
468
  // for some reason this is 0-index
@@ -26,7 +26,6 @@ const glob_1 = __importDefault(require("glob"));
26
26
  const path = __importStar(require("path"));
27
27
  const pascal_case_1 = require("pascal-case");
28
28
  const minimist_1 = __importDefault(require("minimist"));
29
- const process_1 = require("process");
30
29
  const parse_1 = require("../parse_schema/parse");
31
30
  const ast_1 = require("../tsc/ast");
32
31
  function main() {
@@ -70,13 +69,16 @@ function main() {
70
69
  potentialSchemas[(0, pascal_case_1.pascalCase)(schema)] = s;
71
70
  }
72
71
  // console.log(potentialSchemas);
73
- const result = (0, parse_1.parseSchema)(potentialSchemas, globalSchema);
74
- console.log(JSON.stringify(result));
72
+ // NB: do not change this to async/await
73
+ // doing so runs it buffer limit on linux (65536 bytes) and we lose data reading in go
74
+ (0, parse_1.parseSchema)(potentialSchemas, globalSchema).then((result) => {
75
+ console.log(JSON.stringify(result));
76
+ });
75
77
  }
76
78
  try {
77
79
  main();
78
80
  }
79
81
  catch (err) {
80
82
  console.error(err);
81
- (0, process_1.exit)(1);
83
+ process.exit(1);
82
84
  }