pqb 0.43.5 → 0.44.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
@@ -2125,14 +2125,13 @@ const processWhere = (ands, ctx, table, query, data, quotedAs) => {
2125
2125
  } else {
2126
2126
  const item = value;
2127
2127
  const joinAs = `"${getJoinItemSource(item.joinFrom)}"`;
2128
- const { on } = item;
2129
2128
  const q = item.useOuterAliases ? {
2130
2129
  joinedShapes: query.joinedShapes,
2131
2130
  aliases: query.outerAliases,
2132
2131
  shape: query.shape
2133
2132
  } : query;
2134
2133
  ands.push(
2135
- `${onColumnToSql(ctx, q, joinAs, on[0])} ${on.length === 2 ? "=" : on[1]} ${onColumnToSql(ctx, q, joinAs, on.length === 3 ? on[2] : on[1])}`
2134
+ `${onColumnToSql(ctx, q, joinAs, item.from)} ${item.op || "="} ${onColumnToSql(ctx, q, joinAs, item.to)}`
2136
2135
  );
2137
2136
  }
2138
2137
  } else if (key === "IN") {
@@ -2609,7 +2608,7 @@ const resolveCallbacksInArgs = (q, args) => {
2609
2608
  qb.q.and = qb.q.or = qb.q.scopes = void 0;
2610
2609
  qb.q.subQuery = 1;
2611
2610
  qb.q.outerAliases = qb.q.aliases;
2612
- args[i] = resolveSubQueryCallback(qb, arg);
2611
+ args[i] = resolveSubQueryCallbackV2(qb, arg);
2613
2612
  }
2614
2613
  }
2615
2614
  };
@@ -3606,9 +3605,7 @@ const addAllShapesAndParsers = (query, joinKey, shape, parsers, batchParsers, co
3606
3605
  computeds
3607
3606
  );
3608
3607
  };
3609
- const _joinLateral = (self, type, arg, cb, as) => {
3610
- var _a, _b, _c;
3611
- const q = self;
3608
+ const _joinLateralProcessArg = (q, arg, cb) => {
3612
3609
  let relation;
3613
3610
  if (typeof arg === "string") {
3614
3611
  relation = q.relations[arg];
@@ -3630,29 +3627,36 @@ const _joinLateral = (self, type, arg, cb, as) => {
3630
3627
  }
3631
3628
  }
3632
3629
  }
3633
- const query = arg;
3634
- query.q.joinTo = q;
3635
- const joinedAs = getQueryAs(q);
3636
- ((_a = query.q).joinedShapes ?? (_a.joinedShapes = {}))[joinedAs] = q.q.shape;
3637
- let result = resolveSubQueryCallback(query, cb);
3630
+ let result = resolveSubQueryCallbackV2(
3631
+ arg,
3632
+ cb
3633
+ );
3638
3634
  if (relation) {
3639
3635
  result = relation.relationConfig.joinQuery(
3640
3636
  result,
3641
3637
  q
3642
3638
  );
3643
3639
  }
3644
- const joinKey = as || result.q.as || result.table;
3640
+ return result;
3641
+ };
3642
+ const _joinLateral = (self, type, arg, as) => {
3643
+ var _a, _b, _c;
3644
+ const q = self;
3645
+ arg.q.joinTo = q;
3646
+ const joinedAs = getQueryAs(q);
3647
+ ((_a = arg.q).joinedShapes ?? (_a.joinedShapes = {}))[joinedAs] = q.q.shape;
3648
+ const joinKey = as || arg.q.as || arg.table;
3645
3649
  if (joinKey) {
3646
- const shape = getShapeFromSelect(result, true);
3650
+ const shape = getShapeFromSelect(arg, true);
3647
3651
  setQueryObjectValue(q, "joinedShapes", joinKey, shape);
3648
- setQueryObjectValue(q, "joinedParsers", joinKey, result.q.parsers);
3649
- if (result.q.batchParsers) {
3650
- ((_b = q.q).joinedBatchParsers ?? (_b.joinedBatchParsers = {}))[joinKey] = result.q.batchParsers;
3652
+ setQueryObjectValue(q, "joinedParsers", joinKey, arg.q.parsers);
3653
+ if (arg.q.batchParsers) {
3654
+ ((_b = q.q).joinedBatchParsers ?? (_b.joinedBatchParsers = {}))[joinKey] = arg.q.batchParsers;
3651
3655
  }
3652
3656
  }
3653
- as || (as = getQueryAs(result));
3654
- ((_c = q.q).joinedComputeds ?? (_c.joinedComputeds = {}))[as] = result.q.computeds;
3655
- return pushQueryValue(q, "join", [type, result, as]);
3657
+ as || (as = getQueryAs(arg));
3658
+ ((_c = q.q).joinedComputeds ?? (_c.joinedComputeds = {}))[as] = arg.q.computeds;
3659
+ return pushQueryValue(q, "join", [type, arg, as]);
3656
3660
  };
3657
3661
 
3658
3662
  class EnumColumn extends ColumnType {
@@ -5201,7 +5205,7 @@ const processSelectArg = (q, as, arg, columnAs) => {
5201
5205
  let value = arg[key];
5202
5206
  let joinQuery;
5203
5207
  if (typeof value === "function") {
5204
- value = resolveSubQueryCallback(q, value);
5208
+ value = resolveSubQueryCallbackV2(q, value);
5205
5209
  if (isQueryNone(value)) {
5206
5210
  if (value.q.innerJoinLateral) {
5207
5211
  return false;
@@ -5243,7 +5247,6 @@ const processSelectArg = (q, as, arg, columnAs) => {
5243
5247
  q,
5244
5248
  value.q.innerJoinLateral ? "JOIN" : "LEFT JOIN",
5245
5249
  query,
5246
- (q2) => q2,
5247
5250
  key
5248
5251
  );
5249
5252
  }
@@ -6901,13 +6904,13 @@ const getQueryAs = (q) => {
6901
6904
  const makeRegexToFindInSql = (value) => {
6902
6905
  return new RegExp(`${value}(?=(?:[^']*'[^']*')*[^']*$)`, "g");
6903
6906
  };
6904
- const resolveSubQueryCallback = (q, cb) => {
6905
- let arg;
6907
+ const resolveSubQueryCallbackV2 = (q, cb) => {
6908
+ let base;
6906
6909
  if (q.table) {
6907
- if (!q.internal.callbackArg) {
6908
- const base = Object.create(q.baseQuery);
6910
+ base = q.internal.callbackArg;
6911
+ if (!base) {
6912
+ base = Object.create(q.baseQuery);
6909
6913
  base.baseQuery = base;
6910
- q.internal.callbackArg = base;
6911
6914
  const { relations } = q;
6912
6915
  for (const key in relations) {
6913
6916
  Object.defineProperty(base, key, {
@@ -6917,21 +6920,17 @@ const resolveSubQueryCallback = (q, cb) => {
6917
6920
  }
6918
6921
  });
6919
6922
  }
6923
+ q.internal.callbackArg = base;
6920
6924
  }
6921
- arg = Object.create(q.internal.callbackArg);
6922
- arg.q = q.q;
6923
6925
  } else {
6924
- arg = q;
6925
- }
6926
- const { subQuery, relChain, outerAliases } = q.q;
6927
- q.q.subQuery = 1;
6928
- q.q.relChain = void 0;
6929
- q.q.outerAliases = q.q.aliases;
6930
- const result = cb(arg);
6931
- q.q.subQuery = subQuery;
6932
- q.q.relChain = relChain;
6933
- q.q.outerAliases = outerAliases;
6934
- return result;
6926
+ base = q;
6927
+ }
6928
+ const arg = Object.create(base);
6929
+ arg.q = getClonedQueryData(q.q);
6930
+ arg.q.subQuery = 1;
6931
+ arg.q.relChain = void 0;
6932
+ arg.q.outerAliases = q.q.aliases;
6933
+ return cb(arg);
6935
6934
  };
6936
6935
  const joinSubQuery = (q, sub) => {
6937
6936
  if (!("relationConfig" in sub)) return sub;
@@ -8147,7 +8146,7 @@ const processCreateItem = (q, item, rowIndex, ctx, encoders) => {
8147
8146
  );
8148
8147
  } else {
8149
8148
  if (typeof item[key] === "function") {
8150
- item[key] = resolveSubQueryCallback(
8149
+ item[key] = resolveSubQueryCallbackV2(
8151
8150
  q,
8152
8151
  item[key]
8153
8152
  );
@@ -9903,11 +9902,11 @@ class Join {
9903
9902
  * @param cb - {@link JoinLateralCallback}
9904
9903
  */
9905
9904
  joinLateral(arg, cb) {
9905
+ const q = _clone(this);
9906
9906
  return _joinLateral(
9907
- _clone(this),
9907
+ q,
9908
9908
  "JOIN",
9909
- arg,
9910
- cb
9909
+ _joinLateralProcessArg(q, arg, cb)
9911
9910
  );
9912
9911
  }
9913
9912
  /**
@@ -9926,28 +9925,32 @@ class Join {
9926
9925
  * @param cb - {@link JoinLateralCallback}
9927
9926
  */
9928
9927
  leftJoinLateral(arg, cb) {
9928
+ const q = _clone(this);
9929
9929
  return _joinLateral(
9930
9930
  _clone(this),
9931
9931
  "LEFT JOIN",
9932
- arg,
9933
- cb
9932
+ _joinLateralProcessArg(q, arg, cb)
9934
9933
  );
9935
9934
  }
9936
9935
  }
9937
9936
  const makeOnItem = (joinTo, joinFrom, args) => ({
9938
9937
  ON: {
9939
- joinTo,
9940
9938
  joinFrom,
9941
- on: args
9939
+ from: args[0],
9940
+ joinTo,
9941
+ to: args.length === 2 ? args[1] : args[2],
9942
+ op: args.length === 2 ? void 0 : args[1]
9942
9943
  }
9943
9944
  });
9944
9945
  const pushQueryOnForOuter = (q, joinFrom, joinTo, ...on) => {
9945
9946
  return pushQueryValue(q, "and", {
9946
9947
  ON: {
9947
- joinTo: joinFrom,
9948
9948
  joinFrom: joinTo,
9949
+ from: on[0],
9950
+ joinTo: joinFrom,
9951
+ to: on.length === 2 ? on[1] : on[2],
9949
9952
  useOuterAliases: true,
9950
- on
9953
+ op: on.length === 2 ? void 0 : on[1]
9951
9954
  }
9952
9955
  });
9953
9956
  };
@@ -10365,11 +10368,11 @@ const _queryUpdate = (query, arg) => {
10365
10368
  } else {
10366
10369
  let value = set[key];
10367
10370
  if (typeof value === "function") {
10368
- value = resolveSubQueryCallback(
10371
+ value = resolveSubQueryCallbackV2(
10369
10372
  query.baseQuery,
10370
10373
  value
10371
10374
  );
10372
- if (value instanceof Db && value.q.type) {
10375
+ if (value instanceof Db && value.q.type && value.q.subQuery) {
10373
10376
  throw new OrchidOrmInternalError(
10374
10377
  value,
10375
10378
  `Only selecting queries are allowed inside callback of update, ${value.q.type} is given instead.`
@@ -10491,7 +10494,7 @@ class Update {
10491
10494
  *
10492
10495
  * // use query that returns a single value
10493
10496
  * // returning multiple values will result in Postgres error
10494
- * column3: db.otherTable.get('someColumn'),
10497
+ * column3: () => db.otherTable.get('someColumn'),
10495
10498
  *
10496
10499
  * // select a single value from a related record
10497
10500
  * fromRelation: (q) => q.relatedTable.get('someColumn'),
@@ -10508,16 +10511,16 @@ class Update {
10508
10511
  * ```ts
10509
10512
  * await db.table.where({ ...conditions }).update({
10510
10513
  * // `column` will be set to a value of the `otherColumn` of the created record.
10511
- * column: db.otherTable.get('otherColumn').create({ ...data }),
10514
+ * column: () => db.otherTable.get('otherColumn').create({ ...data }),
10512
10515
  *
10513
10516
  * // `column2` will be set to a value of the `otherColumn` of the updated record.
10514
- * column2: db.otherTable
10517
+ * column2: () => db.otherTable
10515
10518
  * .get('otherColumn')
10516
10519
  * .findBy({ ...conditions })
10517
10520
  * .update({ key: 'value' }),
10518
10521
  *
10519
10522
  * // `column3` will be set to a value of the `otherColumn` of the deleted record.
10520
- * column3: db.otherTable
10523
+ * column3: () => db.otherTable
10521
10524
  * .get('otherColumn')
10522
10525
  * .findBy({ ...conditions })
10523
10526
  * .delete(),
@@ -12908,5 +12911,5 @@ function copyTableData(query, arg) {
12908
12911
  return q;
12909
12912
  }
12910
12913
 
12911
- export { Adapter, AfterCommitError, 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, PostgisGeographyPointColumn, 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, _afterCommitError, _clone, _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, _queryResolveAlias, _queryRows, _querySelect, _queryTake, _queryTakeOptional, _queryUnion, _queryUpdate, _queryUpdateOrThrow, _queryUpdateRaw, _queryWhere, _queryWhereExists, _queryWhereIn, _queryWhereNot, _queryWhereNotOneOf, _queryWhereNotSql, _queryWhereOneOf, _queryWhereSql, addColumnParserToQuery, addParserForRawExpression, addParserForSelectItem, addQueryOn, anyShape, applyComputedColumns, checkIfASimpleQuery, cloneQuery, cloneQueryBaseUnscoped, columnCheckToCode, columnCode, columnExcludesToCode, columnForeignKeysToCode, columnIndexesToCode, columnsShapeToCode, commitSql$1 as commitSql, constraintInnerToCode, constraintToCode, copyTableData, countSelect, createDb, defaultSchemaConfig, escapeForLog, escapeForMigration, escapeString, excludeInnerToCode, excludeToCode, extendQuery, filterResult, foreignKeyArgumentToCode, getClonedQueryData, getColumnInfo, getColumnTypes, getFullColumnTable, getPrimaryKeys, getQueryAs, getShapeFromSelect, getSqlText, handleResult, identityToCode, indexInnerToCode, indexToCode, instantiateColumn, isDefaultTimeStamp, isQueryReturnsAll, isSelectingCount, joinSubQuery, logParamToLogObject, makeColumnTypes, makeColumnsByType, makeFnExpression, makeRegexToFindInSql, makeSQL, parseRecord, parseTableData, parseTableDataInput, postgisTypmodToSql, primaryKeyInnerToCode, processComputedBatches, processComputedResult, processSelectArg, pushLimitSQL, pushQueryArray, pushQueryOn, pushQueryOnForOuter, pushQueryOrOn, pushQueryValue, pushTableDataCode, queryFrom, queryFromSql, queryJson, queryMethodByReturnType, queryTypeWithLimitOne, queryWrap, raw, referencesArgsToCode, resolveSubQueryCallback, rollbackSql$1 as rollbackSql, saveSearchAlias, setColumnDefaultParse, setColumnEncode, setColumnParse, setColumnParseNull, setParserForSelectedString, setQueryObjectValue, setQueryOperators, simplifyColumnDefault, sqlFn, sqlQueryArgsToExpression, tableDataMethods, templateLiteralToSQL, testTransaction, throwIfJoinLateral, throwIfNoWhere, toSQL, toSQLCacheKey };
12914
+ export { Adapter, AfterCommitError, 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, PostgisGeographyPointColumn, 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, _afterCommitError, _clone, _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, _queryResolveAlias, _queryRows, _querySelect, _queryTake, _queryTakeOptional, _queryUnion, _queryUpdate, _queryUpdateOrThrow, _queryUpdateRaw, _queryWhere, _queryWhereExists, _queryWhereIn, _queryWhereNot, _queryWhereNotOneOf, _queryWhereNotSql, _queryWhereOneOf, _queryWhereSql, addColumnParserToQuery, addParserForRawExpression, addParserForSelectItem, addQueryOn, anyShape, applyComputedColumns, checkIfASimpleQuery, cloneQuery, cloneQueryBaseUnscoped, columnCheckToCode, columnCode, columnExcludesToCode, columnForeignKeysToCode, columnIndexesToCode, columnsShapeToCode, commitSql$1 as commitSql, constraintInnerToCode, constraintToCode, copyTableData, countSelect, createDb, defaultSchemaConfig, escapeForLog, escapeForMigration, escapeString, excludeInnerToCode, excludeToCode, extendQuery, filterResult, foreignKeyArgumentToCode, getClonedQueryData, getColumnInfo, getColumnTypes, getFullColumnTable, getPrimaryKeys, getQueryAs, getShapeFromSelect, getSqlText, handleResult, identityToCode, indexInnerToCode, indexToCode, instantiateColumn, isDefaultTimeStamp, isQueryReturnsAll, isSelectingCount, joinSubQuery, logParamToLogObject, makeColumnTypes, makeColumnsByType, makeFnExpression, makeRegexToFindInSql, makeSQL, parseRecord, parseTableData, parseTableDataInput, postgisTypmodToSql, primaryKeyInnerToCode, processComputedBatches, processComputedResult, processSelectArg, pushLimitSQL, pushQueryArray, pushQueryOn, pushQueryOnForOuter, pushQueryOrOn, pushQueryValue, pushTableDataCode, queryFrom, queryFromSql, queryJson, queryMethodByReturnType, queryTypeWithLimitOne, queryWrap, raw, referencesArgsToCode, resolveSubQueryCallbackV2, rollbackSql$1 as rollbackSql, saveSearchAlias, setColumnDefaultParse, setColumnEncode, setColumnParse, setColumnParseNull, setParserForSelectedString, setQueryObjectValue, setQueryOperators, simplifyColumnDefault, sqlFn, sqlQueryArgsToExpression, tableDataMethods, templateLiteralToSQL, testTransaction, throwIfJoinLateral, throwIfNoWhere, toSQL, toSQLCacheKey };
12912
12915
  //# sourceMappingURL=index.mjs.map