Files
payload/packages/db-postgres/src/transform/write/array.ts
Gokulsck f43cf185d4 feat: hasMany property for text fields (#4605)
* fix for supporting hasMany property in text field

* Updated docs

* handle text case types for schema and graphql schema

* fix unit test for required failing

* add unit test for has many text field

* add end to end test for has many on text field creation

* support has many feature for text field on postgres

---------

Co-authored-by: Chris Heinz <chrisi.heinz@web.de>
2024-01-04 14:45:00 -05:00

107 lines
2.4 KiB
TypeScript

/* eslint-disable no-param-reassign */
import type { ArrayField } from 'payload/types'
import type { PostgresAdapter } from '../../types'
import type { ArrayRowToInsert, BlockRowToInsert, RelationshipToDelete } from './types'
import { isArrayOfRows } from '../../utilities/isArrayOfRows'
import { traverseFields } from './traverseFields'
type Args = {
adapter: PostgresAdapter
arrayTableName: string
baseTableName: string
blocks: {
[blockType: string]: BlockRowToInsert[]
}
blocksToDelete: Set<string>
data: unknown
field: ArrayField
locale?: string
texts: Record<string, unknown>[]
numbers: Record<string, unknown>[]
path: string
relationships: Record<string, unknown>[]
relationshipsToDelete: RelationshipToDelete[]
selects: {
[tableName: string]: Record<string, unknown>[]
}
}
export const transformArray = ({
adapter,
arrayTableName,
baseTableName,
blocks,
blocksToDelete,
data,
field,
locale,
texts,
numbers,
path,
relationships,
relationshipsToDelete,
selects,
}: Args) => {
const newRows: ArrayRowToInsert[] = []
const hasUUID = adapter.tables[arrayTableName]._uuid
if (isArrayOfRows(data)) {
data.forEach((arrayRow, i) => {
const newRow: ArrayRowToInsert = {
arrays: {},
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 (field.localized) {
newRow.row._locale = locale
}
traverseFields({
adapter,
arrays: newRow.arrays,
baseTableName,
blocks,
blocksToDelete,
columnPrefix: '',
data: arrayRow,
fieldPrefix: '',
fields: field.fields,
locales: newRow.locales,
texts,
numbers,
parentTableName: arrayTableName,
path: `${path || ''}${field.name}.${i}.`,
relationships,
relationshipsToDelete,
row: newRow.row,
selects,
})
newRows.push(newRow)
})
}
return newRows
}