|
|
|
@ -2,6 +2,7 @@
|
|
|
|
|
|
|
|
|
|
const assert = require("assert");
|
|
|
|
|
const matchValue = require("match-value");
|
|
|
|
|
const dbErrors = require("db-errors");
|
|
|
|
|
|
|
|
|
|
const knexIntrospect = require("./introspect");
|
|
|
|
|
const dataloaderFromKnexResults = require("./dataloader-from-knex-results");
|
|
|
|
@ -12,6 +13,14 @@ const dataloaderFromKnexResults = require("./dataloader-from-knex-results");
|
|
|
|
|
- Update record: give each type an update method for that individual record, by ID? For "get previous value, then set new value" style usecases
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
async function wrapErrors(callback) {
|
|
|
|
|
try {
|
|
|
|
|
return callback();
|
|
|
|
|
} catch (error) {
|
|
|
|
|
throw dbErrors.wrapError(error);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function applyFilterPredicate(query, column, predicate, negate = false) {
|
|
|
|
|
if (predicate.__comparator === "not") {
|
|
|
|
|
return applyFilterPredicate(query, column, predicate.comparator, true);
|
|
|
|
@ -111,8 +120,10 @@ module.exports = {
|
|
|
|
|
remoteTypes[remoteTypeName][remoteField] = async function(_, { $make, $getProperty, knex }) {
|
|
|
|
|
let tx = (knex ?? globalKnex);
|
|
|
|
|
|
|
|
|
|
let result = await tx(tableName).columns("id").where({
|
|
|
|
|
[idFieldName]: await $getProperty(this, primaryKey)
|
|
|
|
|
let result = await wrapErrors(async () => {
|
|
|
|
|
return tx(tableName).columns("id").where({
|
|
|
|
|
[idFieldName]: await $getProperty(this, primaryKey)
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return result.map((item) => {
|
|
|
|
@ -205,7 +216,9 @@ module.exports = {
|
|
|
|
|
query = query.limit(limit);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let result = await query;
|
|
|
|
|
let result = await wrapErrors(async () => {
|
|
|
|
|
return query;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// TODO: Map result(s) to types
|
|
|
|
|
if (first === true) {
|
|
|
|
@ -227,7 +240,9 @@ module.exports = {
|
|
|
|
|
let query = tx(tableName).delete();
|
|
|
|
|
query = applyFilterArgument(query, filter);
|
|
|
|
|
|
|
|
|
|
let deletedRowCount = await query;
|
|
|
|
|
let deletedRowCount = await wrapErrors(async () => {
|
|
|
|
|
return query;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return { count: deletedRowCount };
|
|
|
|
|
},
|
|
|
|
@ -246,7 +261,9 @@ module.exports = {
|
|
|
|
|
applyFilterArgument(query, filter);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let affected = await query;
|
|
|
|
|
let affected = await wrapErrors(async () => {
|
|
|
|
|
return query;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return affected.map((item) => {
|
|
|
|
|
dlayerKnexTable.clear(item.id); // yeet from dataloader cache, so that we fetch the updated version instead
|
|
|
|
@ -263,7 +280,9 @@ module.exports = {
|
|
|
|
|
.insert(values)
|
|
|
|
|
.returning("id");
|
|
|
|
|
|
|
|
|
|
let result = await query;
|
|
|
|
|
let result = await wrapErrors(async () => {
|
|
|
|
|
return query;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (isArray) {
|
|
|
|
|
return result.map((item) => {
|
|
|
|
|