brainz-social-old/database-handler.js

47 lines
2 KiB
JavaScript
Raw Normal View History

2023-09-27 07:03:36 -05:00
const db = require('better-sqlite3')('brainz-social.db');
db.pragma('journal_mode = WAL');
export default {
db,
getConfig: (key) => {
const row = db.prepare('SELECT * FROM config WHERE key = ?').get(key);
return row.value;
},
setConfig: (key, value) => {
db.prepare("INSERT OR REPLACE INTO config (key, value) VALUES(?, ?);").run(key, value);
},
createApplication: (client_name, redirect_uri, scopes, website, client_id, client_secret) => {
db.prepare("INSERT INTO applications (client_name, redirect_uri, scopes, website, client_id, client_secret) VALUES (?, ?, ?, ?, ?, ?);").run(client_name, redirect_uri, scopes, website, client_id, client_secret);
},
getApplication: (client_id) => {
return db.prepare("SELECT id, client_id, client_secret, redirect_uri, scopes, website FROM applications WHERE client_id = ?").get(client_id);
},
getApplicationById: (id) => {
return db.prepare("SELECT id, client_id, client_secret, redirect_uri, scopes, website FROM applications WHERE id = ?").get(id);
},
createToken: (token, scope, application_id, user_id, created_at) => {
db.prepare("INSERT INTO oauth_tokens (token, scope, application_id, user_id, created_at, revoked) VALUES (?, ?, ?, ?, ?, false)").run(token, scope, application_id, user_id, created_at);
},
getTokenData: (token) => {
return db.prepare("SELECT application_id, user_id, created_at, revoked FROM oauth_tokens WHERE token = ?").get(token);
2023-09-27 19:29:55 -05:00
},
revokeToken: (token) => {
db.prepare("UPDATE oauth_tokens SET revoked = true WHERE token = ?").run(token);
2023-09-28 18:36:35 -05:00
},
getAccountByUsername: (username) => {
return db.prepare("SELECT id, username, email, password_hash, account_tier FROM accounts WHERE username = ?").get(username);
},
createAccount: (username, email, password_hash) => {
db.prepare("INSERT INTO accounts (username, email, password_hash, account_tier) VALUES (?, ?, ?, 0)").run(username, email, password_hash);
2023-09-27 07:03:36 -05:00
}
};