@snowtop/ent 0.1.0-alpha88 → 0.1.0-alpha89

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.
@@ -46,7 +46,7 @@ export interface Observer<TEnt extends Ent<TViewer>, TBuilder extends Builder<TE
46
46
  observe(builder: TBuilder, input: TInput): void | Promise<void>;
47
47
  }
48
48
  export interface Validator<TEnt extends Ent<TViewer>, TBuilder extends Builder<TEnt, TViewer, TExistingEnt>, TViewer extends Viewer = Viewer, TInput extends Data = Data, TExistingEnt extends TMaybleNullableEnt<TEnt> = MaybeNull<TEnt>> {
49
- validate(builder: TBuilder, input: TInput): Promise<void> | void;
49
+ validate(builder: TBuilder, input: TInput): Promise<void | undefined | Error> | void | Error | undefined;
50
50
  }
51
51
  export interface Action<TEnt extends Ent<TViewer>, TBuilder extends Builder<TEnt, TViewer, TExistingEnt>, TViewer extends Viewer = Viewer, TInput extends Data = Data, TExistingEnt extends TMaybleNullableEnt<TEnt> = MaybeNull<TEnt>> {
52
52
  readonly viewer: Viewer;
@@ -368,13 +368,14 @@ class Orchestrator {
368
368
  // not ideal we're calling this twice. fix...
369
369
  // needed for now. may need to rewrite some of this?
370
370
  const editedFields2 = await this.options.editedFields();
371
- const [errors, _] = await Promise.all([
371
+ const [errors, errs2] = await Promise.all([
372
372
  this.formatAndValidateFields(schemaFields, editedFields2),
373
373
  this.validators(validators, action, builder),
374
374
  ]);
375
375
  if (privacyError !== null) {
376
376
  errors.unshift(privacyError);
377
377
  }
378
+ errors.push(...errs2);
378
379
  return errors;
379
380
  }
380
381
  async triggers(action, builder, triggers) {
@@ -420,16 +421,19 @@ class Orchestrator {
420
421
  }
421
422
  }
422
423
  async validators(validators, action, builder) {
423
- // TODO need to catch errors and return it...
424
- // don't need it initially since what we need this for doesn't have the errors
425
- let promises = [];
426
- validators.forEach((validator) => {
427
- let res = validator.validate(builder, action.getInput());
428
- if (res) {
429
- promises.push(res);
430
- }
431
- });
432
- await Promise.all(promises);
424
+ const errors = [];
425
+ await Promise.all(validators.map(async (v) => {
426
+ try {
427
+ const r = await v.validate(builder, action.getInput());
428
+ if (r instanceof Error) {
429
+ errors.push(r);
430
+ }
431
+ }
432
+ catch (err) {
433
+ errors.push(err);
434
+ }
435
+ }));
436
+ return errors;
433
437
  }
434
438
  isBuilder(val) {
435
439
  return val.placeholderID !== undefined;
package/core/clause.d.ts CHANGED
@@ -4,6 +4,7 @@ export interface Clause {
4
4
  values(): any[];
5
5
  instanceKey(): string;
6
6
  logValues(): any[];
7
+ compositeOp?: string;
7
8
  }
8
9
  export interface SensitiveValue {
9
10
  value(): any;
@@ -37,6 +38,7 @@ export declare class inClause implements Clause {
37
38
  declare class compositeClause implements Clause {
38
39
  private clauses;
39
40
  private sep;
41
+ compositeOp: string;
40
42
  constructor(clauses: Clause[], sep: string);
41
43
  clause(idx: number): string;
42
44
  columns(): string[];
@@ -97,6 +99,7 @@ export declare function LessEq(col: string, value: any): simpleClause;
97
99
  export declare function And(...args: Clause[]): compositeClause;
98
100
  export declare function AndOptional(...args: (Clause | undefined)[]): Clause;
99
101
  export declare function Or(...args: Clause[]): compositeClause;
102
+ export declare function OrOptional(...args: (Clause | undefined)[]): Clause;
100
103
  export declare function In(col: string, ...values: any): Clause;
101
104
  export declare function In(col: string, values: any[], type?: string): Clause;
102
105
  interface TsQuery {
package/core/clause.js CHANGED
@@ -19,7 +19,7 @@ var __importStar = (this && this.__importStar) || function (mod) {
19
19
  return result;
20
20
  };
21
21
  Object.defineProperty(exports, "__esModule", { value: true });
22
- exports.JSONPathValuePredicate = exports.JSONObjectFieldKeyAsText = exports.JSONObjectFieldKeyASJSON = exports.sensitiveValue = exports.TsVectorWebsearchToTsQuery = exports.TsVectorPhraseToTsQuery = exports.TsVectorPlainToTsQuery = exports.TsVectorColTsQuery = exports.WebsearchToTsQuery = exports.PhraseToTsQuery = exports.PlainToTsQuery = exports.TsQuery = exports.In = exports.Or = exports.AndOptional = exports.And = exports.LessEq = exports.GreaterEq = exports.Less = exports.Greater = exports.NotEq = exports.Eq = exports.ArrayNotEq = exports.ArrayEq = exports.PostgresArrayNotOverlaps = exports.PostgresArrayOverlaps = exports.PostgresArrayNotContains = exports.PostgresArrayNotContainsValue = exports.PostgresArrayContains = exports.PostgresArrayContainsValue = exports.inClause = void 0;
22
+ exports.JSONPathValuePredicate = exports.JSONObjectFieldKeyAsText = exports.JSONObjectFieldKeyASJSON = exports.sensitiveValue = exports.TsVectorWebsearchToTsQuery = exports.TsVectorPhraseToTsQuery = exports.TsVectorPlainToTsQuery = exports.TsVectorColTsQuery = exports.WebsearchToTsQuery = exports.PhraseToTsQuery = exports.PlainToTsQuery = exports.TsQuery = exports.In = exports.OrOptional = exports.Or = exports.AndOptional = exports.And = exports.LessEq = exports.GreaterEq = exports.Less = exports.Greater = exports.NotEq = exports.Eq = exports.ArrayNotEq = exports.ArrayEq = exports.PostgresArrayNotOverlaps = exports.PostgresArrayOverlaps = exports.PostgresArrayNotContains = exports.PostgresArrayNotContainsValue = exports.PostgresArrayContains = exports.PostgresArrayContainsValue = exports.inClause = void 0;
23
23
  const db_1 = __importStar(require("./db"));
24
24
  function isSensitive(val) {
25
25
  return (val !== null &&
@@ -59,9 +59,9 @@ class simpleClause {
59
59
  return [this.col];
60
60
  }
61
61
  values() {
62
- const sqliteClause = this.nullClause();
63
- if (sqliteClause) {
64
- return sqliteClause.values();
62
+ const nullClause = this.nullClause();
63
+ if (nullClause) {
64
+ return nullClause.values();
65
65
  }
66
66
  if (isSensitive(this.value)) {
67
67
  return [this.value.value()];
@@ -69,9 +69,9 @@ class simpleClause {
69
69
  return [this.value];
70
70
  }
71
71
  logValues() {
72
- const sqliteClause = this.nullClause();
73
- if (sqliteClause) {
74
- return sqliteClause.logValues();
72
+ const nullClause = this.nullClause();
73
+ if (nullClause) {
74
+ return nullClause.logValues();
75
75
  }
76
76
  if (isSensitive(this.value)) {
77
77
  return [this.value.logValue()];
@@ -79,9 +79,9 @@ class simpleClause {
79
79
  return [this.value];
80
80
  }
81
81
  instanceKey() {
82
- const sqliteClause = this.nullClause();
83
- if (sqliteClause) {
84
- return sqliteClause.instanceKey();
82
+ const nullClause = this.nullClause();
83
+ if (nullClause) {
84
+ return nullClause.instanceKey();
85
85
  }
86
86
  return `${this.col}${this.op}${rawValue(this.value)}`;
87
87
  }
@@ -300,11 +300,17 @@ class compositeClause {
300
300
  constructor(clauses, sep) {
301
301
  this.clauses = clauses;
302
302
  this.sep = sep;
303
+ this.compositeOp = this.sep;
303
304
  }
304
305
  clause(idx) {
305
306
  let clauses = [];
306
307
  for (const clause of this.clauses) {
307
- clauses.push(clause.clause(idx));
308
+ let cls = clause.clause(idx);
309
+ // if composite clause and a different op, add parens so that we enforce order of precedence
310
+ if (clause.compositeOp && clause.compositeOp !== this.sep) {
311
+ cls = `(${cls})`;
312
+ }
313
+ clauses.push(cls);
308
314
  idx = idx + clause.values().length;
309
315
  }
310
316
  return clauses.join(this.sep);
@@ -332,7 +338,14 @@ class compositeClause {
332
338
  }
333
339
  instanceKey() {
334
340
  let keys = [];
335
- this.clauses.forEach((clause) => keys.push(clause.instanceKey()));
341
+ this.clauses.forEach((clause) => {
342
+ if (clause.compositeOp && clause.compositeOp != this.sep) {
343
+ keys.push(`(${clause.instanceKey()})`);
344
+ }
345
+ else {
346
+ keys.push(clause.instanceKey());
347
+ }
348
+ });
336
349
  return keys.join(this.sep);
337
350
  }
338
351
  }
@@ -513,6 +526,15 @@ function Or(...args) {
513
526
  return new compositeClause(args, " OR ");
514
527
  }
515
528
  exports.Or = Or;
529
+ function OrOptional(...args) {
530
+ // @ts-ignore
531
+ let filtered = args.filter((v) => v !== undefined);
532
+ if (filtered.length === 1) {
533
+ return filtered[0];
534
+ }
535
+ return Or(...filtered);
536
+ }
537
+ exports.OrOptional = OrOptional;
516
538
  function In(...args) {
517
539
  if (args.length < 2) {
518
540
  throw new Error(`invalid args passed to In`);
package/core/ent.js CHANGED
@@ -196,6 +196,11 @@ function getEntKey(viewer, id, options) {
196
196
  }
197
197
  exports.getEntKey = getEntKey;
198
198
  async function loadEnt(viewer, id, options) {
199
+ if (typeof id !== "string" &&
200
+ typeof id !== "number" &&
201
+ typeof id !== "bigint") {
202
+ throw new Error(`invalid id ${id} passed to loadEnt`);
203
+ }
199
204
  const r = await getEntLoader(viewer, options).load(id);
200
205
  return r instanceof ErrorWrapper ? null : r;
201
206
  }
@@ -239,6 +244,11 @@ async function loadEntViaKey(viewer, key, options) {
239
244
  }
240
245
  exports.loadEntViaKey = loadEntViaKey;
241
246
  async function loadEntX(viewer, id, options) {
247
+ if (typeof id !== "string" &&
248
+ typeof id !== "number" &&
249
+ typeof id !== "bigint") {
250
+ throw new Error(`invalid id ${id} passed to loadEntX`);
251
+ }
242
252
  const r = await getEntLoader(viewer, options).load(id);
243
253
  if (r instanceof ErrorWrapper) {
244
254
  throw r.error;
package/index.d.ts CHANGED
@@ -13,6 +13,7 @@ declare const query: {
13
13
  And: typeof q.And;
14
14
  AndOptional: typeof q.AndOptional;
15
15
  Or: typeof q.Or;
16
+ OrOptional: typeof q.OrOptional;
16
17
  In: typeof q.In;
17
18
  Greater: typeof q.Greater;
18
19
  Less: typeof q.Less;
package/index.js CHANGED
@@ -112,6 +112,7 @@ const query = {
112
112
  And: q.And,
113
113
  AndOptional: q.AndOptional,
114
114
  Or: q.Or,
115
+ OrOptional: q.OrOptional,
115
116
  In: q.In,
116
117
  Greater: q.Greater,
117
118
  Less: q.Less,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@snowtop/ent",
3
- "version": "0.1.0-alpha88",
3
+ "version": "0.1.0-alpha89",
4
4
  "description": "snowtop ent framework",
5
5
  "main": "index.js",
6
6
  "types": "index.d.ts",
@@ -68,7 +68,8 @@ export interface AssocEdgeGroup {
68
68
  tableName?: string;
69
69
  assocEdges: AssocEdge[];
70
70
  statusEnums?: string[];
71
- nullStates: string | string[];
71
+ viewerBased?: boolean;
72
+ nullStates?: string | string[];
72
73
  nullStateFn?: string;
73
74
  edgeAction?: EdgeGroupAction;
74
75
  }
@@ -210,6 +211,7 @@ export interface FieldOptions {
210
211
  [x: string]: any;
211
212
  }
212
213
  export interface PolymorphicOptions {
214
+ name?: string;
213
215
  types?: string[];
214
216
  hideFromInverseGraphQL?: boolean;
215
217
  disableBuilderType?: boolean;