Last active
September 5, 2022 06:55
-
-
Save GautamPanickar/29e06fccb625790f7f57be5ed504c6fe to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import * as Database from 'better-sqlite3'; | |
import { UserInfoDTO } from '../dtos/user-info-dto'; | |
class DatabaseService { | |
private db: Database; | |
constructor() { | |
this.db = new Database('hurray.db', { verbose: console.log }); | |
} | |
public initializeDB(): void { | |
const userInfo = ` | |
CREATE TABLE IF NOT EXISTS | |
UserInfo | |
( | |
id INTEGER PRIMARY KEY, | |
uid LONG, | |
username TEXT, | |
updatedOn LONG | |
) | |
`; | |
this.db.prepare(userInfo).run(); | |
} | |
public readAllUserInfo(): UserInfoDTO[] { | |
const stmt = this.db.prepare(` | |
SELECT id, uid, username | |
FROM UserInfo | |
`); | |
return stmt.all(); | |
} | |
public saveUserInfo(_: UserInfoDTO): void { | |
this.db.prepare(` | |
INSERT INTO | |
UserInfo (uid, username, updatedOn) | |
VALUES (?, ?, ?) | |
`) | |
.run(_.uid, _.username, Date.now()); | |
} | |
public updateUserInfo(_: UserInfoDTO): void { | |
this.db.prepare(` | |
UPDATE | |
UserInfo | |
SET | |
uid = ?, | |
username = ?, | |
updatedOn = ? | |
WHERE | |
id = ? | |
`).run(_.uid, _.username, Date.now(), _.id); | |
} | |
} | |
export default DatabaseService; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment