@acala-network/chopsticks-db 0.8.5-2 → 0.8.5-4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/browser.d.ts +27 -4
- package/lib/browser.js +56 -7
- package/package.json +4 -10
- package/lib/db/browser.d.ts +0 -2
- package/lib/db/browser.js +0 -55
- package/lib/db/sql-wasm.d.ts +0 -1
- package/lib/db/sql-wasm.js +0 -9
package/lib/browser.d.ts
CHANGED
|
@@ -1,6 +1,29 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
|
|
1
|
+
import { BlockEntry, Database, KeyValueEntry } from '@acala-network/chopsticks-core';
|
|
2
|
+
import { DBSchema, IDBPDatabase } from 'idb';
|
|
3
|
+
interface Schema extends DBSchema {
|
|
4
|
+
keyValue: {
|
|
5
|
+
key: string;
|
|
6
|
+
value: string | null;
|
|
7
|
+
};
|
|
8
|
+
block: {
|
|
9
|
+
key: string;
|
|
10
|
+
value: BlockEntry;
|
|
11
|
+
indexes: {
|
|
12
|
+
byNumber: number;
|
|
13
|
+
};
|
|
14
|
+
};
|
|
15
|
+
}
|
|
16
|
+
export declare class IdbDatabase implements Database {
|
|
17
|
+
datasource: Promise<IDBPDatabase<Schema>>;
|
|
5
18
|
constructor(location: string);
|
|
19
|
+
close(): Promise<void>;
|
|
20
|
+
saveBlock(block: BlockEntry): Promise<void>;
|
|
21
|
+
queryBlock(hash: `0x${string}`): Promise<BlockEntry | null>;
|
|
22
|
+
queryBlockByNumber(number: number): Promise<BlockEntry | null>;
|
|
23
|
+
queryHighestBlock(): Promise<BlockEntry | null>;
|
|
24
|
+
deleteBlock(hash: `0x${string}`): Promise<void>;
|
|
25
|
+
blocksCount(): Promise<number>;
|
|
26
|
+
saveStorage(blockHash: `0x${string}`, key: `0x${string}`, value: `0x${string}` | null): Promise<void>;
|
|
27
|
+
queryStorage(blockHash: `0x${string}`, key: `0x${string}`): Promise<KeyValueEntry | null>;
|
|
6
28
|
}
|
|
29
|
+
export {};
|
package/lib/browser.js
CHANGED
|
@@ -1,12 +1,61 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
4
|
-
const
|
|
5
|
-
|
|
6
|
-
class SqljsDatabase extends base_sql_1.BaseSqlDatabase {
|
|
3
|
+
exports.IdbDatabase = void 0;
|
|
4
|
+
const idb_1 = require("idb");
|
|
5
|
+
class IdbDatabase {
|
|
7
6
|
constructor(location) {
|
|
8
|
-
|
|
9
|
-
|
|
7
|
+
this.datasource = (0, idb_1.openDB)(location, 1, {
|
|
8
|
+
upgrade(db) {
|
|
9
|
+
db.createObjectStore('keyValue');
|
|
10
|
+
const blockStore = db.createObjectStore('block', { keyPath: 'hash' });
|
|
11
|
+
blockStore.createIndex('byNumber', 'number');
|
|
12
|
+
},
|
|
13
|
+
});
|
|
14
|
+
}
|
|
15
|
+
async close() {
|
|
16
|
+
const db = await this.datasource;
|
|
17
|
+
db.close();
|
|
18
|
+
}
|
|
19
|
+
async saveBlock(block) {
|
|
20
|
+
const db = await this.datasource;
|
|
21
|
+
const tx = db.transaction(['block'], 'readwrite');
|
|
22
|
+
const store = tx.objectStore('block');
|
|
23
|
+
store.delete(block.hash);
|
|
24
|
+
store.put(block);
|
|
25
|
+
await tx.done;
|
|
26
|
+
}
|
|
27
|
+
async queryBlock(hash) {
|
|
28
|
+
const db = await this.datasource;
|
|
29
|
+
const block = await db.get('block', hash);
|
|
30
|
+
return block ?? null;
|
|
31
|
+
}
|
|
32
|
+
async queryBlockByNumber(number) {
|
|
33
|
+
const db = await this.datasource;
|
|
34
|
+
const block = await db.getFromIndex('block', 'byNumber', number);
|
|
35
|
+
return block ?? null;
|
|
36
|
+
}
|
|
37
|
+
async queryHighestBlock() {
|
|
38
|
+
const db = await this.datasource;
|
|
39
|
+
const index = db.transaction('block').store.index('byNumber');
|
|
40
|
+
const cursor = await index.openCursor(null, 'prev');
|
|
41
|
+
return cursor?.value ?? null;
|
|
42
|
+
}
|
|
43
|
+
async deleteBlock(hash) {
|
|
44
|
+
const db = await this.datasource;
|
|
45
|
+
await db.delete('block', hash);
|
|
46
|
+
}
|
|
47
|
+
async blocksCount() {
|
|
48
|
+
const db = await this.datasource;
|
|
49
|
+
return db.count('block');
|
|
50
|
+
}
|
|
51
|
+
async saveStorage(blockHash, key, value) {
|
|
52
|
+
const db = await this.datasource;
|
|
53
|
+
await db.put('keyValue', value, `${blockHash}-${key}`);
|
|
54
|
+
}
|
|
55
|
+
async queryStorage(blockHash, key) {
|
|
56
|
+
const db = await this.datasource;
|
|
57
|
+
const value = await db.get('keyValue', `${blockHash}-${key}`);
|
|
58
|
+
return value !== undefined ? { blockHash, key, value } : null;
|
|
10
59
|
}
|
|
11
60
|
}
|
|
12
|
-
exports.
|
|
61
|
+
exports.IdbDatabase = IdbDatabase;
|
package/package.json
CHANGED
|
@@ -1,25 +1,19 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@acala-network/chopsticks-db",
|
|
3
|
-
"version": "0.8.5-
|
|
3
|
+
"version": "0.8.5-4",
|
|
4
4
|
"author": "Acala Developers <hello@acala.network>",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"scripts": {
|
|
7
7
|
"clean": "rm -rf lib tsconfig.tsbuildinfo",
|
|
8
|
-
"
|
|
9
|
-
"build": "yarn pack-wasm; tsc -p ./tsconfig.json"
|
|
8
|
+
"build": "tsc -p ./tsconfig.json"
|
|
10
9
|
},
|
|
11
10
|
"dependencies": {
|
|
12
|
-
"@acala-network/chopsticks-core": "0.8.5-
|
|
13
|
-
"
|
|
14
|
-
"@polkadot/wasm-util": "^7.2.2",
|
|
15
|
-
"localforage": "^1.10.0",
|
|
16
|
-
"sql.js": "^1.8.0",
|
|
11
|
+
"@acala-network/chopsticks-core": "0.8.5-4",
|
|
12
|
+
"idb": "^7.1.1",
|
|
17
13
|
"sqlite3": "^5.1.6",
|
|
18
14
|
"typeorm": "^0.3.17"
|
|
19
15
|
},
|
|
20
16
|
"devDependencies": {
|
|
21
|
-
"@types/sql.js": "^1.4.4",
|
|
22
|
-
"fflate": "^0.8.0",
|
|
23
17
|
"typescript": "^5.1.6"
|
|
24
18
|
},
|
|
25
19
|
"files": [
|
package/lib/db/browser.d.ts
DELETED
package/lib/db/browser.js
DELETED
|
@@ -1,55 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
-
if (k2 === undefined) k2 = k;
|
|
4
|
-
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
-
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
-
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
-
}
|
|
8
|
-
Object.defineProperty(o, k2, desc);
|
|
9
|
-
}) : (function(o, m, k, k2) {
|
|
10
|
-
if (k2 === undefined) k2 = k;
|
|
11
|
-
o[k2] = m[k];
|
|
12
|
-
}));
|
|
13
|
-
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
-
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
-
}) : function(o, v) {
|
|
16
|
-
o["default"] = v;
|
|
17
|
-
});
|
|
18
|
-
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
-
if (mod && mod.__esModule) return mod;
|
|
20
|
-
var result = {};
|
|
21
|
-
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
-
__setModuleDefault(result, mod);
|
|
23
|
-
return result;
|
|
24
|
-
};
|
|
25
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
26
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
27
|
-
};
|
|
28
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
29
|
-
exports.openDb = void 0;
|
|
30
|
-
const typeorm_1 = require("typeorm");
|
|
31
|
-
const localforage_1 = require("localforage");
|
|
32
|
-
const sql_js_1 = __importDefault(require("sql.js"));
|
|
33
|
-
const entities = __importStar(require("./entities"));
|
|
34
|
-
const sql_wasm_1 = require("./sql-wasm");
|
|
35
|
-
const openDb = async (location) => {
|
|
36
|
-
if (!globalThis.localforage) {
|
|
37
|
-
globalThis.localforage = (0, localforage_1.createInstance)({ name: 'chopsticks' });
|
|
38
|
-
}
|
|
39
|
-
const source = new typeorm_1.DataSource({
|
|
40
|
-
type: 'sqljs',
|
|
41
|
-
location,
|
|
42
|
-
entities: Object.values(entities),
|
|
43
|
-
synchronize: true,
|
|
44
|
-
autoSave: true,
|
|
45
|
-
logging: false,
|
|
46
|
-
useLocalForage: true,
|
|
47
|
-
driver: sql_js_1.default,
|
|
48
|
-
sqlJsConfig: {
|
|
49
|
-
wasmBinary: sql_wasm_1.SQL_WASM_BYTES,
|
|
50
|
-
},
|
|
51
|
-
});
|
|
52
|
-
await source.initialize();
|
|
53
|
-
return source;
|
|
54
|
-
};
|
|
55
|
-
exports.openDb = openDb;
|
package/lib/db/sql-wasm.d.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export declare const SQL_WASM_BYTES: Uint8Array;
|