chore: builds hasMany selects
This commit is contained in:
@@ -22,6 +22,7 @@ export const transformForWrite = ({ data, fields, path = '', tableName }: Args):
|
||||
numbers: [],
|
||||
relationships: [],
|
||||
row: {},
|
||||
selects: {},
|
||||
}
|
||||
|
||||
// This function is responsible for building up the
|
||||
@@ -39,6 +40,7 @@ export const transformForWrite = ({ data, fields, path = '', tableName }: Args):
|
||||
path,
|
||||
relationships: rowToInsert.relationships,
|
||||
row: rowToInsert.row,
|
||||
selects: rowToInsert.selects,
|
||||
})
|
||||
|
||||
return rowToInsert
|
||||
|
||||
28
packages/db-postgres/src/transform/write/selects.ts
Normal file
28
packages/db-postgres/src/transform/write/selects.ts
Normal file
@@ -0,0 +1,28 @@
|
||||
/* eslint-disable no-param-reassign */
|
||||
import { isArrayOfRows } from '../../utilities/isArrayOfRows'
|
||||
|
||||
type Args = {
|
||||
data: unknown
|
||||
locale?: string
|
||||
}
|
||||
|
||||
export const transformSelects = ({ data, locale }: Args) => {
|
||||
const newRows: Record<string, unknown>[] = []
|
||||
|
||||
if (isArrayOfRows(data)) {
|
||||
data.forEach((value, i) => {
|
||||
const newRow: Record<string, unknown> = {
|
||||
order: i + 1,
|
||||
value,
|
||||
}
|
||||
|
||||
if (locale) {
|
||||
newRow.locale = locale
|
||||
}
|
||||
|
||||
newRows.push(newRow)
|
||||
})
|
||||
}
|
||||
|
||||
return newRows
|
||||
}
|
||||
@@ -11,6 +11,7 @@ import { transformArray } from './array'
|
||||
import { transformBlocks } from './blocks'
|
||||
import { transformNumbers } from './numbers'
|
||||
import { transformRelationship } from './relationships'
|
||||
import { transformSelects } from './selects'
|
||||
|
||||
type Args = {
|
||||
arrays: {
|
||||
@@ -33,6 +34,9 @@ type Args = {
|
||||
path: string
|
||||
relationships: Record<string, unknown>[]
|
||||
row: Record<string, unknown>
|
||||
selects: {
|
||||
[tableName: string]: Record<string, unknown>[]
|
||||
}
|
||||
}
|
||||
|
||||
export const traverseFields = ({
|
||||
@@ -50,6 +54,7 @@ export const traverseFields = ({
|
||||
path,
|
||||
relationships,
|
||||
row,
|
||||
selects,
|
||||
}: Args) => {
|
||||
fields.forEach((field) => {
|
||||
let columnName = ''
|
||||
@@ -150,6 +155,7 @@ export const traverseFields = ({
|
||||
path: `${path || ''}${field.name}.`,
|
||||
relationships,
|
||||
row,
|
||||
selects,
|
||||
})
|
||||
})
|
||||
} else {
|
||||
@@ -167,6 +173,7 @@ export const traverseFields = ({
|
||||
path: `${path || ''}${field.name}.`,
|
||||
relationships,
|
||||
row,
|
||||
selects,
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -195,6 +202,7 @@ export const traverseFields = ({
|
||||
path: `${path || ''}${tab.name}.`,
|
||||
relationships,
|
||||
row,
|
||||
selects,
|
||||
})
|
||||
})
|
||||
} else {
|
||||
@@ -212,6 +220,7 @@ export const traverseFields = ({
|
||||
path: `${path || ''}${tab.name}.`,
|
||||
relationships,
|
||||
row,
|
||||
selects,
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -230,6 +239,7 @@ export const traverseFields = ({
|
||||
path,
|
||||
relationships,
|
||||
row,
|
||||
selects,
|
||||
})
|
||||
}
|
||||
})
|
||||
@@ -250,6 +260,7 @@ export const traverseFields = ({
|
||||
path,
|
||||
relationships,
|
||||
row,
|
||||
selects,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -313,6 +324,34 @@ export const traverseFields = ({
|
||||
return
|
||||
}
|
||||
|
||||
if (field.type === 'select' && field.hasMany && Array.isArray(fieldData)) {
|
||||
const selectTableName = `${newTableName}_${toSnakeCase(field.name)}`
|
||||
if (!selects[selectTableName]) selects[selectTableName] = []
|
||||
|
||||
if (field.localized) {
|
||||
if (typeof data[field.name] === 'object' && data[field.name] !== null) {
|
||||
Object.entries(data[field.name]).forEach(([localeKey, localeData]) => {
|
||||
if (Array.isArray(localeData)) {
|
||||
const newRows = transformSelects({
|
||||
data: localeData,
|
||||
locale: localeKey,
|
||||
})
|
||||
|
||||
selects[selectTableName] = selects[selectTableName].concat(newRows)
|
||||
}
|
||||
})
|
||||
}
|
||||
} else {
|
||||
const newRows = transformSelects({
|
||||
data: data[field.name],
|
||||
})
|
||||
|
||||
selects[selectTableName] = selects[selectTableName].concat(newRows)
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
if (fieldAffectsData(field)) {
|
||||
const valuesToTransform: { localeKey?: string; ref: unknown; value: unknown }[] = []
|
||||
|
||||
|
||||
@@ -32,4 +32,7 @@ export type RowToInsert = {
|
||||
numbers: Record<string, unknown>[]
|
||||
relationships: Record<string, unknown>[]
|
||||
row: Record<string, unknown>
|
||||
selects: {
|
||||
[tableName: string]: Record<string, unknown>[]
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user