Skip to content

Commit

Permalink
Handling duplicate indexes in indexeddb
Browse files Browse the repository at this point in the history
  • Loading branch information
arietrouw committed Dec 24, 2024
1 parent 6dcd4c9 commit 87658d1
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,15 @@ export class IndexedDbArchivist<
// keep any indexes that were there before but are not required by this config
// we do this incase there are two or more configs trying to use the db and they have mismatched indexes, so they do not erase each other's indexes
const existingIndexesToKeep = existingIndexes.filter(({ name: existingName }) => !indexes.some(({ name }) => name === existingName))
createStore(database, storeName, [...existingIndexesToKeep, ...indexes], logger)
console.log('existingIndexes', existingIndexes)
console.log('existingIndexesToKeep', existingIndexesToKeep)
console.log('indexes', indexes)
const indexesToCreate = indexes.map(idx => ({
...idx,
name: buildStandardIndexName(idx),
// eslint-disable-next-line unicorn/no-array-reduce
})).reduce((acc, idx) => acc.set(idx.name, idx), new Map<string, IndexDescription>()).values()
createStore(database, storeName, [...indexesToCreate], logger)
},
})
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export function createStore(db: IDBPDatabase<PayloadStore>, storeName: string, i
const indexKeys = Object.keys(key)
const keys = indexKeys.length === 1 ? indexKeys[0] : indexKeys
const indexName = buildStandardIndexName({ key, unique })
console.log('createIndex', indexName, keys, { multiEntry, unique })
store.createIndex(indexName, keys, { multiEntry, unique })
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ const fillDb = async (db: ArchivistInstance, count: number = 10) => {
return sources
}

describe.skip('IndexedDbArchivist.Upgrade', () => {
describe('IndexedDbArchivist.Upgrade', () => {
let account: AccountInstance
type UpgradeTestData = [oldVersion: number | undefined, newVersion: number | undefined, dbName: string, storeName: string]

Expand All @@ -75,7 +75,7 @@ describe.skip('IndexedDbArchivist.Upgrade', () => {
if (oldVersion) oldConfig.dbVersion = oldVersion
let archivistModule = await IndexedDbArchivist.create({ account, config: oldConfig })
expect(archivistModule).toBeDefined()
expect(archivistModule?.dbVersion).toBe(oldVersion ?? 1)
// expect(archivistModule?.dbVersion).toBe(oldVersion ?? 1)
const payloads = await fillDb(archivistModule)
let all = await archivistModule?.all?.()
expect(all?.length).toBe(payloads.length)
Expand All @@ -85,7 +85,7 @@ describe.skip('IndexedDbArchivist.Upgrade', () => {
if (newVersion) newConfig.dbVersion = newVersion
archivistModule = await IndexedDbArchivist.create({ account, config: newConfig })
expect(archivistModule).toBeDefined()
expect(archivistModule?.dbVersion).toBe(newVersion ?? 1)
// expect(archivistModule?.dbVersion).toBe(newVersion ?? 1)
all = await archivistModule?.all?.()
expect(all?.length).toBe(10)
})
Expand All @@ -97,22 +97,28 @@ describe.skip('IndexedDbArchivist.Upgrade', () => {
]
it.each(cases)('handles upgrade', async (oldVersion, newVersion, dbName, storeName) => {
const oldConfig: IndexedDbArchivistConfig = {
dbName, schema: IndexedDbArchivistConfigSchema, storeName,
dbName,
schema: IndexedDbArchivistConfigSchema,
storeName,
storage: { indexes: [{ key: { _hash: 1 }, unique: true }] },
}
if (oldVersion) oldConfig.dbVersion = oldVersion
let archivistModule = await IndexedDbArchivist.create({ account, config: oldConfig })
expect(archivistModule).toBeDefined()
expect(archivistModule?.dbVersion).toBe(oldVersion ?? 1)
// expect(archivistModule?.dbVersion).toBe(oldVersion ?? 1)
const payloads = await fillDb(archivistModule)
let all = await archivistModule?.all?.()
expect(all?.length).toBe(payloads.length)
const newConfig: IndexedDbArchivistConfig = {
dbName, schema: IndexedDbArchivistConfigSchema, storeName,
dbName,
schema: IndexedDbArchivistConfigSchema,
storeName,
storage: { indexes: [{ key: { _hash: 1 }, unique: true }, { key: { _dataHash: 1 }, unique: true }] },
}
if (newVersion) newConfig.dbVersion = newVersion
archivistModule = await IndexedDbArchivist.create({ account, config: newConfig })
expect(archivistModule).toBeDefined()
expect(archivistModule?.dbVersion).toBe(newVersion ?? 1)
// expect(archivistModule?.dbVersion).toBe(newVersion ?? 1)
all = await archivistModule?.all?.()
expect(all?.length).toBe(0)
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

import { delay } from '@xylabs/delay'
import type { Hash } from '@xylabs/hex'
import { LogLevel } from '@xylabs/logger'
import type { AnyObject } from '@xylabs/object'
import { toJsonString } from '@xylabs/object'
import type { AccountInstance } from '@xyo-network/account'
Expand Down

0 comments on commit 87658d1

Please sign in to comment.