@ -13,6 +13,7 @@
< / template >
< / template >
< script >
< script >
import { SqlUiFactory } from 'nocodb-sdk'
import colors from '~/mixins/colors'
import colors from '~/mixins/colors'
export default {
export default {
@ -43,7 +44,9 @@ export default {
} ,
} ,
data ( ) {
data ( ) {
return {
return {
localTemplateData : null ,
projectCreation : false ,
projectCreation : false ,
tableCreation : false ,
loaderMessagesIndex : 0 ,
loaderMessagesIndex : 0 ,
loaderMessages : [
loaderMessages : [
'Setting up new database configs' ,
'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 : {
methods : {
async useTemplate ( projectType ) {
async useTemplate ( projectType ) {
if ( ! this . valid ) {
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 )
/ / t h i s . $ e m i t ( ' u s e T e m p l a t e ' , t y p e )
/ / t h i s . p r o j e c t C r e a t i o n = t r u e
this . projectCreation = true
let interv
let interv
try {
try {
interv = setInterval ( ( ) => {
interv = setInterval ( ( ) => {
@ -85,111 +99,126 @@ export default {
this . $store . commit ( 'loader/MutMessage' , this . loaderMessages [ this . loaderMessagesIndex ] )
this . $store . commit ( 'loader/MutMessage' , this . loaderMessages [ this . loaderMessagesIndex ] )
} , 1000 )
} , 1000 )
let projectId , prefix
let project
/ / N o t a v a i l a b l e n o w
if ( this . importToProject ) {
if ( this . importToProject ) {
this . $store . commit ( 'loader/MutMessage' , 'Importing excel template' )
/ / 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 ' )
const res = await this . $store . dispatch ( 'sqlMgr/ActSqlOp' , [ {
/ / 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
/ / / / 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 ,
/ / 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 ,
env : '_noco'
/ / e n v : ' _ n o c o '
} , 'xcModelsCreateFromTemplate' , {
/ / } , ' 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 ' , {
template : this . templateData
/ / t e m p l a t e : t h i s . t e m p l a t e D a t a
} ] )
/ / } ] )
if ( res && res . tables && res . tables . length ) {
/ / 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 ) {
this . $toast . success ( ` Imported ${ res . tables . length } tables successfully ` ) . goAway ( 3000 )
/ / 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 {
} 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
project = await this . $api . project . create ( {
prefix = this . $store . getters [ 'project/GtrProjectPrefix' ]
} else {
const result = await this . $store . dispatch ( 'sqlMgr/ActSqlOp' , [ null , 'projectCreateByWebWithXCDB' , {
title : this . templateData . title ,
title : this . templateData . title ,
projectType ,
external : false
template : this . templateData ,
} )
excelImport : this . excelImport
this . projectCreation = true
} ] )
} catch ( e ) {
projectId = result . id
this . projectCreation = false
prefix = result . prefix
this . $toast
await this . $store . dispatch ( 'project/ActLoadProjectInfo' )
. 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 )
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 ) {
if ( this . importData ) {
this . $store . commit ( 'loader/MutMessage' , 'Importing excel data to project' )
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
this . $router . push ( {
if ( ! this . importToProject ) {
path : ` /nc/ ${ project . id } ` ,
await this . $router . push ( {
path : ` /nc/ ${ projectId } ` ,
query : {
query : {
new : 1
new : 1
}
}
} )
} )
}
this . $emit ( 'success' )
this . $emit ( 'success' )
} catch ( e ) {
} catch ( e ) {
console . log ( e )
console . log ( e )
this . $toast . error ( e . message ) . goAway ( 3000 )
this . $toast . error ( e . message ) . goAway ( 3000 )
this . $store . commit ( 'loader/MutMessage' , null )
} finally {
clearInterval ( interv )
clearInterval ( interv )
}
this . $store . commit ( 'loader/MutMessage' , null )
this . projectCreation = false
this . projectCreation = false
this . tableCreation = false
}
} ,
} ,
async importDataToProject ( { projectId , projectType , prefix = '' } ) {
async importDataToProject ( projectName ) {
/ / 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 )
let total = 0
let total = 0
let progress = 0
let progress = 0
await Promise . all ( this . localTemplateData . tables . map ( v => ( async ( tableMeta ) => {
/ * 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 ] ) = > {
const tableName = tableMeta . table _title
await this . $store . dispatch ( 'meta/ActLoadMeta' , {
const data = this . importData [ tableMeta . ref _table _name ]
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
total += data . length
for ( let i = 0 ; i < data . length ; i += 500 ) {
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 = 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/MutProgress' , Math . round ( progress && 100 * progress / total ) )
this . $store . commit ( 'loader/MutProgress' , Math . round ( progress && 100 * progress / total ) )
const batchData = this . remapColNames ( data . slice ( i , i + 500 ) , tableMeta . columns )
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
progress += batchData . length
}
}
this . $store . commit ( 'loader/MutClear' )
this . $store . commit ( 'loader/MutClear' )
@ -198,7 +227,7 @@ export default {
remapColNames ( batchData , columns ) {
remapColNames ( batchData , columns ) {
return batchData . map ( data => ( columns || [ ] ) . reduce ( ( aggObj , col ) => ( {
return batchData . map ( data => ( columns || [ ] ) . reduce ( ( aggObj , col ) => ( {
... aggObj ,
... aggObj ,
[ col . column _name ] : data [ col . refCn ]
[ col . column _name ] : data [ col . ref _column _name ]
} ) , { } )
} ) , { } )
)
)
}
}