@ -13,6 +13,7 @@
< / template >
< script >
import { SqlUiFactory } from 'nocodb-sdk'
import colors from '~/mixins/colors'
export default {
@ -43,7 +44,9 @@ export default {
} ,
data ( ) {
return {
localTemplateData : null ,
projectCreation : false ,
tableCreation : false ,
loaderMessagesIndex : 0 ,
loaderMessages : [
'Setting up new database configs' ,
@ -69,6 +72,18 @@ export default {
]
}
} ,
watch : {
templateData : {
deep : true ,
handler ( data ) {
this . localTemplateData = JSON . parse ( JSON . stringify ( data ) )
}
}
} ,
created ( ) {
this . localTemplateData = JSON . parse ( JSON . stringify ( this . templateData ) )
} ,
methods : {
async useTemplate ( projectType ) {
if ( ! this . valid ) {
@ -76,8 +91,7 @@ export default {
}
/ / t h i s . $ e m i t ( ' u s e T e m p l a t e ' , t y p e )
this . projectCreation = true
/ / t h i s . p r o j e c t C r e a t i o n = t r u e
let interv
try {
interv = setInterval ( ( ) => {
@ -85,111 +99,126 @@ export default {
this . $store . commit ( 'loader/MutMessage' , this . loaderMessages [ this . loaderMessagesIndex ] )
} , 1000 )
let projectId , prefix
let project
/ / N o t a v a i l a b l e n o w
if ( this . importToProject ) {
this . $store . commit ( 'loader/MutMessage' , 'Importing excel template' )
const res = await this . $store . dispatch ( 'sqlMgr/ActSqlOp' , [ {
/ / t o d o : e x t r a c t b a s e d o n a c t i v e
dbAlias : 'db' , / / t h i s . n o d e s . d b A l i a s ,
env : '_noco'
} , 'xcModelsCreateFromTemplate' , {
template : this . templateData
} ] )
if ( res && res . tables && res . tables . length ) {
this . $toast . success ( ` Imported ${ res . tables . length } tables successfully ` ) . goAway ( 3000 )
/ / t h i s . $ s t o r e . c o m m i t ( ' l o a d e r / M u t M e s s a g e ' , ' I m p o r t i n g e x c e l t e m p l a t e ' )
/ / c o n s t r e s = a w a i t t h i s . $ s t o r e . d i s p a t c h ( ' s q l M g r / A c t S q l O p ' , [ {
/ / / / t o d o : e x t r a c t b a s e d o n a c t i v e
/ / d b A l i a s : ' d b ' , / / t h i s . n o d e s . d b A l i a s ,
/ / e n v : ' _ n o c o '
/ / } , ' x c M o d e l s C r e a t e F r o m T e m p l a t e ' , {
/ / t e m p l a t e : t h i s . t e m p l a t e D a t a
/ / } ] )
/ / i f ( r e s & & r e s . t a b l e s & & r e s . t a b l e s . l e n g t h ) {
/ / t h i s . $ t o a s t . s u c c e s s ( ` I m p o r t e d $ { r e s . t a b l e s . l e n g t h } t a b l e s s u c c e s s f u l l y ` ) . g o A w a y ( 3 0 0 0 )
/ / } e l s e {
/ / t h i s . $ t o a s t . s u c c e s s ( ' T e m p l a t e i m p o r t e d s u c c e s s f u l l y ' ) . g o A w a y ( 3 0 0 0 )
/ / }
/ / p r o j e c t I d = t h i s . $ r o u t e . p a r a m s . p r o j e c t _ i d
/ / p r e f i x = t h i s . $ s t o r e . g e t t e r s [ ' p r o j e c t / G t r P r o j e c t P r e f i x ' ]
} else {
this . $toast . success ( 'Template imported successfully' ) . goAway ( 3000 )
}
/ / C r e a t e a n e m p t y p r o j e c t
try {
this . $e ( "a:project:create:excel" ) ;
projectId = this . $route . params . project _id
prefix = this . $store . getters [ 'project/GtrProjectPrefix' ]
} else {
const result = await this . $store . dispatch ( 'sqlMgr/ActSqlOp' , [ null , 'projectCreateByWebWithXCDB' , {
project = await this . $api . project . create ( {
title : this . templateData . title ,
projectType ,
template : this . templateData ,
excelImport : this . excelImport
} ] )
projectId = result . id
prefix = result . prefix
await this . $store . dispatch ( 'project/ActLoadProjectInfo' )
external : false
} )
this . projectCreation = true
} catch ( e ) {
this . projectCreation = false
this . $toast
. error ( await this . _extractSdkResponseErrorMsg ( e ) )
. goAway ( 3000 )
} finally {
clearInterval ( interv )
}
if ( ! this . projectCreation ) {
/ / f a i l e d t o c r e a t e p r o j e c t
return
}
/ / C r e a t e t a b l e s
try {
for ( const t of this . localTemplateData . tables ) {
/ / e n r i c h s y s t e m f i e l d s i f n o t p r o v i d e d
/ / e . g . i d , c r e a t e d _ a t , u p d a t e d _ a t
const systemColumns = SqlUiFactory
. create ( { client : 'sqlite3' } )
. getNewTableColumns ( )
. filter ( c => c . column _name != 'title' )
const table = await this . $api . dbTable . create ( project . id , {
table _name : t . table _name ,
title : '' ,
columns : [ ... t . columns , ... systemColumns ]
} )
t . table _title = table . title
}
this . tableCreation = true
} catch ( e ) {
this . $toast
. error ( await this . _extractSdkResponseErrorMsg ( e ) )
. goAway ( 3000 )
this . tableCreation = false
} finally {
clearInterval ( interv )
}
}
if ( ! this . tableCreation ) {
/ / f a i l e d t o c r e a t e t a b l e
return
}
/ / B u l k i m p o r t d a t a
if ( this . importData ) {
this . $store . commit ( 'loader/MutMessage' , 'Importing excel data to project' )
await this . importDataToProject ( { projectId , projectType , prefix } )
await this . importDataToProject ( this . templateData . title )
}
this . $store . commit ( 'loader/MutMessage' , null )
this . projectReloading = false
if ( ! this . importToProject ) {
await this . $router . push ( {
path : ` /nc/ ${ projectId } ` ,
this . $router . push ( {
path : ` /nc/ ${ project . id } ` ,
query : {
new : 1
}
} )
}
this . $emit ( 'success' )
} catch ( e ) {
console . log ( e )
this . $toast . error ( e . message ) . goAway ( 3000 )
this . $store . commit ( 'loader/MutMessage' , null )
} finally {
clearInterval ( interv )
}
this . $store . commit ( 'loader/MutMessage' , null )
this . projectCreation = false
this . tableCreation = false
}
} ,
async importDataToProject ( { projectId , projectType , prefix = '' } ) {
/ / t h i s . $ s t o r e . c o m m i t ( ' p r o j e c t / M u t P r o j e c t I d ' , p r o j e c t I d )
this . $ncApis . setProjectId ( projectId )
async importDataToProject ( projectName ) {
let total = 0
let progress = 0
/ * a w a i t P r o m i s e . a l l ( O b j e c t . e n t r i e s ( t h i s . i m p o r t D a t a ) . m a p ( v = > ( a s y n c ( [ t a b l e , d a t a ] ) = > {
await this . $store . dispatch ( 'meta/ActLoadMeta' , {
tn : ` ${ prefix } ${ table } ` , project _id : projectId
} )
/ / t o d o : g e t t a b l e n a m e p r o p e r l y
const api = this . $ncApis . get ( {
table : ` ${ prefix } ${ table } ` ,
type : projectType
} )
await Promise . all ( this . localTemplateData . tables . map ( v => ( async ( tableMeta ) => {
const tableName = tableMeta . table _title
const data = this . importData [ tableMeta . ref _table _name ]
total += data . length
for ( let i = 0 ; i < data . length ; i += 500 ) {
this . $store . commit ( 'loader/MutMessage' , ` Importing data : ${ progress } / ${ total } ` )
this . $store . commit ( 'loader/MutProgress' , Math . round ( progress && 100 * progress / total ) )
const batchData = data . slice ( i , i + 500 )
await api . insertBulk ( batchData )
progress += batchData . length
}
this . $store . commit ( 'loader/MutClear' )
} ) ( v ) ) ) * /
await Promise . all ( this . templateData . tables . map ( v => ( async ( tableMeta ) => {
const table = tableMeta . table _name
const data = this . importData [ tableMeta . refTn ]
await this . $store . dispatch ( 'meta/ActLoadMeta' , {
tn : ` ${ prefix } ${ table } ` , project _id : projectId
} )
/ / t o d o : g e t t a b l e n a m e p r o p e r l y
const api = this . $ncApis . get ( {
table : ` ${ prefix } ${ table } ` ,
type : projectType
} )
total += data . length
for ( let i = 0 ; i < data . length ; i += 500 ) {
this . $store . commit ( 'loader/MutMessage' , ` Importing data : ${ progress } / ${ total } ` )
this . $store . commit ( 'loader/MutMessage' , ` Importing data to ${ projectName } : ${ progress } / ${ total } records ` )
this . $store . commit ( 'loader/MutProgress' , Math . round ( progress && 100 * progress / total ) )
const batchData = this . remapColNames ( data . slice ( i , i + 500 ) , tableMeta . columns )
await api . insertBulk ( batchData )
await this . $api . dbTableRow . bulkCreate (
'noco' ,
projectName ,
tableName ,
batchData
)
progress += batchData . length
}
this . $store . commit ( 'loader/MutClear' )
@ -198,7 +227,7 @@ export default {
remapColNames ( batchData , columns ) {
return batchData . map ( data => ( columns || [ ] ) . reduce ( ( aggObj , col ) => ( {
... aggObj ,
[ col . column _name ] : data [ col . refCn ]
[ col . column _name ] : data [ col . ref _column _name ]
} ) , { } )
)
}