Skip to content
This repository was archived by the owner on Jan 1, 2025. It is now read-only.

Commit db80e1c

Browse files
author
Αξονας
authored
refactor: Enhance code (in SqliteDriver and JSONDriver), correct typing errors, ... (#530)
1 parent 94951a3 commit db80e1c

File tree

3 files changed

+17
-24
lines changed

3 files changed

+17
-24
lines changed

src/drivers/JSONDriver.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ export class JSONDriver extends MemoryDriver {
3333
const contents = readFileSync(this.path, { encoding: "utf-8" });
3434

3535
try {
36+
3637
const data = JSON.parse(contents);
3738
for (const table in data) {
3839
const store = this.getOrCreateTable(table);

src/drivers/PostgresDriver.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ export class PostgresDriver implements IRemoteDriver {
102102
[key]
103103
);
104104

105-
if (queryResult.rowCount < 1) return [null, false];
105+
if (Number(queryResult.rowCount) < 1) return [null, false];
106106
return [JSON.parse(queryResult.rows[0].value), true];
107107
}
108108

@@ -135,7 +135,7 @@ export class PostgresDriver implements IRemoteDriver {
135135
this.checkConnection();
136136

137137
const queryResult = await this.conn!.query(`DELETE FROM ${table}`);
138-
return queryResult.rowCount;
138+
return queryResult.rowCount as number;
139139
}
140140

141141
public async deleteRowByKey(table: string, key: string): Promise<number> {
@@ -145,6 +145,6 @@ export class PostgresDriver implements IRemoteDriver {
145145
`DELETE FROM ${table} WHERE id = $1`,
146146
[key]
147147
);
148-
return queryResult.rowCount;
148+
return queryResult.rowCount as number;
149149
}
150150
}

src/drivers/SqliteDriver.ts

Lines changed: 13 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -43,16 +43,12 @@ export class SqliteDriver implements IDriver {
4343
const prep = this._database.prepare<{ ID: string; json: string }[]>(
4444
`SELECT * FROM ${table}`
4545
);
46-
const data = [];
47-
48-
for (const row of prep.iterate()) {
49-
data.push({
50-
id: (row as { ID: string; json: string }).ID,
51-
value: JSON.parse((row as { ID: string; json: string }).json),
52-
});
53-
}
54-
55-
return data;
46+
return (prep.all() as { ID: string, json: string }[])
47+
.map(row => ({
48+
id: row.ID,
49+
value: JSON.parse(row.json),
50+
})
51+
);
5652
}
5753

5854
public async getRowByKey<T>(
@@ -73,17 +69,13 @@ export class SqliteDriver implements IDriver {
7369
const prep = this._database.prepare<{ ID: string; json: string }[]>(
7470
`SELECT * FROM ${table} WHERE ID LIKE '${query}%'`
7571
);
76-
77-
const data = [];
78-
79-
for (const row of prep.iterate()) {
80-
data.push({
81-
id: (row as { ID: string; json: string }).ID,
82-
value: JSON.parse((row as { id: string; json: string }).json),
83-
});
84-
}
85-
86-
return data;
72+
73+
return (prep.all() as { ID: string, json: string }[])
74+
.map(row => ({
75+
id: row.ID,
76+
value: JSON.parse(row.json),
77+
})
78+
);
8779
}
8880

8981
public async setRowByKey<T>(

0 commit comments

Comments
 (0)