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
68 lines
2.3 KiB
Plaintext
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 |