import { RelationTypes, UITypes, isLinksOrLTAR, isSystemColumn, isVirtualCol } from 'nocodb-sdk' import type { ColumnType, LinkToAnotherRecordType, TableType } from 'nocodb-sdk' import { isColumnRequiredAndNull } from './columnUtils' import type { Row } from '~/lib' export const extractPkFromRow = (row: Record, columns: ColumnType[]) => { if (!row || !columns) return null return columns .filter((c) => c.pk) .map((c) => row?.[c.title as string]) .join('___') } export const rowPkData = (row: Record, columns: ColumnType[]) => { const pkData: Record = {} const pks = columns?.filter((c) => c.pk) if (row && pks && pks.length) { for (const pk of pks) { if (pk.title) pkData[pk.title] = row[pk.title] } } return pkData } export const extractPk = (columns: ColumnType[]) => { if (!columns && !Array.isArray(columns)) return null return columns .filter((c) => c.pk) .map((c) => c.title) .join('___') } export const findIndexByPk = (pk: Record, data: Row[]) => { for (const [i, row] of Object.entries(data)) { if (Object.keys(pk).every((k) => pk[k] === row.row[k])) { return parseInt(i) } } return -1 } // a function to populate insert object and verify if all required fields are present export async function populateInsertObject({ getMeta, row, meta, ltarState, throwError, undo = false, }: { meta: TableType ltarState: Record getMeta: (tableIdOrTitle: string, force?: boolean) => Promise row: Record throwError?: boolean undo?: boolean }) { const missingRequiredColumns = new Set() const insertObj = await meta.columns?.reduce(async (_o: Promise, col) => { const o = await _o // if column is BT relation then check if foreign key is not_null(required) if ( ltarState && col.uidt === UITypes.LinkToAnotherRecord && (col.colOptions).type === RelationTypes.BELONGS_TO ) { if (ltarState[col.title!]) { const colOpt = col.colOptions const childCol = meta.columns!.find((c) => colOpt.fk_child_column_id === c.id) const relatedTableMeta = (await getMeta(colOpt.fk_related_model_id!)) as TableType if (relatedTableMeta && childCol) { o[childCol.title!] = ltarState[col.title!][relatedTableMeta!.columns!.find((c) => c.id === colOpt.fk_parent_column_id)!.title!] missingRequiredColumns.delete(childCol.title) } } } // check all the required columns are not null if (isColumnRequiredAndNull(col, row)) { missingRequiredColumns.add(col.title) } if ((!col.ai || undo) && row?.[col.title as string] !== null) { o[col.title as string] = row?.[col.title as string] } return o }, Promise.resolve({})) if (throwError && missingRequiredColumns.size) { throw new Error(`Missing required columns: ${[...missingRequiredColumns].join(', ')}`) } return { missingRequiredColumns, insertObj } } // a function to get default values of row export const rowDefaultData = (columns: ColumnType[] = []) => { const defaultData: Record = columns.reduce>((acc: Record, col: ColumnType) => { // avoid setting default value for system col, virtual col, rollup, formula, barcode, qrcode, links, ltar if ( !isSystemColumn(col) && !isVirtualCol(col) && !isLinksOrLTAR({ uidt: col.uidt! }) && ![UITypes.Rollup, UITypes.Lookup, UITypes.Formula, UITypes.Barcode, UITypes.QrCode].includes(col.uidt) && col?.cdf ) { const defaultValue = col.cdf acc[col.title!] = typeof defaultValue === 'string' ? defaultValue.replace(/^'/, '').replace(/'$/, '') : defaultValue } return acc }, {} as Record) return defaultData }