@acala-network/chopsticks-db 0.8.5-4 → 0.8.5-6
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/dist/esm/base-sql.js +56 -0
- package/dist/esm/browser.js +57 -0
- package/dist/esm/db/entities.js +50 -0
- package/dist/esm/db/index.js +16 -0
- package/dist/esm/index.js +8 -0
- package/package.json +18 -11
- /package/{lib → dist/cjs}/base-sql.js +0 -0
- /package/{lib → dist/cjs}/browser.js +0 -0
- /package/{lib → dist/cjs}/db/entities.js +0 -0
- /package/{lib → dist/cjs}/db/index.js +0 -0
- /package/{lib → dist/cjs}/index.js +0 -0
- /package/{lib → dist/types}/base-sql.d.ts +0 -0
- /package/{lib → dist/types}/browser.d.ts +0 -0
- /package/{lib → dist/types}/db/entities.d.ts +0 -0
- /package/{lib → dist/types}/db/index.d.ts +0 -0
- /package/{lib → dist/types}/index.d.ts +0 -0
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { BlockEntity, KeyValuePair } from './db/entities';
|
|
2
|
+
export class BaseSqlDatabase {
|
|
3
|
+
constructor() {
|
|
4
|
+
this.close = async () => {
|
|
5
|
+
const db = await this.datasource;
|
|
6
|
+
await db.destroy();
|
|
7
|
+
};
|
|
8
|
+
}
|
|
9
|
+
async saveBlock(block) {
|
|
10
|
+
const db = await this.datasource;
|
|
11
|
+
const { hash, number, header, extrinsics, parentHash, storageDiff } = block;
|
|
12
|
+
await db.transaction(async (transactionalEntityManager) => {
|
|
13
|
+
await transactionalEntityManager.getRepository(BlockEntity).delete({ number });
|
|
14
|
+
await transactionalEntityManager.getRepository(BlockEntity).upsert({
|
|
15
|
+
hash,
|
|
16
|
+
number,
|
|
17
|
+
header,
|
|
18
|
+
extrinsics,
|
|
19
|
+
parentHash,
|
|
20
|
+
storageDiff,
|
|
21
|
+
}, ['hash']);
|
|
22
|
+
});
|
|
23
|
+
}
|
|
24
|
+
async queryBlock(hash) {
|
|
25
|
+
const db = await this.datasource;
|
|
26
|
+
return db.getRepository(BlockEntity).findOne({ where: { hash } });
|
|
27
|
+
}
|
|
28
|
+
async queryBlockByNumber(number) {
|
|
29
|
+
const db = await this.datasource;
|
|
30
|
+
return db.getRepository(BlockEntity).findOne({ where: { number }, order: { number: 'desc' } });
|
|
31
|
+
}
|
|
32
|
+
async queryHighestBlock() {
|
|
33
|
+
const db = await this.datasource;
|
|
34
|
+
return db.getRepository(BlockEntity).findOne({ where: {}, order: { number: 'desc' } });
|
|
35
|
+
}
|
|
36
|
+
async deleteBlock(hash) {
|
|
37
|
+
const db = await this.datasource;
|
|
38
|
+
await db.getRepository(BlockEntity).delete({ hash });
|
|
39
|
+
}
|
|
40
|
+
async blocksCount() {
|
|
41
|
+
const db = await this.datasource;
|
|
42
|
+
return db.getRepository(BlockEntity).count();
|
|
43
|
+
}
|
|
44
|
+
async saveStorage(blockHash, key, value) {
|
|
45
|
+
const db = await this.datasource;
|
|
46
|
+
await db.getRepository(KeyValuePair).upsert({
|
|
47
|
+
blockHash,
|
|
48
|
+
key,
|
|
49
|
+
value,
|
|
50
|
+
}, ['blockHash', 'key']);
|
|
51
|
+
}
|
|
52
|
+
async queryStorage(blockHash, key) {
|
|
53
|
+
const db = await this.datasource;
|
|
54
|
+
return db.getRepository(KeyValuePair).findOne({ where: { blockHash, key } });
|
|
55
|
+
}
|
|
56
|
+
}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import { openDB } from 'idb';
|
|
2
|
+
export class IdbDatabase {
|
|
3
|
+
constructor(location) {
|
|
4
|
+
this.datasource = openDB(location, 1, {
|
|
5
|
+
upgrade(db) {
|
|
6
|
+
db.createObjectStore('keyValue');
|
|
7
|
+
const blockStore = db.createObjectStore('block', { keyPath: 'hash' });
|
|
8
|
+
blockStore.createIndex('byNumber', 'number');
|
|
9
|
+
},
|
|
10
|
+
});
|
|
11
|
+
}
|
|
12
|
+
async close() {
|
|
13
|
+
const db = await this.datasource;
|
|
14
|
+
db.close();
|
|
15
|
+
}
|
|
16
|
+
async saveBlock(block) {
|
|
17
|
+
const db = await this.datasource;
|
|
18
|
+
const tx = db.transaction(['block'], 'readwrite');
|
|
19
|
+
const store = tx.objectStore('block');
|
|
20
|
+
store.delete(block.hash);
|
|
21
|
+
store.put(block);
|
|
22
|
+
await tx.done;
|
|
23
|
+
}
|
|
24
|
+
async queryBlock(hash) {
|
|
25
|
+
const db = await this.datasource;
|
|
26
|
+
const block = await db.get('block', hash);
|
|
27
|
+
return block ?? null;
|
|
28
|
+
}
|
|
29
|
+
async queryBlockByNumber(number) {
|
|
30
|
+
const db = await this.datasource;
|
|
31
|
+
const block = await db.getFromIndex('block', 'byNumber', number);
|
|
32
|
+
return block ?? null;
|
|
33
|
+
}
|
|
34
|
+
async queryHighestBlock() {
|
|
35
|
+
const db = await this.datasource;
|
|
36
|
+
const index = db.transaction('block').store.index('byNumber');
|
|
37
|
+
const cursor = await index.openCursor(null, 'prev');
|
|
38
|
+
return cursor?.value ?? null;
|
|
39
|
+
}
|
|
40
|
+
async deleteBlock(hash) {
|
|
41
|
+
const db = await this.datasource;
|
|
42
|
+
await db.delete('block', hash);
|
|
43
|
+
}
|
|
44
|
+
async blocksCount() {
|
|
45
|
+
const db = await this.datasource;
|
|
46
|
+
return db.count('block');
|
|
47
|
+
}
|
|
48
|
+
async saveStorage(blockHash, key, value) {
|
|
49
|
+
const db = await this.datasource;
|
|
50
|
+
await db.put('keyValue', value, `${blockHash}-${key}`);
|
|
51
|
+
}
|
|
52
|
+
async queryStorage(blockHash, key) {
|
|
53
|
+
const db = await this.datasource;
|
|
54
|
+
const value = await db.get('keyValue', `${blockHash}-${key}`);
|
|
55
|
+
return value !== undefined ? { blockHash, key, value } : null;
|
|
56
|
+
}
|
|
57
|
+
}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { EntitySchema } from 'typeorm';
|
|
2
|
+
export const KeyValuePair = new EntitySchema({
|
|
3
|
+
name: 'KeyValuePair',
|
|
4
|
+
columns: {
|
|
5
|
+
blockHash: {
|
|
6
|
+
primary: true,
|
|
7
|
+
type: 'varchar',
|
|
8
|
+
nullable: false,
|
|
9
|
+
},
|
|
10
|
+
key: {
|
|
11
|
+
primary: true,
|
|
12
|
+
type: 'varchar',
|
|
13
|
+
nullable: false,
|
|
14
|
+
},
|
|
15
|
+
value: {
|
|
16
|
+
type: 'text',
|
|
17
|
+
nullable: true,
|
|
18
|
+
},
|
|
19
|
+
},
|
|
20
|
+
});
|
|
21
|
+
export const BlockEntity = new EntitySchema({
|
|
22
|
+
name: 'Block',
|
|
23
|
+
columns: {
|
|
24
|
+
hash: {
|
|
25
|
+
primary: true,
|
|
26
|
+
type: 'varchar',
|
|
27
|
+
nullable: false,
|
|
28
|
+
},
|
|
29
|
+
number: {
|
|
30
|
+
type: 'int',
|
|
31
|
+
nullable: false,
|
|
32
|
+
},
|
|
33
|
+
header: {
|
|
34
|
+
type: 'text',
|
|
35
|
+
nullable: false,
|
|
36
|
+
},
|
|
37
|
+
parentHash: {
|
|
38
|
+
type: 'varchar',
|
|
39
|
+
nullable: true,
|
|
40
|
+
},
|
|
41
|
+
extrinsics: {
|
|
42
|
+
type: 'simple-array',
|
|
43
|
+
nullable: false,
|
|
44
|
+
},
|
|
45
|
+
storageDiff: {
|
|
46
|
+
type: 'simple-json',
|
|
47
|
+
nullable: true,
|
|
48
|
+
},
|
|
49
|
+
},
|
|
50
|
+
});
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { DataSource } from 'typeorm';
|
|
2
|
+
import * as entities from './entities';
|
|
3
|
+
export const openDb = async (dbPath) => {
|
|
4
|
+
const source = new DataSource({
|
|
5
|
+
type: 'sqlite',
|
|
6
|
+
database: dbPath,
|
|
7
|
+
entities: Object.values(entities),
|
|
8
|
+
synchronize: true,
|
|
9
|
+
logging: false,
|
|
10
|
+
enableWAL: true,
|
|
11
|
+
busyErrorRetry: 1000,
|
|
12
|
+
busyTimeout: 5000, // retry for 5 seconds, sqlite PRAGMA busy_timeout
|
|
13
|
+
});
|
|
14
|
+
await source.initialize();
|
|
15
|
+
return source;
|
|
16
|
+
};
|
package/package.json
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@acala-network/chopsticks-db",
|
|
3
|
-
"version": "0.8.5-
|
|
3
|
+
"version": "0.8.5-6",
|
|
4
4
|
"author": "Acala Developers <hello@acala.network>",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"scripts": {
|
|
7
|
-
"clean": "rm -rf
|
|
8
|
-
"build": "tsc -p ./tsconfig.json"
|
|
7
|
+
"clean": "rm -rf dist",
|
|
8
|
+
"build": "yarn clean && tsc -p ./tsconfig.json && tsc -p ./tsconfig.esm.json"
|
|
9
9
|
},
|
|
10
10
|
"dependencies": {
|
|
11
|
-
"@acala-network/chopsticks-core": "0.8.5-
|
|
11
|
+
"@acala-network/chopsticks-core": "0.8.5-6",
|
|
12
12
|
"idb": "^7.1.1",
|
|
13
13
|
"sqlite3": "^5.1.6",
|
|
14
14
|
"typeorm": "^0.3.17"
|
|
@@ -17,18 +17,25 @@
|
|
|
17
17
|
"typescript": "^5.1.6"
|
|
18
18
|
},
|
|
19
19
|
"files": [
|
|
20
|
-
"
|
|
20
|
+
"dist/esm/**",
|
|
21
|
+
"dist/cjs/**",
|
|
22
|
+
"dist/types/**"
|
|
21
23
|
],
|
|
22
|
-
"main": "./
|
|
23
|
-
"
|
|
24
|
+
"main": "./dist/cjs/index.js",
|
|
25
|
+
"module": "./dist/esm/index.js",
|
|
26
|
+
"types": "./dist/types/index.d.ts",
|
|
24
27
|
"exports": {
|
|
25
28
|
".": {
|
|
26
|
-
"types": "./
|
|
27
|
-
"
|
|
29
|
+
"types": "./dist/types/index.d.ts",
|
|
30
|
+
"require": "./dist/cjs/index.js",
|
|
31
|
+
"import": "./dist/esm/index.js",
|
|
32
|
+
"default": "./dist/esm/index.js"
|
|
28
33
|
},
|
|
29
34
|
"./*": {
|
|
30
|
-
"types": "./
|
|
31
|
-
"
|
|
35
|
+
"types": "./dist/types/index.d.ts",
|
|
36
|
+
"require": "./dist/cjs/index.js",
|
|
37
|
+
"import": "./dist/esm/index.js",
|
|
38
|
+
"default": "./dist/esm/index.js"
|
|
32
39
|
},
|
|
33
40
|
"./package.json": "./package.json"
|
|
34
41
|
}
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|