Removing tables and their related table definitions.
This commit is contained in:
parent
6e699a163d
commit
4cb23759a3
|
@ -176,9 +176,24 @@ export async function addTable(table: Table) {
|
||||||
export async function removeTable(table: Table) {
|
export async function removeTable(table: Table) {
|
||||||
const db = context.getAppDB()
|
const db = context.getAppDB()
|
||||||
try {
|
try {
|
||||||
const definition = await db.get<SQLiteDefinition>(SQLITE_DESIGN_DOC_ID)
|
let response = await Promise.all([
|
||||||
if (definition.sql?.tables?.[table._id!]) {
|
tablesSdk.getAllInternalTables(),
|
||||||
delete definition.sql.tables[table._id!]
|
db.get<SQLiteDefinition>(SQLITE_DESIGN_DOC_ID),
|
||||||
|
])
|
||||||
|
const tables: Table[] = response[0],
|
||||||
|
definition: SQLiteDefinition = response[1]
|
||||||
|
const tableIds = tables
|
||||||
|
.map(tbl => tbl._id!)
|
||||||
|
.filter(id => !id.includes(table._id!))
|
||||||
|
let cleanup = false
|
||||||
|
for (let tableKey of Object.keys(definition.sql?.tables || {})) {
|
||||||
|
// there are no tables matching anymore
|
||||||
|
if (!tableIds.find(id => tableKey.includes(id))) {
|
||||||
|
delete definition.sql.tables[tableKey]
|
||||||
|
cleanup = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (cleanup) {
|
||||||
await db.put(definition)
|
await db.put(definition)
|
||||||
// make sure SQS is cleaned up, tables removed
|
// make sure SQS is cleaned up, tables removed
|
||||||
await db.sqlDiskCleanup()
|
await db.sqlDiskCleanup()
|
||||||
|
|
Loading…
Reference in New Issue