clawlet 0.5.1 → 0.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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/src/storage.ts +23 -57
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "clawlet",
3
- "version": "0.5.1",
3
+ "version": "0.5.2",
4
4
  "description": "A lightweight AI based personal assistant.",
5
5
  "main": "src/cli.ts",
6
6
  "type": "module",
package/src/storage.ts CHANGED
@@ -104,23 +104,8 @@ export class LibSqlListStorage<T = any> {
104
104
  }
105
105
 
106
106
  async replaceAll(name: string, items: T[]): Promise<void> {
107
- const tx = await this.client.transaction();
108
- try {
109
- await tx.execute({
110
- sql: `DELETE FROM ${this.tableName} WHERE name = ?`,
111
- args: [name],
112
- });
113
- for (const item of items) {
114
- await tx.execute({
115
- sql: `INSERT INTO ${this.tableName} (name, item) VALUES (?, ?)`,
116
- args: [name, JSON.stringify(item)]
117
- });
118
- }
119
- await tx.commit();
120
- } catch (e) {
121
- await tx.rollback();
122
- throw e;
123
- }
107
+ this.clear(name);
108
+ this.pushMany(name, items);
124
109
  }
125
110
 
126
111
  async getAll(name: string): Promise<T[]> {
@@ -181,19 +166,9 @@ export class LibSqlFiFoStorage<T> {
181
166
  }
182
167
 
183
168
  public async pushMany(queue: string, items: T[]): Promise<void> {
184
- const tx = await this.client.transaction();
185
- try {
186
- for (const item of items) {
187
- await tx.execute({
188
- sql: `INSERT INTO ${this.tableName} (queue_name, value) VALUES (?, ?)`,
189
- args: [queue, JSON.stringify(item)],
190
- });
191
- }
192
- await tx.commit();
193
- } catch (e) {
194
- await tx.rollback();
195
- throw e;
196
- }
169
+ for (const item of items) {
170
+ this.push(queue, item);
171
+ }
197
172
  }
198
173
 
199
174
  public async empty(queue: string): Promise<boolean> {
@@ -201,33 +176,24 @@ export class LibSqlFiFoStorage<T> {
201
176
  }
202
177
 
203
178
  public async pop(queue: string): Promise<T | null> {
204
- const tx = await this.client.transaction();
205
- try {
206
- const rs = await tx.execute({
207
- sql: `SELECT id, value FROM ${this.tableName} WHERE queue_name = ? ORDER BY id ASC LIMIT 1`,
208
- args: [queue],
209
- });
210
-
211
- if (rs.rows.length === 0) {
212
- await tx.commit();
213
- return null;
214
- }
215
-
216
- const row = rs.rows[0] as any;
217
- const id = row.id;
218
-
219
- await tx.execute({
220
- sql: `DELETE FROM ${this.tableName} WHERE id = ?`,
221
- args: [id!],
222
- });
223
-
224
- await tx.commit();
225
-
226
- return JSON.parse(row.value as string) as T;
227
- } catch (e) {
228
- await tx.rollback();
229
- throw e;
230
- }
179
+ const rs = await this.client.execute({
180
+ sql: `SELECT id, value FROM ${this.tableName} WHERE queue_name = ? ORDER BY id ASC LIMIT 1`,
181
+ args: [queue],
182
+ });
183
+
184
+ if (rs.rows.length === 0) {
185
+ return null;
186
+ }
187
+
188
+ const row = rs.rows[0] as any;
189
+ const id = row.id;
190
+
191
+ await this.client.execute({
192
+ sql: `DELETE FROM ${this.tableName} WHERE id = ?`,
193
+ args: [id!],
194
+ });
195
+
196
+ return JSON.parse(row.value as string) as T;
231
197
  }
232
198
 
233
199
  public async count(queue: string): Promise<number> {