@ -1,15 +1,21 @@
import { TemplateGenerator } from 'nocodb-sdk'
import { TemplateGenerator , UITypes } from 'nocodb-sdk'
import { UITypes } from '~/components/project/spreadsheet/helpers/uiTypes'
import {
import { getCheckboxValue , isCheckboxType } from '~/components/import/templateParsers/parserHelpers'
extractMultiOrSingleSelectProps ,
getCheckboxValue ,
isCheckboxType , isDecimalType , isEmailType ,
isMultiLineTextType , isUrlType
} from '~/components/import/templateParsers/parserHelpers'
const jsonTypeToUidt = {
const jsonTypeToUidt = {
number : UITypes . Number ,
number : UITypes . Number ,
string : UITypes . SingleLineText ,
string : UITypes . SingleLineText ,
date : UITypes . DateTime ,
date : UITypes . DateTime ,
boolean : UITypes . Checkbox ,
boolean : UITypes . Checkbox ,
object : UITypes . LongText
object : UITypes . JSON
}
}
const extractNestedData = ( obj , path ) => path . reduce ( ( val , key ) => val && val [ key ] , obj )
export default class JSONTemplateAdapter extends TemplateGenerator {
export default class JSONTemplateAdapter extends TemplateGenerator {
constructor ( name = 'test' , data , parserConfig = { } ) {
constructor ( name = 'test' , data , parserConfig = { } ) {
super ( )
super ( )
@ -18,7 +24,7 @@ export default class JSONTemplateAdapter extends TemplateGenerator {
... parserConfig
... parserConfig
}
}
this . name = name
this . name = name
this . jsonData = typeof data === 'string' ? JSON . parse ( data ) : data
this . _ jsonData = typeof data === 'string' ? JSON . parse ( data ) : data
this . project = {
this . project = {
title : this . name ,
title : this . name ,
tables : [ ]
tables : [ ]
@ -42,117 +48,103 @@ export default class JSONTemplateAdapter extends TemplateGenerator {
return this . data
return this . data
}
}
parse ( ) {
get jsonData ( ) {
const jsonData = Array . isArray ( this . this . jsonData ) ? this . this . jsonData : [ this . jsonData ]
return Array . isArray ( this . _jsonData ) ? this . _jsonData : [ this . _jsonData ]
}
// for (let i = 0; i < this.wb.SheetNames.length; i++) {
// const columnNamePrefixRef = { id: 0 }
parse ( ) {
const jsonData = this . jsonData
const tn = 'table'
const tn = 'table'
const table = { table _name : tn , ref _table _name : tn , columns : [ ] }
const table = { table _name : tn , ref _table _name : tn , columns : [ ] }
this . data [ tn ] = [ ]
this . data [ tn ] = [ ]
// const ws = this.wb.Sheets[sheet]
for ( const col of Object . keys ( jsonData [ 0 ] ) ) {
// const range = XLSX.utils.decode_range(ws['!ref'])
const columns = this . _parseColumn ( [ col ] , jsonData )
// const rows = XLSX.utils.sheet_to_json(ws, { header: 1, blankrows: false, cellDates: true, defval: null })
table . columns . push ( ... columns )
}
if ( this . config . importData ) { this . _parseTableData ( table ) }
this . project . tables . push ( table )
}
const objKeys = Object . keys ( jsonData [ 0 ] )
getTemplate ( ) {
return this . project
}
for ( let col = 0 ; col < objKeys . length ; col ++ ) {
_parseColumn ( path = [ ] , jsonData = this . jsonData , firstRowVal = path . reduce ( ( val , k ) => val && val [ k ] , this . jsonData [ 0 ] ) ) {
const key = objKeys [ col ]
const columns = [ ]
const cn = objKeys [ col ] . replace ( /\W/g , '_' ) . trim ( )
// 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 = {
const column = {
column _name : cn ,
column _name : cn ,
ref _column _name : cn
ref _column _name : cn ,
path
}
}
table . columns . push ( column )
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 )
}
column . uidt = jsonTypeToUidt [ typeof jsonData [ 0 ] [ key ] ] || UITypes . SingleLineText
return columns
}
// todo: optimize
_getColumnUIDTAndMetas ( colData , defaultType ) {
if ( column . uidt === UITypes . SingleLineText ) {
const colProps = { uidt : defaultType }
// check for long text
// todo: optimize
if ( jsonData . some ( r =>
if ( colProps . uidt === UITypes . SingleLineText ) {
( r [ key ] || '' ) . toString ( ) . match ( /[\r\n]/ ) ||
// check for long text
( r [ key ] || '' ) . toString ( ) . length > 255 )
if ( isMultiLineTextType ( colData ) ) {
) {
colProps . uidt = UITypes . LongText
column . 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
} else {
} else {
const vals = jsonData
Object . assign ( colProps , extractMultiOrSingleSelectProps ( colData ) )
. map ( r => r [ key ] )
. filter ( v => v !== null && v !== undefined && v . toString ( ) . trim ( ) !== '' )
const checkboxType = isCheckboxType ( vals )
if ( checkboxType . length === 1 ) {
column . uidt = UITypes . Checkbox
} else {
// todo: optimize
// check column is multi or single select by comparing unique values
// todo:
// eslint-disable-next-line no-lonely-if
if ( vals . some ( v => v && v . toString ( ) . includes ( ',' ) ) ) {
let flattenedVals = vals . flatMap ( v => v ? v . toString ( ) . trim ( ) . split ( /\s*,\s*/ ) : [ ] )
const uniqueVals = flattenedVals = flattenedVals
. filter ( ( v , i , arr ) => i === arr . findIndex ( v1 => v . toLowerCase ( ) === v1 . toLowerCase ( ) ) )
if ( flattenedVals . length > uniqueVals . length && uniqueVals . length <= Math . ceil ( flattenedVals . length / 2 ) ) {
column . uidt = UITypes . MultiSelect
column . dtxp = ` ' ${ uniqueVals . join ( "','" ) } ' `
}
} else {
const uniqueVals = vals . map ( v => v . toString ( ) . trim ( ) ) . filter ( ( v , i , arr ) => i === arr . findIndex ( v1 => v . toLowerCase ( ) === v1 . toLowerCase ( ) ) )
if ( vals . length > uniqueVals . length && uniqueVals . length <= Math . ceil ( vals . length / 2 ) ) {
column . uidt = UITypes . SingleSelect
column . dtxp = ` ' ${ uniqueVals . join ( "','" ) } ' `
}
}
}
}
} else if ( column . uidt === UITypes . Number ) {
if ( jsonData . slice ( 1 , this . config . maxRowsToParse ) . some ( ( v ) => {
return v && v [ key ] && parseInt ( + v [ key ] ) !== + v [ key ]
} ) ) {
column . uidt = UITypes . Decimal
}
if ( jsonData . every ( ( v , i ) => {
return v [ key ] && v [ key ] . toString ( ) . startsWith ( '$' )
} ) ) {
column . uidt = UITypes . Currency
}
} else if ( column . uidt === UITypes . DateTime ) {
if ( jsonData . every ( ( v , i ) => {
return v [ key ] && v [ key ] . toString ( ) . split ( ' ' ) . length === 1
} ) ) {
column . uidt = UITypes . Date
}
}
}
}
} else if ( colProps . uidt === UITypes . Number ) {
if ( isDecimalType ( colData ) ) {
colProps . uidt = UITypes . Decimal
}
}
}
return colProps
}
// let rowIndex = 0
_parseTableData ( tableMeta ) {
for ( const row of jsonData ) {
for ( const row of this . jsonData ) {
const rowData = { }
const rowData = { }
for ( let i = 0 ; i < table . columns . length ; i ++ ) {
for ( let i = 0 ; i < tableMeta . columns . length ; i ++ ) {
if ( table . columns [ i ] . uidt === UITypes . Checkbox ) {
const value = extractNestedData ( row , tableMeta . columns [ i ] . path || [ ] )
rowData [ table . columns [ i ] . column _name ] = getCheckboxValue ( row [ i ] )
if ( tableMeta . columns [ i ] . uidt === UITypes . Checkbox ) {
} else if ( table . columns [ i ] . uidt === UITypes . Currency ) {
rowData [ tableMeta . columns [ i ] . ref _column _name ] = getCheckboxValue ( value )
rowData [ table . columns [ i ] . column _name ] = ( row [ table . columns [ i ] . ref _column _name ] . replace ( /[^\d.]+/g , '' ) ) || row [ i ]
} else if ( tableMeta . columns [ i ] . uidt === UITypes . SingleSelect || tableMeta . columns [ i ] . uidt === UITypes . MultiSelect ) {
} else if ( table . columns [ i ] . uidt === UITypes . SingleSelect || table . columns [ i ] . uidt === UITypes . MultiSelect ) {
rowData [ tableMeta . columns [ i ] . ref _column _name ] = ( value || '' ) . toString ( ) . trim ( ) || null
rowData [ table . columns [ i ] . column _name ] = ( row [ table . columns [ i ] . ref _column _name ] || '' ) . toString ( ) . trim ( ) || null
} else if ( tableMeta . columns [ i ] . uidt === UITypes . JSON ) {
rowData [ tableMeta . columns [ i ] . ref _column _name ] = JSON . stringify ( value )
} else {
} else {
// toto: do parsing if necessary based on type
// toto: do parsing if necessary based on type
rowData [ table . columns [ i ] . column _name ] = row [ table . columns [ i ] . ref _column _name ]
rowData [ tableMeta . columns [ i ] . column _name ] = value
}
}
}
}
this . data [ tn ] . push ( rowData )
this . data [ tableMeta . ref _table _name ] . push ( rowData )
// rowIndex++
// rowIndex++
}
}
this . project . tables . push ( table )
}
getTemplate ( ) {
return this . project
}
}
}
}