Commit 4266f8c7 authored by Marek Veselý's avatar Marek Veselý
Browse files

Merge branch '1011-resolve-todos-in-importexport-ts' into 'main'

Resolve "Resolve todos in importExport.ts"

Closes #1011

See merge request inject/frontend!901
parents f74fb375 5087875f
Loading
Loading
Loading
Loading
+7 −28
Original line number Diff line number Diff line
@@ -65,12 +65,9 @@ const generateFileContents = async () => {
  const fileMapping: Array<{ name: string; content: string | Blob }> = []

  for (const [tableName, fileName] of Object.entries(TABLE_TO_FILE)) {
    // TODO: maybe there is a better way to type this? maybe dexie has something?
    // eslint-disable-next-line @typescript-eslint/no-explicit-any
    const table = (db as Record<string, any>)[tableName]
    const table = db.table(tableName)
    if (!table) {
      // TODO: can this happen? how to handle this case?
      continue
      throw new Error('Wrong TABLE_TO_FILE name.')
    }

    let records
@@ -90,7 +87,6 @@ const generateFileContents = async () => {
    }

    if (!records || (Array.isArray(records) && records.length === 0)) {
      // TODO: decide if we want to export empty files or not
      continue
    }

@@ -123,13 +119,6 @@ const generateFileContents = async () => {
    })
  })

  // TODO: Export database blob for backup?
  // const expDb = await exportDB(db)
  // fileMapping.push({
  //   name: '_database.db',
  //   content: expDb,
  // })

  return fileMapping
}

@@ -393,12 +382,11 @@ export const loadDbData = async (zip: JSZip) => {

  for (const [fileName, tableName] of Object.entries(FILE_TO_TABLE)) {
    const records = await loadYamlData(zip, fileName)

    if (tableName === 'config') {
      if (!records || typeof records !== 'object' || Array.isArray(records)) {
        throw new Error('Config must be an object')
      }
      console.log(records)

      await db.config.clear()
      applyContentPathsDeep(records, contentMap, llmMap, false)
      const normalized = normalizeConfig(records)
@@ -406,19 +394,15 @@ export const loadDbData = async (zip: JSZip) => {
      continue
    }

    if (!Array.isArray(records)) {
      // TODO: handle empty or invalid files?
      throw new Error('YAML file does not contain array')
    if (!Array.isArray(records) || records.length === 0) {
      continue
    }

    applyContentPathsDeep(records, contentMap, llmMap, false)

    // TODO: maybe there is a better way to type this? maybe dexie has something?
    // eslint-disable-next-line @typescript-eslint/no-explicit-any
    const table = (db as Record<string, any>)[tableName]
    const table = db.table(tableName)
    if (!table || typeof table !== 'object') {
      // TODO: can this happen? how to handle this case?
      continue
      throw new Error('Wrong TABLE_TO_FILE name.')
    }

    if (tableName === 'inject') {
@@ -437,9 +421,4 @@ export const loadDbData = async (zip: JSZip) => {

  await importFiles(zip)
  await importDrive(zip)
  // TODO: optionally restore from database blob for full backup?
  // const dbFile = zip.file('_database.db')
  // if (dbFile) {
  //   await importDB(await dbFile.async('blob'))
  // }
}