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.
151 lines
4.4 KiB
151 lines
4.4 KiB
2 years ago
|
import { TemplateGenerator, UITypes } from 'nocodb-sdk'
|
||
|
import {
|
||
|
extractMultiOrSingleSelectProps,
|
||
|
getCheckboxValue,
|
||
|
isCheckboxType, isDecimalType, isEmailType,
|
||
|
isMultiLineTextType, isUrlType
|
||
|
} from '~/components/import/templateParsers/parserHelpers'
|
||
2 years ago
|
|
||
|
const jsonTypeToUidt = {
|
||
|
number: UITypes.Number,
|
||
|
string: UITypes.SingleLineText,
|
||
|
date: UITypes.DateTime,
|
||
|
boolean: UITypes.Checkbox,
|
||
2 years ago
|
object: UITypes.JSON
|
||
2 years ago
|
}
|
||
|
|
||
2 years ago
|
const extractNestedData = (obj, path) => path.reduce((val, key) => val && val[key], obj)
|
||
|
|
||
2 years ago
|
export default class JSONTemplateAdapter extends TemplateGenerator {
|
||
|
constructor(name = 'test', data, parserConfig = {}) {
|
||
|
super()
|
||
|
this.config = {
|
||
|
maxRowsToParse: 500,
|
||
|
...parserConfig
|
||
|
}
|
||
|
this.name = name
|
||
2 years ago
|
this._jsonData = typeof data === 'string' ? JSON.parse(data) : data
|
||
2 years ago
|
this.project = {
|
||
|
title: this.name,
|
||
|
tables: []
|
||
|
}
|
||
|
this.data = {}
|
||
|
}
|
||
|
|
||
|
async init() {
|
||
|
}
|
||
|
|
||
|
parseData() {
|
||
|
this.columns = this.csv.meta.fields
|
||
|
this.data = this.csv.data
|
||
|
}
|
||
|
|
||
|
getColumns() {
|
||
|
return this.columns
|
||
|
}
|
||
|
|
||
|
getData() {
|
||
|
return this.data
|
||
|
}
|
||
|
|
||
2 years ago
|
get jsonData() {
|
||
|
return Array.isArray(this._jsonData) ? this._jsonData : [this._jsonData]
|
||
|
}
|
||
2 years ago
|
|
||
2 years ago
|
parse() {
|
||
|
const jsonData = this.jsonData
|
||
2 years ago
|
const tn = 'table'
|
||
|
const table = { table_name: tn, ref_table_name: tn, columns: [] }
|
||
|
|
||
|
this.data[tn] = []
|
||
|
|
||
2 years ago
|
for (const col of Object.keys(jsonData[0])) {
|
||
|
const columns = this._parseColumn([col], jsonData)
|
||
|
table.columns.push(...columns)
|
||
|
}
|
||
|
|
||
|
if (this.config.importData) { this._parseTableData(table) }
|
||
|
|
||
|
this.project.tables.push(table)
|
||
|
}
|
||
2 years ago
|
|
||
2 years ago
|
getTemplate() {
|
||
|
return this.project
|
||
|
}
|
||
2 years ago
|
|
||
2 years ago
|
_parseColumn(path = [], jsonData = this.jsonData, firstRowVal = path.reduce((val, k) => val && val[k], this.jsonData[0])) {
|
||
|
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()
|
||
2 years ago
|
|
||
|
const column = {
|
||
|
column_name: cn,
|
||
2 years ago
|
ref_column_name: cn,
|
||
|
path
|
||
2 years ago
|
}
|
||
|
|
||
2 years ago
|
column.uidt = jsonTypeToUidt[typeof firstRowVal] || UITypes.SingleLineText
|
||
|
|
||
|
const colData = jsonData.map(r => extractNestedData(r, path))
|
||
|
Object.assign(column, this._getColumnUIDTAndMetas(colData, column.uidt))
|
||
|
columns.push(column)
|
||
|
}
|
||
2 years ago
|
|
||
2 years ago
|
return columns
|
||
|
}
|
||
2 years ago
|
|
||
2 years ago
|
_getColumnUIDTAndMetas(colData, defaultType) {
|
||
|
const colProps = { uidt: defaultType }
|
||
|
// todo: optimize
|
||
|
if (colProps.uidt === UITypes.SingleLineText) {
|
||
|
// check for long text
|
||
|
if (isMultiLineTextType(colData)) {
|
||
|
colProps.uidt = UITypes.LongText
|
||
|
} if (isEmailType(colData)) {
|
||
|
colProps.uidt = UITypes.Email
|
||
|
} if (isUrlType(colData)) {
|
||
|
colProps.uidt = UITypes.URL
|
||
|
} else {
|
||
|
const checkboxType = isCheckboxType(colData)
|
||
|
if (checkboxType.length === 1) {
|
||
|
colProps.uidt = UITypes.Checkbox
|
||
2 years ago
|
} else {
|
||
2 years ago
|
Object.assign(colProps, extractMultiOrSingleSelectProps(colData))
|
||
2 years ago
|
}
|
||
|
}
|
||
2 years ago
|
} else if (colProps.uidt === UITypes.Number) {
|
||
|
if (isDecimalType(colData)) {
|
||
|
colProps.uidt = UITypes.Decimal
|
||
|
}
|
||
2 years ago
|
}
|
||
2 years ago
|
return colProps
|
||
|
}
|
||
2 years ago
|
|
||
2 years ago
|
_parseTableData(tableMeta) {
|
||
|
for (const row of this.jsonData) {
|
||
2 years ago
|
const rowData = {}
|
||
2 years ago
|
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)
|
||
2 years ago
|
} else {
|
||
|
// toto: do parsing if necessary based on type
|
||
2 years ago
|
rowData[tableMeta.columns[i].column_name] = value
|
||
2 years ago
|
}
|
||
|
}
|
||
2 years ago
|
this.data[tableMeta.ref_table_name].push(rowData)
|
||
2 years ago
|
// rowIndex++
|
||
|
}
|
||
|
}
|
||
|
}
|