import type { SQL } from 'drizzle-orm' import type { LibSQLDatabase } from 'drizzle-orm/libsql' import type { UpdateOne } from 'payload' import { eq } from 'drizzle-orm' import toSnakeCase from 'to-snake-case' import type { DrizzleAdapter, PostgresDB } from './types.js' import { buildQuery } from './queries/buildQuery.js' import { selectDistinct } from './queries/selectDistinct.js' import { transform } from './transform/read/index.js' import { transformForWrite } from './transform/write/index.js' import { upsertRow } from './upsertRow/index.js' import { shouldUseOptimizedUpsertRow } from './upsertRow/shouldUseOptimizedUpsertRow.js' import { getPrimaryDb } from './utilities/getPrimaryDb.js' import { getTransaction } from './utilities/getTransaction.js' import { markWrite } from './utilities/readAfterWrite.js' export const updateOne: UpdateOne = async function updateOne( this: DrizzleAdapter, { 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 let whereToUpdate: SQL | undefined const db = getPrimaryDb(this, await getTransaction(this, req)) if (!idToUpdate) { const { joins, selectFields, where } = buildQuery({ adapter: this, fields: collection.flattenedFields, locale, tableName, where: whereArg, }) // A `where` that needs joins cannot be applied to the UPDATE statement itself, so those // queries keep resolving a matching id first and then update that row unconditionally whereToUpdate = joins.length === 0 ? where : undefined if (options.atomic === true) { if (joins.length > 0) { throw new Error('Atomic where updates do not support predicates that require joins') } if (!shouldUseOptimizedUpsertRow({ data, fields: collection.flattenedFields })) { throw new Error('Atomic where updates only support fields stored on the main table') } const { arraysToPush, row } = transformForWrite({ adapter: this, data, enableAtomicWrites: true, fields: collection.flattenedFields, tableName, }) if (arraysToPush && Object.keys(arraysToPush).length) { throw new Error('Atomic where updates do not support array operations') } const table = this.tables[tableName] const matchingRowQuery = (db as PostgresDB) .select({ id: table.id }) .from(table) .where(where) .limit(1) // PostgreSQL must lock the candidate before applying LIMIT so a waiting caller can // continue to another match if a concurrent update makes the first row ineligible. // NO KEY UPDATE permits concurrent foreign-key checks on the candidate. // SQLite serializes writers and does not support SELECT ... FOR UPDATE. const matchingRow = this.name === 'postgres' ? matchingRowQuery.for('no key update') : matchingRowQuery markWrite(this) const docs = await (db as LibSQLDatabase) .update(table) .set(row) .where(eq(table.id, matchingRow)) .returning() if (!docs[0]) { return null } return transform({ adapter: this, config: this.payload.config, data: docs[0], fields: collection.flattenedFields, joinQuery: false, tableName, }) } // 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 as LibSQLDatabase) .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, where: whereToUpdate, }) if (returning === false) { return null } return result }