This PR adds **atomic** `$push` **support for array fields**. It makes it possible to safely append new items to arrays, which is especially useful when running tasks in parallel (like job queues) where multiple processes might update the same record at the same time. By handling pushes atomically, we avoid race conditions and keep data consistent - especially on postgres, where the current implementation would nuke the entire array table before re-inserting every single array item. The feature works for both localized and unlocalized arrays, and supports pushing either single or multiple items at once. This PR is a requirement for reliably running parallel tasks in the job queue - see https://github.com/payloadcms/payload/pull/13452. Alongside documenting `$push`, this PR also adds documentation for `$inc`. ## Changes to updatedAt behavior https://github.com/payloadcms/payload/pull/13335 allows us to override the updatedAt property instead of the db always setting it to the current date. However, we are not able to skip updating the updatedAt property completely. This means, usage of $push results in 2 postgres db calls: 1. set updatedAt in main row 2. append array row in arrays table This PR changes the behavior to only automatically set updatedAt if it's undefined. If you explicitly set it to `null`, this now allows you to skip the db adapter automatically setting updatedAt. => This allows us to use $push in just one single db call ## Usage Examples ### Pushing a single item to an array ```ts const post = (await payload.db.updateOne({ data: { array: { $push: { text: 'some text 2', id: new mongoose.Types.ObjectId().toHexString(), }, }, }, collection: 'posts', id: post.id, })) ``` ### Pushing a single item to a localized array ```ts const post = (await payload.db.updateOne({ data: { arrayLocalized: { $push: { en: { text: 'some text 2', id: new mongoose.Types.ObjectId().toHexString(), }, es: { text: 'some text 2 es', id: new mongoose.Types.ObjectId().toHexString(), }, }, }, }, collection: 'posts', id: post.id, })) ``` ### Pushing multiple items to an array ```ts const post = (await payload.db.updateOne({ data: { array: { $push: [ { text: 'some text 2', id: new mongoose.Types.ObjectId().toHexString(), }, { text: 'some text 3', id: new mongoose.Types.ObjectId().toHexString(), }, ], }, }, collection: 'posts', id: post.id, })) ``` ### Pushing multiple items to a localized array ```ts const post = (await payload.db.updateOne({ data: { arrayLocalized: { $push: { en: { text: 'some text 2', id: new mongoose.Types.ObjectId().toHexString(), }, es: [ { text: 'some text 2 es', id: new mongoose.Types.ObjectId().toHexString(), }, { text: 'some text 3 es', id: new mongoose.Types.ObjectId().toHexString(), }, ], }, }, }, collection: 'posts', id: post.id, })) ``` --- - To see the specific tasks where the Asana app for GitHub is being used, see below: - https://app.asana.com/0/0/1211110462564647
138 lines
3.2 KiB
TypeScript
138 lines
3.2 KiB
TypeScript
import type { FlattenedArrayField } from 'payload'
|
|
|
|
import { fieldShouldBeLocalized } from 'payload/shared'
|
|
|
|
import type { DrizzleAdapter } from '../../types.js'
|
|
import type {
|
|
ArrayRowToInsert,
|
|
BlockRowToInsert,
|
|
NumberToDelete,
|
|
RelationshipToDelete,
|
|
TextToDelete,
|
|
} from './types.js'
|
|
|
|
import { isArrayOfRows } from '../../utilities/isArrayOfRows.js'
|
|
import { traverseFields } from './traverseFields.js'
|
|
|
|
type Args = {
|
|
adapter: DrizzleAdapter
|
|
arrayTableName: string
|
|
baseTableName: string
|
|
blocks: {
|
|
[blockType: string]: BlockRowToInsert[]
|
|
}
|
|
blocksToDelete: Set<string>
|
|
data: unknown
|
|
field: FlattenedArrayField
|
|
locale?: string
|
|
numbers: Record<string, unknown>[]
|
|
numbersToDelete: NumberToDelete[]
|
|
parentIsLocalized: boolean
|
|
path: string
|
|
relationships: Record<string, unknown>[]
|
|
relationshipsToDelete: RelationshipToDelete[]
|
|
selects: {
|
|
[tableName: string]: Record<string, unknown>[]
|
|
}
|
|
texts: Record<string, unknown>[]
|
|
textsToDelete: TextToDelete[]
|
|
/**
|
|
* Set to a locale code if this set of fields is traversed within a
|
|
* localized array or block field
|
|
*/
|
|
withinArrayOrBlockLocale?: string
|
|
}
|
|
|
|
export const transformArray = ({
|
|
adapter,
|
|
arrayTableName,
|
|
baseTableName,
|
|
blocks,
|
|
blocksToDelete,
|
|
data,
|
|
field,
|
|
locale,
|
|
numbers,
|
|
numbersToDelete,
|
|
parentIsLocalized,
|
|
path,
|
|
relationships,
|
|
relationshipsToDelete,
|
|
selects,
|
|
texts,
|
|
textsToDelete,
|
|
withinArrayOrBlockLocale,
|
|
}: Args) => {
|
|
const newRows: ArrayRowToInsert[] = []
|
|
|
|
const hasUUID = adapter.tables[arrayTableName]._uuid
|
|
|
|
if (isArrayOfRows(data)) {
|
|
data.forEach((arrayRow, i) => {
|
|
const newRow: ArrayRowToInsert = {
|
|
arrays: {},
|
|
arraysToPush: {},
|
|
locales: {},
|
|
row: {
|
|
_order: i + 1,
|
|
},
|
|
}
|
|
|
|
// If we have declared a _uuid field on arrays,
|
|
// that means the ID has to be unique,
|
|
// and our ids within arrays are not unique.
|
|
// So move the ID to a uuid field for storage
|
|
// and allow the database to generate a serial id automatically
|
|
if (hasUUID) {
|
|
newRow.row._uuid = arrayRow.id
|
|
delete arrayRow.id
|
|
}
|
|
|
|
if (locale) {
|
|
newRow.locales[locale] = {
|
|
_locale: locale,
|
|
}
|
|
}
|
|
|
|
if (fieldShouldBeLocalized({ field, parentIsLocalized }) && locale) {
|
|
newRow.row._locale = locale
|
|
}
|
|
|
|
if (withinArrayOrBlockLocale) {
|
|
newRow.row._locale = withinArrayOrBlockLocale
|
|
}
|
|
|
|
traverseFields({
|
|
adapter,
|
|
arrays: newRow.arrays,
|
|
arraysToPush: newRow.arraysToPush,
|
|
baseTableName,
|
|
blocks,
|
|
blocksToDelete,
|
|
columnPrefix: '',
|
|
data: arrayRow,
|
|
fieldPrefix: '',
|
|
fields: field.flattenedFields,
|
|
insideArrayOrBlock: true,
|
|
locales: newRow.locales,
|
|
numbers,
|
|
numbersToDelete,
|
|
parentIsLocalized: parentIsLocalized || field.localized,
|
|
parentTableName: arrayTableName,
|
|
path: `${path || ''}${field.name}.${i}.`,
|
|
relationships,
|
|
relationshipsToDelete,
|
|
row: newRow.row,
|
|
selects,
|
|
texts,
|
|
textsToDelete,
|
|
withinArrayOrBlockLocale,
|
|
})
|
|
|
|
newRows.push(newRow)
|
|
})
|
|
}
|
|
|
|
return newRows
|
|
}
|