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

Enhance code (in SqliteDriver and JSONDriver), correct typing errors,… #530

Merged
merged 1 commit into from Apr 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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