@snowtop/ent 0.1.0-alpha111 → 0.1.0-alpha113

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) {
@@ -0,0 +1,3 @@
1
+ export declare const PACKAGE = "@snowtop/ent";
2
+ export declare const GRAPHQL_PATH = "@snowtop/ent/graphql";
3
+ export declare const SCHEMA_PATH = "@snowtop/ent/schema";
package/core/const.js ADDED
@@ -0,0 +1,6 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.SCHEMA_PATH = exports.GRAPHQL_PATH = exports.PACKAGE = void 0;
4
+ exports.PACKAGE = "@snowtop/ent";
5
+ exports.GRAPHQL_PATH = "@snowtop/ent/graphql";
6
+ exports.SCHEMA_PATH = "@snowtop/ent/schema";
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-alpha113",
4
4
  "description": "snowtop ent framework",
5
5
  "main": "index.js",
6
6
  "types": "index.d.ts",
@@ -1,5 +1,5 @@
1
1
  import { Schema, Field, AssocEdge, AssocEdgeGroup, Action } from "../schema";
2
- import { ActionField, Type, GlobalSchema } from "../schema/schema";
2
+ import { ActionField, Type, GlobalSchema, TransformReadBetaResult } from "../schema/schema";
3
3
  declare enum NullableResult {
4
4
  CONTENTS = "contents",
5
5
  CONTENTS_AND_LIST = "contentsAndList",
@@ -15,6 +15,7 @@ type ProcessedAssocEdge = Omit<AssocEdge, "actionOnlyFields" | "edgeActions"> &
15
15
  interface TransformFlags {
16
16
  transformsSelect?: boolean;
17
17
  transformsDelete?: boolean;
18
+ transformsLoaderCodegen?: TransformReadBetaResult;
18
19
  transformsInsert?: boolean;
19
20
  transformsUpdate?: boolean;
20
21
  }
@@ -2,6 +2,7 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.parseSchema = void 0;
4
4
  const cosmiconfig_1 = require("cosmiconfig");
5
+ const const_1 = require("../core/const");
5
6
  async function processFields(src, patternName) {
6
7
  const ret = [];
7
8
  let m = {};
@@ -158,6 +159,23 @@ async function processPattern(patterns, pattern, processedSchema) {
158
159
  // flag transformsSelect
159
160
  if (pattern.transformRead) {
160
161
  ret.transformsSelect = true;
162
+ if (pattern.transformReadCodegen_BETA) {
163
+ const r = pattern.transformReadCodegen_BETA();
164
+ if (typeof r === "string") {
165
+ ret.transformsLoaderCodegen = {
166
+ code: r,
167
+ imports: [
168
+ {
169
+ importPath: const_1.PACKAGE,
170
+ import: "query",
171
+ },
172
+ ],
173
+ };
174
+ }
175
+ else {
176
+ ret.transformsLoaderCodegen = r;
177
+ }
178
+ }
161
179
  }
162
180
  if (patterns[name] === undefined) {
163
181
  // intentionally processing separately and not passing pattern.name
@@ -258,6 +276,10 @@ async function parseSchema(potentialSchemas, globalSchema) {
258
276
  throw new Error(`can only have one pattern which transforms default querying behavior`);
259
277
  }
260
278
  processedSchema.transformsSelect = true;
279
+ if (ret.transformsLoaderCodegen) {
280
+ processedSchema.transformsLoaderCodegen =
281
+ ret.transformsLoaderCodegen;
282
+ }
261
283
  }
262
284
  if (ret.transformsDelete) {
263
285
  if (processedSchema.transformsDelete) {
@@ -1,8 +1,8 @@
1
- import { FieldOptions, Type, Field, ImportType } from "./schema";
1
+ import { FieldOptions, Type, Field, DeprecatedImportType } from "./schema";
2
2
  import { BaseField, ListField } from "./field";
3
3
  export interface JSONOptions extends FieldOptions {
4
4
  validator?: (val: any) => boolean;
5
- importType?: ImportType;
5
+ importType?: DeprecatedImportType;
6
6
  }
7
7
  interface allJSONOptions extends JSONOptions {
8
8
  jsonAsList?: boolean;
@@ -79,12 +79,23 @@ export interface AssocEdgeGroup {
79
79
  edgeAction?: EdgeGroupAction;
80
80
  }
81
81
  export type Edge = AssocEdge;
82
+ interface ImportPath {
83
+ importPath: string;
84
+ import: string;
85
+ defaultImport?: boolean;
86
+ originalImport?: string;
87
+ }
88
+ export interface TransformReadBetaResult {
89
+ code: string;
90
+ imports: ImportPath[];
91
+ }
82
92
  export interface Pattern {
83
93
  name: string;
84
94
  fields: FieldMap;
85
95
  disableMixin?: boolean;
86
96
  edges?: Edge[];
87
97
  transformRead?: () => Clause;
98
+ transformReadCodegen_BETA?: () => string | TransformReadBetaResult;
88
99
  transformWrite?: <T extends Ent<TViewer>, TViewer extends Viewer = Viewer>(stmt: UpdateOperation<T, TViewer>) => TransformedUpdateOperation<T, TViewer> | null;
89
100
  transformsDelete?: boolean;
90
101
  transformsInsert?: boolean;
@@ -135,7 +146,7 @@ export declare enum DBType {
135
146
  Timetz = "Timetz",
136
147
  List = "List"
137
148
  }
138
- export interface ImportType {
149
+ export interface DeprecatedImportType {
139
150
  path: string;
140
151
  type: string;
141
152
  [x: string]: any;
@@ -160,7 +171,7 @@ export interface Type {
160
171
  intEnumMap?: IntEnumMap;
161
172
  deprecatedIntEnumMap?: IntEnumMap;
162
173
  disableUnknownType?: boolean;
163
- importType?: ImportType;
174
+ importType?: DeprecatedImportType;
164
175
  subFields?: FieldMap;
165
176
  unionFields?: FieldMap;
166
177
  [x: string]: any;
@@ -37,10 +37,11 @@ const readline = __importStar(require("readline"));
37
37
  const imports_1 = require("../imports");
38
38
  const process_1 = require("process");
39
39
  const child_process_1 = require("child_process");
40
+ const const_1 = require("../core/const");
40
41
  // need to use the GQLCapture from the package so that when we call GQLCapture.enable()
41
42
  // we're affecting the local paths as opposed to a different instance
42
43
  // life is hard
43
- const MODULE_PATH = "@snowtop/ent/graphql";
44
+ const MODULE_PATH = const_1.GRAPHQL_PATH;
44
45
  async function readInputs() {
45
46
  return await new Promise((resolve) => {
46
47
  const rl = readline.createInterface({
package/tsc/ast.js CHANGED
@@ -31,6 +31,7 @@ const typescript_1 = __importDefault(require("typescript"));
31
31
  const path = __importStar(require("path"));
32
32
  const js_yaml_1 = require("js-yaml");
33
33
  const fs = __importStar(require("fs"));
34
+ const const_1 = require("../core/const");
34
35
  function getPreText(fileContents, node, sourceFile) {
35
36
  return fileContents.substring(node.getFullStart(), node.getStart(sourceFile));
36
37
  }
@@ -267,7 +268,7 @@ function getCustomInfo() {
267
268
  catch (e) { }
268
269
  return {
269
270
  viewerInfo: {
270
- path: "@snowtop/ent",
271
+ path: const_1.PACKAGE,
271
272
  name: "Viewer",
272
273
  },
273
274
  relativeImports,
@@ -4,6 +4,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
6
  exports.TransformEnt = void 0;
7
+ const const_1 = require("../core/const");
7
8
  const typescript_1 = __importDefault(require("typescript"));
8
9
  const ast_1 = require("./ast");
9
10
  function isPrivacyPolicy(mm) {
@@ -52,7 +53,7 @@ class TransformEnt {
52
53
  return {
53
54
  rawString: classInfo.wrapClassContents(klassContents),
54
55
  traversed,
55
- imports: new Map([["@snowtop/ent", ["PrivacyPolicy"]]]),
56
+ imports: new Map([[const_1.PACKAGE, ["PrivacyPolicy"]]]),
56
57
  };
57
58
  }
58
59
  }
@@ -31,6 +31,7 @@ const typescript_1 = __importDefault(require("typescript"));
31
31
  const fs = __importStar(require("fs"));
32
32
  const path_1 = __importDefault(require("path"));
33
33
  const ast_1 = require("../tsc/ast");
34
+ const const_1 = require("../core/const");
34
35
  function traverseClass(fileContents, sourceFile, node, transformSchema) {
35
36
  const ci = getTransformClassInfo(fileContents, sourceFile, node, transformSchema);
36
37
  if (!ci) {
@@ -307,9 +308,9 @@ function parseFieldElement(element, sourceFile, fileContents, nested) {
307
308
  // find which of these importPaths is being used and use that to replace
308
309
  function findSchemaImportPath(sourceFile) {
309
310
  const paths = {
310
- "@snowtop/ent": true,
311
- "@snowtop/ent/schema": true,
312
- "@snowtop/ent/schema/": true,
311
+ [const_1.PACKAGE]: true,
312
+ [const_1.SCHEMA_PATH]: true,
313
+ [`${const_1.SCHEMA_PATH}/`]: true,
313
314
  };
314
315
  // @ts-ignore
315
316
  const importStatements = sourceFile.statements.filter((stmt) => typescript_1.default.isImportDeclaration(stmt));