mirror of https://github.com/nocodb/nocodb
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
131 lines
4.1 KiB
131 lines
4.1 KiB
2 years ago
|
import { UITypes } from 'nocodb-sdk'
|
||
2 years ago
|
import { getCheckboxValue, getColumnUIDTAndMetas } from './parserHelpers'
|
||
2 years ago
|
import TemplateGenerator from './TemplateGenerator'
|
||
|
|
||
|
const jsonTypeToUidt: Record<string, string> = {
|
||
|
number: UITypes.Number,
|
||
|
string: UITypes.SingleLineText,
|
||
|
date: UITypes.DateTime,
|
||
|
boolean: UITypes.Checkbox,
|
||
|
object: UITypes.JSON,
|
||
|
}
|
||
|
|
||
|
const extractNestedData: any = (obj: any, path: any) => path.reduce((val: any, key: any) => val && val[key], obj)
|
||
|
|
||
|
export default class JSONTemplateAdapter extends TemplateGenerator {
|
||
|
config: Record<string, any>
|
||
|
data: Record<string, any>
|
||
2 years ago
|
_jsonData: string | Record<string, any>
|
||
|
jsonData: Record<string, any>
|
||
2 years ago
|
project: {
|
||
|
tables: Record<string, any>[]
|
||
|
}
|
||
2 years ago
|
|
||
2 years ago
|
columns: object
|
||
1 year ago
|
constructor(data: object, parserConfig = {}, progressCallback?: (msg: string) => void) {
|
||
|
super(progressCallback)
|
||
2 years ago
|
this.config = parserConfig
|
||
2 years ago
|
this._jsonData = data
|
||
2 years ago
|
this.project = {
|
||
|
tables: [],
|
||
|
}
|
||
2 years ago
|
this.jsonData = []
|
||
|
this.data = []
|
||
2 years ago
|
this.columns = {}
|
||
|
}
|
||
|
|
||
2 years ago
|
async init() {
|
||
1 year ago
|
this.progress('Initializing json parser')
|
||
2 years ago
|
const parsedJsonData =
|
||
|
typeof this._jsonData === 'string'
|
||
|
? // for json editor
|
||
|
JSON.parse(this._jsonData)
|
||
|
: // for file upload
|
||
|
JSON.parse(new TextDecoder().decode(this._jsonData as BufferSource))
|
||
|
this.jsonData = Array.isArray(parsedJsonData) ? parsedJsonData : [parsedJsonData]
|
||
2 years ago
|
}
|
||
|
|
||
|
getColumns(): any {
|
||
|
return this.columns
|
||
|
}
|
||
|
|
||
|
getData(): any {
|
||
|
return this.data
|
||
|
}
|
||
|
|
||
|
parse(): any {
|
||
1 year ago
|
this.progress('Parsing json data')
|
||
2 years ago
|
const jsonData = this.jsonData
|
||
|
const tn = 'table'
|
||
|
const table: any = { table_name: tn, ref_table_name: tn, columns: [] }
|
||
|
|
||
|
this.data[tn] = []
|
||
|
|
||
|
for (const col of Object.keys(jsonData[0])) {
|
||
|
const columns = this._parseColumn([col], jsonData)
|
||
|
table.columns.push(...columns)
|
||
|
}
|
||
|
|
||
2 years ago
|
if (this.config.shouldImportData) {
|
||
2 years ago
|
this._parseTableData(table)
|
||
|
}
|
||
|
|
||
|
this.project.tables.push(table)
|
||
|
}
|
||
|
|
||
|
getTemplate() {
|
||
|
return this.project
|
||
|
}
|
||
|
|
||
|
_parseColumn(
|
||
|
path: any = [],
|
||
|
jsonData = this.jsonData,
|
||
|
firstRowVal = path.reduce((val: any, k: any) => val && val[k], this.jsonData[0]),
|
||
|
): any {
|
||
|
const columns = []
|
||
|
// parse nested
|
||
|
if (firstRowVal && typeof firstRowVal === 'object' && !Array.isArray(firstRowVal) && this.config.normalizeNested) {
|
||
|
for (const key of Object.keys(firstRowVal)) {
|
||
|
const normalizedNestedColumns = this._parseColumn([...path, key], this.jsonData, firstRowVal[key])
|
||
|
columns.push(...normalizedNestedColumns)
|
||
|
}
|
||
|
} else {
|
||
|
const cn = path.join('_').replace(/\W/g, '_').trim()
|
||
|
const column: Record<string, any> = {
|
||
|
column_name: cn,
|
||
|
ref_column_name: cn,
|
||
2 years ago
|
uidt: UITypes.SingleLineText,
|
||
2 years ago
|
path,
|
||
|
}
|
||
2 years ago
|
if (this.config.autoSelectFieldTypes) {
|
||
|
column.uidt = jsonTypeToUidt[typeof firstRowVal] || UITypes.SingleLineText
|
||
|
const colData = jsonData.map((r: any) => extractNestedData(r, path))
|
||
|
Object.assign(column, getColumnUIDTAndMetas(colData, column.uidt))
|
||
|
}
|
||
2 years ago
|
columns.push(column)
|
||
|
}
|
||
|
|
||
|
return columns
|
||
|
}
|
||
|
|
||
|
_parseTableData(tableMeta: any) {
|
||
2 years ago
|
for (const row of this.jsonData as any) {
|
||
2 years ago
|
const rowData: any = {}
|
||
|
for (let i = 0; i < tableMeta.columns.length; i++) {
|
||
|
const value = extractNestedData(row, tableMeta.columns[i].path || [])
|
||
|
if (tableMeta.columns[i].uidt === UITypes.Checkbox) {
|
||
|
rowData[tableMeta.columns[i].ref_column_name] = getCheckboxValue(value)
|
||
|
} else if (tableMeta.columns[i].uidt === UITypes.SingleSelect || tableMeta.columns[i].uidt === UITypes.MultiSelect) {
|
||
|
rowData[tableMeta.columns[i].ref_column_name] = (value || '').toString().trim() || null
|
||
|
} else if (tableMeta.columns[i].uidt === UITypes.JSON) {
|
||
|
rowData[tableMeta.columns[i].ref_column_name] = JSON.stringify(value)
|
||
|
} else {
|
||
|
// toto: do parsing if necessary based on type
|
||
|
rowData[tableMeta.columns[i].column_name] = value
|
||
|
}
|
||
|
}
|
||
|
this.data[tableMeta.ref_table_name].push(rowData)
|
||
|
}
|
||
|
}
|
||
|
}
|