pqb 0.38.5 → 0.38.7

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.mjs CHANGED
@@ -1095,9 +1095,9 @@ class NumberAsStringBaseColumn extends ColumnType {
1095
1095
  this.operators = Operators.number;
1096
1096
  }
1097
1097
  }
1098
- class DecimalColumn extends ColumnType {
1098
+ class DecimalColumn extends NumberAsStringBaseColumn {
1099
1099
  constructor(schema, numericPrecision, numericScale) {
1100
- super(schema, schema.stringSchema());
1100
+ super(schema);
1101
1101
  this.operators = Operators.number;
1102
1102
  this.dataType = "numeric";
1103
1103
  this.data.numericPrecision = numericPrecision;
@@ -4569,25 +4569,29 @@ class SelectItemExpression extends Expression {
4569
4569
  }
4570
4570
  }
4571
4571
 
4572
- const _get = (query, returnType, arg) => {
4572
+ const _getSelectableColumn = (q, arg) => {
4573
4573
  var _a, _b;
4574
+ let type = q.q.shape[arg];
4575
+ if (!type) {
4576
+ const index = arg.indexOf(".");
4577
+ if (index !== -1) {
4578
+ const table = arg.slice(0, index);
4579
+ const column = arg.slice(index + 1);
4580
+ if (table === (q.q.as || q.table)) {
4581
+ type = q.shape[column];
4582
+ } else {
4583
+ type = (_b = (_a = q.q.joinedShapes) == null ? void 0 : _a[table]) == null ? void 0 : _b[column];
4584
+ }
4585
+ }
4586
+ }
4587
+ return type;
4588
+ };
4589
+ const _get = (query, returnType, arg) => {
4574
4590
  const q = query.q;
4575
4591
  q.returnType = returnType;
4576
4592
  let type;
4577
4593
  if (typeof arg === "string") {
4578
- type = q.shape[arg];
4579
- if (!type) {
4580
- const index = arg.indexOf(".");
4581
- if (index !== -1) {
4582
- const table = arg.slice(0, index);
4583
- const column = arg.slice(index + 1);
4584
- if (table === (q.as || query.table)) {
4585
- type = q.shape[column];
4586
- } else {
4587
- type = (_b = (_a = q.joinedShapes) == null ? void 0 : _a[table]) == null ? void 0 : _b[column];
4588
- }
4589
- }
4590
- }
4594
+ type = _getSelectableColumn(query, arg);
4591
4595
  q.getColumn = type;
4592
4596
  const selected = setParserForSelectedString(
4593
4597
  query,
@@ -6550,6 +6554,13 @@ const nullableInt = new IntegerColumn(defaultSchemaConfig);
6550
6554
  nullableInt.parseItem = nullableInt.parseFn = (input) => input === null ? null : parseInt(input);
6551
6555
  const nullableFloat = new RealColumn(defaultSchemaConfig);
6552
6556
  nullableFloat.parseItem = nullableFloat.parseFn = (input) => input === null ? null : parseFloat(input);
6557
+ const stringAsNumber = new NumberAsStringBaseColumn(
6558
+ defaultSchemaConfig
6559
+ );
6560
+ const numericResultColumn = (q, arg) => {
6561
+ const type = typeof arg === "string" ? _getSelectableColumn(q, arg) : arg.result.value;
6562
+ return type instanceof NumberBaseColumn ? nullableFloat : stringAsNumber;
6563
+ };
6553
6564
  class AggregateMethods {
6554
6565
  /**
6555
6566
  * Use `exists()` to check if there is at least one record-matching condition.
@@ -6620,7 +6631,7 @@ class AggregateMethods {
6620
6631
  min(arg, options) {
6621
6632
  return makeFnExpression(
6622
6633
  this,
6623
- nullableFloat,
6634
+ numericResultColumn(this, arg),
6624
6635
  "min",
6625
6636
  [arg],
6626
6637
  options
@@ -6650,7 +6661,7 @@ class AggregateMethods {
6650
6661
  max(arg, options) {
6651
6662
  return makeFnExpression(
6652
6663
  this,
6653
- nullableFloat,
6664
+ numericResultColumn(this, arg),
6654
6665
  "max",
6655
6666
  [arg],
6656
6667
  options
@@ -6679,7 +6690,7 @@ class AggregateMethods {
6679
6690
  sum(arg, options) {
6680
6691
  return makeFnExpression(
6681
6692
  this,
6682
- nullableFloat,
6693
+ numericResultColumn(this, arg),
6683
6694
  "sum",
6684
6695
  [arg],
6685
6696
  options
@@ -6705,7 +6716,7 @@ class AggregateMethods {
6705
6716
  avg(arg, options) {
6706
6717
  return makeFnExpression(
6707
6718
  this,
6708
- nullableFloat,
6719
+ numericResultColumn(this, arg),
6709
6720
  "avg",
6710
6721
  [arg],
6711
6722
  options
@@ -6734,7 +6745,7 @@ class AggregateMethods {
6734
6745
  bitAnd(arg, options) {
6735
6746
  return makeFnExpression(
6736
6747
  this,
6737
- nullableFloat,
6748
+ numericResultColumn(this, arg),
6738
6749
  "bit_and",
6739
6750
  [arg],
6740
6751
  options
@@ -6760,7 +6771,7 @@ class AggregateMethods {
6760
6771
  bitOr(arg, options) {
6761
6772
  return makeFnExpression(
6762
6773
  this,
6763
- nullableFloat,
6774
+ numericResultColumn(this, arg),
6764
6775
  "bit_or",
6765
6776
  [arg],
6766
6777
  options
@@ -7374,6 +7385,8 @@ class Create {
7374
7385
  * Each column may accept a specific value, a raw SQL, or a query that returns a single value.
7375
7386
  *
7376
7387
  * ```ts
7388
+ * import { sql } from './baseTable';
7389
+ *
7377
7390
  * const oneRecord = await db.table.create({
7378
7391
  * name: 'John',
7379
7392
  * password: '1234',
@@ -7385,7 +7398,7 @@ class Create {
7385
7398
  *
7386
7399
  * await db.table.create({
7387
7400
  * // raw SQL
7388
- * column1: (q) => q.sql`'John' || ' ' || 'Doe'`,
7401
+ * column1: () => sql`'John' || ' ' || 'Doe'`,
7389
7402
  *
7390
7403
  * // query that returns a single value
7391
7404
  * // returning multiple values will result in Postgres error
@@ -7859,7 +7872,7 @@ class OnConflictQueryBuilder {
7859
7872
  *
7860
7873
  * If the table has columns with **dynamic** default values, such values will be applied as well.
7861
7874
  *
7862
- * You can exclude certain columns from being merged by passing the `exclude` option.
7875
+ * You can exclude certain columns from being merged by passing the `except` option.
7863
7876
  *
7864
7877
  * ```ts
7865
7878
  * // merge the full data
@@ -8100,7 +8113,9 @@ class Having {
8100
8113
  * Arguments of the aggregate function and of the comparison can be raw SQL:
8101
8114
  *
8102
8115
  * ```ts
8103
- * db.table.having((q) => q.count(q.sql('coalesce(one, two)')).gte(q.sql`2 + 2`));
8116
+ * import { sql } from './baseTable';
8117
+ *
8118
+ * db.table.having((q) => q.count(sql('coalesce(one, two)')).gte(sql`2 + 2`));
8104
8119
  * ```
8105
8120
  *
8106
8121
  * @param args - raw SQL template string or one or multiple callbacks returning a boolean expression
@@ -9198,13 +9213,15 @@ class Union {
9198
9213
  * Creates a union query, takes one or more queries or SQL expressions.
9199
9214
  *
9200
9215
  * ```ts
9216
+ * import { sql } from './baseTable';
9217
+ *
9201
9218
  * // The first query of the union
9202
9219
  * db.one
9203
9220
  * .select('id', 'name')
9204
9221
  * // add two more queries to the union
9205
9222
  * .union(
9206
9223
  * db.two.select('id', 'name'),
9207
- * (q = q.sql`SELECT id, name FROM "thirdTable"`),
9224
+ * (q = sql`SELECT id, name FROM "thirdTable"`),
9208
9225
  * )
9209
9226
  * // sub-sequent `union` is equivalent to passing multiple queries into a single `union`
9210
9227
  * .union(db.three.select('id', 'name'));
@@ -9513,6 +9530,8 @@ class Where {
9513
9530
  * Constructing `WHERE` conditions:
9514
9531
  *
9515
9532
  * ```ts
9533
+ * import { sql } from './baseTable'
9534
+ *
9516
9535
  * db.table.where({
9517
9536
  * // column of the current table
9518
9537
  * name: 'John',
@@ -9529,8 +9548,8 @@ class Where {
9529
9548
  * // where column equals to raw SQL
9530
9549
  * // import `sql` from your `BaseTable`
9531
9550
  * column: sql`sql expression`,
9532
- * // or use `(q) => q.sql` for the same
9533
- * column2: (q) => q.sql`sql expression`,
9551
+ * // or use `(q) => sql` for the same
9552
+ * column2: (q) => sql`sql expression`,
9534
9553
  *
9535
9554
  * // reference other columns in such a way:
9536
9555
  * firstName: (q) => q.ref('lastName'),
@@ -10355,6 +10374,8 @@ class Update {
10355
10374
  * and [jsonRemove](/guide/advanced-queries.html#jsonremove) for a JSON column (see `jsonColumn` below).
10356
10375
  *
10357
10376
  * ```ts
10377
+ * import { sql } from './baseTable';
10378
+ *
10358
10379
  * // returns number of updated records by default
10359
10380
  * const updatedCount = await db.table
10360
10381
  * .where({ name: 'old name' })
@@ -10380,7 +10401,7 @@ class Update {
10380
10401
  * column1: 123,
10381
10402
  *
10382
10403
  * // use raw SQL to update the column
10383
- * column2: (q) => q.sql`2 + 2`,
10404
+ * column2: () => sql`2 + 2`,
10384
10405
  *
10385
10406
  * // use query that returns a single value
10386
10407
  * // returning multiple values will result in Postgres error
@@ -11392,11 +11413,13 @@ class ExpressionMethods {
11392
11413
  * Only for referencing a column in the query's table. For referencing joined table's columns, see [ref](#ref).
11393
11414
  *
11394
11415
  * ```ts
11416
+ * import { sql } from './baseTable';
11417
+ *
11395
11418
  * await db.table.select({
11396
11419
  * // select `("table"."id" = 1 OR "table"."name" = 'name') AS "one"`,
11397
11420
  * // returns a boolean
11398
11421
  * one: (q) =>
11399
- * q.sql<boolean>`${q.column('id')} = ${1} OR ${q.column('name')} = ${'name'}`,
11422
+ * sql<boolean>`${q.column('id')} = ${1} OR ${q.column('name')} = ${'name'}`,
11400
11423
  *
11401
11424
  * // selects the same as above, but by building a query
11402
11425
  * two: (q) => q.column('id').equals(1).or(q.column('name').equals('name')),
@@ -11417,11 +11440,13 @@ class ExpressionMethods {
11417
11440
  * and other dynamically defined columns.
11418
11441
  *
11419
11442
  * ```ts
11443
+ * import { sql } from './baseTable';
11444
+ *
11420
11445
  * await db.table.join('otherTable').select({
11421
11446
  * // select `("otherTable"."id" = 1 OR "otherTable"."name" = 'name') AS "one"`,
11422
11447
  * // returns a boolean
11423
11448
  * one: (q) =>
11424
- * q.sql<boolean>`${q.ref('otherTable.id')} = ${1} OR ${q.ref(
11449
+ * sql<boolean>`${q.ref('otherTable.id')} = ${1} OR ${q.ref(
11425
11450
  * 'otherTable.name',
11426
11451
  * )} = ${'name'}`,
11427
11452
  *
@@ -12644,6 +12669,11 @@ const createDb = (_a) => {
12644
12669
  for (const name of Object.getOwnPropertyNames(Db.prototype)) {
12645
12670
  db[name] = Db.prototype[name];
12646
12671
  }
12672
+ db.sql = (...args) => {
12673
+ const sql = raw(...args);
12674
+ sql.columnTypes = ct;
12675
+ return sql;
12676
+ };
12647
12677
  return db;
12648
12678
  };
12649
12679
  const _initQueryBuilder = (adapter, columnTypes, transactionStorage, commonOptions, options) => {
@@ -12799,5 +12829,5 @@ function copyTableData(query, arg) {
12799
12829
  return q;
12800
12830
  }
12801
12831
 
12802
- export { Adapter, AggregateMethods, ArrayColumn, AsMethods, BigIntColumn, BigSerialColumn, BitColumn, BitVaryingColumn, BooleanColumn, BoxColumn, ByteaColumn, CidrColumn, CircleColumn, CitextColumn, Clear, ColumnRefExpression, ColumnType, ComputedColumn, Create, CustomTypeColumn, DateBaseColumn, DateColumn, DateTimeBaseClass, DateTimeTzBaseClass, Db, DecimalColumn, Delete, DomainColumn, DoublePrecisionColumn, DynamicRawSQL, EnumColumn, ExpressionMethods, FnExpression, For, FromMethods, Having, InetColumn, IntegerBaseColumn, IntegerColumn, IntervalColumn, JSONColumn, JSONTextColumn, Join, JsonMethods, LimitedTextBaseColumn, LineColumn, LsegColumn, MacAddr8Column, MacAddrColumn, MergeQueryMethods, MoneyColumn, MoreThanOneRowError, NotFoundError, NumberAsStringBaseColumn, NumberBaseColumn, OnConflictQueryBuilder, OnMethods, Operators, OrExpression, OrchidOrmError, OrchidOrmInternalError, PathColumn, PointColumn, PolygonColumn, QueryBase, QueryError, QueryGet, QueryHooks, QueryLog, QueryMethods, QueryUpsertOrCreate, RawSQL, RealColumn, RefExpression, SearchMethods, Select, SerialColumn, SmallIntColumn, SmallSerialColumn, SqlMethod, StringColumn, TextBaseColumn, TextColumn, Then, TimeColumn, TimestampColumn, TimestampTZColumn, Transaction, TransactionAdapter, TransformMethods, TsQueryColumn, TsVectorColumn, UUIDColumn, UnhandledTypeError, Union, UnknownColumn, Update, VarCharColumn, VirtualColumn, Where, WithMethods, XMLColumn, _initQueryBuilder, _queryAfterSaveCommit, _queryAll, _queryAs, _queryChangeCounter, _queryCreate, _queryCreateFrom, _queryCreateMany, _queryCreateManyFrom, _queryCreateManyRaw, _queryCreateRaw, _queryDefaults, _queryDelete, _queryExec, _queryFindBy, _queryFindByOptional, _queryGet, _queryGetOptional, _queryHookAfterCreate, _queryHookAfterCreateCommit, _queryHookAfterDelete, _queryHookAfterDeleteCommit, _queryHookAfterQuery, _queryHookAfterSave, _queryHookAfterUpdate, _queryHookAfterUpdateCommit, _queryHookBeforeCreate, _queryHookBeforeDelete, _queryHookBeforeQuery, _queryHookBeforeSave, _queryHookBeforeUpdate, _queryInsert, _queryInsertFrom, _queryInsertMany, _queryInsertManyFrom, _queryInsertManyRaw, _queryInsertRaw, _queryJoinOn, _queryJoinOnJsonPathEquals, _queryJoinOrOn, _queryOr, _queryOrNot, _queryRows, _querySelect, _queryTake, _queryTakeOptional, _queryUnion, _queryUpdate, _queryUpdateOrThrow, _queryUpdateRaw, _queryWhere, _queryWhereExists, _queryWhereIn, _queryWhereNot, _queryWhereNotOneOf, _queryWhereNotSql, _queryWhereOneOf, _queryWhereSql, addParserForRawExpression, addParserForSelectItem, addQueryOn, anyShape, applyComputedColumns, checkIfASimpleQuery, cloneQuery, cloneQueryBaseUnscoped, columnCheckToCode, columnCode, columnForeignKeysToCode, columnIndexesToCode, columnsShapeToCode, commitSql$1 as commitSql, constraintInnerToCode, constraintToCode, copyTableData, countSelect, createDb, defaultSchemaConfig, extendQuery, filterResult, foreignKeyArgumentToCode, getClonedQueryData, getColumnInfo, getColumnTypes, getPrimaryKeys, getQueryAs, getShapeFromSelect, getSqlText, handleResult, identityToCode, indexInnerToCode, indexToCode, instantiateColumn, isDefaultTimeStamp, isQueryReturnsAll, isSelectingCount, joinSubQuery, logParamToLogObject, makeColumnTypes, makeColumnsByType, makeFnExpression, makeRegexToFindInSql, makeSQL, parseRecord, parseTableData, parseTableDataInput, primaryKeyInnerToCode, processComputedBatches, processComputedResult, processSelectArg, pushLimitSQL, pushQueryArray, pushQueryOn, pushQueryOnForOuter, pushQueryOrOn, pushQueryValue, pushTableDataCode, queryFrom, queryFromSql, queryJson, queryMethodByReturnType, queryTypeWithLimitOne, queryWrap, quote, quoteString, raw, referencesArgsToCode, resolveSubQueryCallback, rollbackSql$1 as rollbackSql, saveSearchAlias, setParserForSelectedString, setQueryObjectValue, setQueryOperators, simplifyColumnDefault, sqlFn, sqlQueryArgsToExpression, tableDataMethods, templateLiteralToSQL, testTransaction, throwIfNoWhere, toSQL, toSQLCacheKey };
12832
+ export { Adapter, AggregateMethods, ArrayColumn, AsMethods, BigIntColumn, BigSerialColumn, BitColumn, BitVaryingColumn, BooleanColumn, BoxColumn, ByteaColumn, CidrColumn, CircleColumn, CitextColumn, Clear, ColumnRefExpression, ColumnType, ComputedColumn, Create, CustomTypeColumn, DateBaseColumn, DateColumn, DateTimeBaseClass, DateTimeTzBaseClass, Db, DecimalColumn, Delete, DomainColumn, DoublePrecisionColumn, DynamicRawSQL, EnumColumn, ExpressionMethods, FnExpression, For, FromMethods, Having, InetColumn, IntegerBaseColumn, IntegerColumn, IntervalColumn, JSONColumn, JSONTextColumn, Join, JsonMethods, LimitedTextBaseColumn, LineColumn, LsegColumn, MacAddr8Column, MacAddrColumn, MergeQueryMethods, MoneyColumn, MoreThanOneRowError, NotFoundError, NumberAsStringBaseColumn, NumberBaseColumn, OnConflictQueryBuilder, OnMethods, Operators, OrExpression, OrchidOrmError, OrchidOrmInternalError, PathColumn, PointColumn, PolygonColumn, QueryBase, QueryError, QueryGet, QueryHooks, QueryLog, QueryMethods, QueryUpsertOrCreate, RawSQL, RealColumn, RefExpression, SearchMethods, Select, SerialColumn, SmallIntColumn, SmallSerialColumn, SqlMethod, StringColumn, TextBaseColumn, TextColumn, Then, TimeColumn, TimestampColumn, TimestampTZColumn, Transaction, TransactionAdapter, TransformMethods, TsQueryColumn, TsVectorColumn, UUIDColumn, UnhandledTypeError, Union, UnknownColumn, Update, VarCharColumn, VirtualColumn, Where, WithMethods, XMLColumn, _getSelectableColumn, _initQueryBuilder, _queryAfterSaveCommit, _queryAll, _queryAs, _queryChangeCounter, _queryCreate, _queryCreateFrom, _queryCreateMany, _queryCreateManyFrom, _queryCreateManyRaw, _queryCreateRaw, _queryDefaults, _queryDelete, _queryExec, _queryFindBy, _queryFindByOptional, _queryGet, _queryGetOptional, _queryHookAfterCreate, _queryHookAfterCreateCommit, _queryHookAfterDelete, _queryHookAfterDeleteCommit, _queryHookAfterQuery, _queryHookAfterSave, _queryHookAfterUpdate, _queryHookAfterUpdateCommit, _queryHookBeforeCreate, _queryHookBeforeDelete, _queryHookBeforeQuery, _queryHookBeforeSave, _queryHookBeforeUpdate, _queryInsert, _queryInsertFrom, _queryInsertMany, _queryInsertManyFrom, _queryInsertManyRaw, _queryInsertRaw, _queryJoinOn, _queryJoinOnJsonPathEquals, _queryJoinOrOn, _queryOr, _queryOrNot, _queryRows, _querySelect, _queryTake, _queryTakeOptional, _queryUnion, _queryUpdate, _queryUpdateOrThrow, _queryUpdateRaw, _queryWhere, _queryWhereExists, _queryWhereIn, _queryWhereNot, _queryWhereNotOneOf, _queryWhereNotSql, _queryWhereOneOf, _queryWhereSql, addParserForRawExpression, addParserForSelectItem, addQueryOn, anyShape, applyComputedColumns, checkIfASimpleQuery, cloneQuery, cloneQueryBaseUnscoped, columnCheckToCode, columnCode, columnForeignKeysToCode, columnIndexesToCode, columnsShapeToCode, commitSql$1 as commitSql, constraintInnerToCode, constraintToCode, copyTableData, countSelect, createDb, defaultSchemaConfig, extendQuery, filterResult, foreignKeyArgumentToCode, getClonedQueryData, getColumnInfo, getColumnTypes, getPrimaryKeys, getQueryAs, getShapeFromSelect, getSqlText, handleResult, identityToCode, indexInnerToCode, indexToCode, instantiateColumn, isDefaultTimeStamp, isQueryReturnsAll, isSelectingCount, joinSubQuery, logParamToLogObject, makeColumnTypes, makeColumnsByType, makeFnExpression, makeRegexToFindInSql, makeSQL, parseRecord, parseTableData, parseTableDataInput, primaryKeyInnerToCode, processComputedBatches, processComputedResult, processSelectArg, pushLimitSQL, pushQueryArray, pushQueryOn, pushQueryOnForOuter, pushQueryOrOn, pushQueryValue, pushTableDataCode, queryFrom, queryFromSql, queryJson, queryMethodByReturnType, queryTypeWithLimitOne, queryWrap, quote, quoteString, raw, referencesArgsToCode, resolveSubQueryCallback, rollbackSql$1 as rollbackSql, saveSearchAlias, setParserForSelectedString, setQueryObjectValue, setQueryOperators, simplifyColumnDefault, sqlFn, sqlQueryArgsToExpression, tableDataMethods, templateLiteralToSQL, testTransaction, throwIfNoWhere, toSQL, toSQLCacheKey };
12803
12833
  //# sourceMappingURL=index.mjs.map