1
0
mirror of https://github.com/lana-k/sqliteviz.git synced 2025-12-06 18:18:53 +08:00

Create an empty database #44

This commit is contained in:
lana-k
2021-05-05 21:44:44 +02:00
parent 4619461af8
commit bcaebd4840
8 changed files with 130 additions and 102 deletions

View File

@@ -50,7 +50,7 @@ class Database {
delete this.importProgresses[id]
}
async createDb (name, data, progressCounterId) {
async importDb (name, data, progressCounterId) {
const result = await this.pw.postMessage({
action: 'import',
columns: data.columns,
@@ -66,14 +66,15 @@ class Database {
}
async loadDb (file) {
const fileContent = await fu.readAsArrayBuffer(file)
const fileContent = file ? await fu.readAsArrayBuffer(file) : null
const res = await this.pw.postMessage({ action: 'open', buffer: fileContent })
if (res.error) {
throw new Error(res.error)
}
return this.getSchema(file.name.replace(/\.[^.]+$/, ''))
const dbName = file ? file.name.replace(/\.[^.]+$/, '') : 'database'
return this.getSchema(dbName)
}
async getSchema (name) {
@@ -85,12 +86,14 @@ class Database {
const result = await this.execute(getSchemaSql)
// Parse DDL statements to get column names and types
const parsedSchema = []
result.values.forEach(item => {
parsedSchema.push({
name: item[0],
columns: getColumns(item[1])
if (result && result.values) {
result.values.forEach(item => {
parsedSchema.push({
name: item[0],
columns: getColumns(item[1])
})
})
})
}
// Return db name and schema
return {