pqb 0.3.8 → 0.4.0

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.js CHANGED
@@ -783,6 +783,27 @@ 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((item) => {
799
+ const column = item.startsWith('"') ? item.slice(1, -1) : item;
800
+ columns[column] = true;
801
+ });
802
+ }
803
+ }
804
+ return this.columnsCache = columns;
805
+ }
806
+ }
786
807
  class NotFoundError extends PormError {
787
808
  constructor(message = "Record is not found") {
788
809
  super(message);
@@ -3195,13 +3216,11 @@ const queryMethodByReturnType = {
3195
3216
  rowCount: "arrays",
3196
3217
  void: "arrays"
3197
3218
  };
3219
+ let queryError = void 0;
3198
3220
  class Then {
3199
- then(resolve, reject) {
3200
- if (this.query.wrapInTransaction && !this.query.inTransaction) {
3201
- return this.transaction((q) => then(q, resolve, reject));
3202
- } else {
3203
- return then(this, resolve, reject);
3204
- }
3221
+ get then() {
3222
+ queryError = new Error();
3223
+ return maybeWrappedThen;
3205
3224
  }
3206
3225
  async catch(fn) {
3207
3226
  return this.then(void 0, fn);
@@ -3210,6 +3229,13 @@ class Then {
3210
3229
  const handleResult = async (q, result) => {
3211
3230
  return parseResult(q, q.query.returnType || "all", result);
3212
3231
  };
3232
+ function maybeWrappedThen(resolve, reject) {
3233
+ if (this.query.wrapInTransaction && !this.query.inTransaction) {
3234
+ return this.transaction((q) => then(q, resolve, reject));
3235
+ } else {
3236
+ return then(this, resolve, reject);
3237
+ }
3238
+ }
3213
3239
  const then = async (q, resolve, reject) => {
3214
3240
  let sql;
3215
3241
  let logData;
@@ -3249,13 +3275,38 @@ const then = async (q, resolve, reject) => {
3249
3275
  );
3250
3276
  }
3251
3277
  resolve == null ? void 0 : resolve(result);
3252
- } catch (error) {
3278
+ } catch (err) {
3279
+ const error = err instanceof pg.DatabaseError ? assignError(q, err) : err;
3253
3280
  if (q.query.log && sql && logData) {
3254
3281
  q.query.log.onError(error, sql, logData);
3255
3282
  }
3256
3283
  reject == null ? void 0 : reject(error);
3257
3284
  }
3258
3285
  };
3286
+ const assignError = (q, from) => {
3287
+ const to = new q.error();
3288
+ to.stack = queryError.stack;
3289
+ to.message = from.message;
3290
+ to.length = from.length;
3291
+ to.name = from.name;
3292
+ to.severity = from.severity;
3293
+ to.code = from.code;
3294
+ to.detail = from.detail;
3295
+ to.hint = from.hint;
3296
+ to.position = from.position;
3297
+ to.internalPosition = from.internalPosition;
3298
+ to.internalQuery = from.internalQuery;
3299
+ to.where = from.where;
3300
+ to.schema = from.schema;
3301
+ to.table = from.table;
3302
+ to.column = from.column;
3303
+ to.dataType = from.dataType;
3304
+ to.constraint = from.constraint;
3305
+ to.file = from.file;
3306
+ to.line = from.line;
3307
+ to.routine = from.routine;
3308
+ return to;
3309
+ };
3259
3310
  const parseResult = (q, returnType = "all", result) => {
3260
3311
  var _a, _b;
3261
3312
  switch (returnType) {
@@ -5544,7 +5595,7 @@ class Db {
5544
5595
  const defaultSelect = this.defaultSelectColumns.length === columns.length ? void 0 : this.defaultSelectColumns;
5545
5596
  const columnsParsers = {};
5546
5597
  let hasParsers = false;
5547
- let modifyQuery;
5598
+ let modifyQuery = void 0;
5548
5599
  for (const key in shape) {
5549
5600
  const column = shape[key];
5550
5601
  if (column.parseFn) {
@@ -5565,6 +5616,8 @@ class Db {
5565
5616
  } : toSql;
5566
5617
  this.relations = {};
5567
5618
  modifyQuery == null ? void 0 : modifyQuery.forEach((cb) => cb(this));
5619
+ this.error = class extends QueryError {
5620
+ };
5568
5621
  }
5569
5622
  }
5570
5623
  applyMixins(Db, [QueryMethods]);
@@ -5673,6 +5726,7 @@ exports.PolygonColumn = PolygonColumn;
5673
5726
  exports.PormError = PormError;
5674
5727
  exports.PormInternalError = PormInternalError;
5675
5728
  exports.QueryCallbacks = QueryCallbacks;
5729
+ exports.QueryError = QueryError;
5676
5730
  exports.QueryGet = QueryGet;
5677
5731
  exports.QueryLog = QueryLog;
5678
5732
  exports.QueryMethods = QueryMethods;