@snowtop/ent 0.1.0-alpha1 → 0.1.0-alpha100

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.
Files changed (172) hide show
  1. package/action/action.d.ts +38 -30
  2. package/action/action.js +22 -7
  3. package/action/executor.d.ts +4 -4
  4. package/action/executor.js +8 -3
  5. package/action/experimental_action.d.ts +32 -22
  6. package/action/experimental_action.js +35 -9
  7. package/action/index.d.ts +2 -0
  8. package/action/index.js +7 -1
  9. package/action/orchestrator.d.ts +48 -16
  10. package/action/orchestrator.js +343 -81
  11. package/action/privacy.d.ts +2 -2
  12. package/action/relative_value.d.ts +47 -0
  13. package/action/relative_value.js +125 -0
  14. package/action/transaction.d.ts +10 -0
  15. package/action/transaction.js +23 -0
  16. package/auth/auth.d.ts +1 -1
  17. package/core/base.d.ts +54 -27
  18. package/core/base.js +23 -1
  19. package/core/clause.d.ts +105 -3
  20. package/core/clause.js +563 -30
  21. package/core/config.d.ts +30 -1
  22. package/core/config.js +24 -1
  23. package/core/context.d.ts +5 -3
  24. package/core/context.js +20 -2
  25. package/core/convert.d.ts +1 -1
  26. package/core/date.js +1 -5
  27. package/core/db.d.ts +14 -11
  28. package/core/db.js +22 -8
  29. package/core/ent.d.ts +82 -28
  30. package/core/ent.js +692 -202
  31. package/core/loaders/assoc_count_loader.d.ts +3 -2
  32. package/core/loaders/assoc_count_loader.js +10 -2
  33. package/core/loaders/assoc_edge_loader.d.ts +3 -3
  34. package/core/loaders/assoc_edge_loader.js +13 -15
  35. package/core/loaders/index.d.ts +1 -1
  36. package/core/loaders/index.js +1 -3
  37. package/core/loaders/index_loader.d.ts +2 -2
  38. package/core/loaders/index_loader.js +1 -0
  39. package/core/loaders/loader.js +5 -5
  40. package/core/loaders/object_loader.d.ts +13 -7
  41. package/core/loaders/object_loader.js +95 -32
  42. package/core/loaders/query_loader.d.ts +6 -12
  43. package/core/loaders/query_loader.js +52 -11
  44. package/core/loaders/raw_count_loader.d.ts +2 -2
  45. package/core/loaders/raw_count_loader.js +5 -1
  46. package/core/logger.d.ts +1 -1
  47. package/core/logger.js +1 -0
  48. package/core/privacy.d.ts +26 -25
  49. package/core/privacy.js +23 -24
  50. package/core/query/assoc_query.d.ts +7 -6
  51. package/core/query/assoc_query.js +9 -1
  52. package/core/query/custom_clause_query.d.ts +26 -0
  53. package/core/query/custom_clause_query.js +78 -0
  54. package/core/query/custom_query.d.ts +20 -5
  55. package/core/query/custom_query.js +87 -12
  56. package/core/query/index.d.ts +1 -0
  57. package/core/query/index.js +3 -1
  58. package/core/query/query.d.ts +8 -4
  59. package/core/query/query.js +101 -53
  60. package/core/query/shared_assoc_test.d.ts +2 -1
  61. package/core/query/shared_assoc_test.js +35 -45
  62. package/core/query/shared_test.d.ts +8 -1
  63. package/core/query/shared_test.js +469 -236
  64. package/core/viewer.d.ts +4 -3
  65. package/core/viewer.js +5 -1
  66. package/graphql/builtins/connection.js +3 -3
  67. package/graphql/builtins/edge.js +2 -2
  68. package/graphql/builtins/node.js +1 -1
  69. package/graphql/graphql.d.ts +17 -9
  70. package/graphql/graphql.js +47 -30
  71. package/graphql/index.d.ts +1 -1
  72. package/graphql/index.js +3 -4
  73. package/graphql/mutations/union.d.ts +2 -0
  74. package/graphql/mutations/union.js +35 -0
  75. package/graphql/node_resolver.d.ts +0 -1
  76. package/graphql/query/connection_type.d.ts +9 -9
  77. package/graphql/query/connection_type.js +6 -6
  78. package/graphql/query/edge_connection.d.ts +9 -9
  79. package/graphql/query/page_info.d.ts +1 -1
  80. package/graphql/query/page_info.js +4 -4
  81. package/graphql/query/shared_assoc_test.js +3 -3
  82. package/graphql/query/shared_edge_connection.js +1 -19
  83. package/graphql/scalars/time.d.ts +1 -1
  84. package/imports/index.d.ts +6 -1
  85. package/imports/index.js +19 -4
  86. package/index.d.ts +23 -1
  87. package/index.js +32 -6
  88. package/package.json +18 -17
  89. package/parse_schema/parse.d.ts +45 -8
  90. package/parse_schema/parse.js +193 -15
  91. package/schema/base_schema.d.ts +38 -1
  92. package/schema/base_schema.js +53 -2
  93. package/schema/field.d.ts +75 -21
  94. package/schema/field.js +185 -72
  95. package/schema/index.d.ts +4 -2
  96. package/schema/index.js +15 -2
  97. package/schema/json_field.d.ts +13 -1
  98. package/schema/json_field.js +28 -1
  99. package/schema/schema.d.ts +125 -10
  100. package/schema/schema.js +133 -5
  101. package/schema/struct_field.d.ts +27 -0
  102. package/schema/struct_field.js +138 -0
  103. package/schema/union_field.d.ts +23 -0
  104. package/schema/union_field.js +79 -0
  105. package/scripts/custom_compiler.js +10 -6
  106. package/scripts/custom_graphql.js +224 -36
  107. package/scripts/{transform_schema.d.ts → migrate_v0.1.d.ts} +0 -0
  108. package/scripts/migrate_v0.1.js +36 -0
  109. package/scripts/move_types.d.ts +1 -0
  110. package/scripts/move_types.js +117 -0
  111. package/scripts/read_schema.js +35 -6
  112. package/testutils/action/complex_schemas.d.ts +69 -0
  113. package/testutils/action/complex_schemas.js +398 -0
  114. package/testutils/builder.d.ts +52 -49
  115. package/testutils/builder.js +143 -44
  116. package/testutils/context/test_context.d.ts +2 -2
  117. package/testutils/context/test_context.js +7 -1
  118. package/testutils/db/fixture.d.ts +10 -0
  119. package/testutils/db/fixture.js +26 -0
  120. package/testutils/db/{test_db.d.ts → temp_db.d.ts} +26 -9
  121. package/testutils/db/{test_db.js → temp_db.js} +190 -46
  122. package/testutils/db/value.d.ts +7 -0
  123. package/testutils/db/value.js +251 -0
  124. package/testutils/db_mock.d.ts +16 -4
  125. package/testutils/db_mock.js +51 -6
  126. package/testutils/db_time_zone.d.ts +4 -0
  127. package/testutils/db_time_zone.js +41 -0
  128. package/testutils/ent-graphql-tests/index.d.ts +9 -1
  129. package/testutils/ent-graphql-tests/index.js +53 -25
  130. package/testutils/fake_data/const.d.ts +2 -1
  131. package/testutils/fake_data/const.js +3 -0
  132. package/testutils/fake_data/fake_contact.d.ts +10 -10
  133. package/testutils/fake_data/fake_contact.js +23 -21
  134. package/testutils/fake_data/fake_event.d.ts +8 -9
  135. package/testutils/fake_data/fake_event.js +25 -28
  136. package/testutils/fake_data/fake_tag.d.ts +36 -0
  137. package/testutils/fake_data/fake_tag.js +89 -0
  138. package/testutils/fake_data/fake_user.d.ts +10 -11
  139. package/testutils/fake_data/fake_user.js +20 -23
  140. package/testutils/fake_data/index.js +5 -1
  141. package/testutils/fake_data/internal.d.ts +2 -0
  142. package/testutils/fake_data/internal.js +7 -1
  143. package/testutils/fake_data/tag_query.d.ts +13 -0
  144. package/testutils/fake_data/tag_query.js +43 -0
  145. package/testutils/fake_data/test_helpers.d.ts +11 -4
  146. package/testutils/fake_data/test_helpers.js +29 -13
  147. package/testutils/fake_data/user_query.d.ts +13 -6
  148. package/testutils/fake_data/user_query.js +54 -22
  149. package/testutils/fake_log.d.ts +3 -3
  150. package/testutils/parse_sql.d.ts +6 -0
  151. package/testutils/parse_sql.js +16 -2
  152. package/testutils/test_edge_global_schema.d.ts +15 -0
  153. package/testutils/test_edge_global_schema.js +62 -0
  154. package/testutils/write.d.ts +2 -2
  155. package/testutils/write.js +33 -7
  156. package/tsc/ast.d.ts +44 -0
  157. package/tsc/ast.js +277 -0
  158. package/tsc/compilerOptions.d.ts +6 -0
  159. package/tsc/compilerOptions.js +45 -2
  160. package/tsc/move_generated.d.ts +1 -0
  161. package/tsc/move_generated.js +164 -0
  162. package/tsc/transform.d.ts +22 -0
  163. package/tsc/transform.js +181 -0
  164. package/tsc/transform_action.d.ts +22 -0
  165. package/tsc/transform_action.js +183 -0
  166. package/tsc/transform_ent.d.ts +17 -0
  167. package/tsc/transform_ent.js +59 -0
  168. package/tsc/transform_schema.d.ts +27 -0
  169. package/tsc/transform_schema.js +383 -0
  170. package/graphql/enums.d.ts +0 -3
  171. package/graphql/enums.js +0 -25
  172. package/scripts/transform_schema.js +0 -288
@@ -0,0 +1,47 @@
1
+ import { Clause } from "../core/clause";
2
+ export interface RelativeFieldValue<T = BigInt | number> {
3
+ delta: T;
4
+ sqlExpression: (col: string) => Clause;
5
+ eval: (curr: T) => T;
6
+ }
7
+ export interface RelativeNumberValue<T> {
8
+ add?: T;
9
+ subtract?: T;
10
+ divide?: T;
11
+ multiply?: T;
12
+ modulo?: T;
13
+ }
14
+ declare function addNumber(delta: number): RelativeFieldValue<number>;
15
+ declare function addNumber(delta: BigInt): RelativeFieldValue<BigInt>;
16
+ declare function subtractNumber(delta: number): RelativeFieldValue<number>;
17
+ declare function subtractNumber(delta: BigInt): RelativeFieldValue<BigInt>;
18
+ declare function multiplyNumber(delta: number): RelativeFieldValue<number>;
19
+ declare function multiplyNumber(delta: BigInt): RelativeFieldValue<BigInt>;
20
+ declare function divideNumber(delta: number): RelativeFieldValue<number>;
21
+ declare function divideNumber(delta: BigInt): RelativeFieldValue<BigInt>;
22
+ declare function moduloNumber(delta: number): RelativeFieldValue<number>;
23
+ declare function moduloNumber(delta: BigInt): RelativeFieldValue<BigInt>;
24
+ export declare const NumberOps: {
25
+ addNumber: typeof addNumber;
26
+ moduloNumber: typeof moduloNumber;
27
+ divideNumber: typeof divideNumber;
28
+ subtractNumber: typeof subtractNumber;
29
+ multiplyNumber: typeof multiplyNumber;
30
+ };
31
+ export declare function convertRelativeInput(rel: RelativeNumberValue<BigInt>, col: string, existing: BigInt): {
32
+ value: BigInt;
33
+ clause: Clause;
34
+ };
35
+ export declare function convertRelativeInput(rel: RelativeNumberValue<number>, col: string, existing: number): {
36
+ value: number;
37
+ clause: Clause;
38
+ };
39
+ export declare function maybeConvertRelativeInputPlusExpressions(rel: number | RelativeNumberValue<number>, col: string, existing: number, expressions: Map<string, Clause>): number;
40
+ export declare function maybeConvertRelativeInputPlusExpressions(rel: number | RelativeNumberValue<number> | undefined, col: string, existing: number, expressions: Map<string, Clause>): number | undefined;
41
+ export declare function maybeConvertRelativeInputPlusExpressions(rel: number | RelativeNumberValue<number> | null, col: string, existing: number | null, expressions: Map<string, Clause>): number | null;
42
+ export declare function maybeConvertRelativeInputPlusExpressions(rel: number | RelativeNumberValue<number> | null | undefined, col: string, existing: number | null, expressions: Map<string, Clause>): number | undefined | null;
43
+ export declare function maybeConvertRelativeInputPlusExpressions(rel: BigInt | RelativeNumberValue<BigInt>, col: string, existing: BigInt, expressions: Map<string, Clause>): BigInt;
44
+ export declare function maybeConvertRelativeInputPlusExpressions(rel: BigInt | RelativeNumberValue<BigInt> | undefined, col: string, existing: BigInt, expressions: Map<string, Clause>): BigInt | undefined;
45
+ export declare function maybeConvertRelativeInputPlusExpressions(rel: BigInt | RelativeNumberValue<BigInt> | null, col: string, existing: BigInt | null, expressions: Map<string, Clause>): BigInt | null;
46
+ export declare function maybeConvertRelativeInputPlusExpressions(rel: BigInt | RelativeNumberValue<BigInt> | null | undefined, col: string, existing: BigInt | null, expressions: Map<string, Clause>): BigInt | null | undefined;
47
+ export {};
@@ -0,0 +1,125 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.maybeConvertRelativeInputPlusExpressions = exports.convertRelativeInput = exports.NumberOps = void 0;
4
+ const clause_1 = require("../core/clause");
5
+ function addNumber(delta) {
6
+ return {
7
+ delta,
8
+ sqlExpression(col) {
9
+ return (0, clause_1.Add)(col, delta);
10
+ },
11
+ eval(curr) {
12
+ // @ts-expect-error
13
+ return curr + delta;
14
+ },
15
+ };
16
+ }
17
+ function subtractNumber(delta) {
18
+ return {
19
+ delta,
20
+ sqlExpression(col) {
21
+ return (0, clause_1.Subtract)(col, delta);
22
+ },
23
+ eval(curr) {
24
+ // @ts-expect-error
25
+ return curr - delta;
26
+ },
27
+ };
28
+ }
29
+ function multiplyNumber(delta) {
30
+ return {
31
+ delta,
32
+ sqlExpression(col) {
33
+ return (0, clause_1.Multiply)(col, delta);
34
+ },
35
+ eval(curr) {
36
+ // @ts-expect-error
37
+ return curr * delta;
38
+ },
39
+ };
40
+ }
41
+ function divideNumber(delta) {
42
+ return {
43
+ delta,
44
+ sqlExpression(col) {
45
+ return (0, clause_1.Divide)(col, delta);
46
+ },
47
+ eval(curr) {
48
+ // @ts-expect-error
49
+ return curr / delta;
50
+ },
51
+ };
52
+ }
53
+ function moduloNumber(delta) {
54
+ return {
55
+ delta,
56
+ sqlExpression(col) {
57
+ return (0, clause_1.Modulo)(col, delta);
58
+ },
59
+ eval(curr) {
60
+ // @ts-expect-error
61
+ return curr % delta;
62
+ },
63
+ };
64
+ }
65
+ exports.NumberOps = {
66
+ addNumber,
67
+ moduloNumber,
68
+ divideNumber,
69
+ subtractNumber,
70
+ multiplyNumber,
71
+ };
72
+ function convertRelativeInput(rel, col, existing) {
73
+ if (Object.keys(rel).length !== 1) {
74
+ throw new Error(`only 1 key is expected. ${Object.keys(rel).length} given`);
75
+ }
76
+ const ret = (relField) => {
77
+ return {
78
+ value: relField.eval(existing),
79
+ clause: relField.sqlExpression(col),
80
+ };
81
+ };
82
+ if (rel.add !== undefined) {
83
+ // @ts-expect-error
84
+ return ret(addNumber(rel.add));
85
+ }
86
+ if (rel.subtract !== undefined) {
87
+ // @ts-expect-error
88
+ return ret(subtractNumber(rel.subtract));
89
+ }
90
+ if (rel.multiply !== undefined) {
91
+ // @ts-expect-error
92
+ return ret(multiplyNumber(rel.multiply));
93
+ }
94
+ if (rel.divide !== undefined) {
95
+ // @ts-expect-error
96
+ return ret(divideNumber(rel.divide));
97
+ }
98
+ if (rel.modulo !== undefined) {
99
+ // @ts-expect-error
100
+ return ret(moduloNumber(rel.modulo));
101
+ }
102
+ throw new Error(`error in convertRelativeInput. shouldn't have gotten here`);
103
+ }
104
+ exports.convertRelativeInput = convertRelativeInput;
105
+ function maybeConvertRelativeInputPlusExpressions(rel, col, existing, expressions) {
106
+ if (rel === null) {
107
+ return rel;
108
+ }
109
+ if (rel === undefined) {
110
+ return rel;
111
+ }
112
+ if (typeof rel === "bigint" || typeof rel === "number") {
113
+ return rel;
114
+ }
115
+ // // TODO is this the behavior we want? should we coalesce as 0?
116
+ // if (existing === null) {
117
+ // throw new Error(`cannot perform a relative operation on null`);
118
+ // }
119
+ // @ts-ignore
120
+ // shouldn't be failing like it currently is. it thinks rel can be bigint and it shouldn't be???
121
+ const { clause, value } = convertRelativeInput(rel, col, existing);
122
+ expressions.set(col, clause);
123
+ return value;
124
+ }
125
+ exports.maybeConvertRelativeInputPlusExpressions = maybeConvertRelativeInputPlusExpressions;
@@ -0,0 +1,10 @@
1
+ import { Ent, Viewer } from "../core/base";
2
+ import { Action, Builder } from "./action";
3
+ type ActionAny = Action<Ent<Viewer<any, any>>, Builder<Ent<Viewer<any, any>>, Viewer<any, any>, any>, Viewer<any, any>, any, any>;
4
+ export declare class Transaction {
5
+ private viewer;
6
+ private actions;
7
+ constructor(viewer: Viewer, actions: ActionAny[]);
8
+ run(): Promise<void>;
9
+ }
10
+ export {};
@@ -0,0 +1,23 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Transaction = void 0;
4
+ const executor_1 = require("./executor");
5
+ class Transaction {
6
+ constructor(viewer,
7
+ // independent operations
8
+ actions) {
9
+ this.viewer = viewer;
10
+ this.actions = actions;
11
+ }
12
+ async run() {
13
+ const changesets = [];
14
+ await Promise.all(this.actions.map(async (action) => {
15
+ const c = await action.changeset();
16
+ changesets.push(c);
17
+ }));
18
+ const executor = new executor_1.ComplexExecutor(this.viewer, "", // no placeholder, no opers
19
+ [], new Map(), changesets);
20
+ await executor.execute();
21
+ }
22
+ }
23
+ exports.Transaction = Transaction;
package/auth/auth.d.ts CHANGED
@@ -2,7 +2,7 @@
2
2
  import { Viewer } from "../core/base";
3
3
  import { RequestContext } from "../core/context";
4
4
  import { IncomingMessage, ServerResponse } from "http";
5
- export declare type AuthViewer = Viewer | null;
5
+ export type AuthViewer = Viewer | null;
6
6
  export interface AuthHandler {
7
7
  authViewer(ctx: RequestContext): AuthViewer | Promise<AuthViewer>;
8
8
  }
package/core/base.d.ts CHANGED
@@ -5,19 +5,27 @@ export interface Loader<T, V> {
5
5
  loadMany?(keys: T[]): Promise<(V | null)[]>;
6
6
  clearAll(): any;
7
7
  }
8
- export interface LoaderFactory<T, V> {
8
+ export interface LoaderWithLoadMany<T, V> extends Loader<T, V> {
9
+ loadMany(keys: T[]): Promise<V[]>;
10
+ }
11
+ export interface LoaderFactory<K, V> {
9
12
  name: string;
10
- createLoader(context?: Context): Loader<T, V>;
13
+ createLoader(context?: Context): Loader<K, V>;
14
+ }
15
+ interface LoaderFactoryWithLoaderMany<T, V> extends LoaderFactory<T, V> {
16
+ createLoader(context?: Context): LoaderWithLoadMany<T, V>;
11
17
  }
12
18
  export interface ConfigurableLoaderFactory<T, V> extends LoaderFactory<T, V> {
13
19
  createConfigurableLoader(options: EdgeQueryableDataOptions, context?: Context): Loader<T, V>;
14
20
  }
15
- export declare type EdgeQueryableDataOptions = Partial<Pick<QueryableDataOptions, "limit" | "orderby" | "clause">>;
21
+ export type EdgeQueryableDataOptions = Partial<Pick<QueryableDataOptions, "limit" | "orderby" | "clause">>;
16
22
  export interface PrimableLoader<T, V> extends Loader<T, V> {
17
23
  prime(d: Data): void;
24
+ primeAll?(d: Data): void;
18
25
  }
19
26
  interface cache {
20
27
  getLoader<T, V>(name: string, create: () => Loader<T, V>): Loader<T, V>;
28
+ getLoaderWithLoadMany<T, V>(name: string, create: () => LoaderWithLoadMany<T, V>): LoaderWithLoadMany<T, V>;
21
29
  getCachedRows(options: queryOptions): Data[] | null;
22
30
  getCachedRow(options: queryOptions): Data | null;
23
31
  primeCache(options: queryOptions, rows: Data[]): void;
@@ -30,29 +38,30 @@ interface queryOptions {
30
38
  clause: clause.Clause;
31
39
  orderby?: string;
32
40
  }
33
- export interface Context {
34
- getViewer(): Viewer;
41
+ export interface Context<TViewer extends Viewer = Viewer> {
42
+ getViewer(): TViewer;
35
43
  cache?: cache;
36
44
  }
37
- export interface Viewer {
38
- viewerID: ID | null;
39
- viewer: () => Promise<Ent | null>;
45
+ export interface Viewer<TEnt extends any = Ent<any> | null, TID extends any = ID | null> {
46
+ viewerID: TID;
47
+ viewer: () => Promise<TEnt>;
40
48
  instanceKey: () => string;
41
- context?: Context;
49
+ context?: Context<any>;
42
50
  }
43
- export interface Ent {
51
+ export interface Ent<TViewer extends Viewer = Viewer> {
44
52
  id: ID;
45
- viewer: Viewer;
46
- privacyPolicy: PrivacyPolicy<this>;
53
+ viewer: TViewer;
54
+ getPrivacyPolicy(): PrivacyPolicy<this, TViewer>;
47
55
  nodeType: string;
56
+ __setRawDBData<T extends Data = Data>(data: T): any;
48
57
  }
49
58
  export declare type Data = {
50
59
  [key: string]: any;
51
60
  };
52
- export interface EntConstructor<T extends Ent> {
53
- new (viewer: Viewer, data: Data): T;
61
+ export interface EntConstructor<TEnt extends Ent, TViewer extends Viewer = Viewer> {
62
+ new (viewer: TViewer, data: Data): TEnt;
54
63
  }
55
- export declare type ID = string | number;
64
+ export type ID = string | number;
56
65
  export interface DataOptions {
57
66
  tableName: string;
58
67
  context?: Context;
@@ -62,6 +71,7 @@ export interface SelectBaseDataOptions extends DataOptions {
62
71
  }
63
72
  export interface SelectDataOptions extends SelectBaseDataOptions {
64
73
  key: string;
74
+ clause?: clause.Clause | (() => clause.Clause | undefined);
65
75
  }
66
76
  export interface QueryableDataOptions extends SelectBaseDataOptions, QueryDataOptions {
67
77
  }
@@ -71,6 +81,7 @@ export interface QueryDataOptions {
71
81
  orderby?: string;
72
82
  groupby?: string;
73
83
  limit?: number;
84
+ disableTransformations?: boolean;
74
85
  }
75
86
  export interface LoadRowOptions extends QueryableDataOptions {
76
87
  }
@@ -81,16 +92,26 @@ export interface CreateRowOptions extends DataOptions {
81
92
  fieldsToLog?: Data;
82
93
  }
83
94
  export interface EditRowOptions extends CreateRowOptions {
84
- key: string;
95
+ whereClause: clause.Clause;
96
+ expressions?: Map<string, clause.Clause>;
85
97
  }
86
- interface LoadableEntOptions<T extends Ent> {
87
- loaderFactory: LoaderFactory<any, Data | null>;
88
- ent: EntConstructor<T>;
98
+ interface LoadableEntOptions<TEnt extends Ent, TViewer extends Viewer = Viewer> {
99
+ loaderFactory: LoaderFactoryWithOptions;
100
+ ent: EntConstructor<TEnt, TViewer>;
101
+ }
102
+ export interface LoaderFactoryWithOptions extends LoaderFactoryWithLoaderMany<any, Data | null> {
103
+ options?: SelectDataOptions;
89
104
  }
90
- export interface LoadEntOptions<T extends Ent> extends LoadableEntOptions<T>, SelectBaseDataOptions {
105
+ export interface LoadEntOptions<TEnt extends Ent, TViewer extends Viewer = Viewer> extends LoadableEntOptions<TEnt, TViewer>, SelectBaseDataOptions {
106
+ fieldPrivacy?: Map<string, PrivacyPolicy>;
91
107
  }
92
- export interface LoadCustomEntOptions<T extends Ent> extends SelectBaseDataOptions {
93
- ent: EntConstructor<T>;
108
+ export interface SelectCustomDataOptions extends SelectBaseDataOptions {
109
+ loaderFactory: LoaderFactoryWithOptions;
110
+ prime?: boolean;
111
+ }
112
+ export interface LoadCustomEntOptions<TEnt extends Ent, TViewer extends Viewer = Viewer> extends SelectCustomDataOptions {
113
+ ent: EntConstructor<TEnt, TViewer>;
114
+ fieldPrivacy?: Map<string, PrivacyPolicy>;
94
115
  }
95
116
  export interface LoaderInfo {
96
117
  tableName: string;
@@ -108,6 +129,7 @@ declare enum privacyResult {
108
129
  export interface PrivacyResult {
109
130
  result: privacyResult;
110
131
  error?: PrivacyError;
132
+ getError?(policy: PrivacyPolicy, rule: PrivacyPolicyRule, ent?: Ent): PrivacyError;
111
133
  }
112
134
  export interface PrivacyError extends Error {
113
135
  privacyPolicy: PrivacyPolicy<Ent>;
@@ -116,11 +138,16 @@ export interface PrivacyError extends Error {
116
138
  export declare function Allow(): PrivacyResult;
117
139
  export declare function Skip(): PrivacyResult;
118
140
  export declare function Deny(): PrivacyResult;
119
- export declare function DenyWithReason(e: PrivacyError): PrivacyResult;
120
- export interface PrivacyPolicyRule<TEnt extends Ent = Ent> {
121
- apply(v: Viewer, ent?: TEnt): Promise<PrivacyResult>;
141
+ export declare function DenyWithReason(e: PrivacyError | string): PrivacyResult;
142
+ export interface PrivacyPolicyRule<TEnt extends Ent = Ent, TViewer = Viewer> {
143
+ apply(v: TViewer, ent?: TEnt): Promise<PrivacyResult>;
144
+ }
145
+ export interface PrivacyPolicy<TEnt extends Ent = Ent, TViewer = Viewer> {
146
+ rules: PrivacyPolicyRule<TEnt, TViewer>[];
122
147
  }
123
- export interface PrivacyPolicy<TEnt extends Ent = Ent> {
124
- rules: PrivacyPolicyRule<TEnt>[];
148
+ export declare enum WriteOperation {
149
+ Insert = "insert",
150
+ Edit = "edit",
151
+ Delete = "delete"
125
152
  }
126
153
  export {};
package/core/base.js CHANGED
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.DenyWithReason = exports.Deny = exports.Skip = exports.Allow = void 0;
3
+ exports.WriteOperation = exports.DenyWithReason = exports.Deny = exports.Skip = exports.Allow = void 0;
4
4
  // Privacy
5
5
  var privacyResult;
6
6
  (function (privacyResult) {
@@ -30,10 +30,32 @@ function Deny() {
30
30
  return deny;
31
31
  }
32
32
  exports.Deny = Deny;
33
+ class DenyWithReasonError extends Error {
34
+ constructor(privacyPolicy, rule, msg, ent) {
35
+ super(msg);
36
+ this.privacyPolicy = privacyPolicy;
37
+ this.privacyRule = rule;
38
+ this.ent = ent;
39
+ }
40
+ }
33
41
  function DenyWithReason(e) {
42
+ if (typeof e === "string") {
43
+ return {
44
+ result: privacyResult.Deny,
45
+ getError(policy, rule, ent) {
46
+ return new DenyWithReasonError(policy, rule, e, ent);
47
+ },
48
+ };
49
+ }
34
50
  return {
35
51
  result: privacyResult.Deny,
36
52
  error: e,
37
53
  };
38
54
  }
39
55
  exports.DenyWithReason = DenyWithReason;
56
+ var WriteOperation;
57
+ (function (WriteOperation) {
58
+ WriteOperation["Insert"] = "insert";
59
+ WriteOperation["Edit"] = "edit";
60
+ WriteOperation["Delete"] = "delete";
61
+ })(WriteOperation = exports.WriteOperation || (exports.WriteOperation = {}));
package/core/clause.d.ts CHANGED
@@ -1,8 +1,10 @@
1
1
  export interface Clause {
2
2
  clause(idx: number): string;
3
+ columns(): string[];
3
4
  values(): any[];
4
5
  instanceKey(): string;
5
6
  logValues(): any[];
7
+ compositeOp?: string;
6
8
  }
7
9
  export interface SensitiveValue {
8
10
  value(): any;
@@ -12,8 +14,23 @@ declare class simpleClause implements Clause {
12
14
  protected col: string;
13
15
  private value;
14
16
  private op;
15
- constructor(col: string, value: any, op: string);
17
+ private handleNull?;
18
+ constructor(col: string, value: any, op: string, handleNull?: Clause | undefined);
16
19
  clause(idx: number): string;
20
+ private nullClause;
21
+ columns(): string[];
22
+ values(): any[];
23
+ logValues(): any[];
24
+ instanceKey(): string;
25
+ }
26
+ export declare class inClause implements Clause {
27
+ private col;
28
+ private value;
29
+ private type;
30
+ static getPostgresInClauseValuesThreshold(): number;
31
+ constructor(col: string, value: any[], type?: string);
32
+ clause(idx: number): string;
33
+ columns(): string[];
17
34
  values(): any[];
18
35
  logValues(): any[];
19
36
  instanceKey(): string;
@@ -21,20 +38,105 @@ declare class simpleClause implements Clause {
21
38
  declare class compositeClause implements Clause {
22
39
  private clauses;
23
40
  private sep;
41
+ compositeOp: string;
24
42
  constructor(clauses: Clause[], sep: string);
25
43
  clause(idx: number): string;
44
+ columns(): string[];
26
45
  values(): any[];
27
46
  logValues(): any[];
28
47
  instanceKey(): string;
29
48
  }
30
- export declare function Eq(col: string, value: any): simpleClause;
31
- export declare function NotEq(col: string, value: any): simpleClause;
49
+ /**
50
+ * creates a clause to determine if the given value is contained in the array stored in the column in the db
51
+ * only works with postgres gin indexes
52
+ * https://www.postgresql.org/docs/current/indexes-types.html#INDEXES-TYPES-GIN
53
+ */
54
+ export declare function PostgresArrayContainsValue(col: string, value: any): Clause;
55
+ /**
56
+ * creates a clause to determine if every item in the list is stored in the array stored in the column in the db
57
+ * only works with postgres gin indexes
58
+ * https://www.postgresql.org/docs/current/indexes-types.html#INDEXES-TYPES-GIN
59
+ */
60
+ export declare function PostgresArrayContains(col: string, value: any[]): Clause;
61
+ /**
62
+ * creates a clause to determine if the given value is NOT contained in the array stored in the column in the db
63
+ * only works with postgres gin indexes
64
+ * https://www.postgresql.org/docs/current/indexes-types.html#INDEXES-TYPES-GIN
65
+ */
66
+ export declare function PostgresArrayNotContainsValue(col: string, value: any): Clause;
67
+ /**
68
+ * creates a clause to determine if every item in the list is NOT stored in the array stored in the column in the db
69
+ * only works with postgres gin indexes
70
+ * https://www.postgresql.org/docs/current/indexes-types.html#INDEXES-TYPES-GIN
71
+ */
72
+ export declare function PostgresArrayNotContains(col: string, value: any[]): Clause;
73
+ /**
74
+ * creates a clause to determine if the arrays overlap, that is, do they have any elements in common
75
+ * only works with postgres gin indexes
76
+ * https://www.postgresql.org/docs/current/indexes-types.html#INDEXES-TYPES-GIN
77
+ */
78
+ export declare function PostgresArrayOverlaps(col: string, value: any[]): Clause;
79
+ /**
80
+ * creates a clause to determine if the arrays do not overlap, that is, do they have any elements in common
81
+ * only works with postgres gin indexes
82
+ * https://www.postgresql.org/docs/current/indexes-types.html#INDEXES-TYPES-GIN
83
+ */
84
+ export declare function PostgresArrayNotOverlaps(col: string, value: any[]): Clause;
85
+ /**
86
+ * @deprecated use PostgresArrayContainsValue
87
+ */
88
+ export declare function ArrayEq(col: string, value: any): Clause;
89
+ /**
90
+ * @deprecated use PostgresNotArrayContains
91
+ */
92
+ export declare function ArrayNotEq(col: string, value: any): Clause;
93
+ export declare function Eq(col: string, value: any): Clause;
94
+ export declare function NotEq(col: string, value: any): Clause;
32
95
  export declare function Greater(col: string, value: any): simpleClause;
33
96
  export declare function Less(col: string, value: any): simpleClause;
34
97
  export declare function GreaterEq(col: string, value: any): simpleClause;
35
98
  export declare function LessEq(col: string, value: any): simpleClause;
36
99
  export declare function And(...args: Clause[]): compositeClause;
100
+ export declare function AndOptional(...args: (Clause | undefined)[]): Clause;
37
101
  export declare function Or(...args: Clause[]): compositeClause;
102
+ export declare function OrOptional(...args: (Clause | undefined)[]): Clause;
38
103
  export declare function In(col: string, ...values: any): Clause;
104
+ export declare function In(col: string, values: any[], type?: string): Clause;
105
+ interface TsQuery {
106
+ language: "english" | "french" | "german" | "simple";
107
+ value: string;
108
+ }
109
+ export declare function TsQuery(col: string, val: string | TsQuery): Clause;
110
+ export declare function PlainToTsQuery(col: string, val: string | TsQuery): Clause;
111
+ export declare function PhraseToTsQuery(col: string, val: string | TsQuery): Clause;
112
+ export declare function WebsearchToTsQuery(col: string, val: string | TsQuery): Clause;
113
+ export declare function TsVectorColTsQuery(col: string, val: string | TsQuery): Clause;
114
+ export declare function TsVectorPlainToTsQuery(col: string, val: string | TsQuery): Clause;
115
+ export declare function TsVectorPhraseToTsQuery(col: string, val: string | TsQuery): Clause;
116
+ export declare function TsVectorWebsearchToTsQuery(col: string, val: string | TsQuery): Clause;
39
117
  export declare function sensitiveValue(val: any): SensitiveValue;
118
+ export declare function JSONObjectFieldKeyASJSON(col: string, field: string): string;
119
+ export declare function JSONObjectFieldKeyAsText(col: string, field: string): string;
120
+ type predicate = "==" | ">" | "<" | "!=" | ">=" | "<=";
121
+ export declare function JSONPathValuePredicate(dbCol: string, path: string, val: any, pred: predicate): Clause;
122
+ declare class paginationMultipleColumnsSubQueryClause implements Clause {
123
+ private col;
124
+ private op;
125
+ private tableName;
126
+ private uniqueCol;
127
+ private val;
128
+ constructor(col: string, op: string, tableName: string, uniqueCol: string, val: any);
129
+ private buildSimpleQuery;
130
+ clause(idx: number): string;
131
+ columns(): string[];
132
+ values(): any[];
133
+ logValues(): any[];
134
+ instanceKey(): string;
135
+ }
136
+ export declare function PaginationMultipleColsSubQuery(col: string, op: string, tableName: string, uniqueCol: string, val: any): paginationMultipleColumnsSubQueryClause;
137
+ export declare function Add(col: string, value: any): Clause;
138
+ export declare function Subtract(col: string, value: any): Clause;
139
+ export declare function Multiply(col: string, value: any): Clause;
140
+ export declare function Divide(col: string, value: any): Clause;
141
+ export declare function Modulo(col: string, value: any): Clause;
40
142
  export {};