@snowtop/ent 0.1.0-alpha75 → 0.1.0-alpha76
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/core/ent.js +32 -60
- package/core/loaders/object_loader.js +6 -0
- package/package.json +1 -1
package/core/ent.js
CHANGED
|
@@ -128,7 +128,7 @@ async function applyPrivacyPolicyForRowAndStoreInCacheX(viewer, options, row, in
|
|
|
128
128
|
throw ent;
|
|
129
129
|
}
|
|
130
130
|
if (ent === null) {
|
|
131
|
-
throw new Error(`
|
|
131
|
+
throw new Error(`applyPrivacyPolicyForRowImpl returned null when it shouldn't. ent error`);
|
|
132
132
|
}
|
|
133
133
|
return ent;
|
|
134
134
|
}
|
|
@@ -151,6 +151,7 @@ async function loadEntViaKey(viewer, key, options) {
|
|
|
151
151
|
return null;
|
|
152
152
|
}
|
|
153
153
|
// TODO every row.id needs to be audited...
|
|
154
|
+
// https://github.com/lolopinto/ent/issues/1064
|
|
154
155
|
const info = entFromCacheMaybe(viewer, row.id, options);
|
|
155
156
|
if (info.ent !== undefined) {
|
|
156
157
|
return info.ent;
|
|
@@ -158,7 +159,6 @@ async function loadEntViaKey(viewer, key, options) {
|
|
|
158
159
|
return applyPrivacyPolicyForRowAndStoreInCache(viewer, options, row, info);
|
|
159
160
|
}
|
|
160
161
|
exports.loadEntViaKey = loadEntViaKey;
|
|
161
|
-
// need a cached error...
|
|
162
162
|
async function loadEntX(viewer, id, options) {
|
|
163
163
|
const info = entFromCacheMaybe(viewer, id, options);
|
|
164
164
|
if (info.error !== undefined) {
|
|
@@ -175,8 +175,6 @@ async function loadEntX(viewer, id, options) {
|
|
|
175
175
|
return applyPrivacyPolicyForRowAndStoreInCacheX(viewer, options, row, info);
|
|
176
176
|
}
|
|
177
177
|
exports.loadEntX = loadEntX;
|
|
178
|
-
// TODO test this and loadEntViaKey
|
|
179
|
-
// replace loadEntViaClause??
|
|
180
178
|
async function loadEntXViaKey(viewer, key, options) {
|
|
181
179
|
const row = await options.loaderFactory
|
|
182
180
|
.createLoader(viewer.context)
|
|
@@ -242,6 +240,7 @@ async function loadEnts(viewer, options, ...ids) {
|
|
|
242
240
|
});
|
|
243
241
|
if (ent === null) {
|
|
244
242
|
// TODO this should return null if not loadable...
|
|
243
|
+
// https://github.com/lolopinto/ent/issues/1070
|
|
245
244
|
continue;
|
|
246
245
|
}
|
|
247
246
|
// @ts-ignore
|
|
@@ -282,12 +281,11 @@ async function loadEnts(viewer, options, ...ids) {
|
|
|
282
281
|
}
|
|
283
282
|
if (ent !== undefined) {
|
|
284
283
|
// TODO this should return null if not loadable...?
|
|
284
|
+
// TODO https://github.com/lolopinto/ent/issues/1070
|
|
285
285
|
m.set(id, ent);
|
|
286
286
|
}
|
|
287
287
|
}
|
|
288
288
|
return m;
|
|
289
|
-
// TODO do we want to change this to be a map not a list so that it's easy to check for existence?
|
|
290
|
-
// TODO eventually this should be doing a cache then db queyr and maybe depend on dataloader to get all the results at once
|
|
291
289
|
}
|
|
292
290
|
exports.loadEnts = loadEnts;
|
|
293
291
|
// calls loadEnts and returns the results sorted in the order they were passed in
|
|
@@ -324,6 +322,7 @@ async function loadCustomEnts(viewer, options, query) {
|
|
|
324
322
|
const result = new Array(rows.length);
|
|
325
323
|
await Promise.all(rows.map(async (row, idx) => {
|
|
326
324
|
// TODO what if key is different
|
|
325
|
+
// TODO https://github.com/lolopinto/ent/issues/1064
|
|
327
326
|
const info = entFromCacheMaybe(viewer, row.id, options);
|
|
328
327
|
if (info.ent !== undefined) {
|
|
329
328
|
if (info.ent === null) {
|
|
@@ -533,42 +532,27 @@ async function loadRow(options) {
|
|
|
533
532
|
}
|
|
534
533
|
const query = buildQuery(options);
|
|
535
534
|
logQuery(query, options.clause.logValues());
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
if (res.rowCount
|
|
540
|
-
|
|
541
|
-
(0, logger_1.log)("error", "got more than one row for query " + query);
|
|
542
|
-
}
|
|
543
|
-
return null;
|
|
544
|
-
}
|
|
545
|
-
// put the row in the cache...
|
|
546
|
-
if (cache) {
|
|
547
|
-
cache.primeCache(options, res.rows[0]);
|
|
535
|
+
const pool = db_1.default.getInstance().getPool();
|
|
536
|
+
const res = await pool.query(query, options.clause.values());
|
|
537
|
+
if (res.rowCount != 1) {
|
|
538
|
+
if (res.rowCount > 1) {
|
|
539
|
+
(0, logger_1.log)("error", "got more than one row for query " + query);
|
|
548
540
|
}
|
|
549
|
-
return res.rows[0];
|
|
550
|
-
}
|
|
551
|
-
catch (e) {
|
|
552
|
-
// an example of an error being suppressed
|
|
553
|
-
// another one. TODO https://github.com/lolopinto/ent/issues/862
|
|
554
|
-
(0, logger_1.log)("error", e);
|
|
555
541
|
return null;
|
|
556
542
|
}
|
|
543
|
+
// put the row in the cache...
|
|
544
|
+
if (cache) {
|
|
545
|
+
cache.primeCache(options, res.rows[0]);
|
|
546
|
+
}
|
|
547
|
+
return res.rows[0];
|
|
557
548
|
}
|
|
558
549
|
exports.loadRow = loadRow;
|
|
559
550
|
// this always goes to the db, no cache, nothing
|
|
560
551
|
async function performRawQuery(query, values, logValues) {
|
|
561
552
|
const pool = db_1.default.getInstance().getPool();
|
|
562
553
|
logQuery(query, logValues || []);
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
return res.rows;
|
|
566
|
-
}
|
|
567
|
-
catch (e) {
|
|
568
|
-
// TODO need to change every query to catch an error!
|
|
569
|
-
(0, logger_1.log)("error", e);
|
|
570
|
-
return [];
|
|
571
|
-
}
|
|
554
|
+
const res = await pool.queryAll(query, values);
|
|
555
|
+
return res.rows;
|
|
572
556
|
}
|
|
573
557
|
exports.performRawQuery = performRawQuery;
|
|
574
558
|
// TODO this should throw, we can't be hiding errors here
|
|
@@ -1094,40 +1078,26 @@ function isSyncQueryer(queryer) {
|
|
|
1094
1078
|
async function mutateRow(queryer, query, values, logValues, options) {
|
|
1095
1079
|
logQuery(query, logValues);
|
|
1096
1080
|
let cache = options.context?.cache;
|
|
1097
|
-
|
|
1098
|
-
|
|
1099
|
-
|
|
1100
|
-
res = queryer.execSync(query, values);
|
|
1101
|
-
}
|
|
1102
|
-
else {
|
|
1103
|
-
res = await queryer.exec(query, values);
|
|
1104
|
-
}
|
|
1105
|
-
if (cache) {
|
|
1106
|
-
cache.clearCache();
|
|
1107
|
-
}
|
|
1108
|
-
return res;
|
|
1081
|
+
let res;
|
|
1082
|
+
if (isSyncQueryer(queryer)) {
|
|
1083
|
+
res = queryer.execSync(query, values);
|
|
1109
1084
|
}
|
|
1110
|
-
|
|
1111
|
-
|
|
1112
|
-
(0, logger_1.log)("error", err);
|
|
1113
|
-
throw err;
|
|
1085
|
+
else {
|
|
1086
|
+
res = await queryer.exec(query, values);
|
|
1114
1087
|
}
|
|
1088
|
+
if (cache) {
|
|
1089
|
+
cache.clearCache();
|
|
1090
|
+
}
|
|
1091
|
+
return res;
|
|
1115
1092
|
}
|
|
1116
1093
|
function mutateRowSync(queryer, query, values, logValues, options) {
|
|
1117
1094
|
logQuery(query, logValues);
|
|
1118
1095
|
let cache = options.context?.cache;
|
|
1119
|
-
|
|
1120
|
-
|
|
1121
|
-
|
|
1122
|
-
cache.clearCache();
|
|
1123
|
-
}
|
|
1124
|
-
return res;
|
|
1125
|
-
}
|
|
1126
|
-
catch (err) {
|
|
1127
|
-
// TODO:::why is this not rethrowing?
|
|
1128
|
-
(0, logger_1.log)("error", err);
|
|
1129
|
-
throw err;
|
|
1096
|
+
const res = queryer.execSync(query, values);
|
|
1097
|
+
if (cache) {
|
|
1098
|
+
cache.clearCache();
|
|
1130
1099
|
}
|
|
1100
|
+
return res;
|
|
1131
1101
|
}
|
|
1132
1102
|
function buildInsertQuery(options, suffix) {
|
|
1133
1103
|
let fields = [];
|
|
@@ -1516,6 +1486,8 @@ async function applyPrivacyPolicyForRowX(viewer, options, row) {
|
|
|
1516
1486
|
return await applyPrivacyPolicyForEntX(viewer, ent, row, options);
|
|
1517
1487
|
}
|
|
1518
1488
|
// TODO this needs to be changed to use ent cache as needed...
|
|
1489
|
+
// most current callsites fine not using it
|
|
1490
|
+
// custom_query is one that should be updated
|
|
1519
1491
|
async function applyPrivacyPolicyForRows(viewer, rows, options) {
|
|
1520
1492
|
let m = new Map();
|
|
1521
1493
|
// apply privacy logic
|
|
@@ -71,6 +71,9 @@ function createDataLoader(options) {
|
|
|
71
71
|
const rows = await (0, ent_1.loadRows)(rowOptions);
|
|
72
72
|
for (const row of rows) {
|
|
73
73
|
const id = row[col];
|
|
74
|
+
if (id === undefined) {
|
|
75
|
+
throw new Error(`need to query for column ${col} when using an object loader because the query may not be sorted and we need the id to maintain sort order`);
|
|
76
|
+
}
|
|
74
77
|
const idx = m.get(id);
|
|
75
78
|
if (idx === undefined) {
|
|
76
79
|
throw new Error(`malformed query. got ${id} back but didn't query for it`);
|
|
@@ -195,6 +198,9 @@ class ObjectLoader {
|
|
|
195
198
|
}
|
|
196
199
|
}
|
|
197
200
|
exports.ObjectLoader = ObjectLoader;
|
|
201
|
+
// NOTE: if not querying for all columns
|
|
202
|
+
// have to query for the id field as one of the fields
|
|
203
|
+
// because it's used to maintain sort order of the queried ids
|
|
198
204
|
class ObjectLoaderFactory {
|
|
199
205
|
constructor(options) {
|
|
200
206
|
this.options = options;
|