@snowtop/ent 0.0.39-alpha9 → 0.0.40-alpha6
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/orchestrator.d.ts +4 -2
- package/action/orchestrator.js +32 -16
- package/core/base.d.ts +3 -1
- package/core/clause.d.ts +4 -0
- package/core/clause.js +37 -3
- package/core/config.d.ts +6 -0
- package/core/config.js +5 -0
- package/core/db.d.ts +3 -3
- package/core/db.js +2 -0
- package/core/ent.d.ts +0 -2
- package/core/ent.js +56 -17
- package/core/loaders/object_loader.d.ts +6 -2
- package/core/loaders/object_loader.js +41 -4
- package/core/viewer.d.ts +1 -0
- package/core/viewer.js +4 -0
- package/index.d.ts +5 -1
- package/index.js +7 -5
- package/package.json +1 -1
- package/parse_schema/parse.d.ts +2 -1
- package/parse_schema/parse.js +1 -0
- package/schema/index.d.ts +1 -1
- package/schema/index.js +4 -1
- package/schema/schema.d.ts +9 -1
- package/schema/schema.js +44 -1
- package/testutils/builder.js +19 -2
- 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 +13 -4
package/action/orchestrator.d.ts
CHANGED
|
@@ -12,7 +12,7 @@ export interface OrchestratorOptions<T extends Ent, TData extends Data> {
|
|
|
12
12
|
builder: Builder<T>;
|
|
13
13
|
action?: Action<T>;
|
|
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
17
|
}
|
|
18
18
|
interface edgeInputDataOpts {
|
|
@@ -57,9 +57,11 @@ export declare class Orchestrator<T extends Ent> {
|
|
|
57
57
|
private getEdgeOperation;
|
|
58
58
|
private buildEdgeOps;
|
|
59
59
|
private throwError;
|
|
60
|
-
private
|
|
60
|
+
private getEntForPrivacyPolicyImpl;
|
|
61
61
|
private getSQLStatementOperation;
|
|
62
62
|
private getWriteOpForSQLStamentOp;
|
|
63
|
+
getPossibleUnsafeEntForPrivacy(): Promise<T | undefined>;
|
|
64
|
+
private getFieldsInfo;
|
|
63
65
|
private validate;
|
|
64
66
|
private triggers;
|
|
65
67
|
private validators;
|
package/action/orchestrator.js
CHANGED
|
@@ -4,7 +4,6 @@ exports.EntChangeset = exports.Orchestrator = exports.edgeDirection = void 0;
|
|
|
4
4
|
const ent_1 = require("../core/ent");
|
|
5
5
|
const schema_1 = require("../schema/schema");
|
|
6
6
|
const action_1 = require("../action");
|
|
7
|
-
const snake_case_1 = require("snake-case");
|
|
8
7
|
const camel_case_1 = require("camel-case");
|
|
9
8
|
const privacy_1 = require("../core/privacy");
|
|
10
9
|
const executor_1 = require("./executor");
|
|
@@ -235,7 +234,7 @@ class Orchestrator {
|
|
|
235
234
|
}
|
|
236
235
|
return new EntCannotDeleteEntError(privacyPolicy, action, this.existingEnt);
|
|
237
236
|
}
|
|
238
|
-
|
|
237
|
+
getEntForPrivacyPolicyImpl(editedData) {
|
|
239
238
|
if (this.actualOperation !== action_1.WriteOperation.Insert) {
|
|
240
239
|
return this.existingEnt;
|
|
241
240
|
}
|
|
@@ -264,6 +263,24 @@ class Orchestrator {
|
|
|
264
263
|
throw new Error("invalid path");
|
|
265
264
|
}
|
|
266
265
|
}
|
|
266
|
+
// if you're doing custom privacy within an action and want to
|
|
267
|
+
// get either the unsafe ent or the existing ent that's being edited
|
|
268
|
+
async getPossibleUnsafeEntForPrivacy() {
|
|
269
|
+
if (this.actualOperation !== action_1.WriteOperation.Insert) {
|
|
270
|
+
return this.existingEnt;
|
|
271
|
+
}
|
|
272
|
+
const { editedData } = await this.getFieldsInfo();
|
|
273
|
+
return this.getEntForPrivacyPolicyImpl(editedData);
|
|
274
|
+
}
|
|
275
|
+
async getFieldsInfo() {
|
|
276
|
+
const action = this.options.action;
|
|
277
|
+
const builder = this.options.builder;
|
|
278
|
+
// future optimization: can get schemaFields to memoize based on different values
|
|
279
|
+
const schemaFields = (0, schema_1.getFields)(this.options.schema);
|
|
280
|
+
const editedFields = await this.options.editedFields();
|
|
281
|
+
let editedData = await this.getFieldsWithDefaultValues(builder, schemaFields, editedFields, action);
|
|
282
|
+
return { editedData, editedFields, schemaFields };
|
|
283
|
+
}
|
|
267
284
|
async validate() {
|
|
268
285
|
// existing ent required for edit or delete operations
|
|
269
286
|
switch (this.actualOperation) {
|
|
@@ -273,11 +290,9 @@ class Orchestrator {
|
|
|
273
290
|
throw new Error(`existing ent required with operation ${this.actualOperation}`);
|
|
274
291
|
}
|
|
275
292
|
}
|
|
293
|
+
const { schemaFields, editedData } = await this.getFieldsInfo();
|
|
276
294
|
const action = this.options.action;
|
|
277
295
|
const builder = this.options.builder;
|
|
278
|
-
// future optimization: can get schemaFields to memoize based on different values
|
|
279
|
-
const schemaFields = (0, schema_1.getFields)(this.options.schema);
|
|
280
|
-
let editedData = await this.getFieldsWithDefaultValues(builder, schemaFields, action);
|
|
281
296
|
// this runs in following phases:
|
|
282
297
|
// * set default fields and pass to builder so the value can be checked by triggers/observers/validators
|
|
283
298
|
// * privacy policy (use unsafe ent if we have it)
|
|
@@ -285,7 +300,7 @@ class Orchestrator {
|
|
|
285
300
|
// * validators
|
|
286
301
|
let privacyPolicy = action?.getPrivacyPolicy();
|
|
287
302
|
if (privacyPolicy) {
|
|
288
|
-
await (0, privacy_1.applyPrivacyPolicyX)(this.options.viewer, privacyPolicy, this.
|
|
303
|
+
await (0, privacy_1.applyPrivacyPolicyX)(this.options.viewer, privacyPolicy, this.getEntForPrivacyPolicyImpl(editedData), this.throwError.bind(this));
|
|
289
304
|
}
|
|
290
305
|
// have to run triggers which update fields first before field and other validators
|
|
291
306
|
// so running this first to build things up
|
|
@@ -294,8 +309,11 @@ class Orchestrator {
|
|
|
294
309
|
await this.triggers(action, builder, triggers);
|
|
295
310
|
}
|
|
296
311
|
let validators = action?.validators || [];
|
|
312
|
+
// not ideal we're calling this twice. fix...
|
|
313
|
+
// needed for now. may need to rewrite some of this?
|
|
314
|
+
const editedFields2 = await this.options.editedFields();
|
|
297
315
|
await Promise.all([
|
|
298
|
-
this.formatAndValidateFields(schemaFields),
|
|
316
|
+
this.formatAndValidateFields(schemaFields, editedFields2),
|
|
299
317
|
this.validators(validators, action, builder),
|
|
300
318
|
]);
|
|
301
319
|
}
|
|
@@ -330,8 +348,7 @@ class Orchestrator {
|
|
|
330
348
|
isBuilder(val) {
|
|
331
349
|
return val.placeholderID !== undefined;
|
|
332
350
|
}
|
|
333
|
-
async getFieldsWithDefaultValues(builder, schemaFields, action) {
|
|
334
|
-
const editedFields = this.options.editedFields();
|
|
351
|
+
async getFieldsWithDefaultValues(builder, schemaFields, editedFields, action) {
|
|
335
352
|
let data = {};
|
|
336
353
|
let defaultData = {};
|
|
337
354
|
let input = action?.getInput() || {};
|
|
@@ -369,12 +386,12 @@ class Orchestrator {
|
|
|
369
386
|
if (field.format) {
|
|
370
387
|
val = field.format(transformed.data[k]);
|
|
371
388
|
}
|
|
372
|
-
let dbKey =
|
|
389
|
+
let dbKey = (0, schema_1.getStorageKey)(field);
|
|
373
390
|
data[dbKey] = val;
|
|
374
391
|
this.defaultFieldsByTSName[(0, camel_case_1.camelCase)(k)] = val;
|
|
375
392
|
// hmm do we need this?
|
|
376
393
|
// TODO how to do this for local tests?
|
|
377
|
-
//
|
|
394
|
+
// this.defaultFieldsByFieldName[k] = val;
|
|
378
395
|
}
|
|
379
396
|
}
|
|
380
397
|
this.actualOperation = this.getWriteOpForSQLStamentOp(transformed.op);
|
|
@@ -387,7 +404,7 @@ class Orchestrator {
|
|
|
387
404
|
for (const [fieldName, field] of schemaFields) {
|
|
388
405
|
let value = editedFields.get(fieldName);
|
|
389
406
|
let defaultValue = undefined;
|
|
390
|
-
let dbKey =
|
|
407
|
+
let dbKey = (0, schema_1.getStorageKey)(field);
|
|
391
408
|
if (value === undefined) {
|
|
392
409
|
if (this.actualOperation === action_1.WriteOperation.Insert) {
|
|
393
410
|
if (field.defaultToViewerOnCreate && field.defaultValueOnCreate) {
|
|
@@ -483,12 +500,11 @@ class Orchestrator {
|
|
|
483
500
|
}
|
|
484
501
|
return value;
|
|
485
502
|
}
|
|
486
|
-
async formatAndValidateFields(schemaFields) {
|
|
503
|
+
async formatAndValidateFields(schemaFields, editedFields) {
|
|
487
504
|
const op = this.actualOperation;
|
|
488
505
|
if (op === action_1.WriteOperation.Delete) {
|
|
489
506
|
return;
|
|
490
507
|
}
|
|
491
|
-
const editedFields = this.options.editedFields();
|
|
492
508
|
// build up data to be saved...
|
|
493
509
|
let data = {};
|
|
494
510
|
let logValues = {};
|
|
@@ -498,7 +514,7 @@ class Orchestrator {
|
|
|
498
514
|
// null allowed
|
|
499
515
|
value = this.defaultFieldsByFieldName[fieldName];
|
|
500
516
|
}
|
|
501
|
-
let dbKey =
|
|
517
|
+
let dbKey = (0, schema_1.getStorageKey)(field);
|
|
502
518
|
value = await this.transformFieldValue(field, dbKey, value);
|
|
503
519
|
if (value !== undefined) {
|
|
504
520
|
data[dbKey] = value;
|
|
@@ -511,7 +527,7 @@ class Orchestrator {
|
|
|
511
527
|
for (const fieldName in this.defaultFieldsByFieldName) {
|
|
512
528
|
const defaultValue = this.defaultFieldsByFieldName[fieldName];
|
|
513
529
|
let field = schemaFields.get(fieldName);
|
|
514
|
-
let dbKey =
|
|
530
|
+
let dbKey = (0, schema_1.getStorageKey)(field);
|
|
515
531
|
// no value, let's just default
|
|
516
532
|
if (data[dbKey] === undefined) {
|
|
517
533
|
const value = await this.transformFieldValue(field, dbKey, defaultValue);
|
package/core/base.d.ts
CHANGED
|
@@ -62,7 +62,7 @@ export interface SelectBaseDataOptions extends DataOptions {
|
|
|
62
62
|
}
|
|
63
63
|
export interface SelectDataOptions extends SelectBaseDataOptions {
|
|
64
64
|
key: string;
|
|
65
|
-
clause?: clause.Clause;
|
|
65
|
+
clause?: clause.Clause | (() => clause.Clause | undefined);
|
|
66
66
|
}
|
|
67
67
|
export interface QueryableDataOptions extends SelectBaseDataOptions, QueryDataOptions {
|
|
68
68
|
}
|
|
@@ -89,9 +89,11 @@ interface LoadableEntOptions<T extends Ent> {
|
|
|
89
89
|
ent: EntConstructor<T>;
|
|
90
90
|
}
|
|
91
91
|
export interface LoadEntOptions<T extends Ent> extends LoadableEntOptions<T>, SelectBaseDataOptions {
|
|
92
|
+
fieldPrivacy?: Map<string, PrivacyPolicy>;
|
|
92
93
|
}
|
|
93
94
|
export interface LoadCustomEntOptions<T extends Ent> extends SelectBaseDataOptions {
|
|
94
95
|
ent: EntConstructor<T>;
|
|
96
|
+
fieldPrivacy?: Map<string, PrivacyPolicy>;
|
|
95
97
|
}
|
|
96
98
|
export interface LoaderInfo {
|
|
97
99
|
tableName: string;
|
package/core/clause.d.ts
CHANGED
|
@@ -53,5 +53,9 @@ export declare function TsQuery(col: string, val: string | TsQuery): Clause;
|
|
|
53
53
|
export declare function PlainToTsQuery(col: string, val: string | TsQuery): Clause;
|
|
54
54
|
export declare function PhraseToTsQuery(col: string, val: string | TsQuery): Clause;
|
|
55
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;
|
|
56
60
|
export declare function sensitiveValue(val: any): SensitiveValue;
|
|
57
61
|
export {};
|
package/core/clause.js
CHANGED
|
@@ -19,7 +19,7 @@ var __importStar = (this && this.__importStar) || function (mod) {
|
|
|
19
19
|
return result;
|
|
20
20
|
};
|
|
21
21
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
22
|
-
exports.sensitiveValue = exports.WebsearchToTsQuery = exports.PhraseToTsQuery = exports.PlainToTsQuery = exports.TsQuery = exports.In = exports.Or = exports.AndOptional = exports.And = exports.LessEq = exports.GreaterEq = exports.Less = exports.Greater = exports.NotEq = exports.Eq = exports.ArrayLessEq = exports.ArrayGreaterEq = exports.ArrayLess = exports.ArrayGreater = exports.ArrayNotEq = exports.ArrayEq = void 0;
|
|
22
|
+
exports.sensitiveValue = exports.TsVectorWebsearchToTsQuery = exports.TsVectorPhraseToTsQuery = exports.TsVectorPlainToTsQuery = exports.TsVectorColTsQuery = exports.WebsearchToTsQuery = exports.PhraseToTsQuery = exports.PlainToTsQuery = exports.TsQuery = exports.In = exports.Or = exports.AndOptional = exports.And = exports.LessEq = exports.GreaterEq = exports.Less = exports.Greater = exports.NotEq = exports.Eq = exports.ArrayLessEq = exports.ArrayGreaterEq = exports.ArrayLess = exports.ArrayGreater = exports.ArrayNotEq = exports.ArrayEq = void 0;
|
|
23
23
|
const db_1 = __importStar(require("./db"));
|
|
24
24
|
function isSensitive(val) {
|
|
25
25
|
return (val !== null &&
|
|
@@ -236,9 +236,10 @@ class compositeClause {
|
|
|
236
236
|
}
|
|
237
237
|
}
|
|
238
238
|
class tsQueryClause {
|
|
239
|
-
constructor(col, val) {
|
|
239
|
+
constructor(col, val, tsVectorCol) {
|
|
240
240
|
this.col = col;
|
|
241
241
|
this.val = val;
|
|
242
|
+
this.tsVectorCol = tsVectorCol;
|
|
242
243
|
}
|
|
243
244
|
isTsQuery(val) {
|
|
244
245
|
return typeof val !== "string";
|
|
@@ -255,8 +256,12 @@ class tsQueryClause {
|
|
|
255
256
|
clause(idx) {
|
|
256
257
|
const { language } = this.getInfo();
|
|
257
258
|
if (db_1.Dialect.Postgres === db_1.default.getDialect()) {
|
|
259
|
+
if (this.tsVectorCol) {
|
|
260
|
+
return `to_tsvector(${this.col}) @@ ${this.getFunction()}('${language}', $${idx})`;
|
|
261
|
+
}
|
|
258
262
|
return `${this.col} @@ ${this.getFunction()}('${language}', $${idx})`;
|
|
259
263
|
}
|
|
264
|
+
// FYI this doesn't actually work for sqlite since different
|
|
260
265
|
return `${this.col} @@ ${this.getFunction()}('${language}', ?)`;
|
|
261
266
|
}
|
|
262
267
|
values() {
|
|
@@ -272,7 +277,10 @@ class tsQueryClause {
|
|
|
272
277
|
}
|
|
273
278
|
instanceKey() {
|
|
274
279
|
const { language, value } = this.getInfo();
|
|
275
|
-
|
|
280
|
+
if (this.tsVectorCol) {
|
|
281
|
+
return `to_tsvector(${this.col})@@${this.getFunction()}:${language}:${value}`;
|
|
282
|
+
}
|
|
283
|
+
return `${this.col}@@${this.getFunction()}:${language}:${value}`;
|
|
276
284
|
}
|
|
277
285
|
}
|
|
278
286
|
class plainToTsQueryClause extends tsQueryClause {
|
|
@@ -383,6 +391,32 @@ function WebsearchToTsQuery(col, val) {
|
|
|
383
391
|
return new websearchTosQueryClause(col, val);
|
|
384
392
|
}
|
|
385
393
|
exports.WebsearchToTsQuery = WebsearchToTsQuery;
|
|
394
|
+
// TsVectorColTsQuery is used when the column is not a tsvector field e.g.
|
|
395
|
+
// when there's an index just on the field and is not a combination of multiple fields
|
|
396
|
+
function TsVectorColTsQuery(col, val) {
|
|
397
|
+
return new tsQueryClause(col, val, true);
|
|
398
|
+
}
|
|
399
|
+
exports.TsVectorColTsQuery = TsVectorColTsQuery;
|
|
400
|
+
// TsVectorPlainToTsQuery is used when the column is not a tsvector field e.g.
|
|
401
|
+
// when there's an index just on the field and is not a combination of multiple fields
|
|
402
|
+
// TODO do these 4 need TsQuery because would be nice to have language?
|
|
403
|
+
// it seems to default to the config of the column
|
|
404
|
+
function TsVectorPlainToTsQuery(col, val) {
|
|
405
|
+
return new plainToTsQueryClause(col, val, true);
|
|
406
|
+
}
|
|
407
|
+
exports.TsVectorPlainToTsQuery = TsVectorPlainToTsQuery;
|
|
408
|
+
// TsVectorPhraseToTsQuery is used when the column is not a tsvector field e.g.
|
|
409
|
+
// when there's an index just on the field and is not a combination of multiple fields
|
|
410
|
+
function TsVectorPhraseToTsQuery(col, val) {
|
|
411
|
+
return new phraseToTsQueryClause(col, val, true);
|
|
412
|
+
}
|
|
413
|
+
exports.TsVectorPhraseToTsQuery = TsVectorPhraseToTsQuery;
|
|
414
|
+
// TsVectorWebsearchToTsQuery is used when the column is not a tsvector field e.g.
|
|
415
|
+
// when there's an index just on the field and is not a combination of multiple fields
|
|
416
|
+
function TsVectorWebsearchToTsQuery(col, val) {
|
|
417
|
+
return new websearchTosQueryClause(col, val, true);
|
|
418
|
+
}
|
|
419
|
+
exports.TsVectorWebsearchToTsQuery = TsVectorWebsearchToTsQuery;
|
|
386
420
|
// TODO would be nice to support this with building blocks but not supporting for now
|
|
387
421
|
// AND: foo & bar,
|
|
388
422
|
// OR: foo | bar
|
package/core/config.d.ts
CHANGED
|
@@ -9,6 +9,10 @@ declare enum graphQLFieldFormat {
|
|
|
9
9
|
LOWER_CAMEL = "lowerCamel",
|
|
10
10
|
SNAKE_CASE = "snake_case"
|
|
11
11
|
}
|
|
12
|
+
declare enum fieldPrivacyEvaluated {
|
|
13
|
+
AT_ENT_LOAD = "at_ent_load",
|
|
14
|
+
ON_DEMAND = "on_demand"
|
|
15
|
+
}
|
|
12
16
|
export interface Config {
|
|
13
17
|
dbConnectionString?: string;
|
|
14
18
|
dbFile?: string;
|
|
@@ -29,6 +33,8 @@ interface CodegenConfig {
|
|
|
29
33
|
defaultGraphQLMutationName?: graphqlMutationName;
|
|
30
34
|
defaultGraphQLFieldFormat?: graphQLFieldFormat;
|
|
31
35
|
schemaSQLFilePath?: boolean;
|
|
36
|
+
databaseToCompareTo?: string;
|
|
37
|
+
fieldPrivacyEvaluated?: fieldPrivacyEvaluated;
|
|
32
38
|
}
|
|
33
39
|
interface PrettierConfig {
|
|
34
40
|
custom?: boolean;
|
package/core/config.js
CHANGED
|
@@ -40,6 +40,11 @@ var graphQLFieldFormat;
|
|
|
40
40
|
graphQLFieldFormat["LOWER_CAMEL"] = "lowerCamel";
|
|
41
41
|
graphQLFieldFormat["SNAKE_CASE"] = "snake_case";
|
|
42
42
|
})(graphQLFieldFormat || (graphQLFieldFormat = {}));
|
|
43
|
+
var fieldPrivacyEvaluated;
|
|
44
|
+
(function (fieldPrivacyEvaluated) {
|
|
45
|
+
fieldPrivacyEvaluated["AT_ENT_LOAD"] = "at_ent_load";
|
|
46
|
+
fieldPrivacyEvaluated["ON_DEMAND"] = "on_demand";
|
|
47
|
+
})(fieldPrivacyEvaluated || (fieldPrivacyEvaluated = {}));
|
|
43
48
|
function setConfig(cfg) {
|
|
44
49
|
if (cfg.log) {
|
|
45
50
|
(0, logger_1.setLogLevels)(cfg.log);
|
package/core/db.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { Pool,
|
|
2
|
-
export interface Database {
|
|
1
|
+
import { Pool, PoolClient, PoolConfig } from "pg";
|
|
2
|
+
export interface Database extends PoolConfig {
|
|
3
3
|
database?: string;
|
|
4
4
|
user?: string;
|
|
5
5
|
password?: string;
|
|
@@ -16,7 +16,7 @@ export declare enum Dialect {
|
|
|
16
16
|
}
|
|
17
17
|
interface DatabaseInfo {
|
|
18
18
|
dialect: Dialect;
|
|
19
|
-
config:
|
|
19
|
+
config: PoolConfig;
|
|
20
20
|
filePath?: string;
|
|
21
21
|
}
|
|
22
22
|
export default class DB {
|
package/core/db.js
CHANGED
package/core/ent.d.ts
CHANGED
|
@@ -22,8 +22,6 @@ export declare type CustomQuery = string | rawQueryOptions | clause.Clause | Que
|
|
|
22
22
|
export declare function loadCustomData(options: SelectBaseDataOptions, query: CustomQuery, context: Context | undefined): Promise<Data[]>;
|
|
23
23
|
export declare function loadDerivedEnt<T extends Ent>(viewer: Viewer, data: Data, loader: new (viewer: Viewer, data: Data) => T): Promise<T | null>;
|
|
24
24
|
export declare function loadDerivedEntX<T extends Ent>(viewer: Viewer, data: Data, loader: new (viewer: Viewer, data: Data) => T): Promise<T>;
|
|
25
|
-
export declare function applyPrivacyPolicyForEnt<T extends Ent>(viewer: Viewer, ent: T | null): Promise<T | null>;
|
|
26
|
-
export declare function applyPrivacyPolicyForEntX<T extends Ent>(viewer: Viewer, ent: T): Promise<T>;
|
|
27
25
|
export declare function loadRowX(options: LoadRowOptions): Promise<Data>;
|
|
28
26
|
export declare function loadRow(options: LoadRowOptions): Promise<Data | null>;
|
|
29
27
|
export declare function performRawQuery(query: string, values: any[], logValues?: any[]): Promise<Data[]>;
|
package/core/ent.js
CHANGED
|
@@ -22,7 +22,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
22
22
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
23
23
|
};
|
|
24
24
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
25
|
-
exports.getEdgeTypeInGroup = exports.applyPrivacyPolicyForRows = exports.applyPrivacyPolicyForRowX = exports.applyPrivacyPolicyForRow = exports.loadNodesByEdge = exports.loadEdgeForID2 = exports.loadRawEdgeCountX = exports.loadUniqueNode = exports.loadUniqueEdge = exports.loadCustomEdges = exports.loadEdges = exports.defaultEdgeQueryOptions = exports.DefaultLimit = exports.loadEdgeDatas = exports.loadEdgeData = exports.assocEdgeLoader = exports.AssocEdgeData = exports.getCursor = exports.AssocEdge = exports.DeleteNodeOperation = exports.deleteRowsSync = exports.deleteRows = exports.editRowSync = exports.editRow = exports.buildUpdateQuery = exports.createRowSync = exports.createRow = exports.buildInsertQuery = exports.EdgeOperation = exports.EditNodeOperation = exports.buildGroupQuery = exports.buildQuery = exports.loadRows = exports.performRawQuery = exports.loadRow = exports.loadRowX = exports.
|
|
25
|
+
exports.getEdgeTypeInGroup = exports.applyPrivacyPolicyForRows = exports.applyPrivacyPolicyForRowX = exports.applyPrivacyPolicyForRow = exports.loadNodesByEdge = exports.loadEdgeForID2 = exports.loadRawEdgeCountX = exports.loadUniqueNode = exports.loadUniqueEdge = exports.loadCustomEdges = exports.loadEdges = exports.defaultEdgeQueryOptions = exports.DefaultLimit = exports.loadEdgeDatas = exports.loadEdgeData = exports.assocEdgeLoader = exports.AssocEdgeData = exports.getCursor = exports.AssocEdge = exports.DeleteNodeOperation = exports.deleteRowsSync = exports.deleteRows = exports.editRowSync = exports.editRow = exports.buildUpdateQuery = exports.createRowSync = exports.createRow = exports.buildInsertQuery = exports.EdgeOperation = exports.EditNodeOperation = exports.buildGroupQuery = exports.buildQuery = exports.loadRows = exports.performRawQuery = exports.loadRow = exports.loadRowX = exports.loadDerivedEntX = exports.loadDerivedEnt = exports.loadCustomData = exports.loadCustomEnts = exports.loadEntsFromClause = exports.loadEnts = exports.loadEntXFromClause = exports.loadEntFromClause = exports.loadEntXViaKey = exports.loadEntX = exports.loadEntViaKey = exports.loadEnt = void 0;
|
|
26
26
|
const db_1 = __importStar(require("./db"));
|
|
27
27
|
const privacy_1 = require("./privacy");
|
|
28
28
|
const clause = __importStar(require("./clause"));
|
|
@@ -139,8 +139,7 @@ async function loadEntXFromClause(viewer, options, clause) {
|
|
|
139
139
|
context: viewer.context,
|
|
140
140
|
};
|
|
141
141
|
const row = await loadRowX(rowOptions);
|
|
142
|
-
|
|
143
|
-
return await applyPrivacyPolicyForEntX(viewer, ent);
|
|
142
|
+
return await applyPrivacyPolicyForRowX(viewer, options, row);
|
|
144
143
|
}
|
|
145
144
|
exports.loadEntXFromClause = loadEntXFromClause;
|
|
146
145
|
async function loadEnts(viewer, options, ...ids) {
|
|
@@ -207,7 +206,7 @@ async function loadCustomEnts(viewer, options, query) {
|
|
|
207
206
|
const result = new Array(rows.length);
|
|
208
207
|
await Promise.all(rows.map(async (row, idx) => {
|
|
209
208
|
const ent = new options.ent(viewer, row);
|
|
210
|
-
let privacyEnt = await applyPrivacyPolicyForEnt(viewer, ent);
|
|
209
|
+
let privacyEnt = await applyPrivacyPolicyForEnt(viewer, ent, row, options);
|
|
211
210
|
if (privacyEnt) {
|
|
212
211
|
result[idx] = privacyEnt;
|
|
213
212
|
}
|
|
@@ -253,30 +252,60 @@ exports.loadCustomData = loadCustomData;
|
|
|
253
252
|
// Derived ents
|
|
254
253
|
async function loadDerivedEnt(viewer, data, loader) {
|
|
255
254
|
const ent = new loader(viewer, data);
|
|
256
|
-
return await applyPrivacyPolicyForEnt(viewer, ent
|
|
255
|
+
return await applyPrivacyPolicyForEnt(viewer, ent, data, {
|
|
256
|
+
ent: loader,
|
|
257
|
+
});
|
|
257
258
|
}
|
|
258
259
|
exports.loadDerivedEnt = loadDerivedEnt;
|
|
259
260
|
async function loadDerivedEntX(viewer, data, loader) {
|
|
260
261
|
const ent = new loader(viewer, data);
|
|
261
|
-
return await applyPrivacyPolicyForEntX(viewer, ent);
|
|
262
|
+
return await applyPrivacyPolicyForEntX(viewer, ent, data, { ent: loader });
|
|
262
263
|
}
|
|
263
264
|
exports.loadDerivedEntX = loadDerivedEntX;
|
|
264
|
-
|
|
265
|
+
// everything calls into this two so should be fine
|
|
266
|
+
// TODO is there a smarter way to not instantiate two objects here?
|
|
267
|
+
async function applyPrivacyPolicyForEnt(viewer, ent, data, fieldPrivacyOptions) {
|
|
265
268
|
if (ent) {
|
|
266
269
|
const visible = await (0, privacy_1.applyPrivacyPolicy)(viewer, ent.privacyPolicy, ent);
|
|
267
|
-
if (visible) {
|
|
268
|
-
return
|
|
270
|
+
if (!visible) {
|
|
271
|
+
return null;
|
|
269
272
|
}
|
|
273
|
+
return doFieldPrivacy(viewer, ent, data, fieldPrivacyOptions);
|
|
270
274
|
}
|
|
271
275
|
return null;
|
|
272
276
|
}
|
|
273
|
-
|
|
274
|
-
async function applyPrivacyPolicyForEntX(viewer, ent) {
|
|
277
|
+
async function applyPrivacyPolicyForEntX(viewer, ent, data, options) {
|
|
275
278
|
// this will throw
|
|
276
279
|
await (0, privacy_1.applyPrivacyPolicyX)(viewer, ent.privacyPolicy, ent);
|
|
280
|
+
return doFieldPrivacy(viewer, ent, data, options);
|
|
281
|
+
}
|
|
282
|
+
async function doFieldPrivacy(viewer, ent, data, options) {
|
|
283
|
+
if (!options.fieldPrivacy) {
|
|
284
|
+
return ent;
|
|
285
|
+
}
|
|
286
|
+
const promises = [];
|
|
287
|
+
let somethingChanged = false;
|
|
288
|
+
for (const [k, policy] of options.fieldPrivacy) {
|
|
289
|
+
promises.push((async () => {
|
|
290
|
+
// don't do anything if key is null or for some reason missing
|
|
291
|
+
const curr = data[k];
|
|
292
|
+
if (curr === null || curr === undefined) {
|
|
293
|
+
return;
|
|
294
|
+
}
|
|
295
|
+
const r = await (0, privacy_1.applyPrivacyPolicy)(viewer, policy, ent);
|
|
296
|
+
if (!r) {
|
|
297
|
+
data[k] = null;
|
|
298
|
+
somethingChanged = true;
|
|
299
|
+
}
|
|
300
|
+
})());
|
|
301
|
+
}
|
|
302
|
+
await Promise.all(promises);
|
|
303
|
+
if (somethingChanged) {
|
|
304
|
+
// have to create new instance
|
|
305
|
+
return new options.ent(viewer, data);
|
|
306
|
+
}
|
|
277
307
|
return ent;
|
|
278
308
|
}
|
|
279
|
-
exports.applyPrivacyPolicyForEntX = applyPrivacyPolicyForEntX;
|
|
280
309
|
function logQuery(query, logValues) {
|
|
281
310
|
(0, logger_1.log)("query", {
|
|
282
311
|
query: query,
|
|
@@ -322,6 +351,8 @@ async function loadRow(options) {
|
|
|
322
351
|
return res.rows[0];
|
|
323
352
|
}
|
|
324
353
|
catch (e) {
|
|
354
|
+
// an example of an error being suppressed
|
|
355
|
+
// another one. TODO https://github.com/lolopinto/ent/issues/862
|
|
325
356
|
(0, logger_1.log)("error", e);
|
|
326
357
|
return null;
|
|
327
358
|
}
|
|
@@ -453,7 +484,16 @@ class EditNodeOperation {
|
|
|
453
484
|
const opts = loader.getOptions();
|
|
454
485
|
let cls = clause.Eq(options.key, id);
|
|
455
486
|
if (opts.clause) {
|
|
456
|
-
|
|
487
|
+
let optionClause;
|
|
488
|
+
if (typeof opts.clause === "function") {
|
|
489
|
+
optionClause = opts.clause();
|
|
490
|
+
}
|
|
491
|
+
else {
|
|
492
|
+
optionClause = opts.clause;
|
|
493
|
+
}
|
|
494
|
+
if (optionClause) {
|
|
495
|
+
cls = clause.And(optionClause, cls);
|
|
496
|
+
}
|
|
457
497
|
}
|
|
458
498
|
const query = buildQuery({
|
|
459
499
|
fields: opts.fields.length ? opts.fields : ["*"],
|
|
@@ -1150,20 +1190,19 @@ async function applyPrivacyPolicyForRow(viewer, options, row) {
|
|
|
1150
1190
|
return null;
|
|
1151
1191
|
}
|
|
1152
1192
|
const ent = new options.ent(viewer, row);
|
|
1153
|
-
return await applyPrivacyPolicyForEnt(viewer, ent);
|
|
1193
|
+
return await applyPrivacyPolicyForEnt(viewer, ent, row, options);
|
|
1154
1194
|
}
|
|
1155
1195
|
exports.applyPrivacyPolicyForRow = applyPrivacyPolicyForRow;
|
|
1156
1196
|
async function applyPrivacyPolicyForRowX(viewer, options, row) {
|
|
1157
1197
|
const ent = new options.ent(viewer, row);
|
|
1158
|
-
return await applyPrivacyPolicyForEntX(viewer, ent);
|
|
1198
|
+
return await applyPrivacyPolicyForEntX(viewer, ent, row, options);
|
|
1159
1199
|
}
|
|
1160
1200
|
exports.applyPrivacyPolicyForRowX = applyPrivacyPolicyForRowX;
|
|
1161
1201
|
async function applyPrivacyPolicyForRows(viewer, rows, options) {
|
|
1162
1202
|
let m = new Map();
|
|
1163
1203
|
// apply privacy logic
|
|
1164
1204
|
await Promise.all(rows.map(async (row) => {
|
|
1165
|
-
|
|
1166
|
-
let privacyEnt = await applyPrivacyPolicyForEnt(viewer, ent);
|
|
1205
|
+
let privacyEnt = await applyPrivacyPolicyForRow(viewer, options, row);
|
|
1167
1206
|
if (privacyEnt) {
|
|
1168
1207
|
m.set(privacyEnt.id, privacyEnt);
|
|
1169
1208
|
}
|
|
@@ -14,11 +14,15 @@ export declare class ObjectLoader<T> implements Loader<T, Data | null> {
|
|
|
14
14
|
loadMany(keys: T[]): Promise<Data[]>;
|
|
15
15
|
prime(data: Data): void;
|
|
16
16
|
}
|
|
17
|
+
interface ObjectLoaderOptions extends SelectDataOptions {
|
|
18
|
+
instanceKey?: string;
|
|
19
|
+
}
|
|
17
20
|
export declare class ObjectLoaderFactory<T> implements LoaderFactory<T, Data | null> {
|
|
18
|
-
options:
|
|
21
|
+
options: ObjectLoaderOptions;
|
|
19
22
|
name: string;
|
|
20
23
|
private toPrime;
|
|
21
|
-
constructor(options:
|
|
24
|
+
constructor(options: ObjectLoaderOptions);
|
|
22
25
|
createLoader(context?: Context): ObjectLoader<T>;
|
|
23
26
|
addToPrime(factory: ObjectLoaderFactory<T>): void;
|
|
24
27
|
}
|
|
28
|
+
export {};
|
|
@@ -45,7 +45,16 @@ function createDataLoader(options) {
|
|
|
45
45
|
let col = options.key;
|
|
46
46
|
let cls = clause.In(col, ...ids);
|
|
47
47
|
if (options.clause) {
|
|
48
|
-
|
|
48
|
+
let optionClause;
|
|
49
|
+
if (typeof options.clause === "function") {
|
|
50
|
+
optionClause = options.clause();
|
|
51
|
+
}
|
|
52
|
+
else {
|
|
53
|
+
optionClause = options.clause;
|
|
54
|
+
}
|
|
55
|
+
if (optionClause) {
|
|
56
|
+
cls = clause.And(optionClause, cls);
|
|
57
|
+
}
|
|
49
58
|
}
|
|
50
59
|
const rowOptions = {
|
|
51
60
|
...options,
|
|
@@ -119,7 +128,16 @@ class ObjectLoader {
|
|
|
119
128
|
}
|
|
120
129
|
let cls = clause.Eq(this.options.key, key);
|
|
121
130
|
if (this.options.clause) {
|
|
122
|
-
|
|
131
|
+
let optionClause;
|
|
132
|
+
if (typeof this.options.clause === "function") {
|
|
133
|
+
optionClause = this.options.clause();
|
|
134
|
+
}
|
|
135
|
+
else {
|
|
136
|
+
optionClause = this.options.clause;
|
|
137
|
+
}
|
|
138
|
+
if (optionClause) {
|
|
139
|
+
cls = clause.And(optionClause, cls);
|
|
140
|
+
}
|
|
123
141
|
}
|
|
124
142
|
const rowOptions = {
|
|
125
143
|
...this.options,
|
|
@@ -137,7 +155,16 @@ class ObjectLoader {
|
|
|
137
155
|
}
|
|
138
156
|
let cls = clause.In(this.options.key, ...keys);
|
|
139
157
|
if (this.options.clause) {
|
|
140
|
-
|
|
158
|
+
let optionClause;
|
|
159
|
+
if (typeof this.options.clause === "function") {
|
|
160
|
+
optionClause = this.options.clause();
|
|
161
|
+
}
|
|
162
|
+
else {
|
|
163
|
+
optionClause = this.options.clause;
|
|
164
|
+
}
|
|
165
|
+
if (optionClause) {
|
|
166
|
+
cls = clause.And(optionClause, cls);
|
|
167
|
+
}
|
|
141
168
|
}
|
|
142
169
|
const rowOptions = {
|
|
143
170
|
...this.options,
|
|
@@ -160,7 +187,17 @@ class ObjectLoaderFactory {
|
|
|
160
187
|
constructor(options) {
|
|
161
188
|
this.options = options;
|
|
162
189
|
this.toPrime = [];
|
|
163
|
-
|
|
190
|
+
// we don't wanna do it here because we want it to be delayed
|
|
191
|
+
let instanceKey = "";
|
|
192
|
+
if (typeof this.options.clause === "function") {
|
|
193
|
+
if (!options.instanceKey) {
|
|
194
|
+
throw new Error(`need to pass an instanceKey to ObjectLoader if clause is a function`);
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
else if (this.options.clause) {
|
|
198
|
+
instanceKey = this.options.clause.instanceKey();
|
|
199
|
+
}
|
|
200
|
+
this.name = `${options.tableName}:${options.key}:${instanceKey}`;
|
|
164
201
|
}
|
|
165
202
|
createLoader(context) {
|
|
166
203
|
return (0, loader_1.getLoader)(this, () => {
|
package/core/viewer.d.ts
CHANGED
|
@@ -17,6 +17,7 @@ export declare class IDViewer implements Viewer {
|
|
|
17
17
|
context?: Context;
|
|
18
18
|
constructor(viewerID: ID, opts?: Partial<IDViewerOptions>);
|
|
19
19
|
constructor(opts: IDViewerOptions);
|
|
20
|
+
setContext(ctx: Context): this;
|
|
20
21
|
viewer(): Promise<Ent | null>;
|
|
21
22
|
instanceKey(): string;
|
|
22
23
|
}
|
package/core/viewer.js
CHANGED
package/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
export * from "./core/base";
|
|
2
|
-
export { loadEnt, loadCustomData, loadCustomEnts, loadEntX, loadEnts, CustomQuery, loadDerivedEnt, loadDerivedEntX, loadEntViaKey, loadEntXViaKey,
|
|
2
|
+
export { loadEnt, loadCustomData, loadCustomEnts, loadEntX, loadEnts, CustomQuery, loadDerivedEnt, loadDerivedEntX, loadEntViaKey, loadEntXViaKey, performRawQuery, loadRowX, loadRow, loadRows, DataOperation, EditNodeOptions, EditNodeOperation, EdgeOperation, DeleteNodeOperation, AssocEdge, AssocEdgeInputOptions, AssocEdgeInput, AssocEdgeData, loadEdgeData, loadEdgeDatas, loadEdges, loadUniqueEdge, loadUniqueNode, loadRawEdgeCountX, loadEdgeForID2, loadNodesByEdge, getEdgeTypeInGroup, } from "./core/ent";
|
|
3
3
|
import DB from "./core/db";
|
|
4
4
|
export * from "./core/loaders";
|
|
5
5
|
export { DB };
|
|
@@ -28,6 +28,10 @@ declare const query: {
|
|
|
28
28
|
PlainToTsQuery: typeof q.PlainToTsQuery;
|
|
29
29
|
PhraseToTsQuery: typeof q.PhraseToTsQuery;
|
|
30
30
|
WebsearchToTsQuery: typeof q.WebsearchToTsQuery;
|
|
31
|
+
TsVectorColTsQuery: typeof q.TsVectorColTsQuery;
|
|
32
|
+
TsVectorPlainToTsQuery: typeof q.TsVectorPlainToTsQuery;
|
|
33
|
+
TsVectorPhraseToTsQuery: typeof q.TsVectorPhraseToTsQuery;
|
|
34
|
+
TsVectorWebsearchToTsQuery: typeof q.TsVectorWebsearchToTsQuery;
|
|
31
35
|
};
|
|
32
36
|
export { query };
|
|
33
37
|
export { RequestContext, ContextCache } from "./core/context";
|
package/index.js
CHANGED
|
@@ -25,8 +25,8 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
25
25
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
26
26
|
};
|
|
27
27
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
28
|
-
exports.DenyIfViewerOutboundEdgeExistsRule = exports.DenyIfViewerInboundEdgeExistsRule = exports.DenyIfEdgeExistsRule = exports.AllowIfViewerOutboundEdgeExistsRule = exports.AllowIfViewerInboundEdgeExistsRule = exports.AllowIfEdgeExistsRule = exports.DenyIfViewerEqualsRule = exports.AllowIfViewerEqualsRule = exports.DenyIfEntPropertyIsRule = exports.AllowIfEntPropertyIsRule = exports.AllowIfViewerIsEntPropertyRule = exports.AllowIfViewerIsRule = exports.AllowIfFuncRule = exports.AllowIfViewerRule = exports.AllowIfHasIdentity = exports.DenyIfLoggedOutRule = exports.DenyIfLoggedInRule = exports.AlwaysDenyRule = exports.AlwaysAllowRule = exports.EntPrivacyError = exports.DB = exports.getEdgeTypeInGroup = exports.loadNodesByEdge = exports.loadEdgeForID2 = exports.loadRawEdgeCountX = exports.loadUniqueNode = exports.loadUniqueEdge = exports.loadEdges = exports.loadEdgeDatas = exports.loadEdgeData = exports.AssocEdgeData = exports.AssocEdge = exports.DeleteNodeOperation = exports.EdgeOperation = exports.EditNodeOperation = exports.loadRows = exports.loadRow = exports.loadRowX = exports.performRawQuery = exports.
|
|
29
|
-
exports.setLogLevels = exports.loadConfig = exports.LoggedOutViewer = exports.IDViewer = exports.ContextCache = exports.query = exports.AllowIfViewerHasIdentityPrivacyPolicy = exports.AllowIfViewerPrivacyPolicy = exports.AllowIfSubPolicyAllowsRule = exports.AllowIfConditionAppliesRule = exports.AlwaysDenyPrivacyPolicy = exports.AlwaysAllowPrivacyPolicy = exports.applyPrivacyPolicyX = exports.applyPrivacyPolicy = exports.DelayedResultRule = exports.DenyIfEntIsVisiblePolicy = exports.AllowIfEntIsVisiblePolicy = exports.DenyIfEntIsNotVisibleRule = exports.DenyIfEntIsVisibleRule = exports.AllowIfEntIsNotVisibleRule = exports.AllowIfEntIsVisibleRule = exports.DenyIfViewerOutboundEdgeDoesNotExistRule =
|
|
28
|
+
exports.DenyIfViewerInboundEdgeDoesNotExistRule = exports.DenyIfEdgeDoesNotExistRule = exports.DenyIfViewerOutboundEdgeExistsRule = exports.DenyIfViewerInboundEdgeExistsRule = exports.DenyIfEdgeExistsRule = exports.AllowIfViewerOutboundEdgeExistsRule = exports.AllowIfViewerInboundEdgeExistsRule = exports.AllowIfEdgeExistsRule = exports.DenyIfViewerEqualsRule = exports.AllowIfViewerEqualsRule = exports.DenyIfEntPropertyIsRule = exports.AllowIfEntPropertyIsRule = exports.AllowIfViewerIsEntPropertyRule = exports.AllowIfViewerIsRule = exports.AllowIfFuncRule = exports.AllowIfViewerRule = exports.AllowIfHasIdentity = exports.DenyIfLoggedOutRule = exports.DenyIfLoggedInRule = exports.AlwaysDenyRule = exports.AlwaysAllowRule = exports.EntPrivacyError = exports.DB = exports.getEdgeTypeInGroup = exports.loadNodesByEdge = exports.loadEdgeForID2 = exports.loadRawEdgeCountX = exports.loadUniqueNode = exports.loadUniqueEdge = exports.loadEdges = exports.loadEdgeDatas = exports.loadEdgeData = exports.AssocEdgeData = exports.AssocEdge = exports.DeleteNodeOperation = exports.EdgeOperation = exports.EditNodeOperation = exports.loadRows = exports.loadRow = exports.loadRowX = exports.performRawQuery = exports.loadEntXViaKey = exports.loadEntViaKey = exports.loadDerivedEntX = exports.loadDerivedEnt = exports.loadEnts = exports.loadEntX = exports.loadCustomEnts = exports.loadCustomData = exports.loadEnt = void 0;
|
|
29
|
+
exports.setLogLevels = exports.loadConfig = exports.LoggedOutViewer = exports.IDViewer = exports.ContextCache = exports.query = exports.AllowIfViewerHasIdentityPrivacyPolicy = exports.AllowIfViewerPrivacyPolicy = exports.AllowIfSubPolicyAllowsRule = exports.AllowIfConditionAppliesRule = exports.AlwaysDenyPrivacyPolicy = exports.AlwaysAllowPrivacyPolicy = exports.applyPrivacyPolicyX = exports.applyPrivacyPolicy = exports.DelayedResultRule = exports.DenyIfEntIsVisiblePolicy = exports.AllowIfEntIsVisiblePolicy = exports.DenyIfEntIsNotVisibleRule = exports.DenyIfEntIsVisibleRule = exports.AllowIfEntIsNotVisibleRule = exports.AllowIfEntIsVisibleRule = exports.DenyIfViewerOutboundEdgeDoesNotExistRule = void 0;
|
|
30
30
|
__exportStar(require("./core/base"), exports);
|
|
31
31
|
var ent_1 = require("./core/ent");
|
|
32
32
|
Object.defineProperty(exports, "loadEnt", { enumerable: true, get: function () { return ent_1.loadEnt; } });
|
|
@@ -38,8 +38,6 @@ Object.defineProperty(exports, "loadDerivedEnt", { enumerable: true, get: functi
|
|
|
38
38
|
Object.defineProperty(exports, "loadDerivedEntX", { enumerable: true, get: function () { return ent_1.loadDerivedEntX; } });
|
|
39
39
|
Object.defineProperty(exports, "loadEntViaKey", { enumerable: true, get: function () { return ent_1.loadEntViaKey; } });
|
|
40
40
|
Object.defineProperty(exports, "loadEntXViaKey", { enumerable: true, get: function () { return ent_1.loadEntXViaKey; } });
|
|
41
|
-
Object.defineProperty(exports, "applyPrivacyPolicyForEnt", { enumerable: true, get: function () { return ent_1.applyPrivacyPolicyForEnt; } });
|
|
42
|
-
Object.defineProperty(exports, "applyPrivacyPolicyForEntX", { enumerable: true, get: function () { return ent_1.applyPrivacyPolicyForEntX; } });
|
|
43
41
|
Object.defineProperty(exports, "performRawQuery", { enumerable: true, get: function () { return ent_1.performRawQuery; } });
|
|
44
42
|
// even these 3 need to change...
|
|
45
43
|
Object.defineProperty(exports, "loadRowX", { enumerable: true, get: function () { return ent_1.loadRowX; } });
|
|
@@ -62,7 +60,7 @@ Object.defineProperty(exports, "getEdgeTypeInGroup", { enumerable: true, get: fu
|
|
|
62
60
|
const db_1 = __importDefault(require("./core/db"));
|
|
63
61
|
exports.DB = db_1.default;
|
|
64
62
|
__exportStar(require("./core/loaders"), exports);
|
|
65
|
-
// TODO figure out if this should be its own
|
|
63
|
+
// TODO figure out if this should be its own import path e.g. @snowtop/ent/privacy
|
|
66
64
|
var privacy_1 = require("./core/privacy");
|
|
67
65
|
Object.defineProperty(exports, "EntPrivacyError", { enumerable: true, get: function () { return privacy_1.EntPrivacyError; } });
|
|
68
66
|
Object.defineProperty(exports, "AlwaysAllowRule", { enumerable: true, get: function () { return privacy_1.AlwaysAllowRule; } });
|
|
@@ -126,6 +124,10 @@ const query = {
|
|
|
126
124
|
PlainToTsQuery: q.PlainToTsQuery,
|
|
127
125
|
PhraseToTsQuery: q.PhraseToTsQuery,
|
|
128
126
|
WebsearchToTsQuery: q.WebsearchToTsQuery,
|
|
127
|
+
TsVectorColTsQuery: q.TsVectorColTsQuery,
|
|
128
|
+
TsVectorPlainToTsQuery: q.TsVectorPlainToTsQuery,
|
|
129
|
+
TsVectorPhraseToTsQuery: q.TsVectorPhraseToTsQuery,
|
|
130
|
+
TsVectorWebsearchToTsQuery: q.TsVectorWebsearchToTsQuery,
|
|
129
131
|
};
|
|
130
132
|
exports.query = query;
|
|
131
133
|
var context_1 = require("./core/context");
|
package/package.json
CHANGED
package/parse_schema/parse.d.ts
CHANGED
|
@@ -38,10 +38,11 @@ interface ProcessedPattern {
|
|
|
38
38
|
assocEdges: ProcessedAssocEdge[];
|
|
39
39
|
fields: ProcessedField[];
|
|
40
40
|
}
|
|
41
|
-
declare type ProcessedField = Omit<Field, "defaultValueOnEdit" | "defaultValueOnCreate"> & {
|
|
41
|
+
declare type ProcessedField = Omit<Field, "defaultValueOnEdit" | "defaultValueOnCreate" | "privacyPolicy"> & {
|
|
42
42
|
hasDefaultValueOnCreate?: boolean;
|
|
43
43
|
hasDefaultValueOnEdit?: boolean;
|
|
44
44
|
patternName?: string;
|
|
45
|
+
hasFieldPrivacy?: boolean;
|
|
45
46
|
};
|
|
46
47
|
interface patternsDict {
|
|
47
48
|
[key: string]: ProcessedPattern;
|
package/parse_schema/parse.js
CHANGED
|
@@ -7,6 +7,7 @@ function processFields(src, patternName) {
|
|
|
7
7
|
let f = { ...field };
|
|
8
8
|
f.hasDefaultValueOnCreate = field.defaultValueOnCreate != undefined;
|
|
9
9
|
f.hasDefaultValueOnEdit = field.defaultValueOnEdit != undefined;
|
|
10
|
+
f.hasFieldPrivacy = field.privacyPolicy !== undefined;
|
|
10
11
|
if (field.polymorphic) {
|
|
11
12
|
// convert boolean into object
|
|
12
13
|
// we keep boolean as an option to keep API simple
|
package/schema/index.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import Schema from "./schema";
|
|
2
2
|
export { Schema };
|
|
3
|
-
export { Field, AssocEdge, AssocEdgeGroup, InverseAssocEdge, Edge, Pattern, DBType, Type, FieldOptions, SchemaConstructor, SchemaInputType, getFields, ActionOperation, Action, EdgeAction, NoFields, Constraint, Index, ConstraintType, ForeignKeyInfo, requiredField, optionalField, UpdateOperation, TransformedUpdateOperation, SQLStatementOperation, getTransformedReadClause, } from "./schema";
|
|
3
|
+
export { Field, AssocEdge, AssocEdgeGroup, InverseAssocEdge, Edge, Pattern, DBType, Type, FieldOptions, SchemaConstructor, SchemaInputType, getFields, getFieldsWithPrivacy, getStorageKey, ActionOperation, Action, EdgeAction, NoFields, Constraint, Index, ConstraintType, ForeignKeyInfo, requiredField, optionalField, UpdateOperation, TransformedUpdateOperation, SQLStatementOperation, getTransformedReadClause, getObjectLoaderProperties, } from "./schema";
|
|
4
4
|
export { Timestamps, Node, BaseEntSchema, BaseEntSchemaWithTZ, } from "./base_schema";
|
|
5
5
|
export * from "./field";
|
|
6
6
|
export * from "./json_field";
|
package/schema/index.js
CHANGED
|
@@ -10,10 +10,12 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
10
10
|
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
11
11
|
};
|
|
12
12
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
13
|
-
exports.BaseEntSchemaWithTZ = exports.BaseEntSchema = exports.Node = exports.Timestamps = exports.getTransformedReadClause = exports.SQLStatementOperation = exports.optionalField = exports.requiredField = exports.ConstraintType = exports.NoFields = exports.ActionOperation = exports.getFields = exports.DBType = void 0;
|
|
13
|
+
exports.BaseEntSchemaWithTZ = exports.BaseEntSchema = exports.Node = exports.Timestamps = exports.getObjectLoaderProperties = exports.getTransformedReadClause = exports.SQLStatementOperation = exports.optionalField = exports.requiredField = exports.ConstraintType = exports.NoFields = exports.ActionOperation = exports.getStorageKey = exports.getFieldsWithPrivacy = exports.getFields = exports.DBType = void 0;
|
|
14
14
|
var schema_1 = require("./schema");
|
|
15
15
|
Object.defineProperty(exports, "DBType", { enumerable: true, get: function () { return schema_1.DBType; } });
|
|
16
16
|
Object.defineProperty(exports, "getFields", { enumerable: true, get: function () { return schema_1.getFields; } });
|
|
17
|
+
Object.defineProperty(exports, "getFieldsWithPrivacy", { enumerable: true, get: function () { return schema_1.getFieldsWithPrivacy; } });
|
|
18
|
+
Object.defineProperty(exports, "getStorageKey", { enumerable: true, get: function () { return schema_1.getStorageKey; } });
|
|
17
19
|
Object.defineProperty(exports, "ActionOperation", { enumerable: true, get: function () { return schema_1.ActionOperation; } });
|
|
18
20
|
Object.defineProperty(exports, "NoFields", { enumerable: true, get: function () { return schema_1.NoFields; } });
|
|
19
21
|
Object.defineProperty(exports, "ConstraintType", { enumerable: true, get: function () { return schema_1.ConstraintType; } });
|
|
@@ -21,6 +23,7 @@ Object.defineProperty(exports, "requiredField", { enumerable: true, get: functio
|
|
|
21
23
|
Object.defineProperty(exports, "optionalField", { enumerable: true, get: function () { return schema_1.optionalField; } });
|
|
22
24
|
Object.defineProperty(exports, "SQLStatementOperation", { enumerable: true, get: function () { return schema_1.SQLStatementOperation; } });
|
|
23
25
|
Object.defineProperty(exports, "getTransformedReadClause", { enumerable: true, get: function () { return schema_1.getTransformedReadClause; } });
|
|
26
|
+
Object.defineProperty(exports, "getObjectLoaderProperties", { enumerable: true, get: function () { return schema_1.getObjectLoaderProperties; } });
|
|
24
27
|
var base_schema_1 = require("./base_schema");
|
|
25
28
|
Object.defineProperty(exports, "Timestamps", { enumerable: true, get: function () { return base_schema_1.Timestamps; } });
|
|
26
29
|
Object.defineProperty(exports, "Node", { enumerable: true, get: function () { return base_schema_1.Node; } });
|
package/schema/schema.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Data, Ent, LoaderInfo, Viewer } from "../core/base";
|
|
1
|
+
import { Data, Ent, LoaderInfo, PrivacyPolicy, Viewer } from "../core/base";
|
|
2
2
|
import { Builder } from "../action/action";
|
|
3
3
|
import { Clause } from "../core/clause";
|
|
4
4
|
export default interface Schema {
|
|
@@ -161,6 +161,7 @@ export interface FieldOptions {
|
|
|
161
161
|
derivedWhenEmbedded?: boolean;
|
|
162
162
|
polymorphic?: boolean | PolymorphicOptions;
|
|
163
163
|
derivedFields?: Field[];
|
|
164
|
+
privacyPolicy?: PrivacyPolicy | (() => PrivacyPolicy);
|
|
164
165
|
}
|
|
165
166
|
export interface PolymorphicOptions {
|
|
166
167
|
types?: string[];
|
|
@@ -179,7 +180,14 @@ export interface SchemaConstructor {
|
|
|
179
180
|
export declare type SchemaInputType = Schema | SchemaConstructor;
|
|
180
181
|
export declare function getSchema(value: SchemaInputType): Schema;
|
|
181
182
|
export declare function getFields(value: SchemaInputType): Map<string, Field>;
|
|
183
|
+
export declare function getStorageKey(field: Field): string;
|
|
184
|
+
export declare function getFieldsWithPrivacy(value: SchemaInputType): Map<string, PrivacyPolicy>;
|
|
182
185
|
export declare function getTransformedReadClause(value: SchemaInputType): Clause | undefined;
|
|
186
|
+
interface objectLoaderOptions {
|
|
187
|
+
clause?: () => Clause | undefined;
|
|
188
|
+
instanceKey?: string;
|
|
189
|
+
}
|
|
190
|
+
export declare function getObjectLoaderProperties(value: SchemaInputType, tableName: string): objectLoaderOptions | undefined;
|
|
183
191
|
export declare function getTransformedUpdateOp<T extends Ent>(value: SchemaInputType, stmt: UpdateOperation<T>): TransformedUpdateOperation<T> | undefined;
|
|
184
192
|
export declare enum ActionOperation {
|
|
185
193
|
Create = 1,
|
package/schema/schema.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.ConstraintType = exports.optionalField = exports.requiredField = exports.NoFields = exports.ActionOperation = exports.getTransformedUpdateOp = exports.getTransformedReadClause = exports.getFields = exports.getSchema = exports.DBType = exports.SQLStatementOperation = void 0;
|
|
3
|
+
exports.ConstraintType = exports.optionalField = exports.requiredField = exports.NoFields = exports.ActionOperation = exports.getTransformedUpdateOp = exports.getObjectLoaderProperties = exports.getTransformedReadClause = exports.getFieldsWithPrivacy = exports.getStorageKey = exports.getFields = exports.getSchema = exports.DBType = exports.SQLStatementOperation = void 0;
|
|
4
|
+
const snake_case_1 = require("snake-case");
|
|
4
5
|
// we also want this transformation to exist on a per-action basis
|
|
5
6
|
// if it exists on an action, we don't do the global schema transformation
|
|
6
7
|
var SQLStatementOperation;
|
|
@@ -72,6 +73,41 @@ function getFields(value) {
|
|
|
72
73
|
return m;
|
|
73
74
|
}
|
|
74
75
|
exports.getFields = getFields;
|
|
76
|
+
function getStorageKey(field) {
|
|
77
|
+
return field.storageKey || (0, snake_case_1.snakeCase)(field.name);
|
|
78
|
+
}
|
|
79
|
+
exports.getStorageKey = getStorageKey;
|
|
80
|
+
// returns a mapping of storage key to field privacy
|
|
81
|
+
function getFieldsWithPrivacy(value) {
|
|
82
|
+
const schema = getSchema(value);
|
|
83
|
+
function addFields(fields) {
|
|
84
|
+
for (const field of fields) {
|
|
85
|
+
const derivedFields = field.derivedFields;
|
|
86
|
+
if (derivedFields !== undefined) {
|
|
87
|
+
addFields(derivedFields);
|
|
88
|
+
}
|
|
89
|
+
if (field.privacyPolicy) {
|
|
90
|
+
let privacyPolicy;
|
|
91
|
+
if (typeof field.privacyPolicy === "function") {
|
|
92
|
+
privacyPolicy = field.privacyPolicy();
|
|
93
|
+
}
|
|
94
|
+
else {
|
|
95
|
+
privacyPolicy = field.privacyPolicy;
|
|
96
|
+
}
|
|
97
|
+
m.set(getStorageKey(field), privacyPolicy);
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
let m = new Map();
|
|
102
|
+
if (schema.patterns) {
|
|
103
|
+
for (const pattern of schema.patterns) {
|
|
104
|
+
addFields(pattern.fields);
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
addFields(schema.fields);
|
|
108
|
+
return m;
|
|
109
|
+
}
|
|
110
|
+
exports.getFieldsWithPrivacy = getFieldsWithPrivacy;
|
|
75
111
|
function getTransformedReadClause(value) {
|
|
76
112
|
const schema = getSchema(value);
|
|
77
113
|
if (!schema.patterns) {
|
|
@@ -87,6 +123,13 @@ function getTransformedReadClause(value) {
|
|
|
87
123
|
return;
|
|
88
124
|
}
|
|
89
125
|
exports.getTransformedReadClause = getTransformedReadClause;
|
|
126
|
+
function getObjectLoaderProperties(value, tableName) {
|
|
127
|
+
return {
|
|
128
|
+
clause: () => getTransformedReadClause(value),
|
|
129
|
+
instanceKey: `${tableName}:transformedReadClause`,
|
|
130
|
+
};
|
|
131
|
+
}
|
|
132
|
+
exports.getObjectLoaderProperties = getObjectLoaderProperties;
|
|
90
133
|
function getTransformedUpdateOp(value, stmt) {
|
|
91
134
|
const schema = getSchema(value);
|
|
92
135
|
if (!schema.patterns) {
|
package/testutils/builder.js
CHANGED
|
@@ -142,16 +142,33 @@ class SimpleBuilder {
|
|
|
142
142
|
ent: schema.ent,
|
|
143
143
|
tableName: tableName,
|
|
144
144
|
fields: [],
|
|
145
|
+
fieldPrivacy: (0, schema_1.getFieldsWithPrivacy)(schema),
|
|
145
146
|
},
|
|
146
147
|
builder: this,
|
|
147
148
|
action: action,
|
|
148
149
|
schema: this.schema,
|
|
149
150
|
editedFields: () => {
|
|
150
|
-
return
|
|
151
|
+
// to simulate what we do in generated builders where we return a new Map
|
|
152
|
+
const m = new Map();
|
|
153
|
+
for (const [k, v] of this.fields) {
|
|
154
|
+
m.set(k, v);
|
|
155
|
+
}
|
|
156
|
+
return m;
|
|
151
157
|
},
|
|
152
158
|
updateInput: (input) => {
|
|
159
|
+
const knownFields = (0, schema_1.getFields)(this.schema);
|
|
153
160
|
for (const k in input) {
|
|
154
|
-
|
|
161
|
+
if (knownFields.has(k)) {
|
|
162
|
+
this.fields.set(k, input[k]);
|
|
163
|
+
}
|
|
164
|
+
else {
|
|
165
|
+
// related to #510. we do camelCase to pass fields in here but fields may be snakeCase and we want that to pass in tests
|
|
166
|
+
// we do camelCase in
|
|
167
|
+
const sc = (0, snake_case_1.snakeCase)(k);
|
|
168
|
+
if (knownFields.has(sc)) {
|
|
169
|
+
this.fields.set(sc, input[k]);
|
|
170
|
+
}
|
|
171
|
+
}
|
|
155
172
|
}
|
|
156
173
|
},
|
|
157
174
|
});
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { Context, Viewer } from "../../core/base";
|
|
2
2
|
import { ContextCache } from "../../core/context";
|
|
3
|
-
import { LoggedOutViewer } from "../../core/viewer";
|
|
4
3
|
export declare class TestContext implements Context {
|
|
4
|
+
constructor(viewer?: Viewer);
|
|
5
5
|
cache: ContextCache;
|
|
6
|
-
viewer:
|
|
6
|
+
viewer: Viewer;
|
|
7
7
|
getViewer(): Viewer;
|
|
8
8
|
}
|
|
@@ -4,9 +4,15 @@ exports.TestContext = void 0;
|
|
|
4
4
|
const context_1 = require("../../core/context");
|
|
5
5
|
const viewer_1 = require("../../core/viewer");
|
|
6
6
|
class TestContext {
|
|
7
|
-
constructor() {
|
|
7
|
+
constructor(viewer) {
|
|
8
8
|
this.cache = new context_1.ContextCache();
|
|
9
9
|
this.viewer = new viewer_1.LoggedOutViewer(this);
|
|
10
|
+
if (viewer) {
|
|
11
|
+
this.viewer = viewer;
|
|
12
|
+
if (viewer.setContext !== undefined) {
|
|
13
|
+
viewer.setContext(this);
|
|
14
|
+
}
|
|
15
|
+
}
|
|
10
16
|
}
|
|
11
17
|
getViewer() {
|
|
12
18
|
return this.viewer;
|
|
@@ -71,10 +71,11 @@ export declare class TempDB {
|
|
|
71
71
|
constructor(tables: CoreConcept[]);
|
|
72
72
|
getDialect(): Dialect;
|
|
73
73
|
getTables(): Map<string, CoreConcept>;
|
|
74
|
-
beforeAll(): Promise<void>;
|
|
74
|
+
beforeAll(setupConnString?: boolean): Promise<void>;
|
|
75
75
|
getSqliteClient(): SqliteDatabase;
|
|
76
76
|
getPostgresClient(): PGClient;
|
|
77
77
|
afterAll(): Promise<void>;
|
|
78
|
+
getDB(): string;
|
|
78
79
|
dropAll(): Promise<void>;
|
|
79
80
|
drop(...tables: string[]): Promise<void>;
|
|
80
81
|
create(...tables: CoreConcept[]): Promise<void>;
|
package/testutils/db/test_db.js
CHANGED
|
@@ -336,7 +336,7 @@ class TempDB {
|
|
|
336
336
|
getTables() {
|
|
337
337
|
return this.tables;
|
|
338
338
|
}
|
|
339
|
-
async beforeAll() {
|
|
339
|
+
async beforeAll(setupConnString = true) {
|
|
340
340
|
if (this.dialect === db_1.Dialect.Postgres) {
|
|
341
341
|
const user = process.env.POSTGRES_USER || "";
|
|
342
342
|
const password = process.env.POSTGRES_PASSWORD || "";
|
|
@@ -348,11 +348,17 @@ class TempDB {
|
|
|
348
348
|
await this.client.connect();
|
|
349
349
|
this.db = randomDB();
|
|
350
350
|
await this.client.query(`CREATE DATABASE ${this.db}`);
|
|
351
|
-
if (
|
|
352
|
-
|
|
351
|
+
if (setupConnString) {
|
|
352
|
+
if (user && password) {
|
|
353
|
+
process.env.DB_CONNECTION_STRING = `postgres://${user}:${password}@localhost:5432/${this.db}`;
|
|
354
|
+
}
|
|
355
|
+
else {
|
|
356
|
+
process.env.DB_CONNECTION_STRING = `postgres://localhost/${this.db}?`;
|
|
357
|
+
}
|
|
353
358
|
}
|
|
354
359
|
else {
|
|
355
|
-
|
|
360
|
+
// will probably be setup via loadConfig
|
|
361
|
+
delete process.env.DB_CONNECTION_STRING;
|
|
356
362
|
}
|
|
357
363
|
this.dbClient = new pg_1.Client({
|
|
358
364
|
host: "localhost",
|
|
@@ -397,6 +403,9 @@ class TempDB {
|
|
|
397
403
|
await this.client.query(`DROP DATABASE ${this.db}`);
|
|
398
404
|
await this.client.end();
|
|
399
405
|
}
|
|
406
|
+
getDB() {
|
|
407
|
+
return this.db;
|
|
408
|
+
}
|
|
400
409
|
async dropAll() {
|
|
401
410
|
for (const [t, _] of this.tables) {
|
|
402
411
|
await this.drop(t);
|