Add data (without links)

This commit is contained in:
Adria Navarro 2025-04-01 14:06:17 +02:00
parent 6d176ccb74
commit 35a2003edd
2 changed files with 70 additions and 93 deletions

@ -1 +1 @@
Subproject commit ca0b9af3cdd2efd010dd5c466f85d030c408b776 Subproject commit 1dfb5a9337fefa29861026fea86551132491e0bf

View File

@ -59,9 +59,8 @@ export async function generateTables(
const createdTables: GenerateTablesResponse["createdTables"] = [] const createdTables: GenerateTablesResponse["createdTables"] = []
for (const table of response.tables) { for (const table of response.tables) {
const { _id, ...structure } = table.structure
const createdTable = await sdk.tables.create({ const createdTable = await sdk.tables.create({
...structure, ...table.structure,
sourceId: dsId, sourceId: dsId,
schema: {}, schema: {},
primaryDisplay: undefined, primaryDisplay: undefined,
@ -100,105 +99,83 @@ export async function generateTables(
}, },
primaryDisplay: table.primaryDisplay, primaryDisplay: table.primaryDisplay,
}) })
// const readTableCtx = {
// params: { tableId: table._id },
// user: ctx.user,
// throw: ctx.throw,
// } as any
// await tableController.read(readTableCtx, async () => {})
// const updateTableCtx = {
// request: {
// body: {
// ...readTableCtx.body,
// schema: {
// ...readTableCtx.body.schema,
// ...table.schema,
// },
// primaryDisplay: table.primaryDisplay,
// },
// },
// params: { tableId: table._id },
// user: ctx.user,
// throw: ctx.throw,
// } as any
// await tableController.update(updateTableCtx, async () => {})
} }
// if (addData) { if (addData) {
// const createdData: Record<string, Record<string, string>> = {} const createdData: Record<string, Record<string, string>> = {}
// const toUpdateLinks: { const toUpdateLinks: {
// tableId: string tableId: string
// rowId: string rowId: string
// data: Record<string, { rowId: string; tableId: string }> data: Record<string, { rowId: string; tableId: string }>
// }[] = [] }[] = []
// for (const table of Object.values(json.tables)) { for (const table of Object.values(response.tables)) {
// const dataToAdd = json.data?.[table.name] const linksOverride: Record<string, null> = {}
for (const field of table.structure.schema.filter(
f => f.type === FieldType.LINK
)) {
linksOverride[field.name] = null
}
// const linksOverride: Record<string, null> = {} for (const entry of table.data || []) {
// for (const fieldKey of Object.keys(table.schema).filter( const createdRow = await sdk.rows.save(
// f => table.schema[f].type === FieldType.LINK table.structure._id,
// )) { {
// linksOverride[fieldKey] = null ...entry.values.reduce<Record<string, any>>((acc, v) => {
// } acc[v.key] = v.value
return acc
}, {}),
...linksOverride,
_id: undefined,
},
ctx.user._id
)
// for (const entry of dataToAdd || []) { createdData[table.structure._id] ??= {}
// const createdRow = await sdk.rows.save( createdData[table.structure._id][entry.id] = createdRow.row._id!
// table._id!,
// {
// ...entry,
// ...linksOverride,
// _id: undefined,
// },
// ctx.user._id
// )
// createdData[table._id!] ??= {} const overridenLinks = Object.keys(linksOverride).reduce<
// createdData[table._id!][entry._id] = createdRow.row._id! Record<string, { rowId: string; tableId: string }>
>((acc, l) => {
if (entry.values.find(f => f.key === l)) {
acc[l] = {
tableId: (table.structure.schema.find(f => f.name === l) as any)
.tableId,
rowId: entry.id,
}
}
return acc
}, {})
// const overridenLinks = Object.keys(linksOverride).reduce< if (Object.keys(overridenLinks)) {
// Record<string, { rowId: string; tableId: string }> toUpdateLinks.push({
// >((acc, l) => { tableId: createdRow.table._id!,
// if (entry[l]) { rowId: createdRow.row._id!,
// acc[l] = { data: overridenLinks,
// tableId: (table.schema[l] as RelationshipFieldMetadata).tableId, })
// rowId: entry[l], }
// } }
// } }
// return acc
// }, {})
// if (Object.keys(overridenLinks)) { // for (const data of toUpdateLinks.filter(d => Object.keys(d.data).length)) {
// toUpdateLinks.push({ // const persistedRow = await sdk.rows.find(data.tableId, data.rowId)
// tableId: createdRow.table._id!,
// rowId: createdRow.row._id!,
// data: overridenLinks,
// })
// }
// }
// }
// for (const data of toUpdateLinks.filter(d => Object.keys(d.data).length)) { // const updatedLinks = Object.keys(data.data).reduce<
// const persistedRow = await sdk.rows.find(data.tableId, data.rowId) // Record<string, string>
// >((acc, d) => {
// acc[d] = createdData[data.data[d].tableId][data.data[d].rowId]
// return acc
// }, {})
// const updatedLinks = Object.keys(data.data).reduce< // await sdk.rows.save(
// Record<string, string> // data.tableId,
// >((acc, d) => { // {
// acc[d] = createdData[data.data[d].tableId][data.data[d].rowId] // ...persistedRow,
// return acc // ...updatedLinks,
// }, {}) // },
// ctx.user._id
// await sdk.rows.save( // )
// data.tableId, // }
// { }
// ...persistedRow,
// ...updatedLinks,
// },
// ctx.user._id
// )
// }
// }
ctx.body = { ctx.body = {
createdTables, createdTables,