chore: builds hasMany selects

This commit is contained in:
James
2023-09-19 17:15:10 -04:00
parent 32fb5e66f8
commit bbcdf32a90
10 changed files with 204 additions and 37 deletions

View File

@@ -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

View 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
}

View File

@@ -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 }[] = []

View File

@@ -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>[]
}
}