@snowtop/ent 0.1.0-alpha111 → 0.1.0-alpha112

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.
@@ -35,6 +35,7 @@ const executor_1 = require("./executor");
35
35
  const logger_1 = require("../core/logger");
36
36
  const memoizee_1 = __importDefault(require("memoizee"));
37
37
  const clause = __importStar(require("../core/clause"));
38
+ const util_1 = require("util");
38
39
  var edgeDirection;
39
40
  (function (edgeDirection) {
40
41
  edgeDirection[edgeDirection["inboundEdge"] = 0] = "inboundEdge";
@@ -272,12 +273,32 @@ class Orchestrator {
272
273
  }
273
274
  return new EntCannotDeleteEntError(privacyPolicy, action, this.existingEnt);
274
275
  }
275
- getEntForPrivacyPolicyImpl(editedData) {
276
+ async getEntForPrivacyPolicyImpl(schemaFields, editedData) {
276
277
  if (this.actualOperation !== action_1.WriteOperation.Insert) {
277
278
  return this.existingEnt;
278
279
  }
280
+ // need to format fields if possible because ent constructors expect data that's
281
+ // in the format that's coming from the db
282
+ // required for object fields...
283
+ const formatted = { ...editedData };
284
+ for (const [fieldName, field] of schemaFields) {
285
+ if (!field.format) {
286
+ continue;
287
+ }
288
+ let dbKey = this.getStorageKey(fieldName);
289
+ let val = formatted[dbKey];
290
+ if (!val) {
291
+ continue;
292
+ }
293
+ // nested so it's not JSON stringified or anything like that
294
+ val = field.format(formatted[dbKey], true);
295
+ if (util_1.types.isPromise(val)) {
296
+ val = await val;
297
+ }
298
+ formatted[dbKey] = val;
299
+ }
279
300
  // we create an unsafe ent to be used for privacy policies
280
- return new this.options.builder.ent(this.options.builder.viewer, editedData);
301
+ return new this.options.builder.ent(this.options.builder.viewer, formatted);
281
302
  }
282
303
  getSQLStatementOperation() {
283
304
  switch (this.actualOperation) {
@@ -307,8 +328,8 @@ class Orchestrator {
307
328
  if (this.actualOperation !== action_1.WriteOperation.Insert) {
308
329
  return this.existingEnt;
309
330
  }
310
- const { editedData } = await this.memoizedGetFields();
311
- return this.getEntForPrivacyPolicyImpl(editedData);
331
+ const { schemaFields, editedData } = await this.memoizedGetFields();
332
+ return this.getEntForPrivacyPolicyImpl(schemaFields, editedData);
312
333
  }
313
334
  // this gets the fields that were explicitly set plus any default or transformed values
314
335
  // mainly exists to get default fields e.g. default id to be used in triggers
@@ -359,7 +380,7 @@ class Orchestrator {
359
380
  let privacyError = null;
360
381
  if (privacyPolicy) {
361
382
  try {
362
- await (0, privacy_1.applyPrivacyPolicyX)(this.options.viewer, privacyPolicy, this.getEntForPrivacyPolicyImpl(editedData), this.throwError.bind(this));
383
+ await (0, privacy_1.applyPrivacyPolicyX)(this.options.viewer, privacyPolicy, await this.getEntForPrivacyPolicyImpl(schemaFields, editedData), this.throwError.bind(this));
363
384
  }
364
385
  catch (err) {
365
386
  privacyError = err;
@@ -536,11 +557,17 @@ class Orchestrator {
536
557
  if (defaultValue === undefined) {
537
558
  throw new Error(`defaultValueOnCreate() returned undefined for field ${fieldName}`);
538
559
  }
560
+ if (util_1.types.isPromise(defaultValue)) {
561
+ defaultValue = await defaultValue;
562
+ }
539
563
  }
540
564
  }
541
565
  if (field.defaultValueOnEdit &&
542
566
  this.actualOperation === action_1.WriteOperation.Edit) {
543
567
  defaultValue = field.defaultValueOnEdit(builder, input);
568
+ if (util_1.types.isPromise(defaultValue)) {
569
+ defaultValue = await defaultValue;
570
+ }
544
571
  }
545
572
  }
546
573
  if (value !== undefined) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@snowtop/ent",
3
- "version": "0.1.0-alpha111",
3
+ "version": "0.1.0-alpha112",
4
4
  "description": "snowtop ent framework",
5
5
  "main": "index.js",
6
6
  "types": "index.d.ts",