@snowtop/ent 0.1.0-alpha65 → 0.1.0-alpha73

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.
package/core/clause.d.ts CHANGED
@@ -99,4 +99,8 @@ export declare function TsVectorPlainToTsQuery(col: string, val: string | TsQuer
99
99
  export declare function TsVectorPhraseToTsQuery(col: string, val: string | TsQuery): Clause;
100
100
  export declare function TsVectorWebsearchToTsQuery(col: string, val: string | TsQuery): Clause;
101
101
  export declare function sensitiveValue(val: any): SensitiveValue;
102
+ export declare function JSONObjectFieldKeyASJSON(col: string, field: string): string;
103
+ export declare function JSONObjectFieldKeyAsText(col: string, field: string): string;
104
+ declare type predicate = "==" | ">" | "<" | "!=" | ">=" | "<=";
105
+ export declare function JSONPathValuePredicate(dbCol: string, path: string, val: any, pred: predicate): Clause;
102
106
  export {};
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.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 = 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.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 = void 0;
23
23
  const db_1 = __importStar(require("./db"));
24
24
  function isSensitive(val) {
25
25
  return (val !== null &&
@@ -559,3 +559,55 @@ function sensitiveValue(val) {
559
559
  };
560
560
  }
561
561
  exports.sensitiveValue = sensitiveValue;
562
+ // These don't return Clauses but return helpful things that can be passed to clauses
563
+ // https://www.postgresql.org/docs/12/functions-json.html#FUNCTIONS-JSON-OP-TABLE
564
+ // see test in db_clause.test.ts
565
+ // unclear best time to use this...
566
+ function JSONObjectFieldKeyASJSON(col, field) {
567
+ return `${col}->'${field}'`;
568
+ }
569
+ exports.JSONObjectFieldKeyASJSON = JSONObjectFieldKeyASJSON;
570
+ function JSONObjectFieldKeyAsText(col, field) {
571
+ return `${col}->>'${field}'`;
572
+ }
573
+ exports.JSONObjectFieldKeyAsText = JSONObjectFieldKeyAsText;
574
+ class jSONPathValuePredicateClause {
575
+ constructor(col, path, value, pred) {
576
+ this.col = col;
577
+ this.path = path;
578
+ this.value = value;
579
+ this.pred = pred;
580
+ }
581
+ clause(idx) {
582
+ if (db_1.default.getDialect() !== db_1.Dialect.Postgres) {
583
+ throw new Error(`not supported`);
584
+ }
585
+ return `${this.col} @@ $${idx}`;
586
+ }
587
+ columns() {
588
+ return [this.col];
589
+ }
590
+ wrap(val) {
591
+ return `${this.path} ${this.pred} ${JSON.stringify(val)}`;
592
+ }
593
+ values() {
594
+ if (isSensitive(this.value)) {
595
+ return [this.wrap(this.value.value())];
596
+ }
597
+ return [this.wrap(this.value)];
598
+ }
599
+ logValues() {
600
+ if (isSensitive(this.value)) {
601
+ return [this.wrap(this.value.logValue())];
602
+ }
603
+ return [this.wrap(this.value)];
604
+ }
605
+ instanceKey() {
606
+ return `${this.col}${this.path}${rawValue(this.value)}${this.pred}`;
607
+ }
608
+ }
609
+ // https://www.postgresql.org/docs/12/functions-json.html#FUNCTIONS-JSON-OP-TABLE
610
+ function JSONPathValuePredicate(dbCol, path, val, pred) {
611
+ return new jSONPathValuePredicateClause(dbCol, path, val, pred);
612
+ }
613
+ exports.JSONPathValuePredicate = JSONPathValuePredicate;
package/core/ent.js CHANGED
@@ -254,9 +254,10 @@ function isParameterizedQuery(opts) {
254
254
  * }) // doesn't change the query
255
255
  */
256
256
  async function loadCustomData(options, query, context) {
257
- //loaderFactory not here...
258
- // options.
259
257
  function getClause(cls) {
258
+ if (options.clause && options.loaderFactory?.options?.clause) {
259
+ throw new Error(`cannot pass both options.clause && optsions.loaderFactory.options.clause`);
260
+ }
260
261
  let optClause = options.clause || options.loaderFactory?.options?.clause;
261
262
  if (typeof optClause === "function") {
262
263
  optClause = optClause();
package/core/privacy.js CHANGED
@@ -451,11 +451,8 @@ async function applyPrivacyPolicy(v, policy, ent) {
451
451
  exports.applyPrivacyPolicy = applyPrivacyPolicy;
452
452
  // this will throw an exception if fails or return error | null?
453
453
  async function applyPrivacyPolicyX(v, policy, ent, throwErr) {
454
- // right now we apply all at same time. todo: be smart about this in the future
455
- const results = await Promise.all(policy.rules.map((rule) => rule.apply(v, ent)));
456
- for (let i = 0; i < results.length; i++) {
457
- const res = results[i];
458
- const rule = policy.rules[i];
454
+ for (const rule of policy.rules) {
455
+ const res = await rule.apply(v, ent);
459
456
  if (res.result == privacyResult.Allow) {
460
457
  return true;
461
458
  }
package/index.d.ts CHANGED
@@ -26,6 +26,9 @@ declare const query: {
26
26
  PostgresArrayNotContains: typeof q.PostgresArrayNotContains;
27
27
  PostgresArrayOverlaps: typeof q.PostgresArrayOverlaps;
28
28
  PostgresArrayNotOverlaps: typeof q.PostgresArrayNotOverlaps;
29
+ JSONPathValuePredicate: typeof q.JSONPathValuePredicate;
30
+ JSONObjectFieldKeyASJSON: typeof q.JSONObjectFieldKeyASJSON;
31
+ JSONObjectFieldKeyAsText: typeof q.JSONObjectFieldKeyAsText;
29
32
  TsQuery: typeof q.TsQuery;
30
33
  PlainToTsQuery: typeof q.PlainToTsQuery;
31
34
  PhraseToTsQuery: typeof q.PhraseToTsQuery;
package/index.js CHANGED
@@ -123,6 +123,9 @@ const query = {
123
123
  PostgresArrayNotContains: q.PostgresArrayNotContains,
124
124
  PostgresArrayOverlaps: q.PostgresArrayOverlaps,
125
125
  PostgresArrayNotOverlaps: q.PostgresArrayNotOverlaps,
126
+ JSONPathValuePredicate: q.JSONPathValuePredicate,
127
+ JSONObjectFieldKeyASJSON: q.JSONObjectFieldKeyASJSON,
128
+ JSONObjectFieldKeyAsText: q.JSONObjectFieldKeyAsText,
126
129
  TsQuery: q.TsQuery,
127
130
  PlainToTsQuery: q.PlainToTsQuery,
128
131
  PhraseToTsQuery: q.PhraseToTsQuery,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@snowtop/ent",
3
- "version": "0.1.0-alpha65",
3
+ "version": "0.1.0-alpha73",
4
4
  "description": "snowtop ent framework",
5
5
  "main": "index.js",
6
6
  "types": "index.d.ts",
@@ -133,6 +133,10 @@ export interface ImportType {
133
133
  type: string;
134
134
  [x: string]: any;
135
135
  }
136
+ export interface ConvertType {
137
+ path: string;
138
+ function: string;
139
+ }
136
140
  declare type EnumMap = {
137
141
  [key: string]: string;
138
142
  };
@@ -200,6 +204,7 @@ export interface FieldOptions {
200
204
  polymorphic?: boolean | PolymorphicOptions;
201
205
  privacyPolicy?: PrivacyPolicy | (() => PrivacyPolicy);
202
206
  getDerivedFields?(name: string): FieldMap;
207
+ convert?: ConvertType;
203
208
  [x: string]: any;
204
209
  }
205
210
  export interface PolymorphicOptions {