-
Notifications
You must be signed in to change notification settings - Fork 90
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
use indexedDB instead of sqljs (#477)
* add idb for browser * add test * fix test * remove extra package * remove package * remove localforage * using cursor * add more tests * fix test * fix tests * fix lint * fix keyValue key * fix * reduce key value storage size * small fix * undo comment --------- Co-authored-by: Ermal Kaleci <ermalkaleci@gmail.com>
- Loading branch information
1 parent
889fb52
commit a38d901
Showing
8 changed files
with
175 additions
and
141 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
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,13 +1,81 @@ | ||
import { DataSource } from 'typeorm' | ||
import { BlockEntry, Database, KeyValueEntry } from '@acala-network/chopsticks-core' | ||
import { DBSchema, IDBPDatabase, openDB } from 'idb' | ||
|
||
import { BaseSqlDatabase } from './base-sql' | ||
import { openDb } from './db/browser' | ||
|
||
export class SqljsDatabase extends BaseSqlDatabase { | ||
datasource: Promise<DataSource> | ||
interface Schema extends DBSchema { | ||
keyValue: { | ||
key: string | ||
value: string | null | ||
} | ||
block: { | ||
key: string | ||
value: BlockEntry | ||
indexes: { byNumber: number } | ||
} | ||
} | ||
export class IdbDatabase implements Database { | ||
datasource: Promise<IDBPDatabase<Schema>> | ||
|
||
constructor(location: string) { | ||
super() | ||
this.datasource = openDb(location) | ||
this.datasource = openDB<Schema>(location, 1, { | ||
upgrade(db) { | ||
db.createObjectStore('keyValue') | ||
const blockStore = db.createObjectStore('block', { keyPath: 'hash' }) | ||
blockStore.createIndex('byNumber', 'number') | ||
}, | ||
}) | ||
} | ||
|
||
async close(): Promise<void> { | ||
const db = await this.datasource | ||
db.close() | ||
} | ||
|
||
async saveBlock(block: BlockEntry): Promise<void> { | ||
const db = await this.datasource | ||
const tx = db.transaction(['block'], 'readwrite') | ||
const store = tx.objectStore('block') | ||
store.delete(block.hash) | ||
store.put(block) | ||
await tx.done | ||
} | ||
|
||
async queryBlock(hash: `0x${string}`): Promise<BlockEntry | null> { | ||
const db = await this.datasource | ||
const block = await db.get('block', hash) | ||
return block ?? null | ||
} | ||
|
||
async queryBlockByNumber(number: number): Promise<BlockEntry | null> { | ||
const db = await this.datasource | ||
const block = await db.getFromIndex('block', 'byNumber', number) | ||
return block ?? null | ||
} | ||
|
||
async queryHighestBlock(): Promise<BlockEntry | null> { | ||
const db = await this.datasource | ||
const index = db.transaction('block').store.index('byNumber') | ||
const cursor = await index.openCursor(null, 'prev') | ||
return cursor?.value ?? null | ||
} | ||
|
||
async deleteBlock(hash: `0x${string}`): Promise<void> { | ||
const db = await this.datasource | ||
await db.delete('block', hash) | ||
} | ||
|
||
async blocksCount(): Promise<number> { | ||
const db = await this.datasource | ||
return db.count('block') | ||
} | ||
|
||
async saveStorage(blockHash: `0x${string}`, key: `0x${string}`, value: `0x${string}` | null): Promise<void> { | ||
const db = await this.datasource | ||
await db.put('keyValue', value, `${blockHash}-${key}`) | ||
} | ||
|
||
async queryStorage(blockHash: `0x${string}`, key: `0x${string}`): Promise<KeyValueEntry | null> { | ||
const db = await this.datasource | ||
const value = await db.get('keyValue', `${blockHash}-${key}`) | ||
return value !== undefined ? { blockHash, key, value } : null | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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
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
Oops, something went wrong.