Files
klz-cables.com/.pnpm-store/v10/files/f4/e011473e9175501489ad52380189a710637fa6184aecfccdb7b563f797fbe64280dd17d0ca31b30fe5908d7fa1ee3bb0b1a238e8da3e2898cb36151ea09257
Marc Mintel 5397309103
Some checks failed
Build & Deploy / 🔍 Prepare (push) Successful in 20s
Build & Deploy / 🧪 QA (push) Failing after 34s
Build & Deploy / 🏗️ Build (push) Has started running
Build & Deploy / 🚀 Deploy (push) Has been cancelled
Build & Deploy / 🧪 Smoke Test (push) Has been cancelled
Build & Deploy / ⚡ Lighthouse (push) Has been cancelled
Build & Deploy / 🔔 Notify (push) Has been cancelled
fix(products): fix breadcrumbs and product filtering (backport from main)
2026-02-24 16:04:21 +01:00

68 lines
2.3 KiB
Plaintext

import toSnakeCase from 'to-snake-case';
import { buildQuery } from './queries/buildQuery.js';
import { selectDistinct } from './queries/selectDistinct.js';
import { upsertRow } from './upsertRow/index.js';
import { getTransaction } from './utilities/getTransaction.js';
export const updateOne = async function updateOne({ id, collection: collectionSlug, data, joins: joinQuery, locale, options = {
upsert: false
}, req, returning, select, where: whereArg }) {
const collection = this.payload.collections[collectionSlug].config;
const tableName = this.tableNameMap.get(toSnakeCase(collection.slug));
let idToUpdate = id;
const db = await getTransaction(this, req);
if (!idToUpdate) {
const { joins, selectFields, where } = buildQuery({
adapter: this,
fields: collection.flattenedFields,
locale,
tableName,
where: whereArg
});
// selectDistinct will only return if there are joins
const selectDistinctResult = await selectDistinct({
adapter: this,
db,
joins,
query: ({ query })=>query.limit(1),
selectFields,
tableName,
where
});
if (selectDistinctResult?.[0]?.id) {
idToUpdate = selectDistinctResult?.[0]?.id;
// If id wasn't passed but `where` without any joins, retrieve it with findFirst
} else if (whereArg && !joins.length) {
const table = this.tables[tableName];
const docsToUpdate = await db.select({
id: table.id
}).from(table).where(where).limit(1);
idToUpdate = docsToUpdate?.[0]?.id;
}
}
if (!idToUpdate && !options.upsert) {
// TODO: In 4.0, if returning === false, we should differentiate between:
// - No document found to update
// - Document found, but returning === false
return null;
}
const result = await upsertRow({
id: idToUpdate,
adapter: this,
collectionSlug,
data,
db,
fields: collection.flattenedFields,
ignoreResult: returning === false,
joinQuery,
operation: 'update',
req,
select,
tableName
});
if (returning === false) {
return null;
}
return result;
};
//# sourceMappingURL=updateOne.js.map