@snowtop/ent 0.1.0-alpha16 → 0.1.0-alpha160-test1

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 (176) hide show
  1. package/action/action.d.ts +25 -14
  2. package/action/action.js +22 -7
  3. package/action/executor.d.ts +16 -3
  4. package/action/executor.js +89 -28
  5. package/action/experimental_action.d.ts +25 -16
  6. package/action/experimental_action.js +34 -14
  7. package/action/index.d.ts +4 -1
  8. package/action/index.js +7 -1
  9. package/action/operations.d.ts +126 -0
  10. package/action/operations.js +686 -0
  11. package/action/orchestrator.d.ts +43 -12
  12. package/action/orchestrator.js +461 -101
  13. package/action/relative_value.d.ts +47 -0
  14. package/action/relative_value.js +125 -0
  15. package/action/transaction.d.ts +10 -0
  16. package/action/transaction.js +23 -0
  17. package/auth/auth.d.ts +1 -1
  18. package/core/base.d.ts +56 -23
  19. package/core/base.js +7 -1
  20. package/core/clause.d.ts +103 -39
  21. package/core/clause.js +430 -66
  22. package/core/config.d.ts +13 -3
  23. package/core/config.js +10 -1
  24. package/core/const.d.ts +3 -0
  25. package/core/const.js +6 -0
  26. package/core/context.d.ts +6 -3
  27. package/core/context.js +22 -3
  28. package/core/convert.d.ts +1 -1
  29. package/core/date.js +1 -5
  30. package/core/db.d.ts +12 -8
  31. package/core/db.js +21 -9
  32. package/core/ent.d.ts +99 -95
  33. package/core/ent.js +550 -602
  34. package/core/global_schema.d.ts +7 -0
  35. package/core/global_schema.js +51 -0
  36. package/core/loaders/assoc_count_loader.d.ts +5 -2
  37. package/core/loaders/assoc_count_loader.js +19 -3
  38. package/core/loaders/assoc_edge_loader.d.ts +2 -3
  39. package/core/loaders/assoc_edge_loader.js +23 -17
  40. package/core/loaders/index.d.ts +1 -2
  41. package/core/loaders/index.js +1 -5
  42. package/core/loaders/loader.d.ts +3 -3
  43. package/core/loaders/loader.js +4 -21
  44. package/core/loaders/object_loader.d.ts +30 -9
  45. package/core/loaders/object_loader.js +226 -79
  46. package/core/loaders/query_loader.d.ts +7 -13
  47. package/core/loaders/query_loader.js +60 -24
  48. package/core/loaders/raw_count_loader.d.ts +1 -0
  49. package/core/loaders/raw_count_loader.js +8 -3
  50. package/core/logger.d.ts +1 -1
  51. package/core/logger.js +1 -0
  52. package/core/privacy.d.ts +26 -16
  53. package/core/privacy.js +68 -51
  54. package/core/query/assoc_query.d.ts +3 -2
  55. package/core/query/assoc_query.js +10 -2
  56. package/core/query/custom_clause_query.d.ts +29 -0
  57. package/core/query/custom_clause_query.js +105 -0
  58. package/core/query/custom_query.d.ts +19 -2
  59. package/core/query/custom_query.js +111 -13
  60. package/core/query/index.d.ts +1 -0
  61. package/core/query/index.js +3 -1
  62. package/core/query/query.d.ts +18 -4
  63. package/core/query/query.js +135 -58
  64. package/core/query/shared_assoc_test.d.ts +2 -1
  65. package/core/query/shared_assoc_test.js +186 -55
  66. package/core/query/shared_test.d.ts +9 -2
  67. package/core/query/shared_test.js +529 -236
  68. package/core/query_impl.d.ts +8 -0
  69. package/core/query_impl.js +28 -0
  70. package/core/viewer.d.ts +2 -0
  71. package/core/viewer.js +3 -1
  72. package/graphql/graphql.d.ts +108 -22
  73. package/graphql/graphql.js +183 -137
  74. package/graphql/graphql_field_helpers.d.ts +9 -3
  75. package/graphql/graphql_field_helpers.js +22 -2
  76. package/graphql/index.d.ts +2 -2
  77. package/graphql/index.js +5 -5
  78. package/graphql/query/connection_type.d.ts +9 -9
  79. package/graphql/query/shared_assoc_test.js +1 -1
  80. package/graphql/query/shared_edge_connection.js +1 -19
  81. package/graphql/scalars/orderby_direction.d.ts +2 -0
  82. package/graphql/scalars/orderby_direction.js +15 -0
  83. package/imports/dataz/example1/_auth.js +128 -47
  84. package/imports/dataz/example1/_viewer.js +87 -39
  85. package/imports/index.d.ts +7 -2
  86. package/imports/index.js +20 -5
  87. package/index.d.ts +23 -5
  88. package/index.js +35 -10
  89. package/package.json +19 -19
  90. package/parse_schema/parse.d.ts +33 -9
  91. package/parse_schema/parse.js +182 -33
  92. package/schema/base_schema.d.ts +13 -3
  93. package/schema/base_schema.js +13 -0
  94. package/schema/field.d.ts +78 -21
  95. package/schema/field.js +232 -72
  96. package/schema/index.d.ts +2 -2
  97. package/schema/index.js +7 -2
  98. package/schema/json_field.d.ts +16 -4
  99. package/schema/json_field.js +32 -2
  100. package/schema/schema.d.ts +109 -20
  101. package/schema/schema.js +42 -53
  102. package/schema/struct_field.d.ts +15 -3
  103. package/schema/struct_field.js +117 -22
  104. package/schema/union_field.d.ts +1 -1
  105. package/scripts/custom_compiler.js +12 -8
  106. package/scripts/custom_graphql.js +171 -64
  107. package/scripts/migrate_v0.1.js +36 -0
  108. package/scripts/move_types.js +120 -0
  109. package/scripts/read_schema.js +22 -7
  110. package/testutils/action/complex_schemas.d.ts +69 -0
  111. package/testutils/action/complex_schemas.js +405 -0
  112. package/testutils/builder.d.ts +37 -41
  113. package/testutils/builder.js +66 -46
  114. package/testutils/db/fixture.d.ts +10 -0
  115. package/testutils/db/fixture.js +26 -0
  116. package/testutils/db/{test_db.d.ts → temp_db.d.ts} +32 -8
  117. package/testutils/db/{test_db.js → temp_db.js} +251 -48
  118. package/testutils/db/value.d.ts +7 -0
  119. package/testutils/db/value.js +251 -0
  120. package/testutils/db_mock.d.ts +16 -4
  121. package/testutils/db_mock.js +52 -9
  122. package/testutils/db_time_zone.d.ts +4 -0
  123. package/testutils/db_time_zone.js +41 -0
  124. package/testutils/ent-graphql-tests/index.d.ts +7 -1
  125. package/testutils/ent-graphql-tests/index.js +56 -26
  126. package/testutils/fake_comms.js +1 -1
  127. package/testutils/fake_data/const.d.ts +2 -1
  128. package/testutils/fake_data/const.js +3 -0
  129. package/testutils/fake_data/fake_contact.d.ts +7 -3
  130. package/testutils/fake_data/fake_contact.js +13 -7
  131. package/testutils/fake_data/fake_event.d.ts +4 -1
  132. package/testutils/fake_data/fake_event.js +7 -6
  133. package/testutils/fake_data/fake_tag.d.ts +36 -0
  134. package/testutils/fake_data/fake_tag.js +89 -0
  135. package/testutils/fake_data/fake_user.d.ts +8 -5
  136. package/testutils/fake_data/fake_user.js +31 -19
  137. package/testutils/fake_data/index.js +5 -1
  138. package/testutils/fake_data/internal.d.ts +2 -0
  139. package/testutils/fake_data/internal.js +7 -1
  140. package/testutils/fake_data/tag_query.d.ts +13 -0
  141. package/testutils/fake_data/tag_query.js +48 -0
  142. package/testutils/fake_data/test_helpers.d.ts +14 -6
  143. package/testutils/fake_data/test_helpers.js +31 -15
  144. package/testutils/fake_data/user_query.d.ts +16 -6
  145. package/testutils/fake_data/user_query.js +72 -23
  146. package/testutils/fake_log.js +1 -1
  147. package/testutils/parse_sql.d.ts +6 -0
  148. package/testutils/parse_sql.js +16 -2
  149. package/testutils/test_edge_global_schema.d.ts +15 -0
  150. package/testutils/test_edge_global_schema.js +62 -0
  151. package/testutils/write.d.ts +2 -2
  152. package/testutils/write.js +33 -7
  153. package/tsc/ast.d.ts +15 -3
  154. package/tsc/ast.js +114 -23
  155. package/tsc/compilerOptions.js +5 -1
  156. package/tsc/move_generated.d.ts +1 -0
  157. package/tsc/move_generated.js +164 -0
  158. package/tsc/transform.d.ts +22 -0
  159. package/tsc/transform.js +182 -0
  160. package/tsc/transform_action.d.ts +22 -0
  161. package/tsc/transform_action.js +183 -0
  162. package/tsc/transform_ent.d.ts +17 -0
  163. package/tsc/transform_ent.js +60 -0
  164. package/tsc/transform_schema.d.ts +27 -0
  165. package/{scripts → tsc}/transform_schema.js +146 -117
  166. package/core/loaders/index_loader.d.ts +0 -14
  167. package/core/loaders/index_loader.js +0 -27
  168. package/graphql/enums.d.ts +0 -3
  169. package/graphql/enums.js +0 -25
  170. package/scripts/move_generated.js +0 -141
  171. package/scripts/transform_actions.js +0 -266
  172. package/scripts/transform_code.d.ts +0 -1
  173. package/scripts/transform_code.js +0 -111
  174. package/scripts/transform_schema.d.ts +0 -1
  175. /package/scripts/{move_generated.d.ts → migrate_v0.1.d.ts} +0 -0
  176. /package/scripts/{transform_actions.d.ts → move_types.d.ts} +0 -0
package/core/privacy.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { Ent, ID, LoadEntOptions, PrivacyError, PrivacyPolicy, PrivacyPolicyRule, PrivacyResult, Viewer } from "./base";
1
+ import { Ent, ID, LoadEntOptions, PrivacyError, PrivacyPolicy, PrivacyPolicyRule, PrivacyResult, Viewer, EdgeQueryableDataOptionsConfigureLoader } from "./base";
2
2
  export declare class EntPrivacyError extends Error implements PrivacyError {
3
3
  privacyPolicy: PrivacyPolicy;
4
4
  privacyRule: PrivacyPolicyRule;
@@ -6,22 +6,22 @@ export declare class EntPrivacyError extends Error implements PrivacyError {
6
6
  constructor(privacyPolicy: PrivacyPolicy, rule: PrivacyPolicyRule, ent?: Ent);
7
7
  }
8
8
  export declare const AlwaysAllowRule: {
9
- apply(_v: Viewer, _ent?: Ent<Viewer<Ent<any> | null, ID | null>> | undefined): Promise<PrivacyResult>;
9
+ apply(_v: Viewer, _ent?: Ent): Promise<PrivacyResult>;
10
10
  };
11
11
  export declare const AlwaysDenyRule: {
12
- apply(_v: Viewer, _ent?: Ent<Viewer<Ent<any> | null, ID | null>> | undefined): Promise<PrivacyResult>;
12
+ apply(_v: Viewer, _ent?: Ent): Promise<PrivacyResult>;
13
13
  };
14
14
  export declare const DenyIfLoggedOutRule: {
15
- apply(v: Viewer, _ent?: Ent<Viewer<Ent<any> | null, ID | null>> | undefined): Promise<PrivacyResult>;
15
+ apply(v: Viewer, _ent?: Ent): Promise<PrivacyResult>;
16
16
  };
17
17
  export declare const DenyIfLoggedInRule: {
18
- apply(v: Viewer, _ent?: Ent<Viewer<Ent<any> | null, ID | null>> | undefined): Promise<PrivacyResult>;
18
+ apply(v: Viewer, _ent?: Ent): Promise<PrivacyResult>;
19
19
  };
20
20
  export declare const AllowIfHasIdentity: {
21
- apply(v: Viewer, _ent?: Ent<Viewer<Ent<any> | null, ID | null>> | undefined): Promise<PrivacyResult>;
21
+ apply(v: Viewer, _ent?: Ent): Promise<PrivacyResult>;
22
22
  };
23
23
  export declare const AllowIfViewerRule: {
24
- apply(v: Viewer, ent?: Ent<Viewer<Ent<any> | null, ID | null>> | undefined): Promise<PrivacyResult>;
24
+ apply(v: Viewer, ent?: Ent): Promise<PrivacyResult>;
25
25
  };
26
26
  export declare class AllowIfViewerEqualsRule {
27
27
  private id;
@@ -115,51 +115,60 @@ export declare class AllowIfEdgeExistsRule implements PrivacyPolicyRule {
115
115
  private id1;
116
116
  private id2;
117
117
  private edgeType;
118
- constructor(id1: ID, id2: ID, edgeType: string);
118
+ private options?;
119
+ constructor(id1: ID, id2: ID, edgeType: string, options?: EdgeQueryableDataOptionsConfigureLoader | undefined);
119
120
  apply(v: Viewer, _ent?: Ent): Promise<PrivacyResult>;
120
121
  }
121
122
  export declare class AllowIfViewerInboundEdgeExistsRule implements PrivacyPolicyRule {
122
123
  private edgeType;
123
- constructor(edgeType: string);
124
+ private options?;
125
+ constructor(edgeType: string, options?: EdgeQueryableDataOptionsConfigureLoader | undefined);
124
126
  apply(v: Viewer, ent?: Ent): Promise<PrivacyResult>;
125
127
  }
126
128
  export declare class AllowIfViewerOutboundEdgeExistsRule implements PrivacyPolicyRule {
127
129
  private edgeType;
128
- constructor(edgeType: string);
130
+ private options?;
131
+ constructor(edgeType: string, options?: EdgeQueryableDataOptionsConfigureLoader | undefined);
129
132
  apply(v: Viewer, ent?: Ent): Promise<PrivacyResult>;
130
133
  }
131
134
  export declare class DenyIfEdgeExistsRule implements PrivacyPolicyRule {
132
135
  private id1;
133
136
  private id2;
134
137
  private edgeType;
135
- constructor(id1: ID, id2: ID, edgeType: string);
138
+ private options?;
139
+ constructor(id1: ID, id2: ID, edgeType: string, options?: EdgeQueryableDataOptionsConfigureLoader | undefined);
136
140
  apply(v: Viewer, _ent?: Ent): Promise<PrivacyResult>;
137
141
  }
138
142
  export declare class DenyIfViewerInboundEdgeExistsRule implements PrivacyPolicyRule {
139
143
  private edgeType;
140
- constructor(edgeType: string);
144
+ private options?;
145
+ constructor(edgeType: string, options?: EdgeQueryableDataOptionsConfigureLoader | undefined);
141
146
  apply(v: Viewer, ent?: Ent): Promise<PrivacyResult>;
142
147
  }
143
148
  export declare class DenyIfViewerOutboundEdgeExistsRule implements PrivacyPolicyRule {
144
149
  private edgeType;
145
- constructor(edgeType: string);
150
+ private options?;
151
+ constructor(edgeType: string, options?: EdgeQueryableDataOptionsConfigureLoader | undefined);
146
152
  apply(v: Viewer, ent?: Ent): Promise<PrivacyResult>;
147
153
  }
148
154
  export declare class DenyIfEdgeDoesNotExistRule implements PrivacyPolicyRule {
149
155
  private id1;
150
156
  private id2;
151
157
  private edgeType;
152
- constructor(id1: ID, id2: ID, edgeType: string);
158
+ private options?;
159
+ constructor(id1: ID, id2: ID, edgeType: string, options?: EdgeQueryableDataOptionsConfigureLoader | undefined);
153
160
  apply(v: Viewer, _ent?: Ent): Promise<PrivacyResult>;
154
161
  }
155
162
  export declare class DenyIfViewerInboundEdgeDoesNotExistRule implements PrivacyPolicyRule {
156
163
  private edgeType;
157
- constructor(edgeType: string);
164
+ private options?;
165
+ constructor(edgeType: string, options?: EdgeQueryableDataOptionsConfigureLoader | undefined);
158
166
  apply(v: Viewer, ent?: Ent): Promise<PrivacyResult>;
159
167
  }
160
168
  export declare class DenyIfViewerOutboundEdgeDoesNotExistRule implements PrivacyPolicyRule {
161
169
  private edgeType;
162
- constructor(edgeType: string);
170
+ private options?;
171
+ constructor(edgeType: string, options?: EdgeQueryableDataOptionsConfigureLoader | undefined);
163
172
  apply(v: Viewer, ent?: Ent): Promise<PrivacyResult>;
164
173
  }
165
174
  export declare class AllowIfConditionAppliesRule implements PrivacyPolicyRule {
@@ -183,6 +192,7 @@ export declare class AllowIfSubPolicyAllowsRule implements PrivacyPolicyRule {
183
192
  }
184
193
  export declare function applyPrivacyPolicy(v: Viewer, policy: PrivacyPolicy, ent: Ent | undefined): Promise<boolean>;
185
194
  export declare function applyPrivacyPolicyX(v: Viewer, policy: PrivacyPolicy, ent: Ent | undefined, throwErr?: () => Error): Promise<boolean>;
195
+ export declare function applyPrivacyPolicyImpl(v: Viewer, policy: PrivacyPolicy, ent: Ent | undefined, throwErr?: () => Error): Promise<Error | null>;
186
196
  export declare const AlwaysAllowPrivacyPolicy: PrivacyPolicy;
187
197
  export declare const AlwaysDenyPrivacyPolicy: PrivacyPolicy;
188
198
  export declare const AllowIfViewerPrivacyPolicy: PrivacyPolicy;
package/core/privacy.js CHANGED
@@ -1,9 +1,9 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.AllowIfViewerHasIdentityPrivacyPolicy = exports.AllowIfViewerPrivacyPolicy = exports.AlwaysDenyPrivacyPolicy = exports.AlwaysAllowPrivacyPolicy = exports.applyPrivacyPolicyX = exports.applyPrivacyPolicy = exports.AllowIfSubPolicyAllowsRule = exports.DelayedResultRule = exports.AllowIfConditionAppliesRule = exports.DenyIfViewerOutboundEdgeDoesNotExistRule = exports.DenyIfViewerInboundEdgeDoesNotExistRule = exports.DenyIfEdgeDoesNotExistRule = exports.DenyIfViewerOutboundEdgeExistsRule = exports.DenyIfViewerInboundEdgeExistsRule = exports.DenyIfEdgeExistsRule = exports.AllowIfViewerOutboundEdgeExistsRule = exports.AllowIfViewerInboundEdgeExistsRule = exports.AllowIfEdgeExistsRule = exports.DenyIfEntIsNotVisibleRule = exports.DenyIfEntIsVisibleRule = exports.DenyIfEntIsVisiblePolicy = exports.AllowIfEntIsVisiblePolicy = exports.AllowIfEntIsNotVisibleRule = exports.AllowIfEntIsVisibleRule = exports.DenyIfEntPropertyIsRule = exports.AllowIfEntPropertyIsRule = exports.AllowIfViewerIsEntPropertyRule = exports.AllowIfViewerIsRule = exports.DenyIfFuncRule = exports.AllowIfFuncRule = exports.DenyIfViewerEqualsRule = exports.AllowIfViewerEqualsRule = exports.AllowIfViewerRule = exports.AllowIfHasIdentity = exports.DenyIfLoggedInRule = exports.DenyIfLoggedOutRule = exports.AlwaysDenyRule = exports.AlwaysAllowRule = exports.EntPrivacyError = void 0;
3
+ exports.AllowIfViewerHasIdentityPrivacyPolicy = exports.AllowIfViewerPrivacyPolicy = exports.AlwaysDenyPrivacyPolicy = exports.AlwaysAllowPrivacyPolicy = exports.applyPrivacyPolicyImpl = exports.applyPrivacyPolicyX = exports.applyPrivacyPolicy = exports.AllowIfSubPolicyAllowsRule = exports.DelayedResultRule = exports.AllowIfConditionAppliesRule = exports.DenyIfViewerOutboundEdgeDoesNotExistRule = exports.DenyIfViewerInboundEdgeDoesNotExistRule = exports.DenyIfEdgeDoesNotExistRule = exports.DenyIfViewerOutboundEdgeExistsRule = exports.DenyIfViewerInboundEdgeExistsRule = exports.DenyIfEdgeExistsRule = exports.AllowIfViewerOutboundEdgeExistsRule = exports.AllowIfViewerInboundEdgeExistsRule = exports.AllowIfEdgeExistsRule = exports.DenyIfEntIsNotVisibleRule = exports.DenyIfEntIsVisibleRule = exports.DenyIfEntIsVisiblePolicy = exports.AllowIfEntIsVisiblePolicy = exports.AllowIfEntIsNotVisibleRule = exports.AllowIfEntIsVisibleRule = exports.DenyIfEntPropertyIsRule = exports.AllowIfEntPropertyIsRule = exports.AllowIfViewerIsEntPropertyRule = exports.AllowIfViewerIsRule = exports.DenyIfFuncRule = exports.AllowIfFuncRule = exports.DenyIfViewerEqualsRule = exports.AllowIfViewerEqualsRule = exports.AllowIfViewerRule = exports.AllowIfHasIdentity = exports.DenyIfLoggedInRule = exports.DenyIfLoggedOutRule = exports.AlwaysDenyRule = exports.AlwaysAllowRule = exports.EntPrivacyError = void 0;
4
+ const types_1 = require("util/types");
4
5
  const base_1 = require("./base");
5
6
  const ent_1 = require("./ent");
6
- const logger_1 = require("./logger");
7
7
  // copied from ./base
8
8
  var privacyResult;
9
9
  (function (privacyResult) {
@@ -103,7 +103,10 @@ class AllowIfFuncRule {
103
103
  this.fn = fn;
104
104
  }
105
105
  async apply(v, ent) {
106
- const result = await this.fn(v, ent);
106
+ let result = this.fn(v, ent);
107
+ if ((0, types_1.isPromise)(result)) {
108
+ result = await result;
109
+ }
107
110
  if (result) {
108
111
  return (0, base_1.Allow)();
109
112
  }
@@ -116,7 +119,10 @@ class DenyIfFuncRule {
116
119
  this.fn = fn;
117
120
  }
118
121
  async apply(v, ent) {
119
- const result = await this.fn(v, ent);
122
+ let result = this.fn(v, ent);
123
+ if ((0, types_1.isPromise)(result)) {
124
+ result = await result;
125
+ }
120
126
  if (result) {
121
127
  return (0, base_1.Deny)();
122
128
  }
@@ -256,7 +262,7 @@ class DenyIfEntIsNotVisibleRule {
256
262
  }
257
263
  }
258
264
  exports.DenyIfEntIsNotVisibleRule = DenyIfEntIsNotVisibleRule;
259
- async function allowIfEdgeExistsRule(id1, id2, edgeType, context) {
265
+ async function allowIfEdgeExistsRule(id1, id2, edgeType, context, options) {
260
266
  if (id1 && id2) {
261
267
  const edge = await (0, ent_1.loadEdgeForID2)({
262
268
  id1,
@@ -264,6 +270,7 @@ async function allowIfEdgeExistsRule(id1, id2, edgeType, context) {
264
270
  id2,
265
271
  context,
266
272
  ctr: ent_1.AssocEdge,
273
+ queryOptions: options,
267
274
  });
268
275
  if (edge) {
269
276
  return (0, base_1.Allow)();
@@ -272,35 +279,38 @@ async function allowIfEdgeExistsRule(id1, id2, edgeType, context) {
272
279
  return (0, base_1.Skip)();
273
280
  }
274
281
  class AllowIfEdgeExistsRule {
275
- constructor(id1, id2, edgeType) {
282
+ constructor(id1, id2, edgeType, options) {
276
283
  this.id1 = id1;
277
284
  this.id2 = id2;
278
285
  this.edgeType = edgeType;
286
+ this.options = options;
279
287
  }
280
288
  async apply(v, _ent) {
281
- return allowIfEdgeExistsRule(this.id1, this.id2, this.edgeType, v.context);
289
+ return allowIfEdgeExistsRule(this.id1, this.id2, this.edgeType, v.context, this.options);
282
290
  }
283
291
  }
284
292
  exports.AllowIfEdgeExistsRule = AllowIfEdgeExistsRule;
285
293
  class AllowIfViewerInboundEdgeExistsRule {
286
- constructor(edgeType) {
294
+ constructor(edgeType, options) {
287
295
  this.edgeType = edgeType;
296
+ this.options = options;
288
297
  }
289
298
  async apply(v, ent) {
290
- return allowIfEdgeExistsRule(v.viewerID, ent?.id, this.edgeType, v.context);
299
+ return allowIfEdgeExistsRule(v.viewerID, ent?.id, this.edgeType, v.context, this.options);
291
300
  }
292
301
  }
293
302
  exports.AllowIfViewerInboundEdgeExistsRule = AllowIfViewerInboundEdgeExistsRule;
294
303
  class AllowIfViewerOutboundEdgeExistsRule {
295
- constructor(edgeType) {
304
+ constructor(edgeType, options) {
296
305
  this.edgeType = edgeType;
306
+ this.options = options;
297
307
  }
298
308
  async apply(v, ent) {
299
- return allowIfEdgeExistsRule(ent?.id, v.viewerID, this.edgeType, v.context);
309
+ return allowIfEdgeExistsRule(ent?.id, v.viewerID, this.edgeType, v.context, this.options);
300
310
  }
301
311
  }
302
312
  exports.AllowIfViewerOutboundEdgeExistsRule = AllowIfViewerOutboundEdgeExistsRule;
303
- async function denyIfEdgeExistsRule(id1, id2, edgeType, context) {
313
+ async function denyIfEdgeExistsRule(id1, id2, edgeType, context, options) {
304
314
  // edge doesn't exist if no viewer
305
315
  if (id1 && id2) {
306
316
  const edge = await (0, ent_1.loadEdgeForID2)({
@@ -309,6 +319,7 @@ async function denyIfEdgeExistsRule(id1, id2, edgeType, context) {
309
319
  id2,
310
320
  context,
311
321
  ctr: ent_1.AssocEdge,
322
+ queryOptions: options,
312
323
  });
313
324
  if (edge) {
314
325
  return (0, base_1.Deny)();
@@ -316,7 +327,7 @@ async function denyIfEdgeExistsRule(id1, id2, edgeType, context) {
316
327
  }
317
328
  return (0, base_1.Skip)();
318
329
  }
319
- async function denyIfEdgeDoesNotExistRule(id1, id2, edgeType, context) {
330
+ async function denyIfEdgeDoesNotExistRule(id1, id2, edgeType, context, options) {
320
331
  // edge doesn't exist if no viewer
321
332
  if (!id1 || !id2) {
322
333
  return (0, base_1.Deny)();
@@ -327,6 +338,7 @@ async function denyIfEdgeDoesNotExistRule(id1, id2, edgeType, context) {
327
338
  id2,
328
339
  context,
329
340
  ctr: ent_1.AssocEdge,
341
+ queryOptions: options,
330
342
  });
331
343
  if (!edge) {
332
344
  return (0, base_1.Deny)();
@@ -334,60 +346,66 @@ async function denyIfEdgeDoesNotExistRule(id1, id2, edgeType, context) {
334
346
  return (0, base_1.Skip)();
335
347
  }
336
348
  class DenyIfEdgeExistsRule {
337
- constructor(id1, id2, edgeType) {
349
+ constructor(id1, id2, edgeType, options) {
338
350
  this.id1 = id1;
339
351
  this.id2 = id2;
340
352
  this.edgeType = edgeType;
353
+ this.options = options;
341
354
  }
342
355
  async apply(v, _ent) {
343
- return denyIfEdgeExistsRule(this.id1, this.id2, this.edgeType, v.context);
356
+ return denyIfEdgeExistsRule(this.id1, this.id2, this.edgeType, v.context, this.options);
344
357
  }
345
358
  }
346
359
  exports.DenyIfEdgeExistsRule = DenyIfEdgeExistsRule;
347
360
  class DenyIfViewerInboundEdgeExistsRule {
348
- constructor(edgeType) {
361
+ constructor(edgeType, options) {
349
362
  this.edgeType = edgeType;
363
+ this.options = options;
350
364
  }
351
365
  async apply(v, ent) {
352
- return denyIfEdgeExistsRule(v.viewerID, ent?.id, this.edgeType, v.context);
366
+ return denyIfEdgeExistsRule(v.viewerID, ent?.id, this.edgeType, v.context, this.options);
353
367
  }
354
368
  }
355
369
  exports.DenyIfViewerInboundEdgeExistsRule = DenyIfViewerInboundEdgeExistsRule;
356
370
  class DenyIfViewerOutboundEdgeExistsRule {
357
- constructor(edgeType) {
371
+ constructor(edgeType, options) {
358
372
  this.edgeType = edgeType;
373
+ this.options = options;
359
374
  }
360
375
  async apply(v, ent) {
361
- return denyIfEdgeExistsRule(ent?.id, v.viewerID, this.edgeType, v.context);
376
+ return denyIfEdgeExistsRule(ent?.id, v.viewerID, this.edgeType, v.context, this.options);
362
377
  }
363
378
  }
364
379
  exports.DenyIfViewerOutboundEdgeExistsRule = DenyIfViewerOutboundEdgeExistsRule;
365
380
  class DenyIfEdgeDoesNotExistRule {
366
- constructor(id1, id2, edgeType) {
381
+ constructor(id1, id2, edgeType, options) {
367
382
  this.id1 = id1;
368
383
  this.id2 = id2;
369
384
  this.edgeType = edgeType;
385
+ this.options = options;
370
386
  }
371
387
  async apply(v, _ent) {
372
- return denyIfEdgeDoesNotExistRule(this.id1, this.id2, this.edgeType, v.context);
388
+ return denyIfEdgeDoesNotExistRule(this.id1, this.id2, this.edgeType, v.context, this.options);
373
389
  }
374
390
  }
375
391
  exports.DenyIfEdgeDoesNotExistRule = DenyIfEdgeDoesNotExistRule;
376
392
  class DenyIfViewerInboundEdgeDoesNotExistRule {
377
- constructor(edgeType) {
393
+ constructor(edgeType, options) {
378
394
  this.edgeType = edgeType;
395
+ this.options = options;
379
396
  }
380
397
  async apply(v, ent) {
381
- return denyIfEdgeDoesNotExistRule(v.viewerID, ent?.id, this.edgeType, v.context);
398
+ return denyIfEdgeDoesNotExistRule(v.viewerID, ent?.id, this.edgeType, v.context, this.options);
382
399
  }
383
400
  }
384
401
  exports.DenyIfViewerInboundEdgeDoesNotExistRule = DenyIfViewerInboundEdgeDoesNotExistRule;
385
402
  class DenyIfViewerOutboundEdgeDoesNotExistRule {
386
- constructor(edgeType) {
403
+ constructor(edgeType, options) {
387
404
  this.edgeType = edgeType;
405
+ this.options = options;
388
406
  }
389
407
  async apply(v, ent) {
390
- return denyIfEdgeDoesNotExistRule(ent?.id, v.viewerID, this.edgeType, v.context);
408
+ return denyIfEdgeDoesNotExistRule(ent?.id, v.viewerID, this.edgeType, v.context, this.options);
391
409
  }
392
410
  }
393
411
  exports.DenyIfViewerOutboundEdgeDoesNotExistRule = DenyIfViewerOutboundEdgeDoesNotExistRule;
@@ -413,12 +431,14 @@ class DelayedResultRule {
413
431
  this.fn = fn;
414
432
  }
415
433
  async apply(v, ent) {
416
- const rule = await this.fn(v, ent);
434
+ let rule = this.fn(v, ent);
435
+ if ((0, types_1.isPromise)(rule)) {
436
+ rule = await rule;
437
+ }
417
438
  if (!rule) {
418
439
  return (0, base_1.Skip)();
419
440
  }
420
- const res = await rule.apply(v, ent);
421
- return res;
441
+ return rule.apply(v, ent);
422
442
  }
423
443
  }
424
444
  exports.DelayedResultRule = DelayedResultRule;
@@ -437,45 +457,42 @@ class AllowIfSubPolicyAllowsRule {
437
457
  }
438
458
  exports.AllowIfSubPolicyAllowsRule = AllowIfSubPolicyAllowsRule;
439
459
  async function applyPrivacyPolicy(v, policy, ent) {
440
- try {
441
- return await applyPrivacyPolicyX(v, policy, ent);
442
- }
443
- catch (e) {
444
- // TODO privacy errors should not throw
445
- // but other expected errors should throw...
446
- // we shouldn't just hide them
447
- (0, logger_1.log)("debug", e);
448
- return false;
449
- }
460
+ const err = await applyPrivacyPolicyImpl(v, policy, ent);
461
+ return err === null;
450
462
  }
451
463
  exports.applyPrivacyPolicy = applyPrivacyPolicy;
452
- // this will throw an exception if fails or return error | null?
453
464
  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];
465
+ const err = await applyPrivacyPolicyImpl(v, policy, ent, throwErr);
466
+ if (err !== null) {
467
+ throw err;
468
+ }
469
+ return true;
470
+ }
471
+ exports.applyPrivacyPolicyX = applyPrivacyPolicyX;
472
+ // this will throw an exception if fails or return error | null?
473
+ async function applyPrivacyPolicyImpl(v, policy, ent, throwErr) {
474
+ for (const rule of policy.rules) {
475
+ const res = await rule.apply(v, ent);
459
476
  if (res.result == privacyResult.Allow) {
460
- return true;
477
+ return null;
461
478
  }
462
479
  else if (res.result == privacyResult.Deny) {
463
480
  // specific error throw that
464
481
  if (res.error) {
465
- throw res.error;
482
+ return res.error;
466
483
  }
467
484
  if (res.getError) {
468
- throw res.getError(policy, rule, ent);
485
+ return res.getError(policy, rule, ent);
469
486
  }
470
487
  if (throwErr) {
471
- throw throwErr();
488
+ return throwErr();
472
489
  }
473
- throw new EntPrivacyError(policy, rule, ent);
490
+ return new EntPrivacyError(policy, rule, ent);
474
491
  }
475
492
  }
476
- throw new EntInvalidPrivacyPolicyError(policy, ent);
493
+ return new EntInvalidPrivacyPolicyError(policy, ent);
477
494
  }
478
- exports.applyPrivacyPolicyX = applyPrivacyPolicyX;
495
+ exports.applyPrivacyPolicyImpl = applyPrivacyPolicyImpl;
479
496
  exports.AlwaysAllowPrivacyPolicy = {
480
497
  rules: [exports.AlwaysAllowRule],
481
498
  };
@@ -3,8 +3,8 @@ import { AssocEdge } from "../ent";
3
3
  import { AssocEdgeCountLoaderFactory } from "../loaders/assoc_count_loader";
4
4
  import { AssocEdgeLoaderFactory } from "../loaders/assoc_edge_loader";
5
5
  import { EdgeQuery, BaseEdgeQuery, IDInfo } from "./query";
6
- export declare type EdgeQuerySource<TSource extends Ent<TViewer>, TDest extends Ent<TViewer> = Ent<any>, TViewer extends Viewer = Viewer> = TSource | TSource[] | ID | ID[] | EdgeQuery<TDest, Ent, AssocEdge>;
7
- declare type loaderOptionsFunc<TViewer extends Viewer> = (type: string) => LoadEntOptions<Ent, TViewer>;
6
+ export type EdgeQuerySource<TSource extends Ent<TViewer>, TDest extends Ent<TViewer> = Ent<any>, TViewer extends Viewer = Viewer> = TSource | TSource[] | ID | ID[] | EdgeQuery<TDest, Ent, AssocEdge>;
7
+ type loaderOptionsFunc<TViewer extends Viewer> = (type: string) => LoadEntOptions<Ent, TViewer>;
8
8
  export declare abstract class AssocEdgeQueryBase<TSource extends Ent<TViewer>, TDest extends Ent<TViewer>, TEdge extends AssocEdge, TViewer extends Viewer = Viewer> extends BaseEdgeQuery<TSource, TDest, TEdge> implements EdgeQuery<TSource, TDest, TEdge> {
9
9
  viewer: TViewer;
10
10
  src: EdgeQuerySource<TSource, TDest, TViewer>;
@@ -14,6 +14,7 @@ export declare abstract class AssocEdgeQueryBase<TSource extends Ent<TViewer>, T
14
14
  constructor(viewer: TViewer, src: EdgeQuerySource<TSource, TDest, TViewer>, countLoaderFactory: AssocEdgeCountLoaderFactory, dataLoaderFactory: AssocEdgeLoaderFactory<TEdge>, options: LoadEntOptions<TDest, TViewer> | loaderOptionsFunc<TViewer>);
15
15
  private isEdgeQuery;
16
16
  abstract sourceEnt(id: ID): Promise<Ent | null>;
17
+ getTableName(): Promise<string>;
17
18
  private getSingleID;
18
19
  queryRawCount(): Promise<number>;
19
20
  queryAllRawCount(): Promise<Map<ID, number>>;
@@ -3,12 +3,13 @@ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.AssocEdgeQueryBase = void 0;
4
4
  const ent_1 = require("../ent");
5
5
  const query_1 = require("./query");
6
+ // would be nice to someday have an API for assoc asc
6
7
  class AssocEdgeQueryBase extends query_1.BaseEdgeQuery {
7
8
  constructor(viewer, src, countLoaderFactory, dataLoaderFactory,
8
9
  // if function, it's a polymorphic edge and need to provide
9
10
  // a function that goes from edgeType to LoadEntOptions
10
11
  options) {
11
- super(viewer, "time");
12
+ super(viewer, "time", "id2");
12
13
  this.viewer = viewer;
13
14
  this.src = src;
14
15
  this.countLoaderFactory = countLoaderFactory;
@@ -21,6 +22,13 @@ class AssocEdgeQueryBase extends query_1.BaseEdgeQuery {
21
22
  }
22
23
  return false;
23
24
  }
25
+ async getTableName() {
26
+ const edgeData = await (0, ent_1.loadEdgeData)(this.countLoaderFactory.getEdgeType());
27
+ if (!edgeData) {
28
+ throw new Error(`couldn't load edgeData`);
29
+ }
30
+ return edgeData.edgeTable;
31
+ }
24
32
  async getSingleID() {
25
33
  const infos = await this.genIDInfosToFetch();
26
34
  if (infos.length !== 1) {
@@ -35,7 +43,7 @@ class AssocEdgeQueryBase extends query_1.BaseEdgeQuery {
35
43
  return 0;
36
44
  }
37
45
  return this.countLoaderFactory
38
- .createLoader(this.viewer.context)
46
+ .createConfigurableLoader(this.getDefaultEdgeQueryOptions() ?? {}, this.viewer.context)
39
47
  .load(info.id);
40
48
  }
41
49
  async queryAllRawCount() {
@@ -0,0 +1,29 @@
1
+ import { Data, EdgeQueryableDataOptions, Ent, ID, LoadEntOptions, Viewer } from "../base";
2
+ import { Clause } from "../clause";
3
+ import { OrderBy } from "../query_impl";
4
+ import { BaseEdgeQuery, IDInfo } from "./query";
5
+ export interface CustomClauseQueryOptions<TDest extends Ent<TViewer>, TViewer extends Viewer = Viewer> {
6
+ loadEntOptions: LoadEntOptions<TDest, TViewer>;
7
+ clause: Clause;
8
+ name: string;
9
+ primarySortColIsUnique?: boolean;
10
+ orderby?: OrderBy;
11
+ sortColumn?: string;
12
+ orderByDirection?: "ASC" | "DESC";
13
+ nullsPlacement?: "first" | "last";
14
+ disableTransformations?: boolean;
15
+ }
16
+ export declare class CustomClauseQuery<TDest extends Ent<TViewer>, TViewer extends Viewer = Viewer> extends BaseEdgeQuery<any, TDest, Data> {
17
+ viewer: TViewer;
18
+ private options;
19
+ private clause;
20
+ constructor(viewer: TViewer, options: CustomClauseQueryOptions<TDest, TViewer>);
21
+ sourceEnt(_id: ID): Promise<null>;
22
+ getTableName(): string;
23
+ queryRawCount(): Promise<number>;
24
+ queryAllRawCount(): Promise<Map<ID, number>>;
25
+ protected loadRawIDs(_addID: (src: ID) => void): Promise<void>;
26
+ protected loadRawData(_infos: IDInfo[], options: EdgeQueryableDataOptions): Promise<void>;
27
+ dataToID(edge: Data): ID;
28
+ protected loadEntsFromEdges(id: ID, rows: Data[]): Promise<TDest[]>;
29
+ }
@@ -0,0 +1,105 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.CustomClauseQuery = void 0;
4
+ const clause_1 = require("../clause");
5
+ const ent_1 = require("../ent");
6
+ const query_1 = require("./query");
7
+ function getClause(opts) {
8
+ let cls = opts.clause;
9
+ if (opts.disableTransformations) {
10
+ return cls;
11
+ }
12
+ let optClause = opts.loadEntOptions.loaderFactory?.options?.clause;
13
+ if (typeof optClause === "function") {
14
+ optClause = optClause();
15
+ }
16
+ if (!optClause) {
17
+ return cls;
18
+ }
19
+ return (0, clause_1.AndOptional)(cls, optClause);
20
+ }
21
+ class CustomClauseQuery extends query_1.BaseEdgeQuery {
22
+ constructor(viewer, options) {
23
+ let orderby;
24
+ let primarySortCol;
25
+ if (options.orderby &&
26
+ (options.sortColumn || options.orderByDirection || options.nullsPlacement)) {
27
+ throw new Error(`cannot pass orderby and sortColumn|orderByDirection|nullsPlacement`);
28
+ }
29
+ if (options.orderby) {
30
+ primarySortCol = options.orderby[0].column;
31
+ orderby = options.orderby;
32
+ }
33
+ else {
34
+ primarySortCol = options.sortColumn || "id";
35
+ orderby = [
36
+ {
37
+ column: primarySortCol,
38
+ direction: options.orderByDirection ?? "DESC",
39
+ nullsPlacement: options.nullsPlacement,
40
+ },
41
+ ];
42
+ }
43
+ let cursorCol = options.primarySortColIsUnique
44
+ ? primarySortCol
45
+ : options.loadEntOptions.loaderFactory.options?.key || "id";
46
+ super(viewer, {
47
+ orderby,
48
+ cursorCol,
49
+ });
50
+ this.viewer = viewer;
51
+ this.options = options;
52
+ this.clause = getClause(options);
53
+ }
54
+ async sourceEnt(_id) {
55
+ return null;
56
+ }
57
+ getTableName() {
58
+ return this.options.loadEntOptions.tableName;
59
+ }
60
+ async queryRawCount() {
61
+ const row = await (0, ent_1.loadRow)({
62
+ tableName: this.options.loadEntOptions.tableName,
63
+ // sqlite needs as count otherwise it returns count(1)
64
+ fields: ["count(1) as count"],
65
+ clause: this.clause,
66
+ context: this.viewer.context,
67
+ });
68
+ return parseInt(row?.count, 10) || 0;
69
+ }
70
+ async queryAllRawCount() {
71
+ throw new Error(`queryAllRawCount doesn't make sense in CustomClauseQuery`);
72
+ }
73
+ // nothing to do here
74
+ async loadRawIDs(_addID) { }
75
+ async loadRawData(_infos, options) {
76
+ if (!options.orderby) {
77
+ options.orderby = [
78
+ {
79
+ column: this.getSortCol(),
80
+ direction: this.options.orderByDirection ?? "DESC",
81
+ nullsPlacement: this.options.nullsPlacement,
82
+ },
83
+ ];
84
+ }
85
+ if (!options.limit) {
86
+ options.limit = (0, ent_1.getDefaultLimit)();
87
+ }
88
+ const rows = await (0, ent_1.loadRows)({
89
+ tableName: this.options.loadEntOptions.tableName,
90
+ fields: this.options.loadEntOptions.fields,
91
+ clause: (0, clause_1.AndOptional)(this.clause, options.clause),
92
+ orderby: options.orderby,
93
+ limit: options?.limit || (0, ent_1.getDefaultLimit)(),
94
+ context: this.viewer.context,
95
+ });
96
+ this.edges.set(1, rows);
97
+ }
98
+ dataToID(edge) {
99
+ return edge.id;
100
+ }
101
+ async loadEntsFromEdges(id, rows) {
102
+ return (0, ent_1.applyPrivacyPolicyForRows)(this.viewer, rows, this.options.loadEntOptions);
103
+ }
104
+ }
105
+ exports.CustomClauseQuery = CustomClauseQuery;
@@ -1,19 +1,36 @@
1
1
  import { Data, Ent, ID, EdgeQueryableDataOptions, LoadEntOptions, Viewer, LoaderFactory, ConfigurableLoaderFactory } from "../base";
2
+ import { Clause } from "../clause";
3
+ import { OrderBy } from "../query_impl";
2
4
  import { BaseEdgeQuery, IDInfo, EdgeQuery } from "./query";
3
- export interface CustomEdgeQueryOptions<TSource extends Ent<TViewer>, TDest extends Ent<TViewer>, TViewer extends Viewer = Viewer> {
5
+ export interface CustomEdgeQueryOptionsDeprecated<TSource extends Ent<TViewer>, TDest extends Ent<TViewer>, TViewer extends Viewer = Viewer> {
4
6
  src: TSource | ID;
5
7
  countLoaderFactory: LoaderFactory<ID, number>;
6
8
  dataLoaderFactory: ConfigurableLoaderFactory<ID, Data[]>;
7
9
  options: LoadEntOptions<TDest, TViewer>;
8
10
  sortColumn?: string;
9
11
  }
12
+ export interface CustomEdgeQueryOptions<TSource extends Ent<TViewer>, TDest extends Ent<TViewer>, TViewer extends Viewer = Viewer> {
13
+ src: TSource | ID;
14
+ loadEntOptions: LoadEntOptions<TDest, TViewer>;
15
+ groupCol?: string;
16
+ clause?: Clause;
17
+ name: string;
18
+ sortColumn?: string;
19
+ orderby?: OrderBy;
20
+ primarySortColIsUnique?: boolean;
21
+ disableTransformations?: boolean;
22
+ }
10
23
  export declare abstract class CustomEdgeQueryBase<TSource extends Ent<TViewer>, TDest extends Ent<TViewer>, TViewer extends Viewer = Viewer> extends BaseEdgeQuery<TSource, TDest, Data> implements EdgeQuery<TSource, TDest, Data> {
11
24
  viewer: TViewer;
12
25
  private options;
13
26
  private id;
14
- constructor(viewer: TViewer, options: CustomEdgeQueryOptions<TSource, TDest, TViewer>);
27
+ private opts;
28
+ constructor(viewer: TViewer, options: CustomEdgeQueryOptionsDeprecated<TSource, TDest, TViewer> | CustomEdgeQueryOptions<TSource, TDest, TViewer>);
29
+ getTableName(): string;
15
30
  abstract sourceEnt(id: ID): Promise<Ent | null>;
16
31
  private idVisible;
32
+ private getCountLoader;
33
+ private getQueryLoader;
17
34
  queryRawCount(): Promise<number>;
18
35
  queryAllRawCount(): Promise<Map<ID, number>>;
19
36
  protected loadRawIDs(addID: (src: ID | TSource) => void): Promise<void>;