-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Keeping old indexes in indexeddb upgrade
- Loading branch information
Showing
3 changed files
with
129 additions
and
103 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
87 changes: 87 additions & 0 deletions
87
packages/modules/packages/archivist/packages/indexeddb/src/IndexedDbHelpers.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
import type { Logger } from '@xylabs/logger' | ||
import type { IndexDescription, IndexDirection } from '@xyo-network/archivist-model' | ||
import { buildStandardIndexName } from '@xyo-network/archivist-model' | ||
import type { IDBPDatabase, IDBPObjectStore } from 'idb' | ||
import { openDB } from 'idb' | ||
|
||
import type { PayloadStore } from './Archivist.ts' | ||
|
||
export function createStore(db: IDBPDatabase<PayloadStore>, storeName: string, indexes: IndexDescription[], logger?: Logger) { | ||
logger?.log(`Creating store ${storeName}`) | ||
// Create the store | ||
const store = db.createObjectStore(storeName, { | ||
// If it isn't explicitly set, create a value by auto incrementing. | ||
autoIncrement: true, | ||
}) | ||
// Name the store | ||
store.name = storeName | ||
// Create an index on the hash | ||
for (const { | ||
key, multiEntry, unique, | ||
} of indexes) { | ||
const indexKeys = Object.keys(key) | ||
const keys = indexKeys.length === 1 ? indexKeys[0] : indexKeys | ||
const indexName = buildStandardIndexName({ key, unique }) | ||
store.createIndex(indexName, keys, { multiEntry, unique }) | ||
} | ||
} | ||
|
||
export async function getExistingIndexes(db: IDBPDatabase<PayloadStore>, storeName: string): Promise<IndexDescription[]> { | ||
return await useReadOnlyStore(db, storeName, (store) => { | ||
return [...store.indexNames].map((indexName) => { | ||
const index = store.index(indexName) | ||
const key: Record<string, IndexDirection> = {} | ||
if (Array.isArray(index.keyPath)) { | ||
for (const keyPath of index.keyPath) { | ||
key[keyPath] = 1 | ||
} | ||
} else { | ||
key[index.keyPath] = 1 | ||
} | ||
const desc: IndexDescription = { | ||
name: indexName, | ||
key, | ||
unique: index.unique, | ||
multiEntry: index.multiEntry, | ||
} | ||
return desc | ||
}) | ||
}) | ||
} | ||
|
||
export async function useDb<T>(dbName: string, callback: (db: IDBPDatabase<PayloadStore>) => Promise<T> | T): Promise<T> { | ||
const db = await openDB<PayloadStore>(dbName) | ||
try { | ||
return await callback(db) | ||
} finally { | ||
db.close() | ||
} | ||
} | ||
|
||
export async function useReadOnlyStore<T>( | ||
db: IDBPDatabase<PayloadStore>, | ||
storeName: string, | ||
callback: (store: IDBPObjectStore<PayloadStore, [string], string, 'readonly'>) => Promise<T> | T, | ||
): Promise<T> { | ||
const transaction = db.transaction(storeName, 'readonly') | ||
const store = transaction.objectStore(storeName) | ||
try { | ||
return await callback(store) | ||
} finally { | ||
await transaction.done | ||
} | ||
} | ||
|
||
export async function useReadWriteStore<T>( | ||
db: IDBPDatabase<PayloadStore>, | ||
storeName: string, | ||
callback: (store: IDBPObjectStore<PayloadStore, [string], string, 'readwrite'>) => Promise<T> | T, | ||
): Promise<T> { | ||
const transaction = db.transaction(storeName, 'readwrite') | ||
const store = transaction.objectStore(storeName) | ||
try { | ||
return await callback(store) | ||
} finally { | ||
await transaction.done | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters