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

Commit

Permalink
refactor: Enhance code (in SqliteDriver and JSONDriver), correct typi…
Browse files Browse the repository at this point in the history
…ng errors, ... (#530)
  • Loading branch information
Αξονας authored Apr 24, 2024
1 parent 94951a3 commit db80e1c
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 24 deletions.
1 change: 1 addition & 0 deletions src/drivers/JSONDriver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
6 changes: 3 additions & 3 deletions src/drivers/PostgresDriver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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];
}

Expand Down Expand Up @@ -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<number> {
Expand All @@ -145,6 +145,6 @@ export class PostgresDriver implements IRemoteDriver {
`DELETE FROM ${table} WHERE id = $1`,
[key]
);
return queryResult.rowCount;
return queryResult.rowCount as number;
}
}
34 changes: 13 additions & 21 deletions src/drivers/SqliteDriver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<T>(
Expand All @@ -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<T>(
Expand Down

0 comments on commit db80e1c

Please sign in to comment.