@snowtop/ent 0.0.40-alpha5 → 0.0.40-alpha6

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.
@@ -57,9 +57,11 @@ export declare class Orchestrator<T extends Ent> {
57
57
  private getEdgeOperation;
58
58
  private buildEdgeOps;
59
59
  private throwError;
60
- private getEntForPrivacyPolicy;
60
+ private getEntForPrivacyPolicyImpl;
61
61
  private getSQLStatementOperation;
62
62
  private getWriteOpForSQLStamentOp;
63
+ getPossibleUnsafeEntForPrivacy(): Promise<T | undefined>;
64
+ private getFieldsInfo;
63
65
  private validate;
64
66
  private triggers;
65
67
  private validators;
@@ -234,7 +234,7 @@ class Orchestrator {
234
234
  }
235
235
  return new EntCannotDeleteEntError(privacyPolicy, action, this.existingEnt);
236
236
  }
237
- getEntForPrivacyPolicy(editedData) {
237
+ getEntForPrivacyPolicyImpl(editedData) {
238
238
  if (this.actualOperation !== action_1.WriteOperation.Insert) {
239
239
  return this.existingEnt;
240
240
  }
@@ -263,6 +263,24 @@ class Orchestrator {
263
263
  throw new Error("invalid path");
264
264
  }
265
265
  }
266
+ // if you're doing custom privacy within an action and want to
267
+ // get either the unsafe ent or the existing ent that's being edited
268
+ async getPossibleUnsafeEntForPrivacy() {
269
+ if (this.actualOperation !== action_1.WriteOperation.Insert) {
270
+ return this.existingEnt;
271
+ }
272
+ const { editedData } = await this.getFieldsInfo();
273
+ return this.getEntForPrivacyPolicyImpl(editedData);
274
+ }
275
+ async getFieldsInfo() {
276
+ const action = this.options.action;
277
+ const builder = this.options.builder;
278
+ // future optimization: can get schemaFields to memoize based on different values
279
+ const schemaFields = (0, schema_1.getFields)(this.options.schema);
280
+ const editedFields = await this.options.editedFields();
281
+ let editedData = await this.getFieldsWithDefaultValues(builder, schemaFields, editedFields, action);
282
+ return { editedData, editedFields, schemaFields };
283
+ }
266
284
  async validate() {
267
285
  // existing ent required for edit or delete operations
268
286
  switch (this.actualOperation) {
@@ -272,12 +290,9 @@ class Orchestrator {
272
290
  throw new Error(`existing ent required with operation ${this.actualOperation}`);
273
291
  }
274
292
  }
293
+ const { schemaFields, editedData } = await this.getFieldsInfo();
275
294
  const action = this.options.action;
276
295
  const builder = this.options.builder;
277
- // future optimization: can get schemaFields to memoize based on different values
278
- const schemaFields = (0, schema_1.getFields)(this.options.schema);
279
- const editedFields = await this.options.editedFields();
280
- let editedData = await this.getFieldsWithDefaultValues(builder, schemaFields, editedFields, action);
281
296
  // this runs in following phases:
282
297
  // * set default fields and pass to builder so the value can be checked by triggers/observers/validators
283
298
  // * privacy policy (use unsafe ent if we have it)
@@ -285,7 +300,7 @@ class Orchestrator {
285
300
  // * validators
286
301
  let privacyPolicy = action?.getPrivacyPolicy();
287
302
  if (privacyPolicy) {
288
- await (0, privacy_1.applyPrivacyPolicyX)(this.options.viewer, privacyPolicy, this.getEntForPrivacyPolicy(editedData), this.throwError.bind(this));
303
+ await (0, privacy_1.applyPrivacyPolicyX)(this.options.viewer, privacyPolicy, this.getEntForPrivacyPolicyImpl(editedData), this.throwError.bind(this));
289
304
  }
290
305
  // have to run triggers which update fields first before field and other validators
291
306
  // so running this first to build things up
@@ -295,7 +310,7 @@ class Orchestrator {
295
310
  }
296
311
  let validators = action?.validators || [];
297
312
  // not ideal we're calling this twice. fix...
298
- // needed for now. may need to write somet of this?
313
+ // needed for now. may need to rewrite some of this?
299
314
  const editedFields2 = await this.options.editedFields();
300
315
  await Promise.all([
301
316
  this.formatAndValidateFields(schemaFields, editedFields2),
package/core/ent.js CHANGED
@@ -287,6 +287,11 @@ async function doFieldPrivacy(viewer, ent, data, options) {
287
287
  let somethingChanged = false;
288
288
  for (const [k, policy] of options.fieldPrivacy) {
289
289
  promises.push((async () => {
290
+ // don't do anything if key is null or for some reason missing
291
+ const curr = data[k];
292
+ if (curr === null || curr === undefined) {
293
+ return;
294
+ }
290
295
  const r = await (0, privacy_1.applyPrivacyPolicy)(viewer, policy, ent);
291
296
  if (!r) {
292
297
  data[k] = null;
package/core/viewer.d.ts CHANGED
@@ -17,6 +17,7 @@ export declare class IDViewer implements Viewer {
17
17
  context?: Context;
18
18
  constructor(viewerID: ID, opts?: Partial<IDViewerOptions>);
19
19
  constructor(opts: IDViewerOptions);
20
+ setContext(ctx: Context): this;
20
21
  viewer(): Promise<Ent | null>;
21
22
  instanceKey(): string;
22
23
  }
package/core/viewer.js CHANGED
@@ -27,6 +27,10 @@ class IDViewer {
27
27
  this.ent = opts?.ent || null;
28
28
  this.context = opts?.context;
29
29
  }
30
+ setContext(ctx) {
31
+ this.context = ctx;
32
+ return this;
33
+ }
30
34
  async viewer() {
31
35
  return this.ent;
32
36
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@snowtop/ent",
3
- "version": "0.0.40-alpha5",
3
+ "version": "0.0.40-alpha6",
4
4
  "description": "snowtop ent framework",
5
5
  "main": "index.js",
6
6
  "types": "index.d.ts",
package/schema/index.d.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import Schema from "./schema";
2
2
  export { Schema };
3
- export { Field, AssocEdge, AssocEdgeGroup, InverseAssocEdge, Edge, Pattern, DBType, Type, FieldOptions, SchemaConstructor, SchemaInputType, getFields, getFieldsWithPrivacy, ActionOperation, Action, EdgeAction, NoFields, Constraint, Index, ConstraintType, ForeignKeyInfo, requiredField, optionalField, UpdateOperation, TransformedUpdateOperation, SQLStatementOperation, getTransformedReadClause, getObjectLoaderProperties, } from "./schema";
3
+ export { Field, AssocEdge, AssocEdgeGroup, InverseAssocEdge, Edge, Pattern, DBType, Type, FieldOptions, SchemaConstructor, SchemaInputType, getFields, getFieldsWithPrivacy, getStorageKey, ActionOperation, Action, EdgeAction, NoFields, Constraint, Index, ConstraintType, ForeignKeyInfo, requiredField, optionalField, UpdateOperation, TransformedUpdateOperation, SQLStatementOperation, getTransformedReadClause, getObjectLoaderProperties, } from "./schema";
4
4
  export { Timestamps, Node, BaseEntSchema, BaseEntSchemaWithTZ, } from "./base_schema";
5
5
  export * from "./field";
6
6
  export * from "./json_field";
package/schema/index.js CHANGED
@@ -10,11 +10,12 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
10
10
  for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
11
11
  };
12
12
  Object.defineProperty(exports, "__esModule", { value: true });
13
- exports.BaseEntSchemaWithTZ = exports.BaseEntSchema = exports.Node = exports.Timestamps = exports.getObjectLoaderProperties = exports.getTransformedReadClause = exports.SQLStatementOperation = exports.optionalField = exports.requiredField = exports.ConstraintType = exports.NoFields = exports.ActionOperation = exports.getFieldsWithPrivacy = exports.getFields = exports.DBType = void 0;
13
+ exports.BaseEntSchemaWithTZ = exports.BaseEntSchema = exports.Node = exports.Timestamps = exports.getObjectLoaderProperties = exports.getTransformedReadClause = exports.SQLStatementOperation = exports.optionalField = exports.requiredField = exports.ConstraintType = exports.NoFields = exports.ActionOperation = exports.getStorageKey = exports.getFieldsWithPrivacy = exports.getFields = exports.DBType = void 0;
14
14
  var schema_1 = require("./schema");
15
15
  Object.defineProperty(exports, "DBType", { enumerable: true, get: function () { return schema_1.DBType; } });
16
16
  Object.defineProperty(exports, "getFields", { enumerable: true, get: function () { return schema_1.getFields; } });
17
17
  Object.defineProperty(exports, "getFieldsWithPrivacy", { enumerable: true, get: function () { return schema_1.getFieldsWithPrivacy; } });
18
+ Object.defineProperty(exports, "getStorageKey", { enumerable: true, get: function () { return schema_1.getStorageKey; } });
18
19
  Object.defineProperty(exports, "ActionOperation", { enumerable: true, get: function () { return schema_1.ActionOperation; } });
19
20
  Object.defineProperty(exports, "NoFields", { enumerable: true, get: function () { return schema_1.NoFields; } });
20
21
  Object.defineProperty(exports, "ConstraintType", { enumerable: true, get: function () { return schema_1.ConstraintType; } });
@@ -142,6 +142,7 @@ class SimpleBuilder {
142
142
  ent: schema.ent,
143
143
  tableName: tableName,
144
144
  fields: [],
145
+ fieldPrivacy: (0, schema_1.getFieldsWithPrivacy)(schema),
145
146
  },
146
147
  builder: this,
147
148
  action: action,
@@ -1,8 +1,8 @@
1
1
  import { Context, Viewer } from "../../core/base";
2
2
  import { ContextCache } from "../../core/context";
3
- import { LoggedOutViewer } from "../../core/viewer";
4
3
  export declare class TestContext implements Context {
4
+ constructor(viewer?: Viewer);
5
5
  cache: ContextCache;
6
- viewer: LoggedOutViewer;
6
+ viewer: Viewer;
7
7
  getViewer(): Viewer;
8
8
  }
@@ -4,9 +4,15 @@ exports.TestContext = void 0;
4
4
  const context_1 = require("../../core/context");
5
5
  const viewer_1 = require("../../core/viewer");
6
6
  class TestContext {
7
- constructor() {
7
+ constructor(viewer) {
8
8
  this.cache = new context_1.ContextCache();
9
9
  this.viewer = new viewer_1.LoggedOutViewer(this);
10
+ if (viewer) {
11
+ this.viewer = viewer;
12
+ if (viewer.setContext !== undefined) {
13
+ viewer.setContext(this);
14
+ }
15
+ }
10
16
  }
11
17
  getViewer() {
12
18
  return this.viewer;