@acala-network/chopsticks-db 0.8.5-1 → 0.8.5-2

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 CHANGED
@@ -4,12 +4,12 @@ import { HexString } from '@polkadot/util/types';
4
4
  export declare abstract class BaseSqlDatabase implements Database {
5
5
  abstract datasource: Promise<DataSource>;
6
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>;
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
15
  }
package/lib/base-sql.js CHANGED
@@ -1,69 +1,60 @@
1
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
2
  Object.defineProperty(exports, "__esModule", { value: true });
12
3
  exports.BaseSqlDatabase = void 0;
13
4
  const entities_1 = require("./db/entities");
14
5
  class BaseSqlDatabase {
15
6
  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 } });
7
+ this.close = async () => {
8
+ const db = await this.datasource;
9
+ await db.destroy();
10
+ };
11
+ }
12
+ async saveBlock(block) {
13
+ const db = await this.datasource;
14
+ const { hash, number, header, extrinsics, parentHash, storageDiff } = block;
15
+ await db.transaction(async (transactionalEntityManager) => {
16
+ await transactionalEntityManager.getRepository(entities_1.BlockEntity).delete({ number });
17
+ await transactionalEntityManager.getRepository(entities_1.BlockEntity).upsert({
18
+ hash,
19
+ number,
20
+ header,
21
+ extrinsics,
22
+ parentHash,
23
+ storageDiff,
24
+ }, ['hash']);
66
25
  });
67
26
  }
27
+ async queryBlock(hash) {
28
+ const db = await this.datasource;
29
+ return db.getRepository(entities_1.BlockEntity).findOne({ where: { hash } });
30
+ }
31
+ async queryBlockByNumber(number) {
32
+ const db = await this.datasource;
33
+ return db.getRepository(entities_1.BlockEntity).findOne({ where: { number }, order: { number: 'desc' } });
34
+ }
35
+ async queryHighestBlock() {
36
+ const db = await this.datasource;
37
+ return db.getRepository(entities_1.BlockEntity).findOne({ where: {}, order: { number: 'desc' } });
38
+ }
39
+ async deleteBlock(hash) {
40
+ const db = await this.datasource;
41
+ await db.getRepository(entities_1.BlockEntity).delete({ hash });
42
+ }
43
+ async blocksCount() {
44
+ const db = await this.datasource;
45
+ return db.getRepository(entities_1.BlockEntity).count();
46
+ }
47
+ async saveStorage(blockHash, key, value) {
48
+ const db = await this.datasource;
49
+ await db.getRepository(entities_1.KeyValuePair).upsert({
50
+ blockHash,
51
+ key,
52
+ value,
53
+ }, ['blockHash', 'key']);
54
+ }
55
+ async queryStorage(blockHash, key) {
56
+ const db = await this.datasource;
57
+ return db.getRepository(entities_1.KeyValuePair).findOne({ where: { blockHash, key } });
58
+ }
68
59
  }
69
60
  exports.BaseSqlDatabase = BaseSqlDatabase;
package/lib/db/browser.js CHANGED
@@ -22,15 +22,6 @@ var __importStar = (this && this.__importStar) || function (mod) {
22
22
  __setModuleDefault(result, mod);
23
23
  return result;
24
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
25
  var __importDefault = (this && this.__importDefault) || function (mod) {
35
26
  return (mod && mod.__esModule) ? mod : { "default": mod };
36
27
  };
@@ -41,7 +32,7 @@ const localforage_1 = require("localforage");
41
32
  const sql_js_1 = __importDefault(require("sql.js"));
42
33
  const entities = __importStar(require("./entities"));
43
34
  const sql_wasm_1 = require("./sql-wasm");
44
- const openDb = (location) => __awaiter(void 0, void 0, void 0, function* () {
35
+ const openDb = async (location) => {
45
36
  if (!globalThis.localforage) {
46
37
  globalThis.localforage = (0, localforage_1.createInstance)({ name: 'chopsticks' });
47
38
  }
@@ -58,7 +49,7 @@ const openDb = (location) => __awaiter(void 0, void 0, void 0, function* () {
58
49
  wasmBinary: sql_wasm_1.SQL_WASM_BYTES,
59
50
  },
60
51
  });
61
- yield source.initialize();
52
+ await source.initialize();
62
53
  return source;
63
- });
54
+ };
64
55
  exports.openDb = openDb;
@@ -1,4 +1,4 @@
1
- import { BlockEntry, KeyValueEntry } from '@acala-network/chopsticks-core/src/database';
1
+ import { BlockEntry, KeyValueEntry } from '@acala-network/chopsticks-core';
2
2
  import { EntitySchema } from 'typeorm';
3
3
  export declare const KeyValuePair: EntitySchema<KeyValueEntry>;
4
4
  export declare const BlockEntity: EntitySchema<BlockEntry>;
package/lib/db/index.js CHANGED
@@ -22,28 +22,22 @@ var __importStar = (this && this.__importStar) || function (mod) {
22
22
  __setModuleDefault(result, mod);
23
23
  return result;
24
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
25
  Object.defineProperty(exports, "__esModule", { value: true });
35
26
  exports.openDb = void 0;
36
27
  const typeorm_1 = require("typeorm");
37
28
  const entities = __importStar(require("./entities"));
38
- const openDb = (dbPath) => __awaiter(void 0, void 0, void 0, function* () {
29
+ const openDb = async (dbPath) => {
39
30
  const source = new typeorm_1.DataSource({
40
31
  type: 'sqlite',
41
32
  database: dbPath,
42
33
  entities: Object.values(entities),
43
34
  synchronize: true,
44
35
  logging: false,
36
+ enableWAL: true,
37
+ busyErrorRetry: 1000,
38
+ busyTimeout: 5000, // retry for 5 seconds, sqlite PRAGMA busy_timeout
45
39
  });
46
- yield source.initialize();
40
+ await source.initialize();
47
41
  return source;
48
- });
42
+ };
49
43
  exports.openDb = openDb;
package/package.json CHANGED
@@ -1,16 +1,17 @@
1
1
  {
2
2
  "name": "@acala-network/chopsticks-db",
3
- "version": "0.8.5-1",
3
+ "version": "0.8.5-2",
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
8
  "pack-wasm": "scripts/pack-wasm.cjs",
9
- "build": "yarn pack-wasm; tsc -p ./tsconfig.json",
10
- "docs:prep": "typedoc"
9
+ "build": "yarn pack-wasm; tsc -p ./tsconfig.json"
11
10
  },
12
11
  "dependencies": {
13
- "@acala-network/chopsticks-core": "0.8.5-1",
12
+ "@acala-network/chopsticks-core": "0.8.5-2",
13
+ "@polkadot/util": "^12.5.1",
14
+ "@polkadot/wasm-util": "^7.2.2",
14
15
  "localforage": "^1.10.0",
15
16
  "sql.js": "^1.8.0",
16
17
  "sqlite3": "^5.1.6",