@snowtop/ent 0.1.0-alpha → 0.1.0-alpha12
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/action/action.d.ts +2 -0
- package/action/executor.d.ts +1 -1
- package/action/orchestrator.d.ts +16 -3
- package/action/orchestrator.js +157 -47
- package/core/base.d.ts +6 -2
- package/core/base.js +16 -0
- package/core/clause.d.ts +24 -3
- package/core/clause.js +246 -5
- package/core/config.d.ts +18 -0
- package/core/config.js +17 -0
- package/core/db.d.ts +3 -3
- package/core/db.js +2 -0
- package/core/ent.d.ts +2 -4
- package/core/ent.js +72 -25
- package/core/loaders/assoc_edge_loader.d.ts +1 -1
- package/core/loaders/assoc_edge_loader.js +5 -4
- package/core/loaders/index_loader.js +1 -0
- package/core/loaders/object_loader.d.ts +7 -2
- package/core/loaders/object_loader.js +59 -4
- package/core/privacy.d.ts +1 -1
- package/core/privacy.js +3 -0
- package/core/viewer.d.ts +1 -0
- package/core/viewer.js +4 -0
- package/graphql/builtins/connection.js +3 -3
- package/graphql/builtins/edge.js +2 -2
- package/graphql/builtins/node.js +1 -1
- package/graphql/graphql.d.ts +3 -2
- package/graphql/graphql.js +24 -23
- package/graphql/index.d.ts +1 -0
- package/graphql/index.js +3 -1
- package/graphql/mutations/union.d.ts +2 -0
- package/graphql/mutations/union.js +35 -0
- package/graphql/node_resolver.d.ts +0 -1
- package/graphql/query/connection_type.js +6 -6
- package/graphql/query/page_info.js +4 -4
- package/graphql/query/shared_assoc_test.js +2 -2
- package/graphql/scalars/time.d.ts +1 -1
- package/imports/index.d.ts +0 -1
- package/imports/index.js +3 -36
- package/index.d.ts +16 -1
- package/index.js +18 -5
- package/package.json +3 -3
- package/parse_schema/parse.d.ts +23 -4
- package/parse_schema/parse.js +87 -8
- package/schema/base_schema.d.ts +36 -1
- package/schema/base_schema.js +63 -17
- package/schema/field.d.ts +25 -25
- package/schema/field.js +42 -33
- package/schema/index.d.ts +4 -2
- package/schema/index.js +10 -1
- package/schema/json_field.d.ts +6 -6
- package/schema/json_field.js +2 -2
- package/schema/schema.d.ts +70 -6
- package/schema/schema.js +142 -10
- package/schema/struct_field.d.ts +17 -0
- package/schema/struct_field.js +102 -0
- package/schema/union_field.d.ts +23 -0
- package/schema/union_field.js +79 -0
- package/scripts/custom_compiler.js +2 -19
- package/scripts/custom_graphql.js +122 -15
- package/scripts/move_generated.d.ts +1 -0
- package/scripts/move_generated.js +142 -0
- package/scripts/read_schema.js +15 -1
- package/scripts/transform_code.d.ts +1 -0
- package/scripts/transform_code.js +113 -0
- package/scripts/transform_schema.d.ts +1 -0
- package/scripts/transform_schema.js +355 -0
- package/testutils/builder.d.ts +16 -9
- package/testutils/builder.js +80 -8
- package/testutils/context/test_context.d.ts +2 -2
- package/testutils/context/test_context.js +7 -1
- package/testutils/db/test_db.d.ts +2 -1
- package/testutils/db/test_db.js +22 -13
- package/testutils/ent-graphql-tests/index.d.ts +2 -0
- package/testutils/ent-graphql-tests/index.js +26 -17
- package/testutils/fake_data/fake_contact.d.ts +3 -7
- package/testutils/fake_data/fake_contact.js +14 -26
- package/testutils/fake_data/fake_event.d.ts +3 -7
- package/testutils/fake_data/fake_event.js +20 -33
- package/testutils/fake_data/fake_user.d.ts +3 -7
- package/testutils/fake_data/fake_user.js +22 -36
- package/testutils/fake_data/test_helpers.js +1 -1
- package/tsc/ast.d.ts +21 -0
- package/tsc/ast.js +154 -0
- package/tsc/compilerOptions.d.ts +8 -0
- package/tsc/compilerOptions.js +100 -0
package/action/action.d.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { Ent, EntConstructor, Viewer, ID, Data, PrivacyPolicy, Context } from "../core/base";
|
|
2
2
|
import { DataOperation, AssocEdgeInputOptions } from "../core/ent";
|
|
3
3
|
import { Queryer } from "../core/db";
|
|
4
|
+
import { TransformedUpdateOperation, UpdateOperation } from "../schema";
|
|
4
5
|
export declare enum WriteOperation {
|
|
5
6
|
Insert = "insert",
|
|
6
7
|
Edit = "edit",
|
|
@@ -50,6 +51,7 @@ export interface Action<TEnt extends Ent, TBuilder extends Builder<TEnt>, TData
|
|
|
50
51
|
observers?: Observer<TBuilder, TData>[];
|
|
51
52
|
validators?: Validator<TBuilder, TData>[];
|
|
52
53
|
getInput(): TData;
|
|
54
|
+
transformWrite?: <T2 extends Ent>(stmt: UpdateOperation<T2>) => Promise<TransformedUpdateOperation<T2>> | TransformedUpdateOperation<T2> | undefined;
|
|
53
55
|
valid(): Promise<boolean>;
|
|
54
56
|
validX(): Promise<void>;
|
|
55
57
|
viewerForEntLoad?(data: Data): Viewer | Promise<Viewer>;
|
package/action/executor.d.ts
CHANGED
package/action/orchestrator.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { ID, Data, Ent, Viewer, EntConstructor, LoadEntOptions } from "../core/base";
|
|
2
2
|
import { AssocEdgeInputOptions, DataOperation } from "../core/ent";
|
|
3
|
-
import { SchemaInputType } from "../schema/schema";
|
|
3
|
+
import { SchemaInputType, FieldInfoMap } from "../schema/schema";
|
|
4
4
|
import { Changeset, Executor } from "../action/action";
|
|
5
5
|
import { WriteOperation, Builder, Action } from "../action";
|
|
6
6
|
export interface OrchestratorOptions<TEnt extends Ent, TData extends Data> {
|
|
@@ -12,8 +12,9 @@ export interface OrchestratorOptions<TEnt extends Ent, TData extends Data> {
|
|
|
12
12
|
builder: Builder<TEnt>;
|
|
13
13
|
action?: Action<TEnt, Builder<TEnt>, TData>;
|
|
14
14
|
schema: SchemaInputType;
|
|
15
|
-
editedFields(): Map<string, any
|
|
15
|
+
editedFields(): Map<string, any> | Promise<Map<string, any>>;
|
|
16
16
|
updateInput?: (data: TData) => void;
|
|
17
|
+
fieldInfo: FieldInfoMap;
|
|
17
18
|
}
|
|
18
19
|
interface edgeInputDataOpts {
|
|
19
20
|
edgeType: string;
|
|
@@ -41,8 +42,13 @@ export declare class Orchestrator<TEnt extends Ent, TData extends Data> {
|
|
|
41
42
|
viewer: Viewer;
|
|
42
43
|
private defaultFieldsByFieldName;
|
|
43
44
|
private defaultFieldsByTSName;
|
|
45
|
+
private actualOperation;
|
|
46
|
+
private existingEnt?;
|
|
47
|
+
private disableTransformations;
|
|
48
|
+
private memoizedGetFields;
|
|
44
49
|
constructor(options: OrchestratorOptions<TEnt, TData>);
|
|
45
50
|
private addEdge;
|
|
51
|
+
setDisableTransformations(val: boolean): void;
|
|
46
52
|
addInboundEdge<T2 extends Ent>(id1: ID | Builder<T2>, edgeType: string, nodeType: string, options?: AssocEdgeInputOptions): void;
|
|
47
53
|
addOutboundEdge<T2 extends Ent>(id2: ID | Builder<T2>, edgeType: string, nodeType: string, options?: AssocEdgeInputOptions): void;
|
|
48
54
|
removeInboundEdge(id1: ID, edgeType: string): void;
|
|
@@ -53,11 +59,18 @@ export declare class Orchestrator<TEnt extends Ent, TData extends Data> {
|
|
|
53
59
|
private getEdgeOperation;
|
|
54
60
|
private buildEdgeOps;
|
|
55
61
|
private throwError;
|
|
56
|
-
private
|
|
62
|
+
private getEntForPrivacyPolicyImpl;
|
|
63
|
+
private getSQLStatementOperation;
|
|
64
|
+
private getWriteOpForSQLStamentOp;
|
|
65
|
+
getPossibleUnsafeEntForPrivacy(): Promise<TEnt>;
|
|
66
|
+
getEditedData(): Promise<Data>;
|
|
67
|
+
private getFieldsInfo;
|
|
57
68
|
private validate;
|
|
58
69
|
private triggers;
|
|
59
70
|
private validators;
|
|
60
71
|
private isBuilder;
|
|
72
|
+
private getInputKey;
|
|
73
|
+
private getStorageKey;
|
|
61
74
|
private getFieldsWithDefaultValues;
|
|
62
75
|
private hasData;
|
|
63
76
|
private transformFieldValue;
|
package/action/orchestrator.js
CHANGED
|
@@ -1,14 +1,16 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
2
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
6
|
exports.EntChangeset = exports.Orchestrator = exports.edgeDirection = void 0;
|
|
4
7
|
const ent_1 = require("../core/ent");
|
|
5
8
|
const schema_1 = require("../schema/schema");
|
|
6
9
|
const action_1 = require("../action");
|
|
7
|
-
const snake_case_1 = require("snake-case");
|
|
8
|
-
const camel_case_1 = require("camel-case");
|
|
9
10
|
const privacy_1 = require("../core/privacy");
|
|
10
11
|
const executor_1 = require("./executor");
|
|
11
12
|
const logger_1 = require("../core/logger");
|
|
13
|
+
const memoizee_1 = __importDefault(require("memoizee"));
|
|
12
14
|
var edgeDirection;
|
|
13
15
|
(function (edgeDirection) {
|
|
14
16
|
edgeDirection[edgeDirection["inboundEdge"] = 0] = "inboundEdge";
|
|
@@ -62,6 +64,9 @@ class Orchestrator {
|
|
|
62
64
|
this.defaultFieldsByFieldName = {};
|
|
63
65
|
this.defaultFieldsByTSName = {};
|
|
64
66
|
this.viewer = options.viewer;
|
|
67
|
+
this.actualOperation = this.options.operation;
|
|
68
|
+
this.existingEnt = this.options.builder.existingEnt;
|
|
69
|
+
this.memoizedGetFields = (0, memoizee_1.default)(this.getFieldsInfo.bind(this));
|
|
65
70
|
}
|
|
66
71
|
addEdge(edge, op) {
|
|
67
72
|
this.edgeSet.add(edge.edgeType);
|
|
@@ -80,6 +85,9 @@ class Orchestrator {
|
|
|
80
85
|
m1.set(op, m2);
|
|
81
86
|
this.edges.set(edge.edgeType, m1);
|
|
82
87
|
}
|
|
88
|
+
setDisableTransformations(val) {
|
|
89
|
+
this.disableTransformations = val;
|
|
90
|
+
}
|
|
83
91
|
addInboundEdge(id1, edgeType, nodeType, options) {
|
|
84
92
|
this.addEdge(new edgeInputData({
|
|
85
93
|
id: id1,
|
|
@@ -135,24 +143,27 @@ class Orchestrator {
|
|
|
135
143
|
}
|
|
136
144
|
buildMainOp() {
|
|
137
145
|
// this assumes we have validated fields
|
|
138
|
-
switch (this.
|
|
146
|
+
switch (this.actualOperation) {
|
|
139
147
|
case action_1.WriteOperation.Delete:
|
|
140
|
-
return new ent_1.DeleteNodeOperation(this.
|
|
148
|
+
return new ent_1.DeleteNodeOperation(this.existingEnt.id, {
|
|
141
149
|
tableName: this.options.tableName,
|
|
142
150
|
});
|
|
143
151
|
default:
|
|
152
|
+
if (this.actualOperation === action_1.WriteOperation.Edit && !this.existingEnt) {
|
|
153
|
+
throw new Error(`existing ent required with operation ${this.actualOperation}`);
|
|
154
|
+
}
|
|
144
155
|
const opts = {
|
|
145
156
|
fields: this.validatedFields,
|
|
146
157
|
tableName: this.options.tableName,
|
|
147
158
|
fieldsToResolve: this.fieldsToResolve,
|
|
148
159
|
key: this.options.key,
|
|
149
|
-
|
|
160
|
+
loadEntOptions: this.options.loaderOptions,
|
|
150
161
|
placeholderID: this.options.builder.placeholderID,
|
|
151
162
|
};
|
|
152
163
|
if (this.logValues) {
|
|
153
164
|
opts.fieldsToLog = this.logValues;
|
|
154
165
|
}
|
|
155
|
-
this.mainOp = new ent_1.EditNodeOperation(opts, this.
|
|
166
|
+
this.mainOp = new ent_1.EditNodeOperation(opts, this.existingEnt);
|
|
156
167
|
return this.mainOp;
|
|
157
168
|
}
|
|
158
169
|
}
|
|
@@ -219,35 +230,82 @@ class Orchestrator {
|
|
|
219
230
|
if (!privacyPolicy || !action) {
|
|
220
231
|
throw new Error(`shouldn't get here if no privacyPolicy for action`);
|
|
221
232
|
}
|
|
222
|
-
if (this.
|
|
233
|
+
if (this.actualOperation === action_1.WriteOperation.Insert) {
|
|
223
234
|
return new EntCannotCreateEntError(privacyPolicy, action);
|
|
224
235
|
}
|
|
225
|
-
else if (this.
|
|
226
|
-
return new EntCannotEditEntError(privacyPolicy, action, this.
|
|
236
|
+
else if (this.actualOperation === action_1.WriteOperation.Edit) {
|
|
237
|
+
return new EntCannotEditEntError(privacyPolicy, action, this.existingEnt);
|
|
227
238
|
}
|
|
228
|
-
return new EntCannotDeleteEntError(privacyPolicy, action, this.
|
|
239
|
+
return new EntCannotDeleteEntError(privacyPolicy, action, this.existingEnt);
|
|
229
240
|
}
|
|
230
|
-
|
|
231
|
-
if (this.
|
|
232
|
-
return this.
|
|
241
|
+
getEntForPrivacyPolicyImpl(editedData) {
|
|
242
|
+
if (this.actualOperation !== action_1.WriteOperation.Insert) {
|
|
243
|
+
return this.existingEnt;
|
|
233
244
|
}
|
|
234
245
|
// we create an unsafe ent to be used for privacy policies
|
|
235
246
|
return new this.options.builder.ent(this.options.builder.viewer, editedData);
|
|
236
247
|
}
|
|
248
|
+
getSQLStatementOperation() {
|
|
249
|
+
switch (this.actualOperation) {
|
|
250
|
+
case action_1.WriteOperation.Edit:
|
|
251
|
+
return schema_1.SQLStatementOperation.Update;
|
|
252
|
+
case action_1.WriteOperation.Insert:
|
|
253
|
+
return schema_1.SQLStatementOperation.Insert;
|
|
254
|
+
case action_1.WriteOperation.Delete:
|
|
255
|
+
return schema_1.SQLStatementOperation.Delete;
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
getWriteOpForSQLStamentOp(op) {
|
|
259
|
+
switch (op) {
|
|
260
|
+
case schema_1.SQLStatementOperation.Update:
|
|
261
|
+
return action_1.WriteOperation.Edit;
|
|
262
|
+
case schema_1.SQLStatementOperation.Insert:
|
|
263
|
+
return action_1.WriteOperation.Insert;
|
|
264
|
+
case schema_1.SQLStatementOperation.Update:
|
|
265
|
+
return action_1.WriteOperation.Delete;
|
|
266
|
+
default:
|
|
267
|
+
throw new Error("invalid path");
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
// if you're doing custom privacy within an action and want to
|
|
271
|
+
// get either the unsafe ent or the existing ent that's being edited
|
|
272
|
+
async getPossibleUnsafeEntForPrivacy() {
|
|
273
|
+
if (this.actualOperation !== action_1.WriteOperation.Insert) {
|
|
274
|
+
return this.existingEnt;
|
|
275
|
+
}
|
|
276
|
+
const { editedData } = await this.memoizedGetFields();
|
|
277
|
+
return this.getEntForPrivacyPolicyImpl(editedData);
|
|
278
|
+
}
|
|
279
|
+
// this gets the fields that were explicitly set plus any default or transformed values
|
|
280
|
+
// mainly exists to get default fields e.g. default id to be used in triggers
|
|
281
|
+
// NOTE: this API may change in the future
|
|
282
|
+
// doesn't work to get ids for autoincrement keys
|
|
283
|
+
async getEditedData() {
|
|
284
|
+
const { editedData } = await this.memoizedGetFields();
|
|
285
|
+
return editedData;
|
|
286
|
+
}
|
|
287
|
+
// Note: this is memoized. call memoizedGetFields instead
|
|
288
|
+
async getFieldsInfo() {
|
|
289
|
+
const action = this.options.action;
|
|
290
|
+
const builder = this.options.builder;
|
|
291
|
+
// future optimization: can get schemaFields to memoize based on different values
|
|
292
|
+
const schemaFields = (0, schema_1.getFields)(this.options.schema);
|
|
293
|
+
const editedFields = await this.options.editedFields();
|
|
294
|
+
let editedData = await this.getFieldsWithDefaultValues(builder, schemaFields, editedFields, action);
|
|
295
|
+
return { editedData, editedFields, schemaFields };
|
|
296
|
+
}
|
|
237
297
|
async validate() {
|
|
238
298
|
// existing ent required for edit or delete operations
|
|
239
|
-
switch (this.
|
|
299
|
+
switch (this.actualOperation) {
|
|
240
300
|
case action_1.WriteOperation.Delete:
|
|
241
301
|
case action_1.WriteOperation.Edit:
|
|
242
|
-
if (!this.
|
|
243
|
-
throw new Error(
|
|
302
|
+
if (!this.existingEnt) {
|
|
303
|
+
throw new Error(`existing ent required with operation ${this.actualOperation}`);
|
|
244
304
|
}
|
|
245
305
|
}
|
|
306
|
+
const { schemaFields, editedData } = await this.memoizedGetFields();
|
|
246
307
|
const action = this.options.action;
|
|
247
308
|
const builder = this.options.builder;
|
|
248
|
-
// future optimization: can get schemaFields to memoize based on different values
|
|
249
|
-
const schemaFields = (0, schema_1.getFields)(this.options.schema);
|
|
250
|
-
let editedData = this.getFieldsWithDefaultValues(builder, schemaFields, action);
|
|
251
309
|
// this runs in following phases:
|
|
252
310
|
// * set default fields and pass to builder so the value can be checked by triggers/observers/validators
|
|
253
311
|
// * privacy policy (use unsafe ent if we have it)
|
|
@@ -255,7 +313,7 @@ class Orchestrator {
|
|
|
255
313
|
// * validators
|
|
256
314
|
let privacyPolicy = action?.getPrivacyPolicy();
|
|
257
315
|
if (privacyPolicy) {
|
|
258
|
-
await (0, privacy_1.applyPrivacyPolicyX)(this.options.viewer, privacyPolicy, this.
|
|
316
|
+
await (0, privacy_1.applyPrivacyPolicyX)(this.options.viewer, privacyPolicy, this.getEntForPrivacyPolicyImpl(editedData), this.throwError.bind(this));
|
|
259
317
|
}
|
|
260
318
|
// have to run triggers which update fields first before field and other validators
|
|
261
319
|
// so running this first to build things up
|
|
@@ -264,8 +322,11 @@ class Orchestrator {
|
|
|
264
322
|
await this.triggers(action, builder, triggers);
|
|
265
323
|
}
|
|
266
324
|
let validators = action?.validators || [];
|
|
325
|
+
// not ideal we're calling this twice. fix...
|
|
326
|
+
// needed for now. may need to rewrite some of this?
|
|
327
|
+
const editedFields2 = await this.options.editedFields();
|
|
267
328
|
await Promise.all([
|
|
268
|
-
this.formatAndValidateFields(schemaFields),
|
|
329
|
+
this.formatAndValidateFields(schemaFields, editedFields2),
|
|
269
330
|
this.validators(validators, action, builder),
|
|
270
331
|
]);
|
|
271
332
|
}
|
|
@@ -300,18 +361,70 @@ class Orchestrator {
|
|
|
300
361
|
isBuilder(val) {
|
|
301
362
|
return val.placeholderID !== undefined;
|
|
302
363
|
}
|
|
303
|
-
|
|
304
|
-
|
|
364
|
+
getInputKey(k) {
|
|
365
|
+
return this.options.fieldInfo[k].inputKey;
|
|
366
|
+
}
|
|
367
|
+
getStorageKey(k) {
|
|
368
|
+
return this.options.fieldInfo[k].dbCol;
|
|
369
|
+
}
|
|
370
|
+
async getFieldsWithDefaultValues(builder, schemaFields, editedFields, action) {
|
|
305
371
|
let data = {};
|
|
306
372
|
let defaultData = {};
|
|
307
373
|
let input = action?.getInput() || {};
|
|
308
374
|
let updateInput = false;
|
|
375
|
+
// transformations
|
|
376
|
+
// if action transformations. always do it
|
|
377
|
+
// if disable transformations set, don't do schema transform and just do the right thing
|
|
378
|
+
// else apply schema tranformation if it exists
|
|
379
|
+
let transformed;
|
|
380
|
+
if (action?.transformWrite) {
|
|
381
|
+
transformed = await action.transformWrite({
|
|
382
|
+
viewer: builder.viewer,
|
|
383
|
+
op: this.getSQLStatementOperation(),
|
|
384
|
+
data: editedFields,
|
|
385
|
+
existingEnt: this.existingEnt,
|
|
386
|
+
});
|
|
387
|
+
}
|
|
388
|
+
else if (!this.disableTransformations) {
|
|
389
|
+
transformed = (0, schema_1.getTransformedUpdateOp)(this.options.schema, {
|
|
390
|
+
viewer: builder.viewer,
|
|
391
|
+
op: this.getSQLStatementOperation(),
|
|
392
|
+
data: editedFields,
|
|
393
|
+
existingEnt: this.existingEnt,
|
|
394
|
+
});
|
|
395
|
+
}
|
|
396
|
+
if (transformed) {
|
|
397
|
+
if (transformed.data) {
|
|
398
|
+
updateInput = true;
|
|
399
|
+
for (const k in transformed.data) {
|
|
400
|
+
let field = schemaFields.get(k);
|
|
401
|
+
if (!field) {
|
|
402
|
+
throw new Error(`tried to transform field with unknown field ${k}`);
|
|
403
|
+
}
|
|
404
|
+
let val = transformed.data[k];
|
|
405
|
+
if (field.format) {
|
|
406
|
+
val = field.format(transformed.data[k]);
|
|
407
|
+
}
|
|
408
|
+
data[this.getStorageKey(k)] = val;
|
|
409
|
+
this.defaultFieldsByTSName[this.getInputKey(k)] = val;
|
|
410
|
+
// hmm do we need this?
|
|
411
|
+
// TODO how to do this for local tests?
|
|
412
|
+
// this.defaultFieldsByFieldName[k] = val;
|
|
413
|
+
}
|
|
414
|
+
}
|
|
415
|
+
this.actualOperation = this.getWriteOpForSQLStamentOp(transformed.op);
|
|
416
|
+
if (transformed.existingEnt) {
|
|
417
|
+
this.existingEnt = transformed.existingEnt;
|
|
418
|
+
}
|
|
419
|
+
}
|
|
420
|
+
// transforming before doing default fields so that we don't create a new id
|
|
421
|
+
// and anything that depends on the type of operations knows what it is
|
|
309
422
|
for (const [fieldName, field] of schemaFields) {
|
|
310
423
|
let value = editedFields.get(fieldName);
|
|
311
424
|
let defaultValue = undefined;
|
|
312
|
-
let dbKey =
|
|
425
|
+
let dbKey = this.getStorageKey(fieldName);
|
|
313
426
|
if (value === undefined) {
|
|
314
|
-
if (this.
|
|
427
|
+
if (this.actualOperation === action_1.WriteOperation.Insert) {
|
|
315
428
|
if (field.defaultToViewerOnCreate && field.defaultValueOnCreate) {
|
|
316
429
|
throw new Error(`cannot set both defaultToViewerOnCreate and defaultValueOnCreate`);
|
|
317
430
|
}
|
|
@@ -321,12 +434,12 @@ class Orchestrator {
|
|
|
321
434
|
if (field.defaultValueOnCreate) {
|
|
322
435
|
defaultValue = field.defaultValueOnCreate(builder, input);
|
|
323
436
|
if (defaultValue === undefined) {
|
|
324
|
-
throw new Error(`defaultValueOnCreate() returned undefined for field ${
|
|
437
|
+
throw new Error(`defaultValueOnCreate() returned undefined for field ${fieldName}`);
|
|
325
438
|
}
|
|
326
439
|
}
|
|
327
440
|
}
|
|
328
441
|
if (field.defaultValueOnEdit &&
|
|
329
|
-
this.
|
|
442
|
+
this.actualOperation === action_1.WriteOperation.Edit) {
|
|
330
443
|
defaultValue = field.defaultValueOnEdit(builder, input);
|
|
331
444
|
}
|
|
332
445
|
}
|
|
@@ -337,8 +450,7 @@ class Orchestrator {
|
|
|
337
450
|
updateInput = true;
|
|
338
451
|
defaultData[dbKey] = defaultValue;
|
|
339
452
|
this.defaultFieldsByFieldName[fieldName] = defaultValue;
|
|
340
|
-
|
|
341
|
-
this.defaultFieldsByTSName[(0, camel_case_1.camelCase)(fieldName)] = defaultValue;
|
|
453
|
+
this.defaultFieldsByTSName[this.getInputKey(fieldName)] = defaultValue;
|
|
342
454
|
}
|
|
343
455
|
}
|
|
344
456
|
// if there's data changing, add data
|
|
@@ -360,11 +472,11 @@ class Orchestrator {
|
|
|
360
472
|
}
|
|
361
473
|
return false;
|
|
362
474
|
}
|
|
363
|
-
async transformFieldValue(field, dbKey, value) {
|
|
475
|
+
async transformFieldValue(fieldName, field, dbKey, value) {
|
|
364
476
|
// now format and validate...
|
|
365
477
|
if (value === null) {
|
|
366
478
|
if (!field.nullable) {
|
|
367
|
-
throw new Error(`field ${
|
|
479
|
+
throw new Error(`field ${fieldName} set to null for non-nullable field`);
|
|
368
480
|
}
|
|
369
481
|
}
|
|
370
482
|
else if (value === undefined) {
|
|
@@ -374,15 +486,15 @@ class Orchestrator {
|
|
|
374
486
|
// not setting server default as we're depending on the database handling that.
|
|
375
487
|
// server default allowed
|
|
376
488
|
field.serverDefault === undefined &&
|
|
377
|
-
this.
|
|
378
|
-
throw new Error(`required field ${
|
|
489
|
+
this.actualOperation === action_1.WriteOperation.Insert) {
|
|
490
|
+
throw new Error(`required field ${fieldName} not set`);
|
|
379
491
|
}
|
|
380
492
|
}
|
|
381
493
|
else if (this.isBuilder(value)) {
|
|
382
494
|
if (field.valid) {
|
|
383
|
-
const valid = await
|
|
495
|
+
const valid = await field.valid(value);
|
|
384
496
|
if (!valid) {
|
|
385
|
-
throw new Error(`invalid field ${
|
|
497
|
+
throw new Error(`invalid field ${fieldName} with value ${value}`);
|
|
386
498
|
}
|
|
387
499
|
}
|
|
388
500
|
// keep track of dependencies to resolve
|
|
@@ -393,24 +505,22 @@ class Orchestrator {
|
|
|
393
505
|
else {
|
|
394
506
|
if (field.valid) {
|
|
395
507
|
// TODO this could be async. handle this better
|
|
396
|
-
const valid = await
|
|
508
|
+
const valid = await field.valid(value);
|
|
397
509
|
if (!valid) {
|
|
398
|
-
throw new Error(`invalid field ${
|
|
510
|
+
throw new Error(`invalid field ${fieldName} with value ${value}`);
|
|
399
511
|
}
|
|
400
512
|
}
|
|
401
513
|
if (field.format) {
|
|
402
|
-
|
|
403
|
-
value = await Promise.resolve(field.format(value));
|
|
514
|
+
value = await field.format(value);
|
|
404
515
|
}
|
|
405
516
|
}
|
|
406
517
|
return value;
|
|
407
518
|
}
|
|
408
|
-
async formatAndValidateFields(schemaFields) {
|
|
409
|
-
const op = this.
|
|
519
|
+
async formatAndValidateFields(schemaFields, editedFields) {
|
|
520
|
+
const op = this.actualOperation;
|
|
410
521
|
if (op === action_1.WriteOperation.Delete) {
|
|
411
522
|
return;
|
|
412
523
|
}
|
|
413
|
-
const editedFields = this.options.editedFields();
|
|
414
524
|
// build up data to be saved...
|
|
415
525
|
let data = {};
|
|
416
526
|
let logValues = {};
|
|
@@ -420,8 +530,8 @@ class Orchestrator {
|
|
|
420
530
|
// null allowed
|
|
421
531
|
value = this.defaultFieldsByFieldName[fieldName];
|
|
422
532
|
}
|
|
423
|
-
let dbKey =
|
|
424
|
-
value = await this.transformFieldValue(field, dbKey, value);
|
|
533
|
+
let dbKey = this.getStorageKey(fieldName);
|
|
534
|
+
value = await this.transformFieldValue(fieldName, field, dbKey, value);
|
|
425
535
|
if (value !== undefined) {
|
|
426
536
|
data[dbKey] = value;
|
|
427
537
|
logValues[dbKey] = field.logValue(value);
|
|
@@ -433,10 +543,10 @@ class Orchestrator {
|
|
|
433
543
|
for (const fieldName in this.defaultFieldsByFieldName) {
|
|
434
544
|
const defaultValue = this.defaultFieldsByFieldName[fieldName];
|
|
435
545
|
let field = schemaFields.get(fieldName);
|
|
436
|
-
let dbKey =
|
|
546
|
+
let dbKey = this.getStorageKey(fieldName);
|
|
437
547
|
// no value, let's just default
|
|
438
548
|
if (data[dbKey] === undefined) {
|
|
439
|
-
const value = await this.transformFieldValue(field, dbKey, defaultValue);
|
|
549
|
+
const value = await this.transformFieldValue(fieldName, field, dbKey, defaultValue);
|
|
440
550
|
data[dbKey] = value;
|
|
441
551
|
logValues[dbKey] = field.logValue(value);
|
|
442
552
|
}
|
|
@@ -494,7 +604,7 @@ class Orchestrator {
|
|
|
494
604
|
const viewer = await this.viewerForEntLoad(row);
|
|
495
605
|
const ent = await (0, ent_1.applyPrivacyPolicyForRow)(viewer, this.options.loaderOptions, row);
|
|
496
606
|
if (!ent) {
|
|
497
|
-
if (this.
|
|
607
|
+
if (this.actualOperation == action_1.WriteOperation.Insert) {
|
|
498
608
|
throw new Error(`was able to create ent but not load it`);
|
|
499
609
|
}
|
|
500
610
|
else {
|
package/core/base.d.ts
CHANGED
|
@@ -43,7 +43,7 @@ export interface Viewer {
|
|
|
43
43
|
export interface Ent {
|
|
44
44
|
id: ID;
|
|
45
45
|
viewer: Viewer;
|
|
46
|
-
|
|
46
|
+
getPrivacyPolicy(): PrivacyPolicy<this>;
|
|
47
47
|
nodeType: string;
|
|
48
48
|
}
|
|
49
49
|
export declare type Data = {
|
|
@@ -62,6 +62,7 @@ export interface SelectBaseDataOptions extends DataOptions {
|
|
|
62
62
|
}
|
|
63
63
|
export interface SelectDataOptions extends SelectBaseDataOptions {
|
|
64
64
|
key: string;
|
|
65
|
+
clause?: clause.Clause | (() => clause.Clause | undefined);
|
|
65
66
|
}
|
|
66
67
|
export interface QueryableDataOptions extends SelectBaseDataOptions, QueryDataOptions {
|
|
67
68
|
}
|
|
@@ -88,9 +89,11 @@ interface LoadableEntOptions<T extends Ent> {
|
|
|
88
89
|
ent: EntConstructor<T>;
|
|
89
90
|
}
|
|
90
91
|
export interface LoadEntOptions<T extends Ent> extends LoadableEntOptions<T>, SelectBaseDataOptions {
|
|
92
|
+
fieldPrivacy?: Map<string, PrivacyPolicy>;
|
|
91
93
|
}
|
|
92
94
|
export interface LoadCustomEntOptions<T extends Ent> extends SelectBaseDataOptions {
|
|
93
95
|
ent: EntConstructor<T>;
|
|
96
|
+
fieldPrivacy?: Map<string, PrivacyPolicy>;
|
|
94
97
|
}
|
|
95
98
|
export interface LoaderInfo {
|
|
96
99
|
tableName: string;
|
|
@@ -108,6 +111,7 @@ declare enum privacyResult {
|
|
|
108
111
|
export interface PrivacyResult {
|
|
109
112
|
result: privacyResult;
|
|
110
113
|
error?: PrivacyError;
|
|
114
|
+
getError?(policy: PrivacyPolicy, rule: PrivacyPolicyRule, ent?: Ent): PrivacyError;
|
|
111
115
|
}
|
|
112
116
|
export interface PrivacyError extends Error {
|
|
113
117
|
privacyPolicy: PrivacyPolicy<Ent>;
|
|
@@ -116,7 +120,7 @@ export interface PrivacyError extends Error {
|
|
|
116
120
|
export declare function Allow(): PrivacyResult;
|
|
117
121
|
export declare function Skip(): PrivacyResult;
|
|
118
122
|
export declare function Deny(): PrivacyResult;
|
|
119
|
-
export declare function DenyWithReason(e: PrivacyError): PrivacyResult;
|
|
123
|
+
export declare function DenyWithReason(e: PrivacyError | string): PrivacyResult;
|
|
120
124
|
export interface PrivacyPolicyRule<TEnt extends Ent = Ent> {
|
|
121
125
|
apply(v: Viewer, ent?: TEnt): Promise<PrivacyResult>;
|
|
122
126
|
}
|
package/core/base.js
CHANGED
|
@@ -30,7 +30,23 @@ 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,
|
package/core/clause.d.ts
CHANGED
|
@@ -12,8 +12,10 @@ declare class simpleClause implements Clause {
|
|
|
12
12
|
protected col: string;
|
|
13
13
|
private value;
|
|
14
14
|
private op;
|
|
15
|
-
|
|
15
|
+
private handleSqliteNull?;
|
|
16
|
+
constructor(col: string, value: any, op: string, handleSqliteNull?: Clause | undefined);
|
|
16
17
|
clause(idx: number): string;
|
|
18
|
+
private sqliteNull;
|
|
17
19
|
values(): any[];
|
|
18
20
|
logValues(): any[];
|
|
19
21
|
instanceKey(): string;
|
|
@@ -27,14 +29,33 @@ declare class compositeClause implements Clause {
|
|
|
27
29
|
logValues(): any[];
|
|
28
30
|
instanceKey(): string;
|
|
29
31
|
}
|
|
30
|
-
export declare function
|
|
31
|
-
export declare function
|
|
32
|
+
export declare function ArrayEq(col: string, value: any): Clause;
|
|
33
|
+
export declare function ArrayNotEq(col: string, value: any): Clause;
|
|
34
|
+
export declare function ArrayGreater(col: string, value: any): Clause;
|
|
35
|
+
export declare function ArrayLess(col: string, value: any): Clause;
|
|
36
|
+
export declare function ArrayGreaterEq(col: string, value: any): Clause;
|
|
37
|
+
export declare function ArrayLessEq(col: string, value: any): Clause;
|
|
38
|
+
export declare function Eq(col: string, value: any): Clause;
|
|
39
|
+
export declare function NotEq(col: string, value: any): Clause;
|
|
32
40
|
export declare function Greater(col: string, value: any): simpleClause;
|
|
33
41
|
export declare function Less(col: string, value: any): simpleClause;
|
|
34
42
|
export declare function GreaterEq(col: string, value: any): simpleClause;
|
|
35
43
|
export declare function LessEq(col: string, value: any): simpleClause;
|
|
36
44
|
export declare function And(...args: Clause[]): compositeClause;
|
|
45
|
+
export declare function AndOptional(...args: (Clause | undefined)[]): Clause;
|
|
37
46
|
export declare function Or(...args: Clause[]): compositeClause;
|
|
38
47
|
export declare function In(col: string, ...values: any): Clause;
|
|
48
|
+
interface TsQuery {
|
|
49
|
+
language: "english" | "french" | "german" | "simple";
|
|
50
|
+
value: string;
|
|
51
|
+
}
|
|
52
|
+
export declare function TsQuery(col: string, val: string | TsQuery): Clause;
|
|
53
|
+
export declare function PlainToTsQuery(col: string, val: string | TsQuery): Clause;
|
|
54
|
+
export declare function PhraseToTsQuery(col: string, val: string | TsQuery): Clause;
|
|
55
|
+
export declare function WebsearchToTsQuery(col: string, val: string | TsQuery): Clause;
|
|
56
|
+
export declare function TsVectorColTsQuery(col: string, val: string | TsQuery): Clause;
|
|
57
|
+
export declare function TsVectorPlainToTsQuery(col: string, val: string | TsQuery): Clause;
|
|
58
|
+
export declare function TsVectorPhraseToTsQuery(col: string, val: string | TsQuery): Clause;
|
|
59
|
+
export declare function TsVectorWebsearchToTsQuery(col: string, val: string | TsQuery): Clause;
|
|
39
60
|
export declare function sensitiveValue(val: any): SensitiveValue;
|
|
40
61
|
export {};
|