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
3 years ago
|
import { TemplateGenerator, UITypes } from 'nocodb-sdk'
|
||
|
import {
|
||
|
extractMultiOrSingleSelectProps,
|
||
|
getCheckboxValue,
|
||
|
isCheckboxType, isDecimalType, isEmailType,
|
||
|
isMultiLineTextType, isUrlType
|
||
|
} from '~/components/import/templateParsers/parserHelpers'
|
||
3 years ago
|
|
||
|
const jsonTypeToUidt = {
|
||
|
number: UITypes.Number,
|
||
|
string: UITypes.SingleLineText,
|
||
|
date: UITypes.DateTime,
|
||
|
boolean: UITypes.Checkbox,
|
||
3 years ago
|
object: UITypes.JSON
|
||
3 years ago
|
}
|
||
|
|
||
3 years ago
|
const extractNestedData = (obj, path) => path.reduce((val, key) => val && val[key], obj)
|
||
|
|
||
3 years ago
|
export default class JSONTemplateAdapter extends TemplateGenerator {
|
||
|
constructor(name = 'test', data, parserConfig = {}) {
|
||
|
super()
|
||
|
this.config = {
|
||
|
maxRowsToParse: 500,
|
||
|
...parserConfig
|
||
|
}
|
||
|
this.name = name
|
||
3 years ago
|
this._jsonData = typeof data === 'string' ? JSON.parse(data) : data
|
||
3 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
|
||
|
}
|
||
|
|
||
3 years ago
|
get jsonData() {
|
||
|
return Array.isArray(this._jsonData) ? this._jsonData : [this._jsonData]
|
||
|
}
|
||
3 years ago
|
|
||
3 years ago
|
parse() {
|
||
|
const jsonData = this.jsonData
|
||
3 years ago
|
const tn = 'table'
|
||
|
const table = { table_name: tn, ref_table_name: tn, columns: [] }
|
||
|
|
||
|
this.data[tn] = []
|
||
|
|
||
3 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)
|
||
|
}
|
||
3 years ago
|
|
||
3 years ago
|
getTemplate() {
|
||
|
return this.project
|
||
|
}
|
||
3 years ago
|
|
||
3 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()
|
||
3 years ago
|
|
||
|
const column = {
|
||
|
column_name: cn,
|
||
3 years ago
|
ref_column_name: cn,
|
||
|
path
|
||
3 years ago
|
}
|
||
|
|
||
3 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)
|
||
|
}
|
||
3 years ago
|
|
||
3 years ago
|
return columns
|
||
|
}
|
||
3 years ago
|
|
||
3 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
|
||
3 years ago
|
} else {
|
||
3 years ago
|
Object.assign(colProps, extractMultiOrSingleSelectProps(colData))
|
||
3 years ago
|
}
|
||
|
}
|
||
3 years ago
|
} else if (colProps.uidt === UITypes.Number) {
|
||
|
if (isDecimalType(colData)) {
|
||
|
colProps.uidt = UITypes.Decimal
|
||
|
}
|
||
3 years ago
|
}
|
||
3 years ago
|
return colProps
|
||
|
}
|
||
3 years ago
|
|
||
3 years ago
|
_parseTableData(tableMeta) {
|
||
|
for (const row of this.jsonData) {
|
||
3 years ago
|
const rowData = {}
|
||
3 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)
|
||
3 years ago
|
} else {
|
||
|
// toto: do parsing if necessary based on type
|
||
3 years ago
|
rowData[tableMeta.columns[i].column_name] = value
|
||
3 years ago
|
}
|
||
|
}
|
||
3 years ago
|
this.data[tableMeta.ref_table_name].push(rowData)
|
||
3 years ago
|
// rowIndex++
|
||
|
}
|
||
|
}
|
||
|
}
|