pqb 0.1.4 → 0.1.5

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 CHANGED
@@ -608,7 +608,9 @@ declare type CommonQueryData = {
608
608
  from?: string | Query | RawExpression;
609
609
  and?: WhereItem[];
610
610
  or?: WhereItem[][];
611
+ coalesceValue?: unknown | RawExpression;
611
612
  parsers?: ColumnsParsers;
613
+ notFoundDefault?: unknown;
612
614
  defaults?: Record<string, unknown>;
613
615
  beforeQuery?: BeforeCallback<Query>[];
614
616
  afterQuery?: AfterCallback<Query>[];
@@ -1463,8 +1465,8 @@ declare class QueryMethods {
1463
1465
  _limit<T extends Query>(this: T, arg: number | undefined): T;
1464
1466
  offset<T extends Query>(this: T, arg: number | undefined): T;
1465
1467
  _offset<T extends Query>(this: T, arg: number | undefined): T;
1466
- exists<T extends Query>(this: T): SetQueryReturnsValueOptional<T, NumberColumn>;
1467
- _exists<T extends Query>(this: T): SetQueryReturnsValueOptional<T, NumberColumn>;
1468
+ exists<T extends Query>(this: T): SetQueryReturnsValue<T, BooleanColumn>;
1469
+ _exists<T extends Query>(this: T): SetQueryReturnsValue<T, BooleanColumn>;
1468
1470
  truncate<T extends Query>(this: T, options?: {
1469
1471
  restartIdentity?: boolean;
1470
1472
  cascade?: boolean;
package/dist/index.esm.js CHANGED
@@ -2591,7 +2591,13 @@ const pushSubQuerySql = (query, as, values, list) => {
2591
2591
  default:
2592
2592
  throw new UnhandledTypeError(returnType);
2593
2593
  }
2594
- list.push(`(${query.toSql(values).text}) AS ${q(as)}`);
2594
+ let subQuerySql = `(${query.toSql(values).text})`;
2595
+ const { coalesceValue } = query.query;
2596
+ if (coalesceValue !== void 0) {
2597
+ const value = typeof coalesceValue === "object" && coalesceValue && isRaw(coalesceValue) ? getRaw(coalesceValue, values) : quote(coalesceValue);
2598
+ subQuerySql = `COALESCE(${subQuerySql}, ${value})`;
2599
+ }
2600
+ list.push(`${subQuerySql} AS ${q(as)}`);
2595
2601
  };
2596
2602
 
2597
2603
  const aggregateOptionNames = [
@@ -3434,7 +3440,7 @@ const parseResult = (q, returnType, result) => {
3434
3440
  }
3435
3441
  case "value": {
3436
3442
  const value = (_a = result.rows[0]) == null ? void 0 : _a[0];
3437
- return value !== void 0 ? parseValue(value, q) : void 0;
3443
+ return value !== void 0 ? parseValue(value, q) : q.query.notFoundDefault;
3438
3444
  }
3439
3445
  case "valueOrThrow": {
3440
3446
  const value = (_b = result.rows[0]) == null ? void 0 : _b[0];
@@ -5334,7 +5340,9 @@ class QueryMethods {
5334
5340
  return this.clone()._exists();
5335
5341
  }
5336
5342
  _exists() {
5337
- const q = this._getOptional(raw("1"));
5343
+ const q = this._getOptional(raw("true"));
5344
+ q.query.notFoundDefault = false;
5345
+ q.query.coalesceValue = false;
5338
5346
  delete q.query.take;
5339
5347
  return q;
5340
5348
  }