pqb 0.36.13 → 0.36.15

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
@@ -1,6 +1,6 @@
1
1
  import { PoolConfig, Pool, PoolClient } from 'pg';
2
2
  import * as orchid_core from 'orchid-core';
3
- import { QueryResultRow, AdapterConfigBase, AdapterBase, QueryInput, SingleSqlItem, Sql, RecordUnknown, RecordKeyTrue, EmptyObject, QueryBaseCommon, QueryColumns, QueryMetaBase, QueryReturnType, QueryThen, Expression, MaybeArray, TemplateLiteralArgs, QueryColumn, MaybePromise, FnUnknownToUnknown, RecordString, ColumnsShapeBase, ColumnsParsers, PickQueryTable, BatchParsers, HookSelect, QueryLogObject, QueryLogger, QueryDataTransform, ExpressionChain, getValueKey, PickQueryShape, PickQueryTableMetaResult, EmptyTuple, PickQueryMeta, PickQueryMetaResultReturnType, QueryColumnToNullable, SelectableBase, PickQueryMetaShape, PickQueryTableMetaResultShape, PickQueryMetaResultWindows, PickOutputTypeAndOperators, PickQueryResult, ExpressionData, ValExpression, PickOutputType, SQLQueryArgs, ColumnSchemaConfig, DateColumnData, ColumnToCodeCtx, Code, TimeInterval, ColumnTypeSchemaArg, ColumnDataBase, ArrayMethodsData, RawSQLBase, RawSQLValues, ExpressionTypeMethod, DynamicSQLArg, StaticSQLArgs, ForeignKeyTable, ColumnNameOfTable, BaseNumberData, PickColumnBaseData, ColumnWithDefault, StringTypeData, PrimaryKeyColumn, ParseColumn, EncodeColumn, QueryColumnsInit, QueryLogOptions, DefaultSelectColumns, DbBase, QueryCatch, TransactionState, ColumnTypeBase, PickQueryUniqueProperties, PickQueryMetaResult, IsQuery, PickQueryTableMetaResultInputType, MergeObjects, PickQueryResultUniqueColumns, QueryInternalBase, PickQueryReturnType, PickType, ColumnShapeOutput, OperatorsNullable, PickQueryMetaReturnType, UniqueColumn, TimestampHelpers, Codes, ColumnDataCheckBase } from 'orchid-core';
3
+ import { QueryResultRow, AdapterConfigBase, AdapterBase, QueryInput, SingleSqlItem, Sql, RecordUnknown, RecordKeyTrue, EmptyObject, QueryBaseCommon, QueryColumns, QueryMetaBase, QueryReturnType, QueryThen, Expression, MaybeArray, TemplateLiteralArgs, QueryColumn, MaybePromise, FnUnknownToUnknown, RecordString, ColumnsShapeBase, ColumnsParsers, PickQueryTable, BatchParsers, HookSelect, QueryLogObject, QueryLogger, QueryDataTransform, ExpressionChain, getValueKey, PickQueryShape, PickQueryTableMetaResult, EmptyTuple, PickQueryMeta, PickQueryMetaResultReturnType, QueryColumnToNullable, SelectableBase, PickQueryMetaShape, PickQueryTableMetaResultShape, PickQueryMetaResultWindows, PickOutputTypeAndOperators, PickQueryResult, ExpressionData, ValExpression, PickOutputType, SQLQueryArgs, ColumnSchemaConfig, DateColumnData, ColumnToCodeCtx, Code, TimeInterval, ColumnTypeSchemaArg, ColumnDataBase, ArrayMethodsData, RawSQLBase, RawSQLValues, ExpressionTypeMethod, DynamicSQLArg, StaticSQLArgs, ForeignKeyTable, ColumnNameOfTable, BaseNumberData, PickColumnBaseData, ColumnWithDefault, StringTypeData, PrimaryKeyColumn, ParseColumn, EncodeColumn, QueryColumnsInit, QueryLogOptions, DefaultSelectColumns, DbBase, QueryCatch, TransactionState, ColumnTypeBase, PickQueryUniqueProperties, PickQueryMetaResult, IsQuery, PickQueryTableMetaResultInputType, PickQueryResultUniqueColumns, QueryInternalBase, PickQueryReturnType, PickType, ColumnShapeOutput, OperatorsNullable, PickQueryMetaReturnType, UniqueColumn, TimestampHelpers, Codes, ColumnDataCheckBase } from 'orchid-core';
4
4
  import { inspect } from 'node:util';
5
5
  import { AsyncLocalStorage } from 'node:async_hooks';
6
6
 
@@ -2209,22 +2209,16 @@ declare class Where {
2209
2209
  *
2210
2210
  * ### JSONB column operators
2211
2211
  *
2212
- * For the `jsonb` column, note that the `json` type has text operators instead.
2212
+ * JSON functions are available only for the `jsonb` column, note that the `json` type has text operators instead.
2213
2213
  *
2214
- * `jsonPath` operator: compare a column value under a given JSON path with the provided value.
2214
+ * You can use [jsonPathQueryFirst](/guide/json.html#jsonpathqueryfirst) to filter by a JSON value, follow the link for details.
2215
2215
  *
2216
- * Value can be of any type to compare with JSON value, or it can be a sub-query or a raw SQL expression.
2216
+ * The value can be of any type, it can also be returned from a sub-query, raw SQL expression.
2217
2217
  *
2218
2218
  * ```ts
2219
- * db.table.where({
2220
- * jsonbColumn: {
2221
- * jsonPath: [
2222
- * '$.name', // first element is JSON path
2223
- * '=', // second argument is comparison operator
2224
- * 'value', // third argument is a value to compare with
2225
- * ],
2226
- * },
2227
- * });
2219
+ * db.table.where((q) =>
2220
+ * q.get('jsonbColumn').jsonPathQueryFirst('$.name').equals(value),
2221
+ * );
2228
2222
  * ```
2229
2223
  *
2230
2224
  * `jsonSupersetOf`: check if the column value is a superset of provided value.
@@ -2250,7 +2244,7 @@ declare class Where {
2250
2244
  * ```ts
2251
2245
  * db.table.where({
2252
2246
  * jsonbColumn: {
2253
- * jsonSupersetOf: { a: 1 },
2247
+ * jsonSubsetOf: { a: 1 },
2254
2248
  * },
2255
2249
  * });
2256
2250
  * ```
@@ -3285,9 +3279,16 @@ type DbOptions<SchemaConfig extends ColumnSchemaConfig, ColumnTypes> = ({
3285
3279
  };
3286
3280
  interface DbTableOptions<ColumnTypes, Table extends string | undefined, Shape extends QueryColumns> extends QueryLogOptions {
3287
3281
  schema?: string;
3282
+ /**
3283
+ * Prepare all SQL queries before executing,
3284
+ * true by default
3285
+ */
3288
3286
  autoPreparedStatements?: boolean;
3289
3287
  noPrimaryKey?: NoPrimaryKeyOption;
3290
3288
  snakeCase?: boolean;
3289
+ /**
3290
+ * Default language for the full text search
3291
+ */
3291
3292
  language?: string;
3292
3293
  /**
3293
3294
  * See {@link ScopeMethods}
@@ -3297,8 +3298,18 @@ interface DbTableOptions<ColumnTypes, Table extends string | undefined, Shape ex
3297
3298
  * See {@link SoftDeleteMethods}
3298
3299
  */
3299
3300
  softDelete?: SoftDeleteOption<Shape>;
3301
+ /**
3302
+ * Table comment, for migrations generator
3303
+ */
3300
3304
  comment?: string;
3305
+ /**
3306
+ * Computed SQL or JS columns definitions
3307
+ */
3301
3308
  computed?: ComputedOptionsFactory<ColumnTypes, Shape>;
3309
+ /**
3310
+ * For customizing `now()` sql, used in soft delete
3311
+ */
3312
+ nowSQL?: string;
3302
3313
  }
3303
3314
  /**
3304
3315
  * See {@link ScopeMethods}
@@ -3497,7 +3508,7 @@ interface DbResult<ColumnTypes> extends Db<string, never, never, never, never, n
3497
3508
  * })
3498
3509
  * ```
3499
3510
  */
3500
- declare const createDb: <SchemaConfig extends ColumnSchemaConfig<ColumnTypeBase<orchid_core.ColumnTypeSchemaArg, unknown, any, any, unknown, unknown, any, unknown, any, orchid_core.ColumnDataBase>> = DefaultSchemaConfig, ColumnTypes = DefaultColumnTypes<SchemaConfig>>({ log, logger, snakeCase, nowSQL, schemaConfig, columnTypes: ctOrFn, ...options }: DbOptions<SchemaConfig, ColumnTypes>) => DbResult<ColumnTypes>;
3511
+ declare const createDb: <SchemaConfig extends ColumnSchemaConfig<ColumnTypeBase<orchid_core.ColumnTypeSchemaArg, unknown, any, any, unknown, unknown, any, unknown, any, orchid_core.ColumnDataBase>> = DefaultSchemaConfig, ColumnTypes = DefaultColumnTypes<SchemaConfig>>({ log, logger, snakeCase, schemaConfig, columnTypes: ctOrFn, ...options }: DbOptions<SchemaConfig, ColumnTypes>) => DbResult<ColumnTypes>;
3501
3512
  declare const _initQueryBuilder: (adapter: Adapter, columnTypes: unknown, transactionStorage: AsyncLocalStorage<TransactionState>, commonOptions: DbTableOptions<unknown, undefined, QueryColumns>, options: DbSharedOptions) => Db;
3502
3513
 
3503
3514
  interface ToSQLCtx {
@@ -4628,8 +4639,6 @@ type DeleteResult<T extends PickQueryMetaResult> = T['meta']['hasSelect'] extend
4628
4639
  declare const _queryDelete: <T extends PickQueryMetaResult>(query: T) => DeleteResult<T>;
4629
4640
  declare class Delete {
4630
4641
  /**
4631
- * It is aliased to `del` because `delete` is a reserved word in JavaScript.
4632
- *
4633
4642
  * This method deletes one or more rows, based on other conditions specified in the query.
4634
4643
  *
4635
4644
  * By default, `delete` will return a count of deleted records.
@@ -5057,8 +5066,8 @@ declare class QueryLog {
5057
5066
 
5058
5067
  type MergeQuery<T extends PickQueryMetaResultReturnTypeWithDataWindows, Q extends PickQueryMetaResultReturnTypeWithDataWindows> = {
5059
5068
  [K in keyof T]: K extends 'meta' ? {
5060
- [K in keyof T['meta'] | keyof Q['meta']]: K extends 'selectable' ? MergeObjects<T['meta']['selectable'], Q['meta']['selectable']> : K extends keyof Q['meta'] ? Q['meta'][K] : T['meta'][K];
5061
- } : K extends 'result' ? MergeQueryResult<T, Q> : K extends 'returnType' ? Q['returnType'] extends undefined ? T['returnType'] : Q['returnType'] : K extends 'then' ? QueryThen<GetQueryResult<Q['returnType'] extends undefined ? T : Q, MergeQueryResult<T, Q>>> : K extends 'windows' ? MergeObjects<T['windows'], Q['windows']> : K extends 'withData' ? MergeObjects<T['withData'], Q['withData']> : T[K];
5069
+ [K in keyof T['meta'] | keyof Q['meta']]: K extends 'selectable' ? Q['meta']['selectable'] & Omit<T['meta']['selectable'], keyof Q['meta']['selectable']> : K extends keyof Q['meta'] ? Q['meta'][K] : T['meta'][K];
5070
+ } : K extends 'result' ? MergeQueryResult<T, Q> : K extends 'returnType' ? Q['returnType'] extends undefined ? T['returnType'] : Q['returnType'] : K extends 'then' ? QueryThen<GetQueryResult<Q['returnType'] extends undefined ? T : Q, MergeQueryResult<T, Q>>> : K extends 'windows' ? Q['windows'] & Omit<T['windows'], keyof Q['windows']> : K extends 'withData' ? Q['withData'] & Omit<T['withData'], keyof Q['withData']> : T[K];
5062
5071
  };
5063
5072
  type MergeQueryResult<T extends PickQueryMetaResult, Q extends PickQueryMetaResult> = T['meta']['hasSelect'] extends true ? Q['meta']['hasSelect'] extends true ? {
5064
5073
  [K in keyof T['result'] | keyof Q['result']]: K extends keyof Q['result'] ? Q['result'][K] : T['result'][K];
@@ -7135,6 +7144,7 @@ interface QueryInternal<SinglePrimaryKey = any, UniqueColumns = any, UniqueColum
7135
7144
  extensions?: DbExtension[];
7136
7145
  domains?: DbDomainArgRecord;
7137
7146
  tableData: TableData;
7147
+ nowSQL?: string;
7138
7148
  primaryKeys?: string[];
7139
7149
  }
7140
7150
  type SelectableFromShape<Shape extends QueryColumns, Table extends string | undefined> = {
package/dist/index.js CHANGED
@@ -1908,18 +1908,6 @@ const columnWithDotToSql = (ctx, data, shape, column, index, quotedAs, select) =
1908
1908
  }
1909
1909
  return `"${tableName}"."${key}"`;
1910
1910
  };
1911
- const columnToSqlWithAs = (ctx, data, column, quotedAs, select) => {
1912
- const index = column.indexOf(".");
1913
- return index !== -1 ? tableColumnToSqlWithAs(
1914
- ctx,
1915
- data,
1916
- column,
1917
- column.slice(0, index),
1918
- column.slice(index + 1),
1919
- quotedAs,
1920
- select
1921
- ) : ownColumnToSqlWithAs(ctx, data, column, quotedAs, select);
1922
- };
1923
1911
  const tableColumnToSqlWithAs = (ctx, data, column, table, key, quotedAs, select) => {
1924
1912
  var _a, _b, _c;
1925
1913
  if (key === "*") {
@@ -4543,11 +4531,9 @@ class SelectItemExpression extends orchidCore.Expression {
4543
4531
  if (value)
4544
4532
  Object.assign(this, value.operators);
4545
4533
  }
4546
- // `makeSQL` acts similarly to how select args are handled,
4547
- // except that it will use non-aliasing `columnToSql` when `ctx.aliasValue` is true,
4548
- // it is needed for relation sub-queries that returns a single column.
4534
+ // `makeSQL` acts similarly to how select args are handled
4549
4535
  makeSQL(ctx, quotedAs) {
4550
- return typeof this.item === "string" ? this.item === "*" ? selectAllSql(this.query, this.q, quotedAs) : ctx.aliasValue ? columnToSql(ctx, this.q, this.q.shape, this.item, quotedAs, true) : columnToSqlWithAs(ctx, this.q, this.item, quotedAs, true) : selectedObjectToSQL(ctx, quotedAs, this.item);
4536
+ return typeof this.item === "string" ? this.item === "*" ? selectAllSql(this.query, this.q, quotedAs) : columnToSql(ctx, this.q, this.q.shape, this.item, quotedAs, true) : this.item.toSQL(ctx, quotedAs);
4551
4537
  }
4552
4538
  }
4553
4539
 
@@ -7889,8 +7875,6 @@ const _queryDelete = (query) => {
7889
7875
  };
7890
7876
  class Delete {
7891
7877
  /**
7892
- * It is aliased to `del` because `delete` is a reserved word in JavaScript.
7893
- *
7894
7878
  * This method deletes one or more rows, based on other conditions specified in the query.
7895
7879
  *
7896
7880
  * By default, `delete` will return a count of deleted records.
@@ -9833,22 +9817,16 @@ class Where {
9833
9817
  *
9834
9818
  * ### JSONB column operators
9835
9819
  *
9836
- * For the `jsonb` column, note that the `json` type has text operators instead.
9820
+ * JSON functions are available only for the `jsonb` column, note that the `json` type has text operators instead.
9837
9821
  *
9838
- * `jsonPath` operator: compare a column value under a given JSON path with the provided value.
9822
+ * You can use [jsonPathQueryFirst](/guide/json.html#jsonpathqueryfirst) to filter by a JSON value, follow the link for details.
9839
9823
  *
9840
- * Value can be of any type to compare with JSON value, or it can be a sub-query or a raw SQL expression.
9824
+ * The value can be of any type, it can also be returned from a sub-query, raw SQL expression.
9841
9825
  *
9842
9826
  * ```ts
9843
- * db.table.where({
9844
- * jsonbColumn: {
9845
- * jsonPath: [
9846
- * '$.name', // first element is JSON path
9847
- * '=', // second argument is comparison operator
9848
- * 'value', // third argument is a value to compare with
9849
- * ],
9850
- * },
9851
- * });
9827
+ * db.table.where((q) =>
9828
+ * q.get('jsonbColumn').jsonPathQueryFirst('$.name').equals(value),
9829
+ * );
9852
9830
  * ```
9853
9831
  *
9854
9832
  * `jsonSupersetOf`: check if the column value is a superset of provided value.
@@ -9874,7 +9852,7 @@ class Where {
9874
9852
  * ```ts
9875
9853
  * db.table.where({
9876
9854
  * jsonbColumn: {
9877
- * jsonSupersetOf: { a: 1 },
9855
+ * jsonSubsetOf: { a: 1 },
9878
9856
  * },
9879
9857
  * });
9880
9858
  * ```
@@ -11290,14 +11268,14 @@ function enableSoftDelete(q, table, shape, softDelete, scopes) {
11290
11268
  };
11291
11269
  scopes.deleted = scope;
11292
11270
  ((_b = (_a = q.q).scopes) != null ? _b : _a.scopes = {}).nonDeleted = scope;
11293
- const _del = _softDelete(column);
11271
+ const _del = _softDelete(column, q.internal.nowSQL);
11294
11272
  q.baseQuery.delete = function() {
11295
11273
  return _del.call(this.clone());
11296
11274
  };
11297
11275
  }
11298
11276
  const nowSql = new RawSQL("now()");
11299
- const _softDelete = (column) => {
11300
- const set = { [column]: nowSql };
11277
+ const _softDelete = (column, customNowSQL) => {
11278
+ const set = { [column]: customNowSQL ? new RawSQL(customNowSQL) : nowSql };
11301
11279
  return function() {
11302
11280
  return _queryUpdate(this, set);
11303
11281
  };
@@ -12345,6 +12323,7 @@ class Db {
12345
12323
  snakeCase: options.snakeCase,
12346
12324
  noPrimaryKey: options.noPrimaryKey === "ignore",
12347
12325
  comment: options.comment,
12326
+ nowSQL: options.nowSQL,
12348
12327
  tableData
12349
12328
  };
12350
12329
  this.baseQuery = this;
@@ -12586,14 +12565,12 @@ const createDb = (_a) => {
12586
12565
  log,
12587
12566
  logger,
12588
12567
  snakeCase,
12589
- nowSQL,
12590
12568
  schemaConfig = defaultSchemaConfig,
12591
12569
  columnTypes: ctOrFn = makeColumnTypes(schemaConfig)
12592
12570
  } = _b, options = __objRest(_b, [
12593
12571
  "log",
12594
12572
  "logger",
12595
12573
  "snakeCase",
12596
- "nowSQL",
12597
12574
  "schemaConfig",
12598
12575
  "columnTypes"
12599
12576
  ]);
@@ -12604,6 +12581,7 @@ const createDb = (_a) => {
12604
12581
  logger,
12605
12582
  autoPreparedStatements: (_a2 = options.autoPreparedStatements) != null ? _a2 : false,
12606
12583
  noPrimaryKey: (_b2 = options.noPrimaryKey) != null ? _b2 : "error",
12584
+ nowSQL: options.nowSQL,
12607
12585
  snakeCase
12608
12586
  };
12609
12587
  const ct = typeof ctOrFn === "function" ? ctOrFn(makeColumnTypes(schemaConfig)) : ctOrFn;
@@ -12618,16 +12596,19 @@ const createDb = (_a) => {
12618
12596
  commonOptions,
12619
12597
  options
12620
12598
  );
12621
- const tableConstructor = (table, shape, dataFn, options2) => new Db(
12622
- adapter,
12623
- qb,
12624
- table,
12625
- typeof shape === "function" ? getColumnTypes(ct, shape, nowSQL, options2 == null ? void 0 : options2.language) : shape,
12626
- ct,
12627
- transactionStorage,
12628
- __spreadValues(__spreadValues({}, commonOptions), options2),
12629
- parseTableData(dataFn)
12630
- );
12599
+ const { nowSQL } = options;
12600
+ const tableConstructor = (table, shape, dataFn, options2) => {
12601
+ return new Db(
12602
+ adapter,
12603
+ qb,
12604
+ table,
12605
+ typeof shape === "function" ? getColumnTypes(ct, shape, nowSQL, options2 == null ? void 0 : options2.language) : shape,
12606
+ ct,
12607
+ transactionStorage,
12608
+ __spreadValues(__spreadValues({}, commonOptions), options2),
12609
+ parseTableData(dataFn)
12610
+ );
12611
+ };
12631
12612
  const db = Object.assign(tableConstructor, qb, {
12632
12613
  adapter,
12633
12614
  close: () => adapter.close()