@snowtop/ent 0.1.0-alpha14 → 0.1.0-alpha141
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 +27 -16
- package/action/action.js +22 -7
- package/action/executor.d.ts +16 -3
- package/action/executor.js +88 -21
- package/action/experimental_action.d.ts +25 -16
- package/action/experimental_action.js +35 -9
- package/action/index.d.ts +3 -1
- package/action/index.js +7 -1
- package/action/operations.d.ts +125 -0
- package/action/operations.js +684 -0
- package/action/orchestrator.d.ts +34 -11
- package/action/orchestrator.js +355 -92
- package/action/relative_value.d.ts +47 -0
- package/action/relative_value.js +125 -0
- package/action/transaction.d.ts +10 -0
- package/action/transaction.js +23 -0
- package/auth/auth.d.ts +1 -1
- package/core/base.d.ts +51 -21
- package/core/base.js +7 -1
- package/core/clause.d.ts +85 -40
- package/core/clause.js +375 -64
- package/core/config.d.ts +12 -1
- package/core/config.js +7 -1
- package/core/const.d.ts +3 -0
- package/core/const.js +6 -0
- package/core/context.d.ts +4 -2
- package/core/context.js +20 -2
- package/core/convert.d.ts +1 -1
- package/core/date.js +1 -5
- package/core/db.d.ts +12 -8
- package/core/db.js +18 -8
- package/core/ent.d.ts +66 -93
- package/core/ent.js +517 -577
- package/core/global_schema.d.ts +7 -0
- package/core/global_schema.js +51 -0
- package/core/loaders/assoc_count_loader.d.ts +1 -0
- package/core/loaders/assoc_count_loader.js +10 -2
- package/core/loaders/assoc_edge_loader.d.ts +1 -1
- package/core/loaders/assoc_edge_loader.js +8 -11
- package/core/loaders/index.d.ts +1 -1
- package/core/loaders/index.js +1 -3
- package/core/loaders/index_loader.d.ts +3 -3
- package/core/loaders/loader.d.ts +2 -2
- package/core/loaders/loader.js +5 -5
- package/core/loaders/object_loader.d.ts +30 -9
- package/core/loaders/object_loader.js +225 -78
- package/core/loaders/query_loader.d.ts +6 -12
- package/core/loaders/query_loader.js +52 -11
- package/core/loaders/raw_count_loader.js +5 -1
- package/core/logger.d.ts +1 -1
- package/core/logger.js +1 -0
- package/core/privacy.d.ts +7 -6
- package/core/privacy.js +21 -25
- package/core/query/assoc_query.d.ts +3 -2
- package/core/query/assoc_query.js +9 -1
- package/core/query/custom_clause_query.d.ts +27 -0
- package/core/query/custom_clause_query.js +84 -0
- package/core/query/custom_query.d.ts +17 -2
- package/core/query/custom_query.js +87 -12
- package/core/query/index.d.ts +1 -0
- package/core/query/index.js +3 -1
- package/core/query/query.d.ts +7 -3
- package/core/query/query.js +101 -53
- package/core/query/shared_assoc_test.d.ts +2 -1
- package/core/query/shared_assoc_test.js +35 -45
- package/core/query/shared_test.d.ts +8 -1
- package/core/query/shared_test.js +470 -236
- package/core/viewer.js +1 -1
- package/graphql/graphql.d.ts +52 -19
- package/graphql/graphql.js +174 -136
- package/graphql/graphql_field_helpers.d.ts +7 -1
- package/graphql/graphql_field_helpers.js +21 -1
- package/graphql/index.d.ts +2 -2
- package/graphql/index.js +3 -5
- package/graphql/query/connection_type.d.ts +9 -9
- package/graphql/query/shared_assoc_test.js +1 -1
- package/graphql/query/shared_edge_connection.js +1 -19
- package/graphql/scalars/orderby_direction.d.ts +2 -0
- package/graphql/scalars/orderby_direction.js +15 -0
- package/imports/dataz/example1/_auth.js +128 -47
- package/imports/dataz/example1/_viewer.js +87 -39
- package/imports/index.d.ts +6 -1
- package/imports/index.js +19 -4
- package/index.d.ts +14 -5
- package/index.js +26 -10
- package/package.json +18 -17
- package/parse_schema/parse.d.ts +31 -9
- package/parse_schema/parse.js +156 -13
- package/schema/base_schema.d.ts +9 -3
- package/schema/base_schema.js +12 -0
- package/schema/field.d.ts +78 -21
- package/schema/field.js +231 -71
- package/schema/index.d.ts +2 -2
- package/schema/index.js +5 -1
- package/schema/json_field.d.ts +16 -4
- package/schema/json_field.js +32 -2
- package/schema/schema.d.ts +96 -19
- package/schema/schema.js +11 -13
- package/schema/struct_field.d.ts +15 -3
- package/schema/struct_field.js +117 -22
- package/schema/union_field.d.ts +1 -1
- package/scripts/custom_compiler.js +10 -6
- package/scripts/custom_graphql.js +142 -31
- package/scripts/migrate_v0.1.js +36 -0
- package/scripts/move_types.js +120 -0
- package/scripts/read_schema.js +20 -5
- package/testutils/action/complex_schemas.d.ts +69 -0
- package/testutils/action/complex_schemas.js +405 -0
- package/testutils/builder.d.ts +39 -43
- package/testutils/builder.js +75 -49
- package/testutils/db/fixture.d.ts +10 -0
- package/testutils/db/fixture.js +26 -0
- package/testutils/db/{test_db.d.ts → temp_db.d.ts} +25 -8
- package/testutils/db/{test_db.js → temp_db.js} +224 -47
- package/testutils/db/value.d.ts +7 -0
- package/testutils/db/value.js +251 -0
- package/testutils/db_mock.d.ts +16 -4
- package/testutils/db_mock.js +52 -7
- package/testutils/db_time_zone.d.ts +4 -0
- package/testutils/db_time_zone.js +41 -0
- package/testutils/ent-graphql-tests/index.d.ts +7 -1
- package/testutils/ent-graphql-tests/index.js +52 -23
- package/testutils/fake_comms.js +1 -1
- package/testutils/fake_data/const.d.ts +2 -1
- package/testutils/fake_data/const.js +3 -0
- package/testutils/fake_data/fake_contact.d.ts +7 -3
- package/testutils/fake_data/fake_contact.js +13 -7
- package/testutils/fake_data/fake_event.d.ts +4 -1
- package/testutils/fake_data/fake_event.js +7 -6
- package/testutils/fake_data/fake_tag.d.ts +36 -0
- package/testutils/fake_data/fake_tag.js +89 -0
- package/testutils/fake_data/fake_user.d.ts +8 -5
- package/testutils/fake_data/fake_user.js +16 -15
- package/testutils/fake_data/index.js +5 -1
- package/testutils/fake_data/internal.d.ts +2 -0
- package/testutils/fake_data/internal.js +7 -1
- package/testutils/fake_data/tag_query.d.ts +13 -0
- package/testutils/fake_data/tag_query.js +43 -0
- package/testutils/fake_data/test_helpers.d.ts +11 -4
- package/testutils/fake_data/test_helpers.js +28 -12
- package/testutils/fake_data/user_query.d.ts +11 -4
- package/testutils/fake_data/user_query.js +54 -22
- package/testutils/fake_log.js +1 -1
- package/testutils/parse_sql.d.ts +6 -0
- package/testutils/parse_sql.js +16 -2
- package/testutils/test_edge_global_schema.d.ts +15 -0
- package/testutils/test_edge_global_schema.js +62 -0
- package/testutils/write.d.ts +2 -2
- package/testutils/write.js +33 -7
- package/tsc/ast.d.ts +25 -2
- package/tsc/ast.js +141 -17
- package/tsc/compilerOptions.js +5 -1
- package/tsc/move_generated.d.ts +1 -0
- package/tsc/move_generated.js +164 -0
- package/tsc/transform.d.ts +22 -0
- package/tsc/transform.js +181 -0
- package/tsc/transform_action.d.ts +22 -0
- package/tsc/transform_action.js +183 -0
- package/tsc/transform_ent.d.ts +17 -0
- package/tsc/transform_ent.js +60 -0
- package/tsc/transform_schema.d.ts +27 -0
- package/{scripts → tsc}/transform_schema.js +146 -117
- package/graphql/enums.d.ts +0 -3
- package/graphql/enums.js +0 -25
- package/scripts/move_generated.js +0 -142
- package/scripts/transform_code.js +0 -113
- package/scripts/transform_schema.d.ts +0 -1
- /package/scripts/{move_generated.d.ts → migrate_v0.1.d.ts} +0 -0
- /package/scripts/{transform_code.d.ts → move_types.d.ts} +0 -0
package/core/ent.js
CHANGED
|
@@ -1,7 +1,11 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
3
|
if (k2 === undefined) k2 = k;
|
|
4
|
-
Object.
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
5
9
|
}) : (function(o, m, k, k2) {
|
|
6
10
|
if (k2 === undefined) k2 = k;
|
|
7
11
|
o[k2] = m[k];
|
|
@@ -22,13 +26,13 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
22
26
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
23
27
|
};
|
|
24
28
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
25
|
-
exports.getEdgeTypeInGroup = exports.applyPrivacyPolicyForRows = exports.
|
|
29
|
+
exports.getEdgeTypeInGroup = exports.applyPrivacyPolicyForRows = exports.applyPrivacyPolicyForRow = exports.loadNodesByEdge = exports.loadEdgeForID2 = exports.loadRawEdgeCountX = exports.loadUniqueNode = exports.loadUniqueEdge = exports.loadCustomEdges = exports.getEdgeClauseAndFields = exports.loadEdges = exports.DefaultLimit = exports.loadEdgeDatas = exports.loadEdgeData = exports.assocEdgeLoader = exports.AssocEdgeData = exports.getCursor = exports.AssocEdge = exports.deleteRowsSync = exports.deleteRows = exports.editRowSync = exports.editRow = exports.buildUpdateQuery = exports.createRowSync = exports.createRow = exports.buildInsertQuery = exports.buildGroupQuery = exports.buildQuery = exports.loadRows = exports.performRawQuery = exports.___setLogQueryErrorWithError = exports.loadRow = exports.loadRowX = exports.logQuery = exports.loadDerivedEntX = exports.loadDerivedEnt = exports.loadCustomCount = exports.loadCustomData = exports.loadCustomEnts = exports.loadEntsFromClause = exports.loadEntsList = exports.loadEnts = exports.loadEntXFromClause = exports.loadEntFromClause = exports.loadEntXViaKey = exports.loadEntX = exports.loadEntViaKey = exports.loadEnt = exports.getEntKey = void 0;
|
|
26
30
|
const db_1 = __importStar(require("./db"));
|
|
27
31
|
const privacy_1 = require("./privacy");
|
|
28
32
|
const clause = __importStar(require("./clause"));
|
|
29
|
-
const action_1 = require("../action");
|
|
30
33
|
const logger_1 = require("./logger");
|
|
31
34
|
const dataloader_1 = __importDefault(require("dataloader"));
|
|
35
|
+
const global_schema_1 = require("./global_schema");
|
|
32
36
|
// TODO kill this and createDataLoader
|
|
33
37
|
class cacheMap {
|
|
34
38
|
constructor(options) {
|
|
@@ -38,7 +42,7 @@ class cacheMap {
|
|
|
38
42
|
get(key) {
|
|
39
43
|
const ret = this.m.get(key);
|
|
40
44
|
if (ret) {
|
|
41
|
-
(0, logger_1.log)("
|
|
45
|
+
(0, logger_1.log)("cache", {
|
|
42
46
|
"dataloader-cache-hit": key,
|
|
43
47
|
"tableName": this.options.tableName,
|
|
44
48
|
});
|
|
@@ -55,12 +59,41 @@ class cacheMap {
|
|
|
55
59
|
return this.m.clear();
|
|
56
60
|
}
|
|
57
61
|
}
|
|
62
|
+
class entCacheMap {
|
|
63
|
+
constructor(viewer, options) {
|
|
64
|
+
this.viewer = viewer;
|
|
65
|
+
this.options = options;
|
|
66
|
+
this.m = new Map();
|
|
67
|
+
this.logEnabled = false;
|
|
68
|
+
this.logEnabled = (0, logger_1.logEnabled)("cache");
|
|
69
|
+
}
|
|
70
|
+
get(id) {
|
|
71
|
+
const ret = this.m.get(id);
|
|
72
|
+
if (this.logEnabled && ret) {
|
|
73
|
+
const key = getEntKey(this.viewer, id, this.options);
|
|
74
|
+
(0, logger_1.log)("cache", {
|
|
75
|
+
"ent-cache-hit": key,
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
return ret;
|
|
79
|
+
}
|
|
80
|
+
set(key, value) {
|
|
81
|
+
return this.m.set(key, value);
|
|
82
|
+
}
|
|
83
|
+
delete(key) {
|
|
84
|
+
return this.m.delete(key);
|
|
85
|
+
}
|
|
86
|
+
clear() {
|
|
87
|
+
return this.m.clear();
|
|
88
|
+
}
|
|
89
|
+
}
|
|
58
90
|
function createDataLoader(options) {
|
|
59
91
|
const loaderOptions = {};
|
|
60
92
|
// if query logging is enabled, we should log what's happening with loader
|
|
61
93
|
if ((0, logger_1.logEnabled)("query")) {
|
|
62
94
|
loaderOptions.cacheMap = new cacheMap(options);
|
|
63
95
|
}
|
|
96
|
+
// something here brokwn with strict:true
|
|
64
97
|
return new dataloader_1.default(async (ids) => {
|
|
65
98
|
if (!ids.length) {
|
|
66
99
|
return [];
|
|
@@ -84,28 +117,151 @@ function createDataLoader(options) {
|
|
|
84
117
|
return result;
|
|
85
118
|
}, loaderOptions);
|
|
86
119
|
}
|
|
87
|
-
//
|
|
120
|
+
// used to wrap errors that would eventually be thrown in ents
|
|
121
|
+
// not an Error because DataLoader automatically rejects that
|
|
122
|
+
class ErrorWrapper {
|
|
123
|
+
constructor(error) {
|
|
124
|
+
this.error = error;
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
function createEntLoader(viewer, options, map) {
|
|
128
|
+
// share the cache across loaders even if we create a new instance
|
|
129
|
+
const loaderOptions = {};
|
|
130
|
+
loaderOptions.cacheMap = map;
|
|
131
|
+
return new dataloader_1.default(async (ids) => {
|
|
132
|
+
if (!ids.length) {
|
|
133
|
+
return [];
|
|
134
|
+
}
|
|
135
|
+
let result = [];
|
|
136
|
+
const tableName = options.loaderFactory.options?.tableName;
|
|
137
|
+
const loader = options.loaderFactory.createLoader(viewer.context);
|
|
138
|
+
const rows = await loader.loadMany(ids);
|
|
139
|
+
// this is a loader which should return the same order based on passed-in ids
|
|
140
|
+
// so let's depend on that...
|
|
141
|
+
for (let idx = 0; idx < rows.length; idx++) {
|
|
142
|
+
const row = rows[idx];
|
|
143
|
+
// db error
|
|
144
|
+
if (row instanceof Error) {
|
|
145
|
+
result[idx] = row;
|
|
146
|
+
continue;
|
|
147
|
+
}
|
|
148
|
+
else if (!row) {
|
|
149
|
+
if (tableName) {
|
|
150
|
+
result[idx] = new ErrorWrapper(new Error(`couldn't find row for value ${ids[idx]} in table ${tableName}`));
|
|
151
|
+
}
|
|
152
|
+
else {
|
|
153
|
+
result[idx] = new ErrorWrapper(new Error(`couldn't find row for value ${ids[idx]}`));
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
else {
|
|
157
|
+
const r = await applyPrivacyPolicyForRowImpl(viewer, options, row);
|
|
158
|
+
if (r instanceof Error) {
|
|
159
|
+
result[idx] = new ErrorWrapper(r);
|
|
160
|
+
}
|
|
161
|
+
else {
|
|
162
|
+
result[idx] = r;
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
return result;
|
|
167
|
+
}, loaderOptions);
|
|
168
|
+
}
|
|
169
|
+
class EntLoader {
|
|
170
|
+
constructor(viewer, options) {
|
|
171
|
+
this.viewer = viewer;
|
|
172
|
+
this.options = options;
|
|
173
|
+
this.map = new entCacheMap(viewer, options);
|
|
174
|
+
this.loader = createEntLoader(this.viewer, this.options, this.map);
|
|
175
|
+
}
|
|
176
|
+
getMap() {
|
|
177
|
+
return this.map;
|
|
178
|
+
}
|
|
179
|
+
async load(id) {
|
|
180
|
+
return this.loader.load(id);
|
|
181
|
+
}
|
|
182
|
+
async loadMany(ids) {
|
|
183
|
+
return this.loader.loadMany(ids);
|
|
184
|
+
}
|
|
185
|
+
prime(id, ent) {
|
|
186
|
+
this.loader.prime(id, ent);
|
|
187
|
+
}
|
|
188
|
+
clear(id) {
|
|
189
|
+
this.loader.clear(id);
|
|
190
|
+
}
|
|
191
|
+
clearAll() {
|
|
192
|
+
this.loader.clearAll();
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
function getEntLoader(viewer, options) {
|
|
196
|
+
if (!viewer.context?.cache) {
|
|
197
|
+
return new EntLoader(viewer, options);
|
|
198
|
+
}
|
|
199
|
+
const name = `ent-loader:${viewer.instanceKey()}:${options.loaderFactory.name}`;
|
|
200
|
+
return viewer.context.cache.getLoaderWithLoadMany(name, () => new EntLoader(viewer, options));
|
|
201
|
+
}
|
|
202
|
+
function getEntKey(viewer, id, options) {
|
|
203
|
+
return `${viewer.instanceKey()}:${options.loaderFactory.name}:${id}`;
|
|
204
|
+
}
|
|
205
|
+
exports.getEntKey = getEntKey;
|
|
88
206
|
async function loadEnt(viewer, id, options) {
|
|
89
|
-
|
|
90
|
-
|
|
207
|
+
if (typeof id !== "string" &&
|
|
208
|
+
typeof id !== "number" &&
|
|
209
|
+
typeof id !== "bigint") {
|
|
210
|
+
throw new Error(`invalid id ${id} passed to loadEnt`);
|
|
211
|
+
}
|
|
212
|
+
const r = await getEntLoader(viewer, options).load(id);
|
|
213
|
+
return r instanceof ErrorWrapper ? null : r;
|
|
91
214
|
}
|
|
92
215
|
exports.loadEnt = loadEnt;
|
|
216
|
+
async function applyPrivacyPolicyForRowAndStoreInEntLoader(viewer, row, options,
|
|
217
|
+
// can pass in loader when calling this for multi-id cases...
|
|
218
|
+
loader) {
|
|
219
|
+
if (!loader) {
|
|
220
|
+
loader = getEntLoader(viewer, options);
|
|
221
|
+
}
|
|
222
|
+
// TODO every row.id needs to be audited...
|
|
223
|
+
// https://github.com/lolopinto/ent/issues/1064
|
|
224
|
+
const id = row.id;
|
|
225
|
+
// we should check the ent loader cache to see if this is already there
|
|
226
|
+
// TODO hmm... we eventually need a custom data-loader for this too so that it's all done correctly if there's a complicated fetch deep down in graphql
|
|
227
|
+
const result = loader.getMap().get(id);
|
|
228
|
+
if (result !== undefined) {
|
|
229
|
+
return result;
|
|
230
|
+
}
|
|
231
|
+
const r = await applyPrivacyPolicyForRowImpl(viewer, options, row);
|
|
232
|
+
if (r instanceof Error) {
|
|
233
|
+
loader.prime(id, new ErrorWrapper(r));
|
|
234
|
+
return new ErrorWrapper(r);
|
|
235
|
+
}
|
|
236
|
+
else {
|
|
237
|
+
loader.prime(id, r);
|
|
238
|
+
return r;
|
|
239
|
+
}
|
|
240
|
+
}
|
|
93
241
|
// this is the same implementation-wise (right now) as loadEnt. it's just clearer that it's not loaded via ID.
|
|
94
242
|
// used for load via email address etc
|
|
95
243
|
async function loadEntViaKey(viewer, key, options) {
|
|
96
244
|
const row = await options.loaderFactory
|
|
97
245
|
.createLoader(viewer.context)
|
|
98
246
|
.load(key);
|
|
99
|
-
|
|
247
|
+
if (!row) {
|
|
248
|
+
return null;
|
|
249
|
+
}
|
|
250
|
+
const r = await applyPrivacyPolicyForRowAndStoreInEntLoader(viewer, row, options);
|
|
251
|
+
return r instanceof ErrorWrapper ? null : r;
|
|
100
252
|
}
|
|
101
253
|
exports.loadEntViaKey = loadEntViaKey;
|
|
102
254
|
async function loadEntX(viewer, id, options) {
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
throw new Error(
|
|
255
|
+
if (typeof id !== "string" &&
|
|
256
|
+
typeof id !== "number" &&
|
|
257
|
+
typeof id !== "bigint") {
|
|
258
|
+
throw new Error(`invalid id ${id} passed to loadEntX`);
|
|
107
259
|
}
|
|
108
|
-
|
|
260
|
+
const r = await getEntLoader(viewer, options).load(id);
|
|
261
|
+
if (r instanceof ErrorWrapper) {
|
|
262
|
+
throw r.error;
|
|
263
|
+
}
|
|
264
|
+
return r;
|
|
109
265
|
}
|
|
110
266
|
exports.loadEntX = loadEntX;
|
|
111
267
|
async function loadEntXViaKey(viewer, key, options) {
|
|
@@ -116,9 +272,16 @@ async function loadEntXViaKey(viewer, key, options) {
|
|
|
116
272
|
// todo make this better
|
|
117
273
|
throw new Error(`${options.loaderFactory.name}: couldn't find row for value ${key}`);
|
|
118
274
|
}
|
|
119
|
-
|
|
275
|
+
const r = await applyPrivacyPolicyForRowAndStoreInEntLoader(viewer, row, options);
|
|
276
|
+
if (r instanceof ErrorWrapper) {
|
|
277
|
+
throw r.error;
|
|
278
|
+
}
|
|
279
|
+
return r;
|
|
120
280
|
}
|
|
121
281
|
exports.loadEntXViaKey = loadEntXViaKey;
|
|
282
|
+
/**
|
|
283
|
+
* @deprecated use loadCustomEnts
|
|
284
|
+
*/
|
|
122
285
|
async function loadEntFromClause(viewer, options, clause) {
|
|
123
286
|
const rowOptions = {
|
|
124
287
|
...options,
|
|
@@ -126,12 +289,18 @@ async function loadEntFromClause(viewer, options, clause) {
|
|
|
126
289
|
context: viewer.context,
|
|
127
290
|
};
|
|
128
291
|
const row = await loadRow(rowOptions);
|
|
129
|
-
|
|
292
|
+
if (row === null) {
|
|
293
|
+
return null;
|
|
294
|
+
}
|
|
295
|
+
return applyPrivacyPolicyForRow(viewer, options, row);
|
|
130
296
|
}
|
|
131
297
|
exports.loadEntFromClause = loadEntFromClause;
|
|
132
298
|
// same as loadEntFromClause
|
|
133
299
|
// only works for ents where primary key is "id"
|
|
134
300
|
// use loadEnt with a loaderFactory if different
|
|
301
|
+
/**
|
|
302
|
+
* @deprecated use loadCustomEnts
|
|
303
|
+
*/
|
|
135
304
|
async function loadEntXFromClause(viewer, options, clause) {
|
|
136
305
|
const rowOptions = {
|
|
137
306
|
...options,
|
|
@@ -146,37 +315,19 @@ async function loadEnts(viewer, options, ...ids) {
|
|
|
146
315
|
if (!ids.length) {
|
|
147
316
|
return new Map();
|
|
148
317
|
}
|
|
149
|
-
|
|
150
|
-
let rows = [];
|
|
151
|
-
// TODO loadMany everywhere
|
|
152
|
-
const l = options.loaderFactory.createLoader(viewer.context);
|
|
153
|
-
if (l.loadMany) {
|
|
154
|
-
loaded = true;
|
|
155
|
-
rows = await l.loadMany(ids);
|
|
156
|
-
}
|
|
157
|
-
// TODO rewrite all of this
|
|
318
|
+
// result
|
|
158
319
|
let m = new Map();
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
continue;
|
|
164
|
-
}
|
|
165
|
-
if (row instanceof Error) {
|
|
166
|
-
throw row;
|
|
167
|
-
}
|
|
168
|
-
rows2.push(row);
|
|
320
|
+
const ret = await getEntLoader(viewer, options).loadMany(ids);
|
|
321
|
+
for (const r of ret) {
|
|
322
|
+
if (r instanceof Error) {
|
|
323
|
+
throw r;
|
|
169
324
|
}
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
m
|
|
174
|
-
// this is always "id" if not using an ObjectLoaderFactory
|
|
175
|
-
clause.In("id", ...ids), options);
|
|
325
|
+
if (r instanceof ErrorWrapper) {
|
|
326
|
+
continue;
|
|
327
|
+
}
|
|
328
|
+
m.set(r.id, r);
|
|
176
329
|
}
|
|
177
330
|
return m;
|
|
178
|
-
// TODO do we want to change this to be a map not a list so that it's easy to check for existence?
|
|
179
|
-
// TODO eventually this should be doing a cache then db queyr and maybe depend on dataloader to get all the results at once
|
|
180
331
|
}
|
|
181
332
|
exports.loadEnts = loadEnts;
|
|
182
333
|
// calls loadEnts and returns the results sorted in the order they were passed in
|
|
@@ -195,6 +346,9 @@ async function loadEntsList(viewer, options, ...ids) {
|
|
|
195
346
|
exports.loadEntsList = loadEntsList;
|
|
196
347
|
// we return a map here so that any sorting for queries that exist
|
|
197
348
|
// can be done in O(N) time
|
|
349
|
+
/**
|
|
350
|
+
* @deperecated use loadCustomEnts
|
|
351
|
+
*/
|
|
198
352
|
async function loadEntsFromClause(viewer, clause, options) {
|
|
199
353
|
const rowOptions = {
|
|
200
354
|
...options,
|
|
@@ -202,65 +356,129 @@ async function loadEntsFromClause(viewer, clause, options) {
|
|
|
202
356
|
context: viewer.context,
|
|
203
357
|
};
|
|
204
358
|
const rows = await loadRows(rowOptions);
|
|
205
|
-
return
|
|
359
|
+
return applyPrivacyPolicyForRowsDeprecated(viewer, rows, options);
|
|
206
360
|
}
|
|
207
361
|
exports.loadEntsFromClause = loadEntsFromClause;
|
|
208
362
|
async function loadCustomEnts(viewer, options, query) {
|
|
209
363
|
const rows = await loadCustomData(options, query, viewer.context);
|
|
210
|
-
|
|
211
|
-
await Promise.all(rows.map(async (row, idx) => {
|
|
212
|
-
const ent = new options.ent(viewer, row);
|
|
213
|
-
let privacyEnt = await applyPrivacyPolicyForEnt(viewer, ent, row, options);
|
|
214
|
-
if (privacyEnt) {
|
|
215
|
-
result[idx] = privacyEnt;
|
|
216
|
-
}
|
|
217
|
-
}));
|
|
218
|
-
// filter ents that aren't visible because of privacy
|
|
219
|
-
return result.filter((r) => r !== undefined);
|
|
364
|
+
return applyPrivacyPolicyForRows(viewer, rows, options);
|
|
220
365
|
}
|
|
221
366
|
exports.loadCustomEnts = loadCustomEnts;
|
|
222
367
|
function isClause(opts) {
|
|
223
368
|
const cls = opts;
|
|
224
369
|
return cls.clause !== undefined && cls.values !== undefined;
|
|
225
370
|
}
|
|
226
|
-
function
|
|
371
|
+
function isParameterizedQuery(opts) {
|
|
227
372
|
return opts.query !== undefined;
|
|
228
373
|
}
|
|
374
|
+
/**
|
|
375
|
+
* Note that if there's default read transformations (e.g. soft delete) and a clause is passed in
|
|
376
|
+
* either as Clause or QueryDataOptions without {disableTransformations: true}, the default transformation
|
|
377
|
+
* (e.g. soft delete) is applied.
|
|
378
|
+
*
|
|
379
|
+
* Passing a full SQL string or Paramterized SQL string doesn't apply it and the given string is sent to the
|
|
380
|
+
* database as written.
|
|
381
|
+
*
|
|
382
|
+
* e.g.
|
|
383
|
+
* Foo.loadCustom(opts, 'SELECT * FROM foo') // doesn't change the query
|
|
384
|
+
* Foo.loadCustom(opts, { query: 'SELECT * FROM foo WHERE id = ?', values: [1]}) // doesn't change the query
|
|
385
|
+
* Foo.loadCustom(opts, query.Eq('time', Date.now())) // changes the query
|
|
386
|
+
* Foo.loadCustom(opts, {
|
|
387
|
+
* clause: query.LessEq('time', Date.now()),
|
|
388
|
+
* limit: 100,
|
|
389
|
+
* orderby: 'time',
|
|
390
|
+
* }) // changes the query
|
|
391
|
+
* Foo.loadCustom(opts, {
|
|
392
|
+
* clause: query.LessEq('time', Date.now()),
|
|
393
|
+
* limit: 100,
|
|
394
|
+
* orderby: 'time',
|
|
395
|
+
* disableTransformations: false
|
|
396
|
+
* }) // doesn't change the query
|
|
397
|
+
*
|
|
398
|
+
* For queries that pass in a clause, we batch them with an underlying dataloader so that multiple queries with the same clause
|
|
399
|
+
* or parallel queries with the same clause are batched together.
|
|
400
|
+
*
|
|
401
|
+
* If a raw or parameterized query is passed in, we don't attempt to batch them together and they're executed as is.
|
|
402
|
+
* If you end up with a scenario where you may need to coalesce or batch (non-clause) queries here, you should use some kind of memoization here.
|
|
403
|
+
*/
|
|
229
404
|
async function loadCustomData(options, query, context) {
|
|
405
|
+
const rows = await loadCustomDataImpl(options, query, context);
|
|
406
|
+
// prime the data so that subsequent fetches of the row with this id are a cache hit.
|
|
407
|
+
if (options.prime) {
|
|
408
|
+
const loader = options.loaderFactory.createLoader(context);
|
|
409
|
+
if (isPrimableLoader(loader) && loader.primeAll !== undefined) {
|
|
410
|
+
for (const row of rows) {
|
|
411
|
+
loader.primeAll(row);
|
|
412
|
+
}
|
|
413
|
+
}
|
|
414
|
+
}
|
|
415
|
+
return rows;
|
|
416
|
+
}
|
|
417
|
+
exports.loadCustomData = loadCustomData;
|
|
418
|
+
// NOTE: if you use a raw query or paramterized query with this,
|
|
419
|
+
// you should use `SELECT count(*) as count...`
|
|
420
|
+
async function loadCustomCount(options, query, context) {
|
|
421
|
+
// if clause, we'll use the loader and strong typing/coalescing it provides
|
|
422
|
+
if (typeof query !== "string" && isClause(query)) {
|
|
423
|
+
return options.loaderFactory.createCountLoader(context).load(query);
|
|
424
|
+
}
|
|
425
|
+
const rows = await loadCustomDataImpl({
|
|
426
|
+
...options,
|
|
427
|
+
fields: ["count(1) as count"],
|
|
428
|
+
}, query, context);
|
|
429
|
+
if (rows.length) {
|
|
430
|
+
return parseInt(rows[0].count);
|
|
431
|
+
}
|
|
432
|
+
return 0;
|
|
433
|
+
}
|
|
434
|
+
exports.loadCustomCount = loadCustomCount;
|
|
435
|
+
function isPrimableLoader(loader) {
|
|
436
|
+
return loader != undefined;
|
|
437
|
+
}
|
|
438
|
+
async function loadCustomDataImpl(options, query, context) {
|
|
230
439
|
if (typeof query === "string") {
|
|
231
440
|
// no caching, perform raw query
|
|
232
|
-
return
|
|
441
|
+
return performRawQuery(query, [], []);
|
|
233
442
|
}
|
|
234
443
|
else if (isClause(query)) {
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
context: context,
|
|
240
|
-
});
|
|
444
|
+
const r = await options.loaderFactory
|
|
445
|
+
.createTypedLoader(context)
|
|
446
|
+
.load(query);
|
|
447
|
+
return r;
|
|
241
448
|
}
|
|
242
|
-
else if (
|
|
449
|
+
else if (isParameterizedQuery(query)) {
|
|
243
450
|
// no caching, perform raw query
|
|
244
|
-
return
|
|
451
|
+
return performRawQuery(query.query, query.values || [], query.logValues);
|
|
245
452
|
}
|
|
246
453
|
else {
|
|
247
454
|
// this will have rudimentary caching but nothing crazy
|
|
248
|
-
|
|
455
|
+
let cls = query.clause;
|
|
456
|
+
if (!query.disableTransformations) {
|
|
457
|
+
cls = clause.getCombinedClause(options.loaderFactory.options, query.clause);
|
|
458
|
+
}
|
|
459
|
+
return loadRows({
|
|
249
460
|
...query,
|
|
250
461
|
...options,
|
|
251
462
|
context: context,
|
|
463
|
+
// @ts-expect-error
|
|
464
|
+
clause: cls,
|
|
252
465
|
});
|
|
253
466
|
}
|
|
254
467
|
}
|
|
255
|
-
exports.loadCustomData = loadCustomData;
|
|
256
468
|
// Derived ents
|
|
469
|
+
// no ent caching
|
|
257
470
|
async function loadDerivedEnt(viewer, data, loader) {
|
|
258
471
|
const ent = new loader(viewer, data);
|
|
259
|
-
|
|
472
|
+
const r = await applyPrivacyPolicyForEnt(viewer, ent, data, {
|
|
260
473
|
ent: loader,
|
|
261
474
|
});
|
|
475
|
+
if (r instanceof Error) {
|
|
476
|
+
return null;
|
|
477
|
+
}
|
|
478
|
+
return r;
|
|
262
479
|
}
|
|
263
480
|
exports.loadDerivedEnt = loadDerivedEnt;
|
|
481
|
+
// won't have caching yet either
|
|
264
482
|
async function loadDerivedEntX(viewer, data, loader) {
|
|
265
483
|
const ent = new loader(viewer, data);
|
|
266
484
|
return await applyPrivacyPolicyForEntX(viewer, ent, data, { ent: loader });
|
|
@@ -269,19 +487,21 @@ exports.loadDerivedEntX = loadDerivedEntX;
|
|
|
269
487
|
// everything calls into this two so should be fine
|
|
270
488
|
// TODO is there a smarter way to not instantiate two objects here?
|
|
271
489
|
async function applyPrivacyPolicyForEnt(viewer, ent, data, fieldPrivacyOptions) {
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
if (!visible) {
|
|
275
|
-
return null;
|
|
276
|
-
}
|
|
490
|
+
const error = await (0, privacy_1.applyPrivacyPolicyImpl)(viewer, ent.getPrivacyPolicy(), ent);
|
|
491
|
+
if (error === null) {
|
|
277
492
|
return doFieldPrivacy(viewer, ent, data, fieldPrivacyOptions);
|
|
278
493
|
}
|
|
279
|
-
return
|
|
494
|
+
return error;
|
|
280
495
|
}
|
|
281
496
|
async function applyPrivacyPolicyForEntX(viewer, ent, data, options) {
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
497
|
+
const r = await applyPrivacyPolicyForEnt(viewer, ent, data, options);
|
|
498
|
+
if (r instanceof Error) {
|
|
499
|
+
throw r;
|
|
500
|
+
}
|
|
501
|
+
if (r === null) {
|
|
502
|
+
throw new Error(`couldn't apply privacyPoliy for ent ${ent.id}`);
|
|
503
|
+
}
|
|
504
|
+
return r;
|
|
285
505
|
}
|
|
286
506
|
async function doFieldPrivacy(viewer, ent, data, options) {
|
|
287
507
|
if (!options.fieldPrivacy) {
|
|
@@ -289,13 +509,16 @@ async function doFieldPrivacy(viewer, ent, data, options) {
|
|
|
289
509
|
}
|
|
290
510
|
const promises = [];
|
|
291
511
|
let somethingChanged = false;
|
|
512
|
+
const origData = {
|
|
513
|
+
...data,
|
|
514
|
+
};
|
|
292
515
|
for (const [k, policy] of options.fieldPrivacy) {
|
|
516
|
+
const curr = data[k];
|
|
517
|
+
if (curr === null || curr === undefined) {
|
|
518
|
+
continue;
|
|
519
|
+
}
|
|
293
520
|
promises.push((async () => {
|
|
294
521
|
// don't do anything if key is null or for some reason missing
|
|
295
|
-
const curr = data[k];
|
|
296
|
-
if (curr === null || curr === undefined) {
|
|
297
|
-
return;
|
|
298
|
-
}
|
|
299
522
|
const r = await (0, privacy_1.applyPrivacyPolicy)(viewer, policy, ent);
|
|
300
523
|
if (!r) {
|
|
301
524
|
data[k] = null;
|
|
@@ -306,8 +529,11 @@ async function doFieldPrivacy(viewer, ent, data, options) {
|
|
|
306
529
|
await Promise.all(promises);
|
|
307
530
|
if (somethingChanged) {
|
|
308
531
|
// have to create new instance
|
|
309
|
-
|
|
532
|
+
const ent = new options.ent(viewer, data);
|
|
533
|
+
ent.__setRawDBData(origData);
|
|
534
|
+
return ent;
|
|
310
535
|
}
|
|
536
|
+
ent.__setRawDBData(origData);
|
|
311
537
|
return ent;
|
|
312
538
|
}
|
|
313
539
|
function logQuery(query, logValues) {
|
|
@@ -317,6 +543,7 @@ function logQuery(query, logValues) {
|
|
|
317
543
|
});
|
|
318
544
|
(0, logger_1.logTrace)();
|
|
319
545
|
}
|
|
546
|
+
exports.logQuery = logQuery;
|
|
320
547
|
// TODO long term figure out if this API should be exposed
|
|
321
548
|
async function loadRowX(options) {
|
|
322
549
|
const result = await loadRow(options);
|
|
@@ -339,29 +566,26 @@ async function loadRow(options) {
|
|
|
339
566
|
}
|
|
340
567
|
const query = buildQuery(options);
|
|
341
568
|
logQuery(query, options.clause.logValues());
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
if (res.rowCount
|
|
346
|
-
|
|
347
|
-
(0, logger_1.log)("error", "got more than one row for query " + query);
|
|
348
|
-
}
|
|
349
|
-
return null;
|
|
350
|
-
}
|
|
351
|
-
// put the row in the cache...
|
|
352
|
-
if (cache) {
|
|
353
|
-
cache.primeCache(options, res.rows[0]);
|
|
569
|
+
const pool = db_1.default.getInstance().getPool();
|
|
570
|
+
const res = await pool.query(query, options.clause.values());
|
|
571
|
+
if (res.rowCount != 1) {
|
|
572
|
+
if (res.rowCount > 1) {
|
|
573
|
+
(0, logger_1.log)("error", "got more than one row for query " + query);
|
|
354
574
|
}
|
|
355
|
-
return res.rows[0];
|
|
356
|
-
}
|
|
357
|
-
catch (e) {
|
|
358
|
-
// an example of an error being suppressed
|
|
359
|
-
// another one. TODO https://github.com/lolopinto/ent/issues/862
|
|
360
|
-
(0, logger_1.log)("error", e);
|
|
361
575
|
return null;
|
|
362
576
|
}
|
|
577
|
+
// put the row in the cache...
|
|
578
|
+
if (cache) {
|
|
579
|
+
cache.primeCache(options, res.rows[0]);
|
|
580
|
+
}
|
|
581
|
+
return res.rows[0];
|
|
363
582
|
}
|
|
364
583
|
exports.loadRow = loadRow;
|
|
584
|
+
var _logQueryWithError = false;
|
|
585
|
+
function ___setLogQueryErrorWithError(val) {
|
|
586
|
+
_logQueryWithError = val || false;
|
|
587
|
+
}
|
|
588
|
+
exports.___setLogQueryErrorWithError = ___setLogQueryErrorWithError;
|
|
365
589
|
// this always goes to the db, no cache, nothing
|
|
366
590
|
async function performRawQuery(query, values, logValues) {
|
|
367
591
|
const pool = db_1.default.getInstance().getPool();
|
|
@@ -371,9 +595,11 @@ async function performRawQuery(query, values, logValues) {
|
|
|
371
595
|
return res.rows;
|
|
372
596
|
}
|
|
373
597
|
catch (e) {
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
598
|
+
if (_logQueryWithError) {
|
|
599
|
+
const msg = e.message;
|
|
600
|
+
throw new Error(`error \`${msg}\` running query: \`${query}\` with values: \`${logValues}\``);
|
|
601
|
+
}
|
|
602
|
+
throw e;
|
|
377
603
|
}
|
|
378
604
|
}
|
|
379
605
|
exports.performRawQuery = performRawQuery;
|
|
@@ -432,400 +658,32 @@ function buildGroupQuery(options) {
|
|
|
432
658
|
];
|
|
433
659
|
}
|
|
434
660
|
exports.buildGroupQuery = buildGroupQuery;
|
|
435
|
-
class EditNodeOperation {
|
|
436
|
-
constructor(options, existingEnt = null) {
|
|
437
|
-
this.options = options;
|
|
438
|
-
this.existingEnt = existingEnt;
|
|
439
|
-
this.placeholderID = options.placeholderID;
|
|
440
|
-
}
|
|
441
|
-
resolve(executor) {
|
|
442
|
-
if (!this.options.fieldsToResolve.length) {
|
|
443
|
-
return;
|
|
444
|
-
}
|
|
445
|
-
let fields = this.options.fields;
|
|
446
|
-
this.options.fieldsToResolve.forEach((fieldName) => {
|
|
447
|
-
let value = fields[fieldName];
|
|
448
|
-
if (!value) {
|
|
449
|
-
throw new Error(`trying to resolve field ${fieldName} but not a valid field`);
|
|
450
|
-
}
|
|
451
|
-
let ent = executor.resolveValue(value.placeholderID);
|
|
452
|
-
if (!ent) {
|
|
453
|
-
throw new Error(`couldn't resolve field \`${fieldName}\` with value ${value.placeholderID}`);
|
|
454
|
-
}
|
|
455
|
-
fields[fieldName] = ent.id;
|
|
456
|
-
});
|
|
457
|
-
this.options.fields = fields;
|
|
458
|
-
}
|
|
459
|
-
hasData(data) {
|
|
460
|
-
for (const _k in data) {
|
|
461
|
-
return true;
|
|
462
|
-
}
|
|
463
|
-
return false;
|
|
464
|
-
}
|
|
465
|
-
async performWrite(queryer, context) {
|
|
466
|
-
let options = {
|
|
467
|
-
...this.options,
|
|
468
|
-
context,
|
|
469
|
-
};
|
|
470
|
-
if (this.existingEnt) {
|
|
471
|
-
if (this.hasData(options.fields)) {
|
|
472
|
-
// even this with returning * may not always work if transformed...
|
|
473
|
-
// we can have a transformed flag to see if it should be returned?
|
|
474
|
-
this.row = await editRow(queryer, options, this.existingEnt.id, "RETURNING *");
|
|
475
|
-
}
|
|
476
|
-
else {
|
|
477
|
-
this.row = this.existingEnt["data"];
|
|
478
|
-
}
|
|
479
|
-
}
|
|
480
|
-
else {
|
|
481
|
-
this.row = await createRow(queryer, options, "RETURNING *");
|
|
482
|
-
}
|
|
483
|
-
}
|
|
484
|
-
reloadRow(queryer, id, options) {
|
|
485
|
-
// TODO this isn't always an ObjectLoader. should throw or figure out a way to get query
|
|
486
|
-
// and run this on its own...
|
|
487
|
-
const loader = this.options.loadEntOptions.loaderFactory.createLoader(options.context);
|
|
488
|
-
const opts = loader.getOptions();
|
|
489
|
-
let cls = clause.Eq(options.key, id);
|
|
490
|
-
if (opts.clause) {
|
|
491
|
-
let optionClause;
|
|
492
|
-
if (typeof opts.clause === "function") {
|
|
493
|
-
optionClause = opts.clause();
|
|
494
|
-
}
|
|
495
|
-
else {
|
|
496
|
-
optionClause = opts.clause;
|
|
497
|
-
}
|
|
498
|
-
if (optionClause) {
|
|
499
|
-
cls = clause.And(optionClause, cls);
|
|
500
|
-
}
|
|
501
|
-
}
|
|
502
|
-
const query = buildQuery({
|
|
503
|
-
fields: opts.fields.length ? opts.fields : ["*"],
|
|
504
|
-
tableName: options.tableName,
|
|
505
|
-
clause: cls,
|
|
506
|
-
});
|
|
507
|
-
// special case log here because we're not going through any of the normal
|
|
508
|
-
// methods here because those are async and this is sync
|
|
509
|
-
// this is the only place we're doing this so only handling here
|
|
510
|
-
logQuery(query, [id]);
|
|
511
|
-
const r = queryer.querySync(query, [id]);
|
|
512
|
-
if (r.rows.length === 1) {
|
|
513
|
-
this.row = r.rows[0];
|
|
514
|
-
}
|
|
515
|
-
}
|
|
516
|
-
performWriteSync(queryer, context) {
|
|
517
|
-
let options = {
|
|
518
|
-
...this.options,
|
|
519
|
-
context,
|
|
520
|
-
};
|
|
521
|
-
if (this.existingEnt) {
|
|
522
|
-
if (this.hasData(this.options.fields)) {
|
|
523
|
-
editRowSync(queryer, options, this.existingEnt.id, "RETURNING *");
|
|
524
|
-
this.reloadRow(queryer, this.existingEnt.id, options);
|
|
525
|
-
}
|
|
526
|
-
else {
|
|
527
|
-
this.row = this.existingEnt["data"];
|
|
528
|
-
}
|
|
529
|
-
}
|
|
530
|
-
else {
|
|
531
|
-
createRowSync(queryer, options, "RETURNING *");
|
|
532
|
-
const id = options.fields[options.key];
|
|
533
|
-
this.reloadRow(queryer, id, options);
|
|
534
|
-
}
|
|
535
|
-
}
|
|
536
|
-
returnedRow() {
|
|
537
|
-
return this.row;
|
|
538
|
-
}
|
|
539
|
-
createdEnt(viewer) {
|
|
540
|
-
if (!this.row) {
|
|
541
|
-
return null;
|
|
542
|
-
}
|
|
543
|
-
return new this.options.loadEntOptions.ent(viewer, this.row);
|
|
544
|
-
}
|
|
545
|
-
}
|
|
546
|
-
exports.EditNodeOperation = EditNodeOperation;
|
|
547
|
-
class EdgeOperation {
|
|
548
|
-
constructor(edgeInput, options) {
|
|
549
|
-
this.edgeInput = edgeInput;
|
|
550
|
-
this.options = options;
|
|
551
|
-
}
|
|
552
|
-
async preFetch(queryer, context) {
|
|
553
|
-
let edgeData = await loadEdgeData(this.edgeInput.edgeType);
|
|
554
|
-
if (!edgeData) {
|
|
555
|
-
throw new Error(`error loading edge data for ${this.edgeInput.edgeType}`);
|
|
556
|
-
}
|
|
557
|
-
this.edgeData = edgeData;
|
|
558
|
-
}
|
|
559
|
-
async performWrite(queryer, context) {
|
|
560
|
-
if (!this.edgeData) {
|
|
561
|
-
throw new Error(`error fetching edgeData for type ${this.edgeInput.edgeType}`);
|
|
562
|
-
}
|
|
563
|
-
switch (this.options.operation) {
|
|
564
|
-
case action_1.WriteOperation.Delete:
|
|
565
|
-
return this.performDeleteWrite(queryer, this.edgeData, this.edgeInput, context);
|
|
566
|
-
case action_1.WriteOperation.Insert:
|
|
567
|
-
case action_1.WriteOperation.Edit:
|
|
568
|
-
return this.performInsertWrite(queryer, this.edgeData, this.edgeInput, context);
|
|
569
|
-
}
|
|
570
|
-
}
|
|
571
|
-
performWriteSync(queryer, context) {
|
|
572
|
-
if (!this.edgeData) {
|
|
573
|
-
throw new Error(`error fetching edgeData for type ${this.edgeInput.edgeType}`);
|
|
574
|
-
}
|
|
575
|
-
switch (this.options.operation) {
|
|
576
|
-
case action_1.WriteOperation.Delete:
|
|
577
|
-
return this.performDeleteWriteSync(queryer, this.edgeData, this.edgeInput, context);
|
|
578
|
-
case action_1.WriteOperation.Insert:
|
|
579
|
-
case action_1.WriteOperation.Edit:
|
|
580
|
-
return this.performInsertWriteSync(queryer, this.edgeData, this.edgeInput, context);
|
|
581
|
-
}
|
|
582
|
-
}
|
|
583
|
-
getDeleteRowParams(edgeData, edge, context) {
|
|
584
|
-
return {
|
|
585
|
-
options: {
|
|
586
|
-
tableName: edgeData.edgeTable,
|
|
587
|
-
context,
|
|
588
|
-
},
|
|
589
|
-
clause: clause.And(clause.Eq("id1", edge.id1), clause.Eq("id2", edge.id2), clause.Eq("edge_type", edge.edgeType)),
|
|
590
|
-
};
|
|
591
|
-
}
|
|
592
|
-
async performDeleteWrite(q, edgeData, edge, context) {
|
|
593
|
-
const params = this.getDeleteRowParams(edgeData, edge, context);
|
|
594
|
-
return deleteRows(q, params.options, params.clause);
|
|
595
|
-
}
|
|
596
|
-
performDeleteWriteSync(q, edgeData, edge, context) {
|
|
597
|
-
const params = this.getDeleteRowParams(edgeData, edge, context);
|
|
598
|
-
return deleteRowsSync(q, params.options, params.clause);
|
|
599
|
-
}
|
|
600
|
-
getInsertRowParams(edgeData, edge, context) {
|
|
601
|
-
const fields = {
|
|
602
|
-
id1: edge.id1,
|
|
603
|
-
id2: edge.id2,
|
|
604
|
-
id1_type: edge.id1Type,
|
|
605
|
-
id2_type: edge.id2Type,
|
|
606
|
-
edge_type: edge.edgeType,
|
|
607
|
-
data: edge.data || null,
|
|
608
|
-
};
|
|
609
|
-
if (edge.time) {
|
|
610
|
-
fields["time"] = edge.time.toISOString();
|
|
611
|
-
}
|
|
612
|
-
else {
|
|
613
|
-
// todo make this a schema field like what we do in generated base files...
|
|
614
|
-
// maybe when actions exist?
|
|
615
|
-
fields["time"] = new Date().toISOString();
|
|
616
|
-
}
|
|
617
|
-
return [
|
|
618
|
-
{
|
|
619
|
-
tableName: edgeData.edgeTable,
|
|
620
|
-
fields: fields,
|
|
621
|
-
fieldsToLog: fields,
|
|
622
|
-
context,
|
|
623
|
-
},
|
|
624
|
-
"ON CONFLICT(id1, edge_type, id2) DO UPDATE SET data = EXCLUDED.data",
|
|
625
|
-
];
|
|
626
|
-
}
|
|
627
|
-
async performInsertWrite(q, edgeData, edge, context) {
|
|
628
|
-
const [options, suffix] = this.getInsertRowParams(edgeData, edge, context);
|
|
629
|
-
await createRow(q, options, suffix);
|
|
630
|
-
}
|
|
631
|
-
performInsertWriteSync(q, edgeData, edge, context) {
|
|
632
|
-
const [options, suffix] = this.getInsertRowParams(edgeData, edge, context);
|
|
633
|
-
createRowSync(q, options, suffix);
|
|
634
|
-
}
|
|
635
|
-
resolveImpl(executor, placeholder, desc) {
|
|
636
|
-
let ent = executor.resolveValue(placeholder);
|
|
637
|
-
if (!ent) {
|
|
638
|
-
throw new Error(`could not resolve placeholder value ${placeholder} for ${desc} for edge ${this.edgeInput.edgeType}`);
|
|
639
|
-
}
|
|
640
|
-
if (ent.id === undefined) {
|
|
641
|
-
throw new Error(`id of resolved ent is not defined`);
|
|
642
|
-
}
|
|
643
|
-
return [ent.id, ent.nodeType];
|
|
644
|
-
}
|
|
645
|
-
resolve(executor) {
|
|
646
|
-
if (this.options.id1Placeholder) {
|
|
647
|
-
[this.edgeInput.id1, this.edgeInput.id1Type] = this.resolveImpl(executor, this.edgeInput.id1, "id1");
|
|
648
|
-
}
|
|
649
|
-
if (this.options.id2Placeholder) {
|
|
650
|
-
[this.edgeInput.id2, this.edgeInput.id2Type] = this.resolveImpl(executor, this.edgeInput.id2, "id2");
|
|
651
|
-
}
|
|
652
|
-
if (this.options.dataPlaceholder) {
|
|
653
|
-
if (!this.edgeInput.data) {
|
|
654
|
-
throw new Error(`data placeholder set but edgeInput data undefined`);
|
|
655
|
-
}
|
|
656
|
-
let [data, _] = this.resolveImpl(executor, this.edgeInput.data.toString(), "data");
|
|
657
|
-
this.edgeInput.data = data.toString();
|
|
658
|
-
}
|
|
659
|
-
}
|
|
660
|
-
symmetricEdge() {
|
|
661
|
-
return new EdgeOperation({
|
|
662
|
-
id1: this.edgeInput.id2,
|
|
663
|
-
id1Type: this.edgeInput.id2Type,
|
|
664
|
-
id2: this.edgeInput.id1,
|
|
665
|
-
id2Type: this.edgeInput.id1Type,
|
|
666
|
-
edgeType: this.edgeInput.edgeType,
|
|
667
|
-
time: this.edgeInput.time,
|
|
668
|
-
data: this.edgeInput.data,
|
|
669
|
-
}, {
|
|
670
|
-
operation: this.options.operation,
|
|
671
|
-
id1Placeholder: this.options.id2Placeholder,
|
|
672
|
-
id2Placeholder: this.options.id1Placeholder,
|
|
673
|
-
dataPlaceholder: this.options.dataPlaceholder,
|
|
674
|
-
});
|
|
675
|
-
}
|
|
676
|
-
inverseEdge(edgeData) {
|
|
677
|
-
return new EdgeOperation({
|
|
678
|
-
id1: this.edgeInput.id2,
|
|
679
|
-
id1Type: this.edgeInput.id2Type,
|
|
680
|
-
id2: this.edgeInput.id1,
|
|
681
|
-
id2Type: this.edgeInput.id1Type,
|
|
682
|
-
edgeType: edgeData.inverseEdgeType,
|
|
683
|
-
time: this.edgeInput.time,
|
|
684
|
-
data: this.edgeInput.data,
|
|
685
|
-
}, {
|
|
686
|
-
operation: this.options.operation,
|
|
687
|
-
id1Placeholder: this.options.id2Placeholder,
|
|
688
|
-
id2Placeholder: this.options.id1Placeholder,
|
|
689
|
-
dataPlaceholder: this.options.dataPlaceholder,
|
|
690
|
-
});
|
|
691
|
-
}
|
|
692
|
-
static resolveIDs(srcBuilder, // id1
|
|
693
|
-
destID) {
|
|
694
|
-
let destIDVal;
|
|
695
|
-
let destPlaceholder = false;
|
|
696
|
-
if (this.isBuilder(destID)) {
|
|
697
|
-
destIDVal = destID.placeholderID;
|
|
698
|
-
destPlaceholder = true;
|
|
699
|
-
}
|
|
700
|
-
else {
|
|
701
|
-
destIDVal = destID;
|
|
702
|
-
}
|
|
703
|
-
let srcIDVal;
|
|
704
|
-
let srcType;
|
|
705
|
-
let srcPlaceholder = false;
|
|
706
|
-
if (srcBuilder.existingEnt) {
|
|
707
|
-
srcIDVal = srcBuilder.existingEnt.id;
|
|
708
|
-
srcType = srcBuilder.existingEnt.nodeType;
|
|
709
|
-
}
|
|
710
|
-
else {
|
|
711
|
-
srcPlaceholder = true;
|
|
712
|
-
// get placeholder.
|
|
713
|
-
srcIDVal = srcBuilder.placeholderID;
|
|
714
|
-
// expected to be filled later
|
|
715
|
-
srcType = "";
|
|
716
|
-
}
|
|
717
|
-
return [srcIDVal, srcType, srcPlaceholder, destIDVal, destPlaceholder];
|
|
718
|
-
}
|
|
719
|
-
static isBuilder(val) {
|
|
720
|
-
return val.placeholderID !== undefined;
|
|
721
|
-
}
|
|
722
|
-
static resolveData(data) {
|
|
723
|
-
if (!data) {
|
|
724
|
-
return [undefined, false];
|
|
725
|
-
}
|
|
726
|
-
if (this.isBuilder(data)) {
|
|
727
|
-
return [data.placeholderID.toString(), true];
|
|
728
|
-
}
|
|
729
|
-
return [data, false];
|
|
730
|
-
}
|
|
731
|
-
static inboundEdge(builder, edgeType, id1, nodeType, options) {
|
|
732
|
-
let [id2Val, id2Type, id2Placeholder, id1Val, id1Placeholder] = EdgeOperation.resolveIDs(builder, id1);
|
|
733
|
-
let [data, dataPlaceholder] = EdgeOperation.resolveData(options?.data);
|
|
734
|
-
const edge = {
|
|
735
|
-
id1: id1Val,
|
|
736
|
-
edgeType: edgeType,
|
|
737
|
-
id2: id2Val,
|
|
738
|
-
id2Type: id2Type,
|
|
739
|
-
id1Type: nodeType,
|
|
740
|
-
...options,
|
|
741
|
-
};
|
|
742
|
-
if (data) {
|
|
743
|
-
edge.data = data;
|
|
744
|
-
}
|
|
745
|
-
return new EdgeOperation(edge, {
|
|
746
|
-
operation: action_1.WriteOperation.Insert,
|
|
747
|
-
id2Placeholder,
|
|
748
|
-
id1Placeholder,
|
|
749
|
-
dataPlaceholder,
|
|
750
|
-
});
|
|
751
|
-
}
|
|
752
|
-
static outboundEdge(builder, edgeType, id2, nodeType, options) {
|
|
753
|
-
let [id1Val, id1Type, id1Placeholder, id2Val, id2Placeholder] = EdgeOperation.resolveIDs(builder, id2);
|
|
754
|
-
let [data, dataPlaceholder] = EdgeOperation.resolveData(options?.data);
|
|
755
|
-
const edge = {
|
|
756
|
-
id1: id1Val,
|
|
757
|
-
edgeType: edgeType,
|
|
758
|
-
id2: id2Val,
|
|
759
|
-
id2Type: nodeType,
|
|
760
|
-
id1Type: id1Type,
|
|
761
|
-
...options,
|
|
762
|
-
};
|
|
763
|
-
if (data) {
|
|
764
|
-
edge.data = data;
|
|
765
|
-
}
|
|
766
|
-
return new EdgeOperation(edge, {
|
|
767
|
-
operation: action_1.WriteOperation.Insert,
|
|
768
|
-
id1Placeholder,
|
|
769
|
-
id2Placeholder,
|
|
770
|
-
dataPlaceholder,
|
|
771
|
-
});
|
|
772
|
-
}
|
|
773
|
-
static removeInboundEdge(builder, edgeType, id1) {
|
|
774
|
-
if (!builder.existingEnt) {
|
|
775
|
-
throw new Error("cannot remove an edge from a non-existing ent");
|
|
776
|
-
}
|
|
777
|
-
const edge = {
|
|
778
|
-
id1: id1,
|
|
779
|
-
edgeType: edgeType,
|
|
780
|
-
id2: builder.existingEnt.id,
|
|
781
|
-
id2Type: "",
|
|
782
|
-
id1Type: "",
|
|
783
|
-
};
|
|
784
|
-
return new EdgeOperation(edge, {
|
|
785
|
-
operation: action_1.WriteOperation.Delete,
|
|
786
|
-
});
|
|
787
|
-
}
|
|
788
|
-
static removeOutboundEdge(builder, edgeType, id2) {
|
|
789
|
-
if (!builder.existingEnt) {
|
|
790
|
-
throw new Error("cannot remove an edge from a non-existing ent");
|
|
791
|
-
}
|
|
792
|
-
const edge = {
|
|
793
|
-
id2: id2,
|
|
794
|
-
edgeType: edgeType,
|
|
795
|
-
id1: builder.existingEnt.id,
|
|
796
|
-
id2Type: "",
|
|
797
|
-
id1Type: "",
|
|
798
|
-
};
|
|
799
|
-
return new EdgeOperation(edge, {
|
|
800
|
-
operation: action_1.WriteOperation.Delete,
|
|
801
|
-
});
|
|
802
|
-
}
|
|
803
|
-
}
|
|
804
|
-
exports.EdgeOperation = EdgeOperation;
|
|
805
661
|
function isSyncQueryer(queryer) {
|
|
806
662
|
return queryer.execSync !== undefined;
|
|
807
663
|
}
|
|
808
664
|
async function mutateRow(queryer, query, values, logValues, options) {
|
|
809
665
|
logQuery(query, logValues);
|
|
810
666
|
let cache = options.context?.cache;
|
|
667
|
+
let res;
|
|
811
668
|
try {
|
|
812
|
-
let res;
|
|
813
669
|
if (isSyncQueryer(queryer)) {
|
|
814
670
|
res = queryer.execSync(query, values);
|
|
815
671
|
}
|
|
816
672
|
else {
|
|
817
673
|
res = await queryer.exec(query, values);
|
|
818
674
|
}
|
|
819
|
-
|
|
820
|
-
|
|
675
|
+
}
|
|
676
|
+
catch (e) {
|
|
677
|
+
if (_logQueryWithError) {
|
|
678
|
+
const msg = e.message;
|
|
679
|
+
throw new Error(`error \`${msg}\` running query: \`${query}\``);
|
|
821
680
|
}
|
|
822
|
-
|
|
681
|
+
throw e;
|
|
823
682
|
}
|
|
824
|
-
|
|
825
|
-
|
|
826
|
-
(0, logger_1.log)("error", err);
|
|
827
|
-
throw err;
|
|
683
|
+
if (cache) {
|
|
684
|
+
cache.clearCache();
|
|
828
685
|
}
|
|
686
|
+
return res;
|
|
829
687
|
}
|
|
830
688
|
function mutateRowSync(queryer, query, values, logValues, options) {
|
|
831
689
|
logQuery(query, logValues);
|
|
@@ -837,10 +695,12 @@ function mutateRowSync(queryer, query, values, logValues, options) {
|
|
|
837
695
|
}
|
|
838
696
|
return res;
|
|
839
697
|
}
|
|
840
|
-
catch (
|
|
841
|
-
|
|
842
|
-
|
|
843
|
-
|
|
698
|
+
catch (e) {
|
|
699
|
+
if (_logQueryWithError) {
|
|
700
|
+
const msg = e.message;
|
|
701
|
+
throw new Error(`error \`${msg}\` running query: \`${query}\``);
|
|
702
|
+
}
|
|
703
|
+
throw e;
|
|
844
704
|
}
|
|
845
705
|
}
|
|
846
706
|
function buildInsertQuery(options, suffix) {
|
|
@@ -867,8 +727,26 @@ function buildInsertQuery(options, suffix) {
|
|
|
867
727
|
const cols = fields.join(", ");
|
|
868
728
|
const vals = valsString.join(", ");
|
|
869
729
|
let query = `INSERT INTO ${options.tableName} (${cols}) VALUES (${vals})`;
|
|
730
|
+
if (options.onConflict) {
|
|
731
|
+
let onConflict = "";
|
|
732
|
+
if (options.onConflict.onConflictConstraint) {
|
|
733
|
+
onConflict = `ON CONFLICT ON CONSTRAINT ${options.onConflict.onConflictConstraint}`;
|
|
734
|
+
}
|
|
735
|
+
else {
|
|
736
|
+
onConflict = `ON CONFLICT(${options.onConflict.onConflictCols.join(", ")})`;
|
|
737
|
+
}
|
|
738
|
+
if (options.onConflict.updateCols?.length) {
|
|
739
|
+
onConflict += ` DO UPDATE SET ${options.onConflict.updateCols
|
|
740
|
+
.map((f) => `${f} = EXCLUDED.${f}`)
|
|
741
|
+
.join(", ")}`;
|
|
742
|
+
}
|
|
743
|
+
else {
|
|
744
|
+
onConflict += ` DO NOTHING`;
|
|
745
|
+
}
|
|
746
|
+
query = query + " " + onConflict;
|
|
747
|
+
}
|
|
870
748
|
if (suffix) {
|
|
871
|
-
query
|
|
749
|
+
query += " " + suffix;
|
|
872
750
|
}
|
|
873
751
|
return [query, values, logValues];
|
|
874
752
|
}
|
|
@@ -893,32 +771,47 @@ function createRowSync(queryer, options, suffix) {
|
|
|
893
771
|
return null;
|
|
894
772
|
}
|
|
895
773
|
exports.createRowSync = createRowSync;
|
|
896
|
-
function buildUpdateQuery(options,
|
|
774
|
+
function buildUpdateQuery(options, suffix) {
|
|
897
775
|
let valsString = [];
|
|
898
776
|
let values = [];
|
|
899
777
|
let logValues = [];
|
|
900
778
|
const dialect = db_1.default.getDialect();
|
|
901
779
|
let idx = 1;
|
|
902
780
|
for (const key in options.fields) {
|
|
903
|
-
|
|
904
|
-
|
|
905
|
-
|
|
906
|
-
|
|
907
|
-
|
|
908
|
-
|
|
909
|
-
|
|
781
|
+
if (options.expressions && options.expressions.has(key)) {
|
|
782
|
+
const cls = options.expressions.get(key);
|
|
783
|
+
valsString.push(`${key} = ${cls.clause(idx)}`);
|
|
784
|
+
// TODO need to test a clause with more than one value...
|
|
785
|
+
const newVals = cls.values();
|
|
786
|
+
idx += newVals.length;
|
|
787
|
+
values.push(...newVals);
|
|
788
|
+
logValues.push(...cls.logValues());
|
|
910
789
|
}
|
|
911
790
|
else {
|
|
912
|
-
|
|
791
|
+
const val = options.fields[key];
|
|
792
|
+
values.push(val);
|
|
793
|
+
if (options.fieldsToLog) {
|
|
794
|
+
logValues.push(options.fieldsToLog[key]);
|
|
795
|
+
}
|
|
796
|
+
// TODO would be nice to use clause here. need update version of the queries so that
|
|
797
|
+
// we don't have to handle dialect specifics here
|
|
798
|
+
// can't use clause because of IS NULL
|
|
799
|
+
// valsString.push(clause.Eq(key, val).clause(idx));
|
|
800
|
+
if (dialect === db_1.Dialect.Postgres) {
|
|
801
|
+
valsString.push(`${key} = $${idx}`);
|
|
802
|
+
}
|
|
803
|
+
else {
|
|
804
|
+
valsString.push(`${key} = ?`);
|
|
805
|
+
}
|
|
806
|
+
idx++;
|
|
913
807
|
}
|
|
914
808
|
}
|
|
915
809
|
const vals = valsString.join(", ");
|
|
916
810
|
let query = `UPDATE ${options.tableName} SET ${vals} WHERE `;
|
|
917
|
-
|
|
918
|
-
|
|
919
|
-
|
|
920
|
-
|
|
921
|
-
query = query + `${options.key} = ?`;
|
|
811
|
+
query = query + options.whereClause.clause(idx);
|
|
812
|
+
values.push(...options.whereClause.values());
|
|
813
|
+
if (options.fieldsToLog) {
|
|
814
|
+
logValues.push(...options.whereClause.logValues());
|
|
922
815
|
}
|
|
923
816
|
if (suffix) {
|
|
924
817
|
query = query + " " + suffix;
|
|
@@ -926,10 +819,8 @@ function buildUpdateQuery(options, id, suffix) {
|
|
|
926
819
|
return [query, values, logValues];
|
|
927
820
|
}
|
|
928
821
|
exports.buildUpdateQuery = buildUpdateQuery;
|
|
929
|
-
async function editRow(queryer, options,
|
|
930
|
-
const [query, values, logValues] = buildUpdateQuery(options,
|
|
931
|
-
// add id as value to prepared query
|
|
932
|
-
values.push(id);
|
|
822
|
+
async function editRow(queryer, options, suffix) {
|
|
823
|
+
const [query, values, logValues] = buildUpdateQuery(options, suffix);
|
|
933
824
|
const res = await mutateRow(queryer, query, values, logValues, options);
|
|
934
825
|
if (res?.rowCount == 1) {
|
|
935
826
|
// for now assume id primary key
|
|
@@ -940,10 +831,8 @@ async function editRow(queryer, options, id, suffix) {
|
|
|
940
831
|
return null;
|
|
941
832
|
}
|
|
942
833
|
exports.editRow = editRow;
|
|
943
|
-
function editRowSync(queryer, options,
|
|
944
|
-
const [query, values, logValues] = buildUpdateQuery(options,
|
|
945
|
-
// add id as value to prepared query
|
|
946
|
-
values.push(id);
|
|
834
|
+
function editRowSync(queryer, options, suffix) {
|
|
835
|
+
const [query, values, logValues] = buildUpdateQuery(options, suffix);
|
|
947
836
|
const res = mutateRowSync(queryer, query, values, logValues, options);
|
|
948
837
|
if (res?.rowCount == 1) {
|
|
949
838
|
// for now assume id primary key
|
|
@@ -964,27 +853,6 @@ function deleteRowsSync(queryer, options, cls) {
|
|
|
964
853
|
mutateRowSync(queryer, query, cls.values(), cls.logValues(), options);
|
|
965
854
|
}
|
|
966
855
|
exports.deleteRowsSync = deleteRowsSync;
|
|
967
|
-
class DeleteNodeOperation {
|
|
968
|
-
constructor(id, options) {
|
|
969
|
-
this.id = id;
|
|
970
|
-
this.options = options;
|
|
971
|
-
}
|
|
972
|
-
async performWrite(queryer, context) {
|
|
973
|
-
let options = {
|
|
974
|
-
...this.options,
|
|
975
|
-
context,
|
|
976
|
-
};
|
|
977
|
-
return deleteRows(queryer, options, clause.Eq("id", this.id));
|
|
978
|
-
}
|
|
979
|
-
performWriteSync(queryer, context) {
|
|
980
|
-
let options = {
|
|
981
|
-
...this.options,
|
|
982
|
-
context,
|
|
983
|
-
};
|
|
984
|
-
return deleteRowsSync(queryer, options, clause.Eq("id", this.id));
|
|
985
|
-
}
|
|
986
|
-
}
|
|
987
|
-
exports.DeleteNodeOperation = DeleteNodeOperation;
|
|
988
856
|
class AssocEdge {
|
|
989
857
|
constructor(data) {
|
|
990
858
|
this.id1 = data.id1;
|
|
@@ -994,21 +862,22 @@ class AssocEdge {
|
|
|
994
862
|
this.edgeType = data.edge_type;
|
|
995
863
|
this.time = data.time;
|
|
996
864
|
this.data = data.data;
|
|
865
|
+
this.rawData = data;
|
|
866
|
+
}
|
|
867
|
+
__getRawData() {
|
|
868
|
+
// incase there's extra db fields. useful for tests
|
|
869
|
+
// in production, a subclass of this should be in use so we won't need this...
|
|
870
|
+
return this.rawData;
|
|
997
871
|
}
|
|
998
872
|
getCursor() {
|
|
999
873
|
return getCursor({
|
|
1000
874
|
row: this,
|
|
1001
|
-
col: "
|
|
1002
|
-
conv: (t) => {
|
|
1003
|
-
if (typeof t === "string") {
|
|
1004
|
-
return Date.parse(t);
|
|
1005
|
-
}
|
|
1006
|
-
return t.getTime();
|
|
1007
|
-
},
|
|
875
|
+
col: "id2",
|
|
1008
876
|
});
|
|
1009
877
|
}
|
|
1010
878
|
}
|
|
1011
879
|
exports.AssocEdge = AssocEdge;
|
|
880
|
+
// TODO eventually update this for sortCol time unique keys
|
|
1012
881
|
function getCursor(opts) {
|
|
1013
882
|
const { row, col, conv } = opts;
|
|
1014
883
|
// row: Data, col: string, conv?: (any) => any) {
|
|
@@ -1088,52 +957,80 @@ const edgeFields = [
|
|
|
1088
957
|
];
|
|
1089
958
|
exports.DefaultLimit = 1000;
|
|
1090
959
|
// TODO default limit from somewhere
|
|
1091
|
-
function defaultEdgeQueryOptions(id1, edgeType) {
|
|
960
|
+
function defaultEdgeQueryOptions(id1, edgeType, id2) {
|
|
961
|
+
let cls = clause.And(clause.Eq("id1", id1), clause.Eq("edge_type", edgeType));
|
|
962
|
+
if (id2) {
|
|
963
|
+
cls = clause.And(cls, clause.Eq("id2", id2));
|
|
964
|
+
}
|
|
1092
965
|
return {
|
|
1093
|
-
clause:
|
|
966
|
+
clause: cls,
|
|
1094
967
|
orderby: "time DESC",
|
|
1095
968
|
limit: exports.DefaultLimit,
|
|
1096
969
|
};
|
|
1097
970
|
}
|
|
1098
|
-
exports.defaultEdgeQueryOptions = defaultEdgeQueryOptions;
|
|
1099
971
|
async function loadEdges(options) {
|
|
1100
972
|
return loadCustomEdges({ ...options, ctr: AssocEdge });
|
|
1101
973
|
}
|
|
1102
974
|
exports.loadEdges = loadEdges;
|
|
975
|
+
function getEdgeClauseAndFields(cls, options) {
|
|
976
|
+
let fields = edgeFields;
|
|
977
|
+
const transformEdgeRead = (0, global_schema_1.__getGlobalSchema)()?.transformEdgeRead;
|
|
978
|
+
if (transformEdgeRead) {
|
|
979
|
+
const transformClause = transformEdgeRead();
|
|
980
|
+
if (!options.disableTransformations) {
|
|
981
|
+
cls = clause.And(cls, transformClause);
|
|
982
|
+
}
|
|
983
|
+
fields = edgeFields.concat(transformClause.columns());
|
|
984
|
+
}
|
|
985
|
+
return {
|
|
986
|
+
cls,
|
|
987
|
+
fields,
|
|
988
|
+
};
|
|
989
|
+
}
|
|
990
|
+
exports.getEdgeClauseAndFields = getEdgeClauseAndFields;
|
|
1103
991
|
async function loadCustomEdges(options) {
|
|
1104
|
-
const {
|
|
992
|
+
const { cls: actualClause, fields, defaultOptions, tableName, } = await loadEgesInfo(options);
|
|
993
|
+
const rows = await loadRows({
|
|
994
|
+
tableName,
|
|
995
|
+
fields: fields,
|
|
996
|
+
clause: actualClause,
|
|
997
|
+
orderby: options.queryOptions?.orderby || defaultOptions.orderby,
|
|
998
|
+
limit: options.queryOptions?.limit || defaultOptions.limit,
|
|
999
|
+
context: options.context,
|
|
1000
|
+
});
|
|
1001
|
+
return rows.map((row) => {
|
|
1002
|
+
return new options.ctr(row);
|
|
1003
|
+
});
|
|
1004
|
+
}
|
|
1005
|
+
exports.loadCustomEdges = loadCustomEdges;
|
|
1006
|
+
async function loadEgesInfo(options, id2) {
|
|
1007
|
+
const { id1, edgeType } = options;
|
|
1105
1008
|
const edgeData = await loadEdgeData(edgeType);
|
|
1106
1009
|
if (!edgeData) {
|
|
1107
1010
|
throw new Error(`error loading edge data for ${edgeType}`);
|
|
1108
1011
|
}
|
|
1109
|
-
const defaultOptions = defaultEdgeQueryOptions(id1, edgeType);
|
|
1012
|
+
const defaultOptions = defaultEdgeQueryOptions(id1, edgeType, id2);
|
|
1110
1013
|
let cls = defaultOptions.clause;
|
|
1111
1014
|
if (options.queryOptions?.clause) {
|
|
1112
1015
|
cls = clause.And(cls, options.queryOptions.clause);
|
|
1113
1016
|
}
|
|
1114
|
-
|
|
1017
|
+
return {
|
|
1018
|
+
...getEdgeClauseAndFields(cls, options),
|
|
1019
|
+
defaultOptions,
|
|
1115
1020
|
tableName: edgeData.edgeTable,
|
|
1116
|
-
|
|
1117
|
-
clause: cls,
|
|
1118
|
-
orderby: options.queryOptions?.orderby || defaultOptions.orderby,
|
|
1119
|
-
limit: options.queryOptions?.limit || defaultOptions.limit,
|
|
1120
|
-
context,
|
|
1121
|
-
});
|
|
1122
|
-
return rows.map((row) => {
|
|
1123
|
-
return new options.ctr(row);
|
|
1124
|
-
});
|
|
1021
|
+
};
|
|
1125
1022
|
}
|
|
1126
|
-
exports.loadCustomEdges = loadCustomEdges;
|
|
1127
1023
|
async function loadUniqueEdge(options) {
|
|
1128
1024
|
const { id1, edgeType, context } = options;
|
|
1129
1025
|
const edgeData = await loadEdgeData(edgeType);
|
|
1130
1026
|
if (!edgeData) {
|
|
1131
1027
|
throw new Error(`error loading edge data for ${edgeType}`);
|
|
1132
1028
|
}
|
|
1029
|
+
const { cls, fields } = getEdgeClauseAndFields(clause.And(clause.Eq("id1", id1), clause.Eq("edge_type", edgeType)), options);
|
|
1133
1030
|
const row = await loadRow({
|
|
1134
1031
|
tableName: edgeData.edgeTable,
|
|
1135
|
-
fields:
|
|
1136
|
-
clause:
|
|
1032
|
+
fields: fields,
|
|
1033
|
+
clause: cls,
|
|
1137
1034
|
context,
|
|
1138
1035
|
});
|
|
1139
1036
|
if (!row) {
|
|
@@ -1160,21 +1057,28 @@ async function loadRawEdgeCountX(options) {
|
|
|
1160
1057
|
if (!edgeData) {
|
|
1161
1058
|
throw new Error(`error loading edge data for ${edgeType}`);
|
|
1162
1059
|
}
|
|
1060
|
+
const { cls } = getEdgeClauseAndFields(clause.And(clause.Eq("id1", id1), clause.Eq("edge_type", edgeType)), options);
|
|
1163
1061
|
const row = await loadRowX({
|
|
1164
1062
|
tableName: edgeData.edgeTable,
|
|
1165
1063
|
// sqlite needs as count otherwise it returns count(1)
|
|
1166
1064
|
fields: ["count(1) as count"],
|
|
1167
|
-
clause:
|
|
1065
|
+
clause: cls,
|
|
1168
1066
|
context,
|
|
1169
1067
|
});
|
|
1170
1068
|
return parseInt(row["count"], 10) || 0;
|
|
1171
1069
|
}
|
|
1172
1070
|
exports.loadRawEdgeCountX = loadRawEdgeCountX;
|
|
1173
1071
|
async function loadEdgeForID2(options) {
|
|
1174
|
-
|
|
1175
|
-
|
|
1176
|
-
|
|
1177
|
-
|
|
1072
|
+
const { cls: actualClause, fields, tableName, } = await loadEgesInfo(options, options.id2);
|
|
1073
|
+
const row = await loadRow({
|
|
1074
|
+
tableName,
|
|
1075
|
+
fields: fields,
|
|
1076
|
+
clause: actualClause,
|
|
1077
|
+
context: options.context,
|
|
1078
|
+
});
|
|
1079
|
+
if (row) {
|
|
1080
|
+
return new options.ctr(row);
|
|
1081
|
+
}
|
|
1178
1082
|
}
|
|
1179
1083
|
exports.loadEdgeForID2 = loadEdgeForID2;
|
|
1180
1084
|
async function loadNodesByEdge(viewer, id1, edgeType, options) {
|
|
@@ -1190,19 +1094,20 @@ async function loadNodesByEdge(viewer, id1, edgeType, options) {
|
|
|
1190
1094
|
}
|
|
1191
1095
|
exports.loadNodesByEdge = loadNodesByEdge;
|
|
1192
1096
|
async function applyPrivacyPolicyForRow(viewer, options, row) {
|
|
1193
|
-
|
|
1194
|
-
|
|
1195
|
-
}
|
|
1196
|
-
const ent = new options.ent(viewer, row);
|
|
1197
|
-
return await applyPrivacyPolicyForEnt(viewer, ent, row, options);
|
|
1097
|
+
const r = await applyPrivacyPolicyForRowImpl(viewer, options, row);
|
|
1098
|
+
return r instanceof Error ? null : r;
|
|
1198
1099
|
}
|
|
1199
1100
|
exports.applyPrivacyPolicyForRow = applyPrivacyPolicyForRow;
|
|
1101
|
+
async function applyPrivacyPolicyForRowImpl(viewer, options, row) {
|
|
1102
|
+
const ent = new options.ent(viewer, row);
|
|
1103
|
+
return applyPrivacyPolicyForEnt(viewer, ent, row, options);
|
|
1104
|
+
}
|
|
1200
1105
|
async function applyPrivacyPolicyForRowX(viewer, options, row) {
|
|
1201
1106
|
const ent = new options.ent(viewer, row);
|
|
1202
1107
|
return await applyPrivacyPolicyForEntX(viewer, ent, row, options);
|
|
1203
1108
|
}
|
|
1204
|
-
|
|
1205
|
-
async function
|
|
1109
|
+
// deprecated. doesn't use entcache
|
|
1110
|
+
async function applyPrivacyPolicyForRowsDeprecated(viewer, rows, options) {
|
|
1206
1111
|
let m = new Map();
|
|
1207
1112
|
// apply privacy logic
|
|
1208
1113
|
await Promise.all(rows.map(async (row) => {
|
|
@@ -1213,27 +1118,62 @@ async function applyPrivacyPolicyForRows(viewer, rows, options) {
|
|
|
1213
1118
|
}));
|
|
1214
1119
|
return m;
|
|
1215
1120
|
}
|
|
1216
|
-
|
|
1217
|
-
|
|
1218
|
-
|
|
1219
|
-
|
|
1220
|
-
|
|
1221
|
-
|
|
1222
|
-
|
|
1223
|
-
|
|
1224
|
-
|
|
1225
|
-
|
|
1121
|
+
async function applyPrivacyPolicyForRows(viewer, rows, options) {
|
|
1122
|
+
const result = new Array(rows.length);
|
|
1123
|
+
if (!rows.length) {
|
|
1124
|
+
return [];
|
|
1125
|
+
}
|
|
1126
|
+
const entLoader = getEntLoader(viewer, options);
|
|
1127
|
+
await Promise.all(rows.map(async (row, idx) => {
|
|
1128
|
+
const r = await applyPrivacyPolicyForRowAndStoreInEntLoader(viewer, row, options, entLoader);
|
|
1129
|
+
if (r instanceof ErrorWrapper) {
|
|
1130
|
+
return;
|
|
1131
|
+
}
|
|
1132
|
+
result[idx] = r;
|
|
1133
|
+
}));
|
|
1134
|
+
// filter ents that aren't visible because of privacy
|
|
1135
|
+
return result.filter((r) => r !== undefined);
|
|
1226
1136
|
}
|
|
1137
|
+
exports.applyPrivacyPolicyForRows = applyPrivacyPolicyForRows;
|
|
1227
1138
|
// given a viewer, an id pair, and a map of edgeEnum to EdgeType
|
|
1228
1139
|
// return the edgeEnum that's set in the group
|
|
1229
1140
|
async function getEdgeTypeInGroup(viewer, id1, id2, m) {
|
|
1230
1141
|
let promises = [];
|
|
1231
|
-
|
|
1232
|
-
|
|
1142
|
+
const edgeDatas = await loadEdgeDatas(...Array.from(m.values()));
|
|
1143
|
+
let tableToEdgeEnumMap = new Map();
|
|
1144
|
+
for (const [edgeEnum, edgeType] of m) {
|
|
1145
|
+
const edgeData = edgeDatas.get(edgeType);
|
|
1146
|
+
if (!edgeData) {
|
|
1147
|
+
throw new Error(`could not load edge data for '${edgeType}'`);
|
|
1148
|
+
}
|
|
1149
|
+
const l = tableToEdgeEnumMap.get(edgeData.edgeTable) ?? [];
|
|
1150
|
+
l.push(edgeEnum);
|
|
1151
|
+
tableToEdgeEnumMap.set(edgeData.edgeTable, l);
|
|
1233
1152
|
}
|
|
1153
|
+
tableToEdgeEnumMap.forEach((edgeEnums, tableName) => {
|
|
1154
|
+
promises.push((async () => {
|
|
1155
|
+
const edgeTypes = edgeEnums.map((edgeEnum) => m.get(edgeEnum));
|
|
1156
|
+
const { cls, fields } = getEdgeClauseAndFields(clause.And(clause.Eq("id1", id1), clause.In("edge_type", edgeTypes), clause.Eq("id2", id2)), {});
|
|
1157
|
+
const rows = await loadRows({
|
|
1158
|
+
tableName,
|
|
1159
|
+
fields,
|
|
1160
|
+
clause: cls,
|
|
1161
|
+
context: viewer.context,
|
|
1162
|
+
});
|
|
1163
|
+
const row = rows[0];
|
|
1164
|
+
if (row) {
|
|
1165
|
+
const edgeType = row.edge_type;
|
|
1166
|
+
for (const [k, v] of m) {
|
|
1167
|
+
if (v === edgeType) {
|
|
1168
|
+
return [k, new AssocEdge(row)];
|
|
1169
|
+
}
|
|
1170
|
+
}
|
|
1171
|
+
}
|
|
1172
|
+
})());
|
|
1173
|
+
});
|
|
1234
1174
|
const results = await Promise.all(promises);
|
|
1235
1175
|
for (const res of results) {
|
|
1236
|
-
if (res[1]) {
|
|
1176
|
+
if (res && res[1]) {
|
|
1237
1177
|
return [res[0], res[1]];
|
|
1238
1178
|
}
|
|
1239
1179
|
}
|