@acala-network/chopsticks-db 0.8.5-0
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/base-sql.d.ts +15 -0
- package/lib/base-sql.js +69 -0
- package/lib/browser.d.ts +6 -0
- package/lib/browser.js +12 -0
- package/lib/db/browser.d.ts +2 -0
- package/lib/db/browser.js +64 -0
- package/lib/db/entities.d.ts +4 -0
- package/lib/db/entities.js +53 -0
- package/lib/db/index.d.ts +2 -0
- package/lib/db/index.js +49 -0
- package/lib/db/sql-wasm.d.ts +1 -0
- package/lib/db/sql-wasm.js +9 -0
- package/lib/index.d.ts +6 -0
- package/lib/index.js +12 -0
- package/package.json +40 -0
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { BlockEntry, Database, KeyValueEntry } from '@acala-network/chopsticks-core';
|
|
2
|
+
import { DataSource } from 'typeorm';
|
|
3
|
+
import { HexString } from '@polkadot/util/types';
|
|
4
|
+
export declare abstract class BaseSqlDatabase implements Database {
|
|
5
|
+
abstract datasource: Promise<DataSource>;
|
|
6
|
+
close: () => Promise<void>;
|
|
7
|
+
saveBlock: (block: BlockEntry) => Promise<void>;
|
|
8
|
+
queryBlock: (hash: HexString) => Promise<BlockEntry | null>;
|
|
9
|
+
queryBlockByNumber: (number: number) => Promise<BlockEntry | null>;
|
|
10
|
+
queryHighestBlock: () => Promise<BlockEntry | null>;
|
|
11
|
+
deleteBlock: (hash: HexString) => Promise<void>;
|
|
12
|
+
blocksCount: () => Promise<number>;
|
|
13
|
+
saveStorage: (blockHash: HexString, key: HexString, value: HexString | null) => Promise<void>;
|
|
14
|
+
queryStorage: (blockHash: HexString, key: HexString) => Promise<KeyValueEntry | null>;
|
|
15
|
+
}
|
package/lib/base-sql.js
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.BaseSqlDatabase = void 0;
|
|
13
|
+
const entities_1 = require("./db/entities");
|
|
14
|
+
class BaseSqlDatabase {
|
|
15
|
+
constructor() {
|
|
16
|
+
this.close = () => __awaiter(this, void 0, void 0, function* () {
|
|
17
|
+
const db = yield this.datasource;
|
|
18
|
+
yield db.destroy();
|
|
19
|
+
});
|
|
20
|
+
this.saveBlock = (block) => __awaiter(this, void 0, void 0, function* () {
|
|
21
|
+
const db = yield this.datasource;
|
|
22
|
+
const { hash, number, header, extrinsics, parentHash, storageDiff } = block;
|
|
23
|
+
yield db.transaction((transactionalEntityManager) => __awaiter(this, void 0, void 0, function* () {
|
|
24
|
+
yield transactionalEntityManager.getRepository(entities_1.BlockEntity).delete({ number });
|
|
25
|
+
yield transactionalEntityManager.getRepository(entities_1.BlockEntity).upsert({
|
|
26
|
+
hash,
|
|
27
|
+
number,
|
|
28
|
+
header,
|
|
29
|
+
extrinsics,
|
|
30
|
+
parentHash,
|
|
31
|
+
storageDiff,
|
|
32
|
+
}, ['hash']);
|
|
33
|
+
}));
|
|
34
|
+
});
|
|
35
|
+
this.queryBlock = (hash) => __awaiter(this, void 0, void 0, function* () {
|
|
36
|
+
const db = yield this.datasource;
|
|
37
|
+
return db.getRepository(entities_1.BlockEntity).findOne({ where: { hash } });
|
|
38
|
+
});
|
|
39
|
+
this.queryBlockByNumber = (number) => __awaiter(this, void 0, void 0, function* () {
|
|
40
|
+
const db = yield this.datasource;
|
|
41
|
+
return db.getRepository(entities_1.BlockEntity).findOne({ where: { number }, order: { number: 'desc' } });
|
|
42
|
+
});
|
|
43
|
+
this.queryHighestBlock = () => __awaiter(this, void 0, void 0, function* () {
|
|
44
|
+
const db = yield this.datasource;
|
|
45
|
+
return db.getRepository(entities_1.BlockEntity).findOne({ where: {}, order: { number: 'desc' } });
|
|
46
|
+
});
|
|
47
|
+
this.deleteBlock = (hash) => __awaiter(this, void 0, void 0, function* () {
|
|
48
|
+
const db = yield this.datasource;
|
|
49
|
+
yield db.getRepository(entities_1.BlockEntity).delete({ hash });
|
|
50
|
+
});
|
|
51
|
+
this.blocksCount = () => __awaiter(this, void 0, void 0, function* () {
|
|
52
|
+
const db = yield this.datasource;
|
|
53
|
+
return db.getRepository(entities_1.BlockEntity).count();
|
|
54
|
+
});
|
|
55
|
+
this.saveStorage = (blockHash, key, value) => __awaiter(this, void 0, void 0, function* () {
|
|
56
|
+
const db = yield this.datasource;
|
|
57
|
+
yield db.getRepository(entities_1.KeyValuePair).upsert({
|
|
58
|
+
blockHash,
|
|
59
|
+
key,
|
|
60
|
+
value,
|
|
61
|
+
}, ['blockHash', 'key']);
|
|
62
|
+
});
|
|
63
|
+
this.queryStorage = (blockHash, key) => __awaiter(this, void 0, void 0, function* () {
|
|
64
|
+
const db = yield this.datasource;
|
|
65
|
+
return db.getRepository(entities_1.KeyValuePair).findOne({ where: { blockHash, key } });
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
exports.BaseSqlDatabase = BaseSqlDatabase;
|
package/lib/browser.d.ts
ADDED
package/lib/browser.js
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.SqljsDatabase = void 0;
|
|
4
|
+
const base_sql_1 = require("./base-sql");
|
|
5
|
+
const browser_1 = require("./db/browser");
|
|
6
|
+
class SqljsDatabase extends base_sql_1.BaseSqlDatabase {
|
|
7
|
+
constructor(location) {
|
|
8
|
+
super();
|
|
9
|
+
this.datasource = (0, browser_1.openDb)(location);
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
exports.SqljsDatabase = SqljsDatabase;
|
|
@@ -0,0 +1,64 @@
|
|
|
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 __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
26
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
27
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
28
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
29
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
30
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
31
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
32
|
+
});
|
|
33
|
+
};
|
|
34
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
35
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
36
|
+
};
|
|
37
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
38
|
+
exports.openDb = void 0;
|
|
39
|
+
const typeorm_1 = require("typeorm");
|
|
40
|
+
const localforage_1 = require("localforage");
|
|
41
|
+
const sql_js_1 = __importDefault(require("sql.js"));
|
|
42
|
+
const entities = __importStar(require("./entities"));
|
|
43
|
+
const sql_wasm_1 = require("./sql-wasm");
|
|
44
|
+
const openDb = (location) => __awaiter(void 0, void 0, void 0, function* () {
|
|
45
|
+
if (!globalThis.localforage) {
|
|
46
|
+
globalThis.localforage = (0, localforage_1.createInstance)({ name: 'chopsticks' });
|
|
47
|
+
}
|
|
48
|
+
const source = new typeorm_1.DataSource({
|
|
49
|
+
type: 'sqljs',
|
|
50
|
+
location,
|
|
51
|
+
entities: Object.values(entities),
|
|
52
|
+
synchronize: true,
|
|
53
|
+
autoSave: true,
|
|
54
|
+
logging: false,
|
|
55
|
+
useLocalForage: true,
|
|
56
|
+
driver: sql_js_1.default,
|
|
57
|
+
sqlJsConfig: {
|
|
58
|
+
wasmBinary: sql_wasm_1.SQL_WASM_BYTES,
|
|
59
|
+
},
|
|
60
|
+
});
|
|
61
|
+
yield source.initialize();
|
|
62
|
+
return source;
|
|
63
|
+
});
|
|
64
|
+
exports.openDb = openDb;
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.BlockEntity = exports.KeyValuePair = void 0;
|
|
4
|
+
const typeorm_1 = require("typeorm");
|
|
5
|
+
exports.KeyValuePair = new typeorm_1.EntitySchema({
|
|
6
|
+
name: 'KeyValuePair',
|
|
7
|
+
columns: {
|
|
8
|
+
blockHash: {
|
|
9
|
+
primary: true,
|
|
10
|
+
type: 'varchar',
|
|
11
|
+
nullable: false,
|
|
12
|
+
},
|
|
13
|
+
key: {
|
|
14
|
+
primary: true,
|
|
15
|
+
type: 'varchar',
|
|
16
|
+
nullable: false,
|
|
17
|
+
},
|
|
18
|
+
value: {
|
|
19
|
+
type: 'text',
|
|
20
|
+
nullable: true,
|
|
21
|
+
},
|
|
22
|
+
},
|
|
23
|
+
});
|
|
24
|
+
exports.BlockEntity = new typeorm_1.EntitySchema({
|
|
25
|
+
name: 'Block',
|
|
26
|
+
columns: {
|
|
27
|
+
hash: {
|
|
28
|
+
primary: true,
|
|
29
|
+
type: 'varchar',
|
|
30
|
+
nullable: false,
|
|
31
|
+
},
|
|
32
|
+
number: {
|
|
33
|
+
type: 'int',
|
|
34
|
+
nullable: false,
|
|
35
|
+
},
|
|
36
|
+
header: {
|
|
37
|
+
type: 'text',
|
|
38
|
+
nullable: false,
|
|
39
|
+
},
|
|
40
|
+
parentHash: {
|
|
41
|
+
type: 'varchar',
|
|
42
|
+
nullable: true,
|
|
43
|
+
},
|
|
44
|
+
extrinsics: {
|
|
45
|
+
type: 'simple-array',
|
|
46
|
+
nullable: false,
|
|
47
|
+
},
|
|
48
|
+
storageDiff: {
|
|
49
|
+
type: 'simple-json',
|
|
50
|
+
nullable: true,
|
|
51
|
+
},
|
|
52
|
+
},
|
|
53
|
+
});
|
package/lib/db/index.js
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
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 __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
26
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
27
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
28
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
29
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
30
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
31
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
32
|
+
});
|
|
33
|
+
};
|
|
34
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
35
|
+
exports.openDb = void 0;
|
|
36
|
+
const typeorm_1 = require("typeorm");
|
|
37
|
+
const entities = __importStar(require("./entities"));
|
|
38
|
+
const openDb = (dbPath) => __awaiter(void 0, void 0, void 0, function* () {
|
|
39
|
+
const source = new typeorm_1.DataSource({
|
|
40
|
+
type: 'sqlite',
|
|
41
|
+
database: dbPath,
|
|
42
|
+
entities: Object.values(entities),
|
|
43
|
+
synchronize: true,
|
|
44
|
+
logging: false,
|
|
45
|
+
});
|
|
46
|
+
yield source.initialize();
|
|
47
|
+
return source;
|
|
48
|
+
});
|
|
49
|
+
exports.openDb = openDb;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const SQL_WASM_BYTES: Uint8Array;
|