Wrap errors using db-errors
This commit is contained in:
parent
0f3f9404f9
commit
5f2ebf47eb
|
@ -7,6 +7,7 @@
|
||||||
"license": "WTFPL OR CC0-1.0",
|
"license": "WTFPL OR CC0-1.0",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"dataloader": "^2.2.2",
|
"dataloader": "^2.2.2",
|
||||||
|
"db-errors": "^0.2.3",
|
||||||
"match-value": "^1.1.0",
|
"match-value": "^1.1.0",
|
||||||
"syncpipe": "^1.0.0"
|
"syncpipe": "^1.0.0"
|
||||||
}
|
}
|
||||||
|
|
29
src/index.js
29
src/index.js
|
@ -2,6 +2,7 @@
|
||||||
|
|
||||||
const assert = require("assert");
|
const assert = require("assert");
|
||||||
const matchValue = require("match-value");
|
const matchValue = require("match-value");
|
||||||
|
const dbErrors = require("db-errors");
|
||||||
|
|
||||||
const knexIntrospect = require("./introspect");
|
const knexIntrospect = require("./introspect");
|
||||||
const dataloaderFromKnexResults = require("./dataloader-from-knex-results");
|
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
|
- 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) {
|
function applyFilterPredicate(query, column, predicate, negate = false) {
|
||||||
if (predicate.__comparator === "not") {
|
if (predicate.__comparator === "not") {
|
||||||
return applyFilterPredicate(query, column, predicate.comparator, true);
|
return applyFilterPredicate(query, column, predicate.comparator, true);
|
||||||
|
@ -111,9 +120,11 @@ module.exports = {
|
||||||
remoteTypes[remoteTypeName][remoteField] = async function(_, { $make, $getProperty, knex }) {
|
remoteTypes[remoteTypeName][remoteField] = async function(_, { $make, $getProperty, knex }) {
|
||||||
let tx = (knex ?? globalKnex);
|
let tx = (knex ?? globalKnex);
|
||||||
|
|
||||||
let result = await tx(tableName).columns("id").where({
|
let result = await wrapErrors(async () => {
|
||||||
|
return tx(tableName).columns("id").where({
|
||||||
[idFieldName]: await $getProperty(this, primaryKey)
|
[idFieldName]: await $getProperty(this, primaryKey)
|
||||||
});
|
});
|
||||||
|
});
|
||||||
|
|
||||||
return result.map((item) => {
|
return result.map((item) => {
|
||||||
return $make(typeName, { id: item.id });
|
return $make(typeName, { id: item.id });
|
||||||
|
@ -205,7 +216,9 @@ module.exports = {
|
||||||
query = query.limit(limit);
|
query = query.limit(limit);
|
||||||
}
|
}
|
||||||
|
|
||||||
let result = await query;
|
let result = await wrapErrors(async () => {
|
||||||
|
return query;
|
||||||
|
});
|
||||||
|
|
||||||
// TODO: Map result(s) to types
|
// TODO: Map result(s) to types
|
||||||
if (first === true) {
|
if (first === true) {
|
||||||
|
@ -227,7 +240,9 @@ module.exports = {
|
||||||
let query = tx(tableName).delete();
|
let query = tx(tableName).delete();
|
||||||
query = applyFilterArgument(query, filter);
|
query = applyFilterArgument(query, filter);
|
||||||
|
|
||||||
let deletedRowCount = await query;
|
let deletedRowCount = await wrapErrors(async () => {
|
||||||
|
return query;
|
||||||
|
});
|
||||||
|
|
||||||
return { count: deletedRowCount };
|
return { count: deletedRowCount };
|
||||||
},
|
},
|
||||||
|
@ -246,7 +261,9 @@ module.exports = {
|
||||||
applyFilterArgument(query, filter);
|
applyFilterArgument(query, filter);
|
||||||
}
|
}
|
||||||
|
|
||||||
let affected = await query;
|
let affected = await wrapErrors(async () => {
|
||||||
|
return query;
|
||||||
|
});
|
||||||
|
|
||||||
return affected.map((item) => {
|
return affected.map((item) => {
|
||||||
dlayerKnexTable.clear(item.id); // yeet from dataloader cache, so that we fetch the updated version instead
|
dlayerKnexTable.clear(item.id); // yeet from dataloader cache, so that we fetch the updated version instead
|
||||||
|
@ -263,7 +280,9 @@ module.exports = {
|
||||||
.insert(values)
|
.insert(values)
|
||||||
.returning("id");
|
.returning("id");
|
||||||
|
|
||||||
let result = await query;
|
let result = await wrapErrors(async () => {
|
||||||
|
return query;
|
||||||
|
});
|
||||||
|
|
||||||
if (isArray) {
|
if (isArray) {
|
||||||
return result.map((item) => {
|
return result.map((item) => {
|
||||||
|
|
|
@ -12,6 +12,11 @@ dataloader@^2.2.2:
|
||||||
resolved "https://registry.yarnpkg.com/dataloader/-/dataloader-2.2.2.tgz#216dc509b5abe39d43a9b9d97e6e5e473dfbe3e0"
|
resolved "https://registry.yarnpkg.com/dataloader/-/dataloader-2.2.2.tgz#216dc509b5abe39d43a9b9d97e6e5e473dfbe3e0"
|
||||||
integrity sha512-8YnDaaf7N3k/q5HnTJVuzSyLETjoZjVmHc4AeKAzOvKHEFQKcn64OKBfzHYtE9zGjctNM7V9I0MfnUVLpi7M5g==
|
integrity sha512-8YnDaaf7N3k/q5HnTJVuzSyLETjoZjVmHc4AeKAzOvKHEFQKcn64OKBfzHYtE9zGjctNM7V9I0MfnUVLpi7M5g==
|
||||||
|
|
||||||
|
db-errors@^0.2.3:
|
||||||
|
version "0.2.3"
|
||||||
|
resolved "https://registry.yarnpkg.com/db-errors/-/db-errors-0.2.3.tgz#a6a38952e00b20e790f2695a6446b3c65497ffa2"
|
||||||
|
integrity sha512-OOgqgDuCavHXjYSJoV2yGhv6SeG8nk42aoCSoyXLZUH7VwFG27rxbavU1z+VrZbZjphw5UkDQwUlD21MwZpUng==
|
||||||
|
|
||||||
match-value@^1.1.0:
|
match-value@^1.1.0:
|
||||||
version "1.1.0"
|
version "1.1.0"
|
||||||
resolved "https://registry.yarnpkg.com/match-value/-/match-value-1.1.0.tgz#ad311ef8bbe2d344a53ec3104e28fe221984b98e"
|
resolved "https://registry.yarnpkg.com/match-value/-/match-value-1.1.0.tgz#ad311ef8bbe2d344a53ec3104e28fe221984b98e"
|
||||||
|
|
Loading…
Reference in a new issue