@snowtop/ent 0.2.4 → 0.2.5

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.
@@ -1,10 +1,11 @@
1
1
  import { Orchestrator } from "./orchestrator";
2
- import { Viewer, Ent, Data } from "../core/base";
2
+ import { Viewer, Ent, Data, Context } from "../core/base";
3
3
  import { Action, WriteOperation, Builder, Trigger, Observer, Changeset, Validator } from "./action";
4
4
  export interface ActionOptions<TEnt extends Ent<TViewer>, TViewer extends Viewer, TData extends Data, TExistingEnt extends TMaybleNullableEnt<TEnt> = MaybeNull<TEnt>> {
5
5
  existingEnt: TExistingEnt;
6
6
  input?: TData;
7
7
  operation?: WriteOperation;
8
+ viewerForEntLoad?(data: Data, ctx?: Context<TViewer>): TViewer | Promise<TViewer>;
8
9
  }
9
10
  type MaybeNull<T extends Ent> = T | null;
10
11
  type TMaybleNullableEnt<T extends Ent> = T | MaybeNull<T>;
@@ -23,10 +24,12 @@ export declare class BaseAction<TEnt extends Ent<TViewer>, TViewer extends Viewe
23
24
  builderCtr: BuilderConstructor<TEnt, TViewer, TInput, TExistingEnt>;
24
25
  builder: EntBuilder<TEnt, TViewer, TInput, TExistingEnt>;
25
26
  private input;
27
+ options: ActionOptions<TEnt, TViewer, TInput, TExistingEnt>;
26
28
  getPrivacyPolicy(): import("../core/base").PrivacyPolicy<Ent<Viewer<Ent<any> | null, import("../core/base").ID | null>>, Viewer<Ent<any> | null, import("../core/base").ID | null>>;
27
29
  getTriggers(): Trigger<TEnt, EntBuilder<TEnt, TViewer, TInput, TExistingEnt>, TViewer, TInput, TExistingEnt>[];
28
30
  getObservers(): Observer<TEnt, EntBuilder<TEnt, TViewer, TInput, TExistingEnt>, TViewer, TInput, TExistingEnt>[];
29
31
  getValidators(): Validator<TEnt, EntBuilder<TEnt, TViewer, TInput, TExistingEnt>, TViewer, TInput, TExistingEnt>[];
32
+ viewerForEntLoad(data: Data, context?: Context<TViewer> | undefined): TViewer | Promise<TViewer>;
30
33
  constructor(viewer: TViewer, builderCtr: BuilderConstructor<TEnt, TViewer, TInput, TExistingEnt>, options: ActionOptions<TEnt, TViewer, TInput, TExistingEnt>);
31
34
  static createBuilder<TEnt extends Ent<TViewer>, TViewer extends Viewer, TInput extends Data, TExistingEnt extends TMaybleNullableEnt<TEnt> = MaybeNull<TEnt>>(viewer: Viewer, builderCtr: BuilderConstructor<TEnt, TViewer, TInput, TExistingEnt>, options: ActionOptions<TEnt, TViewer, TInput, TExistingEnt>): Builder<TEnt>;
32
35
  /**
@@ -44,7 +47,7 @@ export interface BuilderConstructor<TEnt extends Ent<TViewer>, TViewer extends V
44
47
  new (viewer: TViewer, operation: WriteOperation, action: Action<TEnt, any, TViewer, TInput, TExistingEnt>, existingEnt: TExistingEnt): EntBuilder<TEnt, TViewer, TInput, TExistingEnt>;
45
48
  }
46
49
  export declare function updateRawObject<TEnt extends Ent<TViewer>, TViewer extends Viewer, TInput extends Data>(viewer: TViewer, builderCtr: BuilderConstructor<TEnt, TViewer, TInput, TEnt>, existingEnt: TEnt, input: TInput): Promise<TEnt>;
47
- export declare function getSimpleEditAction<TEnt extends Ent<TViewer>, TViewer extends Viewer, TInput extends Data>(viewer: TViewer, builderCtr: BuilderConstructor<TEnt, TViewer, TInput, TEnt>, existingEnt: TEnt, input: TInput): BaseAction<TEnt, TViewer, TInput, TEnt>;
48
- export declare function getSimpleDeleteAction<TEnt extends Ent<TViewer>, TViewer extends Viewer, TInput extends Data>(viewer: TViewer, builderCtr: BuilderConstructor<TEnt, TViewer, TInput, TEnt>, existingEnt: TEnt, input: TInput): BaseAction<TEnt, TViewer, TInput, TEnt>;
49
- export declare function getSimpleInsertAction<TEnt extends Ent<TViewer>, TViewer extends Viewer, TInput extends Data>(viewer: TViewer, builderCtr: BuilderConstructor<TEnt, TViewer, TInput, null>, input: TInput): BaseAction<TEnt, TViewer, TInput, null>;
50
+ export declare function getSimpleEditAction<TEnt extends Ent<TViewer>, TViewer extends Viewer, TInput extends Data>(viewer: TViewer, builderCtr: BuilderConstructor<TEnt, TViewer, TInput, TEnt>, existingEnt: TEnt, input: TInput, opts?: Omit<ActionOptions<TEnt, TViewer, TInput, TEnt>, "operation" | "input" | "existingEnt">): BaseAction<TEnt, TViewer, TInput, TEnt>;
51
+ export declare function getSimpleDeleteAction<TEnt extends Ent<TViewer>, TViewer extends Viewer, TInput extends Data>(viewer: TViewer, builderCtr: BuilderConstructor<TEnt, TViewer, TInput, TEnt>, existingEnt: TEnt, input: TInput, opts?: Omit<ActionOptions<TEnt, TViewer, TInput, null>, "operation" | "input" | "existingEnt">): BaseAction<TEnt, TViewer, TInput, TEnt>;
52
+ export declare function getSimpleInsertAction<TEnt extends Ent<TViewer>, TViewer extends Viewer, TInput extends Data>(viewer: TViewer, builderCtr: BuilderConstructor<TEnt, TViewer, TInput, null>, input: TInput, opts?: Omit<ActionOptions<TEnt, TViewer, TInput, null>, "operation" | "input" | "existingEnt">): BaseAction<TEnt, TViewer, TInput, null>;
50
53
  export {};
@@ -16,6 +16,12 @@ class BaseAction {
16
16
  getValidators() {
17
17
  return [];
18
18
  }
19
+ viewerForEntLoad(data, context) {
20
+ if (this.options.viewerForEntLoad) {
21
+ return this.options.viewerForEntLoad(data, context);
22
+ }
23
+ return this.viewer;
24
+ }
19
25
  constructor(viewer, builderCtr, options) {
20
26
  this.viewer = viewer;
21
27
  this.builderCtr = builderCtr;
@@ -30,6 +36,7 @@ class BaseAction {
30
36
  }
31
37
  this.input = options?.input || {};
32
38
  this.builder = new builderCtr(viewer, operation, this, options.existingEnt);
39
+ this.options = options;
33
40
  }
34
41
  static createBuilder(viewer, builderCtr, options) {
35
42
  let action = new BaseAction(viewer, builderCtr, options);
@@ -93,27 +100,30 @@ exports.updateRawObject = updateRawObject;
93
100
  // does do field validation
94
101
  // useful to batch a bunch of writes together with BaseAction.bulkAction
95
102
  // note that only editable fields in the builder can be passed here
96
- function getSimpleEditAction(viewer, builderCtr, existingEnt, input) {
103
+ function getSimpleEditAction(viewer, builderCtr, existingEnt, input, opts) {
97
104
  return new BaseAction(viewer, builderCtr, {
98
105
  existingEnt: existingEnt,
99
106
  operation: action_1.WriteOperation.Edit,
100
107
  input,
108
+ ...opts,
101
109
  });
102
110
  }
103
111
  exports.getSimpleEditAction = getSimpleEditAction;
104
- function getSimpleDeleteAction(viewer, builderCtr, existingEnt, input) {
112
+ function getSimpleDeleteAction(viewer, builderCtr, existingEnt, input, opts) {
105
113
  return new BaseAction(viewer, builderCtr, {
106
114
  existingEnt: existingEnt,
107
115
  operation: action_1.WriteOperation.Delete,
108
116
  input,
117
+ ...opts,
109
118
  });
110
119
  }
111
120
  exports.getSimpleDeleteAction = getSimpleDeleteAction;
112
- function getSimpleInsertAction(viewer, builderCtr, input) {
121
+ function getSimpleInsertAction(viewer, builderCtr, input, opts) {
113
122
  return new BaseAction(viewer, builderCtr, {
114
123
  operation: action_1.WriteOperation.Insert,
115
124
  input,
116
125
  existingEnt: null,
126
+ ...opts,
117
127
  });
118
128
  }
119
129
  exports.getSimpleInsertAction = getSimpleInsertAction;
@@ -73,6 +73,14 @@ export declare class Orchestrator<TEnt extends Ent<TViewer>, TInput extends Data
73
73
  private getSQLStatementOperation;
74
74
  private getWriteOpForSQLStamentOp;
75
75
  getPossibleUnsafeEntForPrivacy(): Promise<TEnt>;
76
+ /**
77
+ * This gets the fields that were explicitly set plus any default or transformed values
78
+ * mainly exists to get default fields e.g. default id to be used in triggers
79
+ * NOTE: this API may change in the future
80
+ * doesn't work to get ids for autoincrement keys
81
+ * PS contrasted with getValidatedFields() which returns the format that would be written to the db
82
+ * i.e. includes lists which have been converted to JSON strings, etc
83
+ */
76
84
  getEditedData(): Promise<Data>;
77
85
  /**
78
86
  * @returns validated and formatted fields that would be written to the db
@@ -93,6 +93,7 @@ class Orchestrator {
93
93
  this.edgeSet = new Set();
94
94
  this.edges = new Map();
95
95
  this.conditionalEdges = new Map();
96
+ this.validatedFields = null;
96
97
  this.changesets = [];
97
98
  this.dependencies = new Map();
98
99
  this.fieldsToResolve = [];
@@ -401,10 +402,14 @@ class Orchestrator {
401
402
  const { schemaFields, editedData } = await this.memoizedGetFields();
402
403
  return this.getEntForPrivacyPolicyImpl(schemaFields, editedData, this.options.viewer);
403
404
  }
404
- // this gets the fields that were explicitly set plus any default or transformed values
405
- // mainly exists to get default fields e.g. default id to be used in triggers
406
- // NOTE: this API may change in the future
407
- // doesn't work to get ids for autoincrement keys
405
+ /**
406
+ * This gets the fields that were explicitly set plus any default or transformed values
407
+ * mainly exists to get default fields e.g. default id to be used in triggers
408
+ * NOTE: this API may change in the future
409
+ * doesn't work to get ids for autoincrement keys
410
+ * PS contrasted with getValidatedFields() which returns the format that would be written to the db
411
+ * i.e. includes lists which have been converted to JSON strings, etc
412
+ */
408
413
  async getEditedData() {
409
414
  const { editedData } = await this.memoizedGetFields();
410
415
  return editedData;
@@ -519,7 +524,7 @@ class Orchestrator {
519
524
  return errors;
520
525
  }
521
526
  async triggers(action, builder, triggers) {
522
- let groups = [];
527
+ const groups = [];
523
528
  let lastArray = 0;
524
529
  let prevWasArray = false;
525
530
  for (let i = 0; i < triggers.length; i++) {
@@ -696,29 +701,12 @@ class Orchestrator {
696
701
  data[dbKey] = value;
697
702
  }
698
703
  if (defaultValue !== undefined) {
699
- // Format defaults early so JSON/list defaults are DB-ready in edited data.
700
- let formattedDefaultValue = defaultValue;
701
- if (defaultValue !== null && !this.isBuilder(defaultValue) && field.format) {
702
- let valid = true;
703
- if (field.valid) {
704
- valid = field.valid(defaultValue);
705
- if ((0, types_1.isPromise)(valid)) {
706
- valid = await valid;
707
- }
708
- }
709
- if (valid) {
710
- formattedDefaultValue = field.format(defaultValue);
711
- if ((0, types_1.isPromise)(formattedDefaultValue)) {
712
- formattedDefaultValue = await formattedDefaultValue;
713
- }
714
- }
715
- }
716
704
  updateInput = true;
717
705
  if (updateOnlyIfOther) {
718
- defaultData[dbKey] = formattedDefaultValue;
706
+ defaultData[dbKey] = defaultValue;
719
707
  }
720
708
  else {
721
- data[dbKey] = formattedDefaultValue;
709
+ data[dbKey] = defaultValue;
722
710
  }
723
711
  this.defaultFieldsByFieldName[fieldName] = defaultValue;
724
712
  this.defaultFieldsByTSName[this.getInputKey(fieldName)] = defaultValue;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@snowtop/ent",
3
- "version": "0.2.4",
3
+ "version": "0.2.5",
4
4
  "description": "snowtop ent framework",
5
5
  "main": "index.js",
6
6
  "types": "index.d.ts",