Skip to content

Commit

Permalink
restoreFromSql calback state
Browse files Browse the repository at this point in the history
  • Loading branch information
budiadiono committed Oct 10, 2018
1 parent 9e9485d commit 3ff2342
Showing 1 changed file with 23 additions and 2 deletions.
25 changes: 23 additions & 2 deletions src/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -191,16 +191,31 @@ export class Db<T> {
}
}

async restoreFromSql(recreateTables: boolean = true): Promise<boolean> {
async restoreFromSql(
callback: (
status:
| 'select-file'
| 'start-restore'
| 'recreate-tables'
| 'restoring'
| 'done'
| 'cancel'
| 'error'
) => void,
recreateTables: boolean = true
): Promise<boolean> {
try {
callback('select-file')
const res: any = await DocumentPicker.getDocumentAsync({
type: '*/*'
})

if (res.type === 'success') {
callback('start-restore')
return new Promise<boolean>(resolve => {
FileSystem.readAsStringAsync(res.uri).then(async sql => {
if (recreateTables) {
callback('recreate-tables')
await this.dropAllTables()

debug('recreate tables...')
Expand All @@ -211,6 +226,7 @@ export class Db<T> {
}

debug('starting restore database...')
callback('restoring')
const sqls = sql.split('INSERT INTO')
this.sqliteDb.transaction(
t => {
Expand All @@ -224,18 +240,23 @@ export class Db<T> {
}
}
},
error => {},
error => {
callback('error')
},
() => {
debug('restore database done!')
callback('done')
resolve(true)
}
)
})
})
}

callback('cancel')
return false
} catch (error) {
callback('error')
throw error
}
}
Expand Down

0 comments on commit 3ff2342

Please sign in to comment.