@peerbit/indexer-sqlite3 1.0.7 → 1.1.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.
@@ -35,17 +35,18 @@ class Statement implements IStatement {
35
35
 
36
36
  async bind(values: any[]) {
37
37
  await this.statement.bind(values);
38
- return this;
38
+ return this as IStatement;
39
39
  }
40
40
 
41
41
  async finalize() {
42
- if ((await this.statement.finalize()) > 0) {
42
+ const out = await this.statement.finalize();
43
+ if (out != null && out > 0) {
43
44
  throw new Error("Error finalizing statement");
44
45
  }
45
46
  }
46
47
 
47
48
  get(values?: BindableValue[]) {
48
- if (values) {
49
+ if (values && values?.length > 0) {
49
50
  this.statement.bind(values);
50
51
  }
51
52
  let step = this.statement.step();
@@ -66,7 +67,7 @@ class Statement implements IStatement {
66
67
 
67
68
  async reset() {
68
69
  await this.statement.reset();
69
- return this;
70
+ return this as IStatement;
70
71
  }
71
72
 
72
73
  all(values: BindableValue[]) {
@@ -92,7 +93,7 @@ const log = (...args: any) => console.log(...args);
92
93
  // eslint-disable-next-line no-console
93
94
  const error = (...args: any) => console.error(...args);
94
95
 
95
- let poolUtil: SAHPoolUtil = undefined;
96
+ let poolUtil: SAHPoolUtil | undefined = undefined;
96
97
  let sqlite3: Awaited<ReturnType<typeof sqlite3InitModule>> | undefined =
97
98
  undefined;
98
99
 
@@ -122,14 +123,14 @@ const create = async (directory?: string) => {
122
123
 
123
124
  poolUtil =
124
125
  poolUtil ||
125
- (await sqlite3.installOpfsSAHPoolVfs({
126
+ (await sqlite3!.installOpfsSAHPoolVfs({
126
127
  directory: "peerbit/sqlite", // encodeName("peerbit")
127
128
  }));
128
129
 
129
130
  await poolUtil.reserveMinimumCapacity(100);
130
131
  sqliteDb = new poolUtil.OpfsSAHPoolDb(dbFileName);
131
132
  } else {
132
- sqliteDb = new sqlite3.oo1.DB(":memory:");
133
+ sqliteDb = new sqlite3!.oo1.DB(":memory:");
133
134
  }
134
135
 
135
136
  sqliteDb.exec("PRAGMA journal_mode = WAL");
@@ -139,24 +140,30 @@ const create = async (directory?: string) => {
139
140
  return {
140
141
  close,
141
142
  exec: (sql: string) => {
142
- return sqliteDb.exec(sql);
143
+ return sqliteDb!.exec(sql);
143
144
  },
144
145
  open,
145
- prepare: (sql: string, id?: string) => {
146
+ prepare: async (sql: string, id?: string) => {
146
147
  if (id == null) {
147
148
  id = uuid();
148
149
  }
149
- const statement = sqliteDb.prepare(sql);
150
+ let prev = statements.get(id);
151
+ if (prev) {
152
+ await prev.reset();
153
+ return prev;
154
+ }
155
+
156
+ const statement = sqliteDb!.prepare(sql);
150
157
  const wrappedStatement = new Statement(statement, id);
151
158
  statements.set(id, wrappedStatement);
152
159
  return wrappedStatement;
153
160
  },
154
161
  get(sql: string) {
155
- return sqliteDb.exec({ sql, rowMode: "array" });
162
+ return sqliteDb!.exec({ sql, rowMode: "array" });
156
163
  },
157
164
 
158
165
  run(sql: string, bind: any[]) {
159
- return sqliteDb.exec(sql, { bind, rowMode: "array" });
166
+ return sqliteDb!.exec(sql, { bind, rowMode: "array" });
160
167
  },
161
168
  status: () => (sqliteDb?.isOpen() ? "open" : "closed"),
162
169
  statements,
package/src/types.ts CHANGED
@@ -9,13 +9,18 @@ export type Database = {
9
9
  prepare: (sql: string, id?: string) => Promise<Statement> | Statement;
10
10
  close: (err?: (err: any) => any) => Promise<any> | any;
11
11
  open(): Promise<any> | any;
12
+ statements: {
13
+ get: (id: string) => Statement | undefined;
14
+ size: number;
15
+ };
12
16
  status: () => Promise<"open" | "closed"> | "open" | "closed";
13
17
  };
14
18
 
15
- export type StatementGetResult = { [key: string]: SQLLiteValue };
19
+ export type StatementGetResult = { [key: string]: SQLLiteValue } | undefined;
16
20
 
17
21
  export type Statement = {
18
22
  id: string;
23
+
19
24
  bind: (
20
25
  values: BindableValue[],
21
26
  err?: (err: any) => any,