|
|
|
@ -3,7 +3,7 @@ const { UITypes } = require('nocodb-sdk');
|
|
|
|
|
const jsonfile = require("jsonfile"); |
|
|
|
|
|
|
|
|
|
let api = {} |
|
|
|
|
let ncIn = jsonfile.readFileSync('x.json') |
|
|
|
|
let ncIn = jsonfile.readFileSync('sample.json') |
|
|
|
|
let ncProject = {} |
|
|
|
|
let link = [] |
|
|
|
|
let lookup = [] |
|
|
|
@ -15,18 +15,19 @@ const ncConfig = {
|
|
|
|
|
projectName: "x2", |
|
|
|
|
baseURL: "http://localhost:8080", |
|
|
|
|
headers: { |
|
|
|
|
'xc-auth': "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJlbWFpbCI6InVzZXJAbm9jb2RiLmNvbSIsImZpcnN0bmFtZSI6bnVsbCwibGFzdG5hbWUiOm51bGwsImlkIjoidXNfaGJ1aDFmMTNmemc4dTEiLCJyb2xlcyI6InVzZXIsc3VwZXIiLCJpYXQiOjE2NTMwNTU5MzR9.nADVbCbSE0WEbPrpKuq_dlMHrrxieQurYPiOIU2Gf4k" |
|
|
|
|
'xc-auth': "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJlbWFpbCI6InVzZXJAbm9jb2RiLmNvbSIsImZpcnN0bmFtZSI6bnVsbCwibGFzdG5hbWUiOm51bGwsImlkIjoidXNfazk0cTg3NGF6bTh5MngiLCJyb2xlcyI6InVzZXIsc3VwZXIiLCJpYXQiOjE2NTMzMTQ1MTZ9.h0YjZ9lLlIYYWQkgKWCoT5OuYNMfStuAjT_EwSasM6Q" |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
async function createBaseTables() { |
|
|
|
|
for(let i=0; i<ncIn.length; i++) { |
|
|
|
|
let tblSchema = ncIn[i] |
|
|
|
|
let reducedColumnSet = tblSchema.columns.filter(a => a.uidt !== UITypes.LinkToAnotherRecord && a.uidt !== UITypes.Lookup && a.uidt !== UITypes.Formula); |
|
|
|
|
let reducedColumnSet = tblSchema.columns.filter(a => a.uidt !== UITypes.LinkToAnotherRecord && a.uidt !== UITypes.Lookup && a.uidt !== UITypes.Rollup && a.uidt !== UITypes.Formula); |
|
|
|
|
link.push(...tblSchema.columns.filter(a => a.uidt === UITypes.LinkToAnotherRecord)) |
|
|
|
|
lookup.push(...tblSchema.columns.filter(a => a.uidt === UITypes.Lookup)) |
|
|
|
|
rollup.push(...tblSchema.columns.filter(a => a.uidt === UITypes.Rollup)) |
|
|
|
|
formula.push(...tblSchema.columns.filter(a => a.uidt === UITypes.Formula)) |
|
|
|
|
formula.map(a => a['table_id'] = tblSchema.id) |
|
|
|
|
|
|
|
|
|
let tbl = await api.dbTable.create(ncProject.id, { |
|
|
|
|
title: tblSchema.title, |
|
|
|
@ -49,14 +50,26 @@ function isLinkCreated(pId, cId) {
|
|
|
|
|
return true; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// retrieve nc-view column ID from corresponding nc-column ID
|
|
|
|
|
async function nc_getViewColumnId(viewId, viewType, ncColumnId) { |
|
|
|
|
// retrieve view Info
|
|
|
|
|
let viewDetails; |
|
|
|
|
|
|
|
|
|
if (viewType === 'form') |
|
|
|
|
viewDetails = (await api.dbView.formRead(viewId)).columns; |
|
|
|
|
else if (viewType === 'gallery') |
|
|
|
|
viewDetails = (await api.dbView.galleryRead(viewId)).columns; |
|
|
|
|
else viewDetails = await api.dbView.gridColumnsList(viewId); |
|
|
|
|
|
|
|
|
|
return viewDetails.find(x => x.fk_column_id === ncColumnId)?.id; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
async function createFormula() { |
|
|
|
|
for (let i = 0; i < formula.length; i++) { |
|
|
|
|
let tbl = await api.dbTableColumn.create(srcTbl.id, { |
|
|
|
|
uidt: UITypes.LinkToAnotherRecord, |
|
|
|
|
title: link[i].title, |
|
|
|
|
parentId: srcTbl.id, |
|
|
|
|
childId: dstTbl.id, |
|
|
|
|
type: link[i].colOptions.type |
|
|
|
|
let tbl = await api.dbTableColumn.create(ncTables[formula[i].table_id].id, { |
|
|
|
|
uidt: UITypes.Formula, |
|
|
|
|
title: formula[i].title, |
|
|
|
|
formula_raw: formula[i].formula_raw |
|
|
|
|
}); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
@ -143,6 +156,7 @@ async function createRollup() {
|
|
|
|
|
let tbl = await api.dbTableColumn.create(srcTbl.id, { |
|
|
|
|
uidt: UITypes.Rollup, |
|
|
|
|
title: rollup[i].title, |
|
|
|
|
column_name: rollup[i].title, |
|
|
|
|
fk_relation_column_id: v2_fk_relation_column_id, |
|
|
|
|
fk_rollup_column_id: v2_rollup_column_id, |
|
|
|
|
rollup_function: rollup[i].colOptions.rollup_function |
|
|
|
@ -154,6 +168,86 @@ async function createRollup() {
|
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
async function configureGrid() { |
|
|
|
|
for(let i=0; i<ncIn.length; i++) { |
|
|
|
|
let tblSchema = ncIn[i]; |
|
|
|
|
let tblId = ncTables[tblSchema.id].id; |
|
|
|
|
let gridList = tblSchema.views.filter(a => a.type === 3); |
|
|
|
|
let srcTbl = await api.dbTable.read(tblId); |
|
|
|
|
|
|
|
|
|
const view = await api.dbView.list(tblId); |
|
|
|
|
|
|
|
|
|
// create / rename view
|
|
|
|
|
for(let gridCnt=0; gridCnt< gridList.length; gridCnt++) { |
|
|
|
|
let viewCreated = {} |
|
|
|
|
// rename first view; default view already created
|
|
|
|
|
if(gridCnt === 0) { |
|
|
|
|
viewCreated = await api.dbView.update(view.list[0].id, {title: gridList[gridCnt].title}); |
|
|
|
|
} |
|
|
|
|
// create new views
|
|
|
|
|
else { |
|
|
|
|
viewCreated = await api.dbView.gridCreate(tblId, {title: gridList[gridCnt].title}); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// column visibility
|
|
|
|
|
for(let colCnt = 0; colCnt < gridList[gridCnt].columns.length; colCnt++) { |
|
|
|
|
let ncColumnId = srcTbl.columns.find(a => a.title === gridList[gridCnt].columns[colCnt].title)?.id |
|
|
|
|
let ncViewColumnId = await nc_getViewColumnId( viewCreated.id, "grid", ncColumnId ) |
|
|
|
|
// column order & visibility
|
|
|
|
|
await api.dbViewColumn.update(viewCreated.id, ncViewColumnId, { |
|
|
|
|
show: gridList[gridCnt].columns[colCnt].show, |
|
|
|
|
order: gridList[gridCnt].columns[colCnt].order, |
|
|
|
|
}); |
|
|
|
|
await api.dbView.gridColumnUpdate(ncViewColumnId, {width: gridList[gridCnt].columns[colCnt].width}) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
async function configureGallery() { |
|
|
|
|
for(let i=0; i<ncIn.length; i++) { |
|
|
|
|
let tblSchema = ncIn[i]; |
|
|
|
|
let tblId = ncTables[tblSchema.id].id; |
|
|
|
|
let galleryList = tblSchema.views.filter(a => a.type === 2); |
|
|
|
|
for(let cnt=0; cnt< galleryList.length; cnt++) { |
|
|
|
|
const viewCreated = await api.dbView.galleryCreate(tblId, {title: galleryList[cnt].title}); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
async function configureForm() { |
|
|
|
|
for(let i=0; i<ncIn.length; i++) { |
|
|
|
|
let tblSchema = ncIn[i]; |
|
|
|
|
let tblId = ncTables[tblSchema.id].id; |
|
|
|
|
let formList = tblSchema.views.filter(a => a.type === 1); |
|
|
|
|
let srcTbl = await api.dbTable.read(tblId); |
|
|
|
|
|
|
|
|
|
for(let formCnt=0; formCnt< formList.length; formCnt++) { |
|
|
|
|
const formData = { |
|
|
|
|
title: formList[formCnt].title, |
|
|
|
|
...formList[formCnt].property |
|
|
|
|
}; |
|
|
|
|
const viewCreated = await api.dbView.formCreate(tblId, formData); |
|
|
|
|
|
|
|
|
|
// column visibility
|
|
|
|
|
for(let colCnt = 0; colCnt < formList[formCnt].columns.length; colCnt++) { |
|
|
|
|
let ncColumnId = srcTbl.columns.find(a => a.title === formList[formCnt].columns[colCnt].title)?.id |
|
|
|
|
let ncViewColumnId = await nc_getViewColumnId( viewCreated.id, "form", ncColumnId ) |
|
|
|
|
// column order & visibility
|
|
|
|
|
await api.dbView.formColumnUpdate(ncViewColumnId, { |
|
|
|
|
show: formList[formCnt].columns[colCnt].show, |
|
|
|
|
order: formList[formCnt].columns[colCnt].order, |
|
|
|
|
label: formList[formCnt].columns[colCnt].label, |
|
|
|
|
description: formList[formCnt].columns[colCnt].description, |
|
|
|
|
required: formList[formCnt].columns[colCnt].required, |
|
|
|
|
}); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
async function importSchema() { |
|
|
|
|
api = new Api(ncConfig); |
|
|
|
|
|
|
|
|
@ -162,10 +256,20 @@ async function importSchema() {
|
|
|
|
|
if (p) await api.project.delete(p.id); |
|
|
|
|
ncProject = await api.project.create({ title: ncConfig.projectName }) |
|
|
|
|
await createBaseTables() |
|
|
|
|
await createFormula() |
|
|
|
|
await createLinks() |
|
|
|
|
await createLookup() |
|
|
|
|
await createRollup() |
|
|
|
|
await createFormula() |
|
|
|
|
|
|
|
|
|
// configure grid
|
|
|
|
|
await configureGrid() |
|
|
|
|
|
|
|
|
|
// configure gallery
|
|
|
|
|
await configureGallery() |
|
|
|
|
|
|
|
|
|
// configure form
|
|
|
|
|
await configureForm() |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
(async() => { |
|
|
|
|
await importSchema() |
|
|
|
|