pqb 0.36.16 → 0.37.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.mjs CHANGED
@@ -2204,37 +2204,16 @@ const processWhere = (ands, ctx, table, query, data, quotedAs) => {
2204
2204
  );
2205
2205
  } else {
2206
2206
  const item = value;
2207
- const leftColumn = columnToSql(
2208
- ctx,
2209
- query,
2210
- query.shape,
2211
- item.on[0],
2212
- `"${getJoinItemSource(item.joinFrom)}"`
2207
+ const joinAs = `"${getJoinItemSource(item.joinFrom)}"`;
2208
+ const { on } = item;
2209
+ const q = item.useOuterJoinOverrides ? {
2210
+ joinedShapes: query.joinedShapes,
2211
+ joinOverrides: query.outerJoinOverrides,
2212
+ shape: query.shape
2213
+ } : query;
2214
+ ands.push(
2215
+ `${onColumnToSql(ctx, q, joinAs, on[0])} ${on.length === 2 ? "=" : on[1]} ${onColumnToSql(ctx, q, joinAs, on.length === 3 ? on[2] : on[1])}`
2213
2216
  );
2214
- const joinTo = getJoinItemSource(item.joinTo);
2215
- const joinedShape = query.joinedShapes[joinTo];
2216
- let op;
2217
- let rightColumn;
2218
- if (item.on.length === 2) {
2219
- op = "=";
2220
- rightColumn = columnToSql(
2221
- ctx,
2222
- query,
2223
- joinedShape,
2224
- item.on[1],
2225
- `"${joinTo}"`
2226
- );
2227
- } else {
2228
- op = item.on[1];
2229
- rightColumn = columnToSql(
2230
- ctx,
2231
- query,
2232
- joinedShape,
2233
- item.on[2],
2234
- `"${joinTo}"`
2235
- );
2236
- }
2237
- ands.push(`${leftColumn} ${op} ${rightColumn}`);
2238
2217
  }
2239
2218
  } else if (key === "IN") {
2240
2219
  toArray(value).forEach((item) => {
@@ -2330,6 +2309,7 @@ const processWhere = (ands, ctx, table, query, data, quotedAs) => {
2330
2309
  }
2331
2310
  }
2332
2311
  };
2312
+ const onColumnToSql = (ctx, query, joinAs, column) => columnToSql(ctx, query, query.shape, column, joinAs);
2333
2313
  const getJoinItemSource = (joinItem) => {
2334
2314
  return typeof joinItem === "string" ? joinItem : getQueryAs(joinItem);
2335
2315
  };
@@ -5968,12 +5948,14 @@ const makeRegexToFindInSql = (value) => {
5968
5948
  return new RegExp(`${value}(?=(?:[^']*'[^']*')*[^']*$)`, "g");
5969
5949
  };
5970
5950
  const resolveSubQueryCallback = (q, cb) => {
5971
- const { subQuery, relChain } = q.q;
5951
+ const { subQuery, relChain, outerJoinOverrides } = q.q;
5972
5952
  q.q.subQuery = 1;
5973
5953
  q.q.relChain = void 0;
5954
+ q.q.outerJoinOverrides = q.q.joinOverrides;
5974
5955
  const result = cb(q);
5975
5956
  q.q.subQuery = subQuery;
5976
5957
  q.q.relChain = relChain;
5958
+ q.q.outerJoinOverrides = outerJoinOverrides;
5977
5959
  return result;
5978
5960
  };
5979
5961
  const joinSubQuery = (q, sub) => {
@@ -8403,7 +8385,7 @@ class Join {
8403
8385
  *
8404
8386
  * All the `join` methods accept the same arguments, but returning type is different because with `join` it's guaranteed to load joined table, and with `leftJoin` the joined table columns may be `NULL` when no matching record was found.
8405
8387
  *
8406
- * For the following examples, imagine we have a `User` table with `id` and `name`, and `Message` table with `id`, `text`, messages belongs to user via `userId` column:
8388
+ * For the following examples, imagine you have a `User` table with `id` and `name`, and `Message` table with `id`, `text`, messages belongs to user via `userId` column:
8407
8389
  *
8408
8390
  * ```ts
8409
8391
  * export class UserTable extends BaseTable {
@@ -8481,7 +8463,7 @@ class Join {
8481
8463
  * ```ts
8482
8464
  * const result = await db.user
8483
8465
  * .join('messages')
8484
- * // after joining a table, we can use it in `where` conditions:
8466
+ * // after joining a table, you can use it in `where` conditions:
8485
8467
  * .where({ 'messages.text': { startsWith: 'Hi' } })
8486
8468
  * .select(
8487
8469
  * 'name', // name is User column, table name may be omitted
@@ -8492,8 +8474,8 @@ class Join {
8492
8474
  * const ok: { name: string; text: string }[] = result;
8493
8475
  * ```
8494
8476
  *
8495
- * The first argument can also be a callback, where instead of relation name as a string we're picking it as a property of `q`.
8496
- * In such a way, we can alias the relation with `as`, add `where` conditions, use other query methods.
8477
+ * The first argument can also be a callback, where instead of relation name as a string you're picking it as a property of `q`.
8478
+ * In such a way, you can alias the relation with `as`, add `where` conditions, use other query methods.
8497
8479
  *
8498
8480
  * ```ts
8499
8481
  * const result = await db.user.join((q) =>
@@ -8510,7 +8492,7 @@ class Join {
8510
8492
  * (q) => q.messages.as('m'),
8511
8493
  * (q) =>
8512
8494
  * q
8513
- * .on('text', 'name') // additionally, match message with user name
8495
+ * .on('messages.text', 'user.name') // additionally, match message with user name
8514
8496
  * .where({ text: 'some text' }), // you can add `where` in a second callback as well.
8515
8497
  * );
8516
8498
  * ```
@@ -8564,17 +8546,16 @@ class Join {
8564
8546
  * Joined table can be references from `where` and `select` by a table name.
8565
8547
  *
8566
8548
  * ```ts
8567
- * // Join message where userId = id:
8568
8549
  * db.user
8569
- * .join(db.message, 'userId', 'id')
8550
+ * .join(db.message, 'userId', 'user.id')
8570
8551
  * .where({ 'message.text': { startsWith: 'Hi' } })
8571
8552
  * .select('name', 'message.text');
8572
8553
  * ```
8573
8554
  *
8574
- * Columns in the join list may be prefixed with table names for clarity:
8555
+ * The name of the joining table can be omitted, but not the name of the main table:
8575
8556
  *
8576
8557
  * ```ts
8577
- * db.user.join(db.message, 'message.userId', 'user.id');
8558
+ * db.user.join(db.message, 'userId', 'user.id');
8578
8559
  * ```
8579
8560
  *
8580
8561
  * Joined table can have an alias for referencing it further:
@@ -8605,7 +8586,7 @@ class Join {
8605
8586
  * You can provide a custom comparison operator
8606
8587
  *
8607
8588
  * ```ts
8608
- * db.user.join(db.message, 'userId', '!=', 'id');
8589
+ * db.user.join(db.message, 'userId', '!=', 'user.id');
8609
8590
  * ```
8610
8591
  *
8611
8592
  * Join can accept raw SQL for the `ON` part of join:
@@ -8640,11 +8621,11 @@ class Join {
8640
8621
  *
8641
8622
  * ```ts
8642
8623
  * db.user.join(db.message, {
8643
- * userId: 'id',
8644
- *
8645
- * // with table names:
8646
8624
  * 'message.userId': 'user.id',
8647
8625
  *
8626
+ * // joined table name may be omitted
8627
+ * userId: 'user.id',
8628
+ *
8648
8629
  * // value can be a raw SQL expression:
8649
8630
  * text: sql`lower("user"."name")`,
8650
8631
  * });
@@ -8663,17 +8644,16 @@ class Join {
8663
8644
  * db.message,
8664
8645
  * (q) =>
8665
8646
  * q
8666
- * // left column is the db.message column, right column is the db.user column
8667
- * .on('userId', 'id')
8668
- * // table names can be provided:
8669
8647
  * .on('message.userId', 'user.id')
8648
+ * // joined table name may be omitted
8649
+ * .on('userId', 'user.id')
8670
8650
  * // operator can be specified:
8671
- * .on('userId', '!=', 'id')
8651
+ * .on('userId', '!=', 'user.id')
8672
8652
  * // operator can be specified with table names as well:
8673
8653
  * .on('message.userId', '!=', 'user.id')
8674
8654
  * // `.orOn` takes the same arguments as `.on` and acts like `.or`:
8675
- * .on('userId', 'id') // where message.userId = user.id
8676
- * .orOn('text', 'name'), // or message.text = user.name
8655
+ * .on('userId', 'user.id') // where message.userId = user.id
8656
+ * .orOn('text', 'user.name'), // or message.text = user.name
8677
8657
  * );
8678
8658
  * ```
8679
8659
  *
@@ -8682,7 +8662,7 @@ class Join {
8682
8662
  * ```ts
8683
8663
  * db.user.join(db.message, (q) =>
8684
8664
  * q
8685
- * .on('userId', 'id')
8665
+ * .on('userId', 'user.id')
8686
8666
  * .where({
8687
8667
  * // not prefixed column name is for joined table:
8688
8668
  * text: { startsWith: 'hello' },
@@ -8717,7 +8697,7 @@ class Join {
8717
8697
  * .where({ text: { startsWith: 'Hi' } })
8718
8698
  * .as('t'),
8719
8699
  * 'userId',
8720
- * 'id',
8700
+ * 'user.id',
8721
8701
  * );
8722
8702
  * ```
8723
8703
  *
@@ -8784,7 +8764,7 @@ class Join {
8784
8764
  *
8785
8765
  * // the same query, but joining table explicitly
8786
8766
  * const result2: typeof result = await db.user
8787
- * .leftJoin(db.message, 'userId', 'id')
8767
+ * .leftJoin(db.message, 'userId', 'user.id')
8788
8768
  * .select('name', 'message.text');
8789
8769
  *
8790
8770
  * // result has the following type:
@@ -8886,7 +8866,7 @@ class Join {
8886
8866
  * // select message columns
8887
8867
  * .select('text')
8888
8868
  * // join the message to the user, column names can be prefixed with table names
8889
- * .on('authorId', 'id')
8869
+ * .on('authorId', 'user.id')
8890
8870
  * // message columns are available without prefixing,
8891
8871
  * // outer table columns are available with a table name
8892
8872
  * .where({ text: 'some text', 'user.name': 'name' })
@@ -8968,14 +8948,22 @@ class Join {
8968
8948
  );
8969
8949
  }
8970
8950
  }
8971
- const makeOnItem = (joinTo, joinFrom, args) => {
8972
- return {
8951
+ const makeOnItem = (joinTo, joinFrom, args) => ({
8952
+ ON: {
8953
+ joinTo,
8954
+ joinFrom,
8955
+ on: args
8956
+ }
8957
+ });
8958
+ const pushQueryOnForOuter = (q, joinFrom, joinTo, ...on) => {
8959
+ return pushQueryValue(q, "and", {
8973
8960
  ON: {
8974
- joinTo,
8975
- joinFrom,
8976
- on: args
8961
+ joinTo: joinFrom,
8962
+ joinFrom: joinTo,
8963
+ useOuterJoinOverrides: true,
8964
+ on
8977
8965
  }
8978
- };
8966
+ });
8979
8967
  };
8980
8968
  const pushQueryOn = (q, joinFrom, joinTo, ...on) => {
8981
8969
  return pushQueryValue(
@@ -9026,16 +9014,13 @@ class OnMethods {
9026
9014
  *
9027
9015
  * ```ts
9028
9016
  * q
9029
- * // left column is the db.message column, right column is the db.user column
9030
- * .on('userId', 'id')
9031
- * // table names can be provided:
9032
9017
  * .on('message.userId', 'user.id')
9018
+ * // joined table name may be omitted
9019
+ * .on('userId', 'user.id')
9033
9020
  * // operator can be specified:
9034
- * .on('userId', '!=', 'id')
9021
+ * .on('userId', '!=', 'user.id')
9035
9022
  * // operator can be specified with table names as well:
9036
9023
  * .on('message.userId', '!=', 'user.id')
9037
- * // `.orOn` takes the same arguments as `.on` and acts like `.or`:
9038
- * .on('userId', 'id') // where message.userId = user.id
9039
9024
  * ```
9040
9025
  *
9041
9026
  * @param args - columns to join with
@@ -9380,6 +9365,7 @@ const resolveCallbacksInArgs = (q, args) => {
9380
9365
  qb.q = getClonedQueryData(q.q);
9381
9366
  qb.q.and = qb.q.or = qb.q.scopes = void 0;
9382
9367
  qb.q.subQuery = 1;
9368
+ qb.q.outerJoinOverrides = qb.q.joinOverrides;
9383
9369
  args[i] = resolveSubQueryCallback(qb, arg);
9384
9370
  }
9385
9371
  }
@@ -12769,5 +12755,5 @@ function copyTableData(query, arg) {
12769
12755
  return q;
12770
12756
  }
12771
12757
 
12772
- 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, 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 };
12758
+ 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 };
12773
12759
  //# sourceMappingURL=index.mjs.map