-
Notifications
You must be signed in to change notification settings - Fork 91
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Also renames database.sqlite to session_db.sqlite (for session storage)
- Loading branch information
1 parent
6bfb7e2
commit b2e5c21
Showing
6 changed files
with
71 additions
and
6 deletions.
There are no files selected for viewing
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import sqlite3 from 'sqlite3'; | ||
|
||
export class QRCodesDB { | ||
qrTableName = "qr_codes"; | ||
db = null; | ||
ready = null; | ||
|
||
constructor(filename) { | ||
this.db = new sqlite3.Database(filename); | ||
this.ready = this.init(); | ||
} | ||
|
||
async hasQrCodesTable() { | ||
const query = ` | ||
SELECT name FROM sqlite_schema | ||
WHERE | ||
type = 'table' AND | ||
name = ?; | ||
`; | ||
const rows = await this.query(query, [this.qrTableName]); | ||
return rows.length === 1; | ||
} | ||
|
||
async init() { | ||
const hasQrCodesTable = await this.hasQrCodesTable(); | ||
if (!hasQrCodesTable) { | ||
const query = ` | ||
CREATE TABLE ${this.qrTableName} ( | ||
id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, | ||
productId VARCHAR(255) NOT NULL, | ||
goToCheckout TINYINT NOT NULL, | ||
discountCode VARCHAR(255) NOT NULL, | ||
hits INTEGER, | ||
conversions INTEGER | ||
) | ||
`; | ||
await this.query(query); | ||
} | ||
} | ||
|
||
query(sql, params = []) { | ||
return new Promise((resolve, reject) => { | ||
this.db.all(sql, params, (err, result) => { | ||
if (err) { | ||
reject(err); | ||
return; | ||
} | ||
resolve(result); | ||
}); | ||
}); | ||
} | ||
} |
Binary file not shown.
File renamed without changes.