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
75 lines
2.7 KiB
Plaintext
75 lines
2.7 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 updateMany = async function updateMany({ collection: collectionSlug, data, joins: joinQuery, limit, locale, req, returning, select, sort: sortArg, where: whereToUse }) {
|
|
const collection = this.payload.collections[collectionSlug].config;
|
|
const tableName = this.tableNameMap.get(toSnakeCase(collection.slug));
|
|
const sort = sortArg !== undefined && sortArg !== null ? sortArg : collection.defaultSort;
|
|
const { joins, orderBy, selectFields, where } = buildQuery({
|
|
adapter: this,
|
|
fields: collection.flattenedFields,
|
|
locale,
|
|
sort,
|
|
tableName,
|
|
where: whereToUse
|
|
});
|
|
const db = await getTransaction(this, req);
|
|
let idsToUpdate = [];
|
|
const selectDistinctResult = await selectDistinct({
|
|
adapter: this,
|
|
db,
|
|
joins,
|
|
query: ({ query })=>orderBy ? query.orderBy(()=>orderBy.map(({ column, order })=>order(column))) : query,
|
|
selectFields,
|
|
tableName,
|
|
where
|
|
});
|
|
if (selectDistinctResult?.[0]?.id) {
|
|
idsToUpdate = selectDistinctResult?.map((doc)=>doc.id);
|
|
} else if (whereToUse && !joins.length) {
|
|
// If id wasn't passed but `where` without any joins, retrieve it with findFirst
|
|
const _db = db;
|
|
const table = this.tables[tableName];
|
|
let query = _db.select({
|
|
id: table.id
|
|
}).from(table).where(where).$dynamic();
|
|
if (typeof limit === 'number' && limit > 0) {
|
|
query = query.limit(limit);
|
|
}
|
|
if (orderBy) {
|
|
query = query.orderBy(()=>orderBy.map(({ column, order })=>order(column)));
|
|
}
|
|
const docsToUpdate = await query;
|
|
idsToUpdate = docsToUpdate?.map((doc)=>doc.id);
|
|
}
|
|
if (!idsToUpdate.length) {
|
|
return [];
|
|
}
|
|
const results = [];
|
|
// TODO: We need to batch this to reduce the amount of db calls. This can get very slow if we are updating a lot of rows.
|
|
for (const idToUpdate of idsToUpdate){
|
|
const result = await upsertRow({
|
|
id: idToUpdate,
|
|
adapter: this,
|
|
collectionSlug,
|
|
data,
|
|
db,
|
|
fields: collection.flattenedFields,
|
|
ignoreResult: returning === false,
|
|
joinQuery,
|
|
operation: 'update',
|
|
req,
|
|
select,
|
|
tableName
|
|
});
|
|
results.push(result);
|
|
}
|
|
if (returning === false) {
|
|
return null;
|
|
}
|
|
return results;
|
|
};
|
|
|
|
//# sourceMappingURL=updateMany.js.map |