diff --git a/packages/nocodb/tests/export-import/importSchema.js b/packages/nocodb/tests/export-import/importSchema.js index d120e6d9b6..d0bcd9dae5 100644 --- a/packages/nocodb/tests/export-import/importSchema.js +++ b/packages/nocodb/tests/export-import/importSchema.js @@ -1,6 +1,9 @@ const Api = require('nocodb-sdk').Api; const { UITypes } = require('nocodb-sdk'); const jsonfile = require("jsonfile"); +const dayjs = require("dayjs"); +const axios = require("axios"); +const FormData = require("form-data"); let api = {} let ncIn = jsonfile.readFileSync('sample.json') @@ -12,6 +15,7 @@ let formula = [] let ncTables = {} const ncConfig = { + srcProject: "sample", projectName: "x2", baseURL: "http://localhost:8080", headers: { @@ -20,6 +24,7 @@ const ncConfig = { } async function createBaseTables() { + console.log(`createBaseTables`) for(let i=0; i a.uidt !== UITypes.LinkToAnotherRecord && a.uidt !== UITypes.Lookup && a.uidt !== UITypes.Rollup && a.uidt !== UITypes.Formula); @@ -74,7 +79,9 @@ async function createFormula() { } } +let rootLinks = [] async function createLinks() { + console.log(`createLinks`) for (let i = 0; i < link.length; i++) { if (((link[i].colOptions.type === 'mm') && (false === isLinkCreated(link[i].colOptions.fk_parent_column_id, link[i].colOptions.fk_child_column_id))) @@ -92,14 +99,18 @@ async function createLinks() { ncTables[tbl.title] = tbl; ncTables[tbl.id] = tbl; ncTables[link[i].colOptions.fk_model_id] = tbl; + // for data-link procedure later + rootLinks.push({linkColumn: link[i], linkSrcTbl: srcTbl}) let v2ColSchema = tbl.columns.find(x => x.title === link[i].title) // read related table again after link is created dstTbl = await api.dbTable.read(dstTbl.id); - let v2SymmetricColumn = (link[i].colOptions.type === 'mm') ? dstTbl.columns.find(x => x.uidt === UITypes.LinkToAnotherRecord && x?.colOptions.fk_parent_column_id === v2ColSchema.colOptions.fk_child_column_id && x?.colOptions.fk_child_column_id === v2ColSchema.colOptions.fk_parent_column_id) : + let v2SymmetricColumn = (link[i].colOptions.type === 'mm') ? + dstTbl.columns.find(x => x.uidt === UITypes.LinkToAnotherRecord && x?.colOptions.fk_parent_column_id === v2ColSchema.colOptions.fk_child_column_id && x?.colOptions.fk_child_column_id === v2ColSchema.colOptions.fk_parent_column_id) : dstTbl.columns.find(x => x.uidt === UITypes.LinkToAnotherRecord && x?.colOptions.fk_parent_column_id === v2ColSchema.colOptions.fk_parent_column_id && x?.colOptions.fk_child_column_id === v2ColSchema.colOptions.fk_child_column_id) - let v1SymmetricColumn = (link[i].colOptions.type === 'mm') ? link.find(x => x.colOptions.fk_parent_column_id === link[i].colOptions.fk_child_column_id && x.colOptions.fk_child_column_id === link[i].colOptions.fk_parent_column_id) : + let v1SymmetricColumn = (link[i].colOptions.type === 'mm') ? + link.find(x => x.colOptions.fk_parent_column_id === link[i].colOptions.fk_child_column_id && x.colOptions.fk_child_column_id === link[i].colOptions.fk_parent_column_id) : link.find(x => x.colOptions.fk_parent_column_id === link[i].colOptions.fk_parent_column_id && x.colOptions.fk_child_column_id === link[i].colOptions.fk_child_column_id); tbl = await api.dbTableColumn.update(v2SymmetricColumn.id, { @@ -127,6 +138,8 @@ function get_v2Id(v1ColId) { } async function createLookup() { + console.log(`createLookup`) + for(let i=0; i a.title === gridList[gridCnt].columns[colCnt].title)?.id - let ncViewColumnId = await nc_getViewColumnId( viewCreated.id, "grid", ncColumnId ) + // let ncViewColumnId = await nc_getViewColumnId( viewCreated.id, "grid", ncColumnId ) + let ncViewColumnId = viewDetails.find(x => x.fk_column_id === ncColumnId)?.id; // column order & visibility await api.dbViewColumn.update(viewCreated.id, ncViewColumnId, { show: gridList[gridCnt].columns[colCnt].show, @@ -205,6 +227,8 @@ async function configureGrid() { } async function configureGallery() { + console.log(`configureGallery`) + for(let i=0; i a.pk).title + + // fixme: iterate page information + let recList = await api.dbTableRow.list("nc", ncConfig.srcProject, tblSchema.title, {}, { + query: { limit: 500 } + }) + for (let recCnt = 0; recCnt < recList.list.length; recCnt++) { + let record = await api.dbTableRow.read("nc", ncConfig.srcProject, tblSchema.title, recList.list[recCnt][pk]) + + // post-processing on the record + for (const [key, value] of Object.entries(record)) { + let table = ncTables[tblId] + // retrieve datatype + const dt = table.columns.find(x => x.title === key)?.uidt; + if (dt === UITypes.LinkToAnotherRecord) delete record[key]; + if (dt === UITypes.Lookup) delete record[key]; + if (dt === UITypes.Rollup) delete record[key]; + } + await api.dbTableRow.create("nc", ncConfig.projectName, tblSchema.title, record) + } + } +} + +async function restoreLinks() { + console.log(`restoreLinks`) + + for(let i=0; i a.pk).title + + // fixme: iterate page information + let recList = await api.dbTableRow.list("nc", ncConfig.srcProject, rootLinks[i].linkSrcTbl.title, {}, { + query: { limit: 500 } + }) + + for (let recCnt = 0; recCnt < recList.list.length; recCnt++) { + let record = await api.dbTableRow.read("nc", ncConfig.srcProject, rootLinks[i].linkSrcTbl.title, recList.list[recCnt][pk]) + let linkField = record[rootLinks[i].linkColumn.title]; + if(linkField.length) { + await api.dbTableRow.nestedAdd("nc", ncConfig.projectName, rootLinks[i].linkSrcTbl.title, + record[pk], + rootLinks[i].linkColumn.colOptions.type, + encodeURIComponent(rootLinks[i].linkColumn.title), + linkField[0][pk] + ) + } } } } + async function importSchema() { api = new Api(ncConfig); @@ -255,21 +336,21 @@ async function importSchema() { const p = x.list.find(a => a.title === ncConfig.projectName); if (p) await api.project.delete(p.id); ncProject = await api.project.create({ title: ncConfig.projectName }) + await createBaseTables() await createLinks() await createLookup() await createRollup() await createFormula() - // configure grid + // configure views await configureGrid() - - // configure gallery await configureGallery() - - // configure form await configureForm() + // restore data + await restoreBaseData() + await restoreLinks() } (async() => { await importSchema()