pqb 0.67.2 → 0.67.3

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
@@ -4540,7 +4540,11 @@ const loadRelations = async (state, result, savepointState, renames) => {
4540
4540
  const q = state.query;
4541
4541
  const primaryKeys = requirePrimaryKeys(q, "Cannot select a relation of a table that has no primary keys");
4542
4542
  const selectQuery = _unscope(q, "nonDeleted");
4543
- selectQuery.q.type = selectQuery.q.returnType = void 0;
4543
+ selectQuery.q.type = void 0;
4544
+ selectQuery.q.returnType = void 0;
4545
+ selectQuery.q.with = void 0;
4546
+ selectQuery.q.appendQueries = void 0;
4547
+ selectQuery.q.valuesJoinedAs = void 0;
4544
4548
  const matchSourceTableIds = {};
4545
4549
  for (const pkey of primaryKeys) matchSourceTableIds[pkey] = { in: result.map((row) => row[pkey]) };
4546
4550
  (selectQuery.q.and ??= []).push(matchSourceTableIds);
@@ -6938,22 +6942,33 @@ const setMutativeQueriesSelectRelationsSqlState = (d, as, rel) => {
6938
6942
  const handleInsertAndUpdateSelectRelationsSqlState = (ctx, state) => {
6939
6943
  if (state) ctx.topCtx.mutativeQueriesSelectRelationsSqlState = state;
6940
6944
  };
6945
+ const unsetValuesJoinedAsForMutativeSelectRelations = (query) => {
6946
+ if (!query.q.selectRelation || !query.q.valuesJoinedAs) return;
6947
+ const { valuesJoinedAs } = query.q;
6948
+ query.q.valuesJoinedAs = void 0;
6949
+ return valuesJoinedAs;
6950
+ };
6951
+ const restoreValuesJoinedAsForMutativeSelectRelations = (query, valuesJoinedAs) => {
6952
+ if (valuesJoinedAs) query.q.valuesJoinedAs = valuesJoinedAs;
6953
+ };
6941
6954
  const handleDeleteSelectRelationsSqlState = (ctx, query, relationSelectState) => {
6942
6955
  const selectRelations = relationSelectState?.value;
6943
6956
  if (!selectRelations) return;
6944
6957
  const selectPrimaryKeysQuery = prepareSubQueryForSql(query, _clone(query));
6958
+ selectPrimaryKeysQuery.q.valuesJoinedAs = void 0;
6945
6959
  const primaryKeys = requirePrimaryKeys(query, "primary keys are required for selecting relation in delete");
6946
6960
  _addToHookSelect(selectPrimaryKeysQuery, primaryKeys, true);
6947
6961
  const { as: cteAs } = moveQueryToCte(ctx, selectPrimaryKeysQuery, void 0, true);
6948
6962
  const relKeys = Object.keys(selectRelations);
6949
6963
  const hookSelect = selectPrimaryKeysQuery.q.hookSelect;
6964
+ const queryAs = getQueryAs(query);
6950
6965
  const join = {
6951
6966
  type: "JOIN",
6952
6967
  args: {
6953
6968
  w: cteAs,
6954
6969
  a: [Object.fromEntries(primaryKeys.map((key) => {
6955
6970
  const selected = hookSelect.get(key);
6956
- return [cteAs + "." + (selected.as || selected.select), key];
6971
+ return [cteAs + "." + (selected.as || selected.select), `${queryAs}.${key}`];
6957
6972
  }))]
6958
6973
  }
6959
6974
  };
@@ -7688,6 +7703,14 @@ function pushLimitSQL(sql, values, q) {
7688
7703
  }
7689
7704
  }
7690
7705
  const pushUpdateSql = (ctx, query, q, quotedAs, isSubSql) => {
7706
+ const valuesJoinedAs = unsetValuesJoinedAsForMutativeSelectRelations(query);
7707
+ try {
7708
+ return pushUpdateSqlWithoutValuesJoinedAs(ctx, query, q, quotedAs, isSubSql);
7709
+ } finally {
7710
+ restoreValuesJoinedAsForMutativeSelectRelations(query, valuesJoinedAs);
7711
+ }
7712
+ };
7713
+ const pushUpdateSqlWithoutValuesJoinedAs = (ctx, query, q, quotedAs, isSubSql) => {
7691
7714
  const quotedTable = `"${query.table || q.from}"`;
7692
7715
  const from = quoteTableWithSchema(query);
7693
7716
  const set = [];
@@ -9546,6 +9569,8 @@ const toSql = (table, type, topCtx, isSubSql, cteName, calledByThen, dontAddTabl
9546
9569
  _queryInsert(upsertOrCreate, query.upsertInsert());
9547
9570
  upsertOrCreate.q.type = "upsert";
9548
9571
  }
9572
+ upsertOrCreate.q.appendQueries = query.upsertCreateAppendQueries;
9573
+ upsertOrCreate.q.asFns = query.upsertCreateAsFns;
9549
9574
  const { makeSql: makeSecondSql } = moveMutativeQueryToCteBase(toSql, ctx, upsertOrCreate, "insert");
9550
9575
  sql.push(makeFirstSql(isSubSql), "UNION ALL", makeSecondSql(isSubSql));
9551
9576
  } else {
@@ -9701,9 +9726,9 @@ const addTopCteInternal = (place, ctx, item, type, dontAddTableHook) => {
9701
9726
  topCTE.stack.pop();
9702
9727
  };
9703
9728
  const addWithToSql = (ctx, sql, isSubSql) => {
9704
- if (!isSubSql && ctx.topCtx.topCTE) {
9729
+ if (!isSubSql && ctx.topCtx.topCTE?.append.length) {
9705
9730
  const sqls = [];
9706
- if (!isSubSql && ctx.topCtx.topCTE) for (const append of ctx.topCtx.topCTE.append) sqls.push(...append);
9731
+ for (const append of ctx.topCtx.topCTE.append) sqls.push(...append);
9707
9732
  sql.text = "WITH " + sqls.join(", ") + " " + sql.text;
9708
9733
  }
9709
9734
  };
@@ -12347,6 +12372,9 @@ var QueryExpressions = class {
12347
12372
  const _appendQuery = (main, append, asFn) => {
12348
12373
  return pushQueryValueImmutable(pushQueryValueImmutable(main, "appendQueries", prepareSubQueryForSql(main, append)), "asFns", asFn);
12349
12374
  };
12375
+ const _appendQueryOnUpsertCreate = (main, append, asFn) => {
12376
+ return pushQueryValueImmutable(pushQueryValueImmutable(main, "upsertCreateAppendQueries", prepareSubQueryForSql(main, append)), "upsertCreateAsFns", asFn);
12377
+ };
12350
12378
  const mergableObjects = new Set([
12351
12379
  "shape",
12352
12380
  "withShapes",
@@ -14309,6 +14337,6 @@ const testTransaction = {
14309
14337
  if (db.internal[trxForTest]?.length === 0) return db.q.adapter.close();
14310
14338
  }
14311
14339
  };
14312
- export { AdapterClass, ArrayColumn, BigIntColumn, BigSerialColumn, BitColumn, BitVaryingColumn, BooleanColumn, BoxColumn, ByteaColumn, CidrColumn, CircleColumn, CitextColumn, Column, CustomTypeColumn, DateBaseColumn, DateColumn, DateTimeBaseClass, DateTimeTzBaseClass, Db, DecimalColumn, DomainColumn, DoublePrecisionColumn, DynamicRawSQL, EnumColumn, Expression, InetColumn, IntegerBaseColumn, IntegerColumn, IntervalColumn, JSONColumn, JSONTextColumn, LimitedTextBaseColumn, LineColumn, LsegColumn, MacAddr8Column, MacAddrColumn, MoneyColumn, NotFoundError, NumberAsStringBaseColumn, NumberBaseColumn, Operators, OrchidOrmInternalError, PathColumn, PointColumn, PolygonColumn, PostgisGeographyPointColumn, QueryError, QueryHookUtils, QueryHooks, RawSql, RealColumn, SerialColumn, SmallIntColumn, SmallSerialColumn, StringColumn, TextBaseColumn, TextColumn, TimeColumn, TimestampColumn, TimestampTZColumn, TransactionAdapterClass, TsQueryColumn, TsVectorColumn, UUIDColumn, UnknownColumn, VarCharColumn, VirtualColumn, XMLColumn, _appendQuery, _clone, _createDbSqlMethod, _hookSelectColumns, _initQueryBuilder, _orCreate, _prependWith, _queryCreate, _queryCreateMany, _queryCreateManyFrom, _queryDefaults, _queryDelete, _queryFindBy, _queryFindByOptional, _queryHookAfterCreate, _queryHookAfterUpdate, _queryInsert, _queryInsertMany, _queryJoinOn, _queryRows, _querySelect, _queryTake, _queryTakeOptional, _queryUpdate, _queryUpdateOrThrow, _queryUpsert, _queryWhere, _queryWhereExists, _queryWhereIn, addCode, addTopCte, addTopCteSql, applyMixins, assignDbDataToColumn, backtickQuote, cloneQueryBaseUnscoped, codeToString, colors, columnsShapeToCode, constraintInnerToCode, consumeColumnName, copyTableData, createDbWithAdapter, deepCompare, defaultSchemaConfig, emptyArray, emptyObject, escapeForMigration, escapeString, excludeInnerToCode, exhaustive, getCallerFilePath, getClonedQueryData, getColumnBaseType, getColumnInfo, getColumnTypes, getDateAsDateFn, getDateAsNumberFn, getDriverErrorCode, getFreeAlias, getFreeSetAlias, getImportPath, getPrimaryKeys, getQueryAs, getQuerySchema, getShapeFromSelect, getSqlText, getStackTrace, getSupportedDefaultPrivileges, indexInnerToCode, internalSchemaConfig, isExpression, isQueryReturnsAll, isRawSQL, logColors, logParamToLogObject, makeColumnNullable, makeColumnTypes, makeColumnsByType, makeConnectRetryConfig, noop, objectHasValues, omit, parseTableData, parseTableDataInput, pathToLog, pick, pluralize, prepareSubQueryForSql, primaryKeyInnerToCode, pushQueryOnForOuter, pushQueryValueImmutable, pushTableDataCode, quoteIdentifier, quoteObjectKey, quoteTableWithSchema, raw, rawSqlToCode, referencesArgsToCode, returnArg, setColumnData, setColumnEncode, setColumnParse, setColumnParseNull, setCurrentColumnName, setDataValue, setDefaultLanguage, setFreeAlias, setQueryObjectValueImmutable, singleQuote, tableDataMethods, testTransaction, toArray, toCamelCase, toPascalCase, toSnakeCase, wrapAdapterFnWithConnectRetry };
14340
+ export { AdapterClass, ArrayColumn, BigIntColumn, BigSerialColumn, BitColumn, BitVaryingColumn, BooleanColumn, BoxColumn, ByteaColumn, CidrColumn, CircleColumn, CitextColumn, Column, CustomTypeColumn, DateBaseColumn, DateColumn, DateTimeBaseClass, DateTimeTzBaseClass, Db, DecimalColumn, DomainColumn, DoublePrecisionColumn, DynamicRawSQL, EnumColumn, Expression, InetColumn, IntegerBaseColumn, IntegerColumn, IntervalColumn, JSONColumn, JSONTextColumn, LimitedTextBaseColumn, LineColumn, LsegColumn, MacAddr8Column, MacAddrColumn, MoneyColumn, NotFoundError, NumberAsStringBaseColumn, NumberBaseColumn, Operators, OrchidOrmInternalError, PathColumn, PointColumn, PolygonColumn, PostgisGeographyPointColumn, QueryError, QueryHookUtils, QueryHooks, RawSql, RealColumn, SerialColumn, SmallIntColumn, SmallSerialColumn, StringColumn, TextBaseColumn, TextColumn, TimeColumn, TimestampColumn, TimestampTZColumn, TransactionAdapterClass, TsQueryColumn, TsVectorColumn, UUIDColumn, UnknownColumn, VarCharColumn, VirtualColumn, XMLColumn, _appendQuery, _appendQueryOnUpsertCreate, _clone, _createDbSqlMethod, _hookSelectColumns, _initQueryBuilder, _orCreate, _prependWith, _queryCreate, _queryCreateMany, _queryCreateManyFrom, _queryDefaults, _queryDelete, _queryFindBy, _queryFindByOptional, _queryHookAfterCreate, _queryHookAfterUpdate, _queryInsert, _queryInsertMany, _queryJoinOn, _queryRows, _querySelect, _queryTake, _queryTakeOptional, _queryUpdate, _queryUpdateOrThrow, _queryUpsert, _queryWhere, _queryWhereExists, _queryWhereIn, addCode, addTopCte, addTopCteSql, applyMixins, assignDbDataToColumn, backtickQuote, cloneQueryBaseUnscoped, codeToString, colors, columnsShapeToCode, constraintInnerToCode, consumeColumnName, copyTableData, createDbWithAdapter, deepCompare, defaultSchemaConfig, emptyArray, emptyObject, escapeForMigration, escapeString, excludeInnerToCode, exhaustive, getCallerFilePath, getClonedQueryData, getColumnBaseType, getColumnInfo, getColumnTypes, getDateAsDateFn, getDateAsNumberFn, getDriverErrorCode, getFreeAlias, getFreeSetAlias, getImportPath, getPrimaryKeys, getQueryAs, getQuerySchema, getShapeFromSelect, getSqlText, getStackTrace, getSupportedDefaultPrivileges, indexInnerToCode, internalSchemaConfig, isExpression, isQueryReturnsAll, isRawSQL, logColors, logParamToLogObject, makeColumnNullable, makeColumnTypes, makeColumnsByType, makeConnectRetryConfig, noop, objectHasValues, omit, parseTableData, parseTableDataInput, pathToLog, pick, pluralize, prepareSubQueryForSql, primaryKeyInnerToCode, pushQueryOnForOuter, pushQueryValueImmutable, pushTableDataCode, quoteIdentifier, quoteObjectKey, quoteTableWithSchema, raw, rawSqlToCode, referencesArgsToCode, returnArg, setColumnData, setColumnEncode, setColumnParse, setColumnParseNull, setCurrentColumnName, setDataValue, setDefaultLanguage, setFreeAlias, setQueryObjectValueImmutable, singleQuote, tableDataMethods, testTransaction, toArray, toCamelCase, toPascalCase, toSnakeCase, wrapAdapterFnWithConnectRetry };
14313
14341
 
14314
14342
  //# sourceMappingURL=index.mjs.map