import toSnakeCase from 'to-snake-case'; import { findMany } from './find/findMany.js'; import { upsertRow } from './upsertRow/index.js'; import { shouldUseOptimizedUpsertRow } from './upsertRow/shouldUseOptimizedUpsertRow.js'; import { getTransaction } from './utilities/getTransaction.js'; export const updateJobs = async function updateMany({ id, data, limit: limitArg, req, returning, sort: sortArg, where: whereArg }) { if (!data?.log?.length && !(data.log && typeof data.log === 'object' && '$push' in data.log)) { delete data.log; } const whereToUse = id ? { id: { equals: id } } : whereArg; const limit = id ? 1 : limitArg; const collection = this.payload.collections['payload-jobs'].config; const tableName = this.tableNameMap.get(toSnakeCase(collection.slug)); const sort = sortArg !== undefined && sortArg !== null ? sortArg : collection.defaultSort; const useOptimizedUpsertRow = shouldUseOptimizedUpsertRow({ data, fields: collection.flattenedFields }); if (useOptimizedUpsertRow && id) { const db = await getTransaction(this, req); const result = await upsertRow({ id, adapter: this, collectionSlug: 'payload-jobs', data, db, fields: collection.flattenedFields, ignoreResult: returning === false, operation: 'update', req, tableName }); return returning === false ? null : [ result ]; } const jobs = await findMany({ adapter: this, collectionSlug: 'payload-jobs', fields: collection.flattenedFields, limit: id ? 1 : limit, pagination: false, req, sort, tableName, where: whereToUse }); if (!jobs.docs.length) { return []; } const db = await getTransaction(this, req); 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 job of jobs.docs){ const updateData = useOptimizedUpsertRow ? data : { ...job, ...data }; const result = await upsertRow({ id: job.id, adapter: this, collectionSlug: 'payload-jobs', data: updateData, db, fields: collection.flattenedFields, ignoreResult: returning === false, operation: 'update', req, tableName }); results.push(result); } if (returning === false) { return null; } return results; }; //# sourceMappingURL=updateJobs.js.map