From 56a7f0b6a1923b1d98a3516e47a8b7bad7321019 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=CE=91=CE=BE=CE=BF=CE=BD=CE=B1=CF=82?= Date: Sun, 21 Apr 2024 10:08:08 +0200 Subject: [PATCH] Enhance code (in SqliteDriver and JSONDriver), correct typing errors, ... --- src/drivers/JSONDriver.ts | 1 + src/drivers/PostgresDriver.ts | 6 +++--- src/drivers/SqliteDriver.ts | 34 +++++++++++++--------------------- 3 files changed, 17 insertions(+), 24 deletions(-) diff --git a/src/drivers/JSONDriver.ts b/src/drivers/JSONDriver.ts index 4c764d21..5a409147 100644 --- a/src/drivers/JSONDriver.ts +++ b/src/drivers/JSONDriver.ts @@ -33,6 +33,7 @@ export class JSONDriver extends MemoryDriver { const contents = readFileSync(this.path, { encoding: "utf-8" }); try { + const data = JSON.parse(contents); for (const table in data) { const store = this.getOrCreateTable(table); diff --git a/src/drivers/PostgresDriver.ts b/src/drivers/PostgresDriver.ts index cec8f097..9931c9fd 100644 --- a/src/drivers/PostgresDriver.ts +++ b/src/drivers/PostgresDriver.ts @@ -102,7 +102,7 @@ export class PostgresDriver implements IRemoteDriver { [key] ); - if (queryResult.rowCount < 1) return [null, false]; + if (Number(queryResult.rowCount) < 1) return [null, false]; return [JSON.parse(queryResult.rows[0].value), true]; } @@ -135,7 +135,7 @@ export class PostgresDriver implements IRemoteDriver { this.checkConnection(); const queryResult = await this.conn!.query(`DELETE FROM ${table}`); - return queryResult.rowCount; + return queryResult.rowCount as number; } public async deleteRowByKey(table: string, key: string): Promise { @@ -145,6 +145,6 @@ export class PostgresDriver implements IRemoteDriver { `DELETE FROM ${table} WHERE id = $1`, [key] ); - return queryResult.rowCount; + return queryResult.rowCount as number; } } diff --git a/src/drivers/SqliteDriver.ts b/src/drivers/SqliteDriver.ts index b82c5b85..527f84c1 100644 --- a/src/drivers/SqliteDriver.ts +++ b/src/drivers/SqliteDriver.ts @@ -43,16 +43,12 @@ export class SqliteDriver implements IDriver { const prep = this._database.prepare<{ ID: string; json: string }[]>( `SELECT * FROM ${table}` ); - const data = []; - - for (const row of prep.iterate()) { - data.push({ - id: (row as { ID: string; json: string }).ID, - value: JSON.parse((row as { ID: string; json: string }).json), - }); - } - - return data; + return (prep.all() as { ID: string, json: string }[]) + .map(row => ({ + id: row.ID, + value: JSON.parse(row.json), + }) + ); } public async getRowByKey( @@ -73,17 +69,13 @@ export class SqliteDriver implements IDriver { const prep = this._database.prepare<{ ID: string; json: string }[]>( `SELECT * FROM ${table} WHERE ID LIKE '${query}%'` ); - - const data = []; - - for (const row of prep.iterate()) { - data.push({ - id: (row as { ID: string; json: string }).ID, - value: JSON.parse((row as { id: string; json: string }).json), - }); - } - - return data; + + return (prep.all() as { ID: string, json: string }[]) + .map(row => ({ + id: row.ID, + value: JSON.parse(row.json), + }) + ); } public async setRowByKey(