pqb 0.3.7 → 0.3.9
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/dist/index.d.ts +31 -6
- package/dist/index.esm.js +57 -9
- package/dist/index.esm.js.map +1 -1
- package/dist/index.js +56 -7
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
- package/pnpm-lock.yaml +2821 -0
- package/src/errors.ts +44 -0
- package/src/queryMethods/then.test.ts +84 -4
- package/src/queryMethods/then.ts +51 -18
- package/src/test-utils.ts +8 -0
package/dist/index.js
CHANGED
|
@@ -783,6 +783,26 @@ const aggregateToSql = (ctx, model, item, quotedAs) => {
|
|
|
783
783
|
|
|
784
784
|
class PormError extends Error {
|
|
785
785
|
}
|
|
786
|
+
class QueryError extends pg.DatabaseError {
|
|
787
|
+
get isUnique() {
|
|
788
|
+
return this.code === "23505";
|
|
789
|
+
}
|
|
790
|
+
get columns() {
|
|
791
|
+
var _a;
|
|
792
|
+
if (this.columnsCache)
|
|
793
|
+
return this.columnsCache;
|
|
794
|
+
const columns = {};
|
|
795
|
+
if (this.detail) {
|
|
796
|
+
const list = (_a = this.detail.match(/\((.*)\)=/)) == null ? void 0 : _a[1];
|
|
797
|
+
if (list) {
|
|
798
|
+
list.split(", ").forEach((column) => {
|
|
799
|
+
columns[column.startsWith('"') ? column.slice(1, -1) : column] = true;
|
|
800
|
+
});
|
|
801
|
+
}
|
|
802
|
+
}
|
|
803
|
+
return this.columnsCache = columns;
|
|
804
|
+
}
|
|
805
|
+
}
|
|
786
806
|
class NotFoundError extends PormError {
|
|
787
807
|
constructor(message = "Record is not found") {
|
|
788
808
|
super(message);
|
|
@@ -3195,13 +3215,11 @@ const queryMethodByReturnType = {
|
|
|
3195
3215
|
rowCount: "arrays",
|
|
3196
3216
|
void: "arrays"
|
|
3197
3217
|
};
|
|
3218
|
+
let queryError = void 0;
|
|
3198
3219
|
class Then {
|
|
3199
|
-
then(
|
|
3200
|
-
|
|
3201
|
-
|
|
3202
|
-
} else {
|
|
3203
|
-
return then(this, resolve, reject);
|
|
3204
|
-
}
|
|
3220
|
+
get then() {
|
|
3221
|
+
queryError = new QueryError();
|
|
3222
|
+
return maybeWrappedThen;
|
|
3205
3223
|
}
|
|
3206
3224
|
async catch(fn) {
|
|
3207
3225
|
return this.then(void 0, fn);
|
|
@@ -3210,6 +3228,13 @@ class Then {
|
|
|
3210
3228
|
const handleResult = async (q, result) => {
|
|
3211
3229
|
return parseResult(q, q.query.returnType || "all", result);
|
|
3212
3230
|
};
|
|
3231
|
+
function maybeWrappedThen(resolve, reject) {
|
|
3232
|
+
if (this.query.wrapInTransaction && !this.query.inTransaction) {
|
|
3233
|
+
return this.transaction((q) => then(q, resolve, reject));
|
|
3234
|
+
} else {
|
|
3235
|
+
return then(this, resolve, reject);
|
|
3236
|
+
}
|
|
3237
|
+
}
|
|
3213
3238
|
const then = async (q, resolve, reject) => {
|
|
3214
3239
|
let sql;
|
|
3215
3240
|
let logData;
|
|
@@ -3249,13 +3274,36 @@ const then = async (q, resolve, reject) => {
|
|
|
3249
3274
|
);
|
|
3250
3275
|
}
|
|
3251
3276
|
resolve == null ? void 0 : resolve(result);
|
|
3252
|
-
} catch (
|
|
3277
|
+
} catch (err) {
|
|
3278
|
+
const error = err instanceof pg.DatabaseError ? assignError(queryError, err) : err;
|
|
3253
3279
|
if (q.query.log && sql && logData) {
|
|
3254
3280
|
q.query.log.onError(error, sql, logData);
|
|
3255
3281
|
}
|
|
3256
3282
|
reject == null ? void 0 : reject(error);
|
|
3257
3283
|
}
|
|
3258
3284
|
};
|
|
3285
|
+
const assignError = (to, from) => {
|
|
3286
|
+
to.message = from.message;
|
|
3287
|
+
to.length = from.length;
|
|
3288
|
+
to.name = from.name;
|
|
3289
|
+
to.severity = from.severity;
|
|
3290
|
+
to.code = from.code;
|
|
3291
|
+
to.detail = from.detail;
|
|
3292
|
+
to.hint = from.hint;
|
|
3293
|
+
to.position = from.position;
|
|
3294
|
+
to.internalPosition = from.internalPosition;
|
|
3295
|
+
to.internalQuery = from.internalQuery;
|
|
3296
|
+
to.where = from.where;
|
|
3297
|
+
to.schema = from.schema;
|
|
3298
|
+
to.table = from.table;
|
|
3299
|
+
to.column = from.column;
|
|
3300
|
+
to.dataType = from.dataType;
|
|
3301
|
+
to.constraint = from.constraint;
|
|
3302
|
+
to.file = from.file;
|
|
3303
|
+
to.line = from.line;
|
|
3304
|
+
to.routine = from.routine;
|
|
3305
|
+
return to;
|
|
3306
|
+
};
|
|
3259
3307
|
const parseResult = (q, returnType = "all", result) => {
|
|
3260
3308
|
var _a, _b;
|
|
3261
3309
|
switch (returnType) {
|
|
@@ -5673,6 +5721,7 @@ exports.PolygonColumn = PolygonColumn;
|
|
|
5673
5721
|
exports.PormError = PormError;
|
|
5674
5722
|
exports.PormInternalError = PormInternalError;
|
|
5675
5723
|
exports.QueryCallbacks = QueryCallbacks;
|
|
5724
|
+
exports.QueryError = QueryError;
|
|
5676
5725
|
exports.QueryGet = QueryGet;
|
|
5677
5726
|
exports.QueryLog = QueryLog;
|
|
5678
5727
|
exports.QueryMethods = QueryMethods;
|