39 lines
1.2 KiB
TypeScript
39 lines
1.2 KiB
TypeScript
import * as dotenv from 'dotenv';
|
|
|
|
dotenv.config();
|
|
|
|
const mariadb = require('mariadb');
|
|
const pool = mariadb.createPool({
|
|
host: process.env.DB_HOST,
|
|
user: process.env.DB_USER,
|
|
password: process.env.DB_PASSWORD,
|
|
database: process.env.TWITCH_HIGHLIGHTS_DATABASE,
|
|
connectionLimit: 5
|
|
});
|
|
|
|
export const createHighlightEntry = async (req_body: any) => {
|
|
let conn;
|
|
try {
|
|
conn = await pool.getConnection();
|
|
|
|
const streamers = await conn.query('SELECT streamer_id FROM streamers WHERE username = ?', req_body.streamer);
|
|
let streamer_id: number = -1;
|
|
|
|
for (let row in streamers) {
|
|
if (row !== 'meta') {
|
|
streamer_id = streamers[row].streamer_id;
|
|
}
|
|
}
|
|
|
|
const params = [streamer_id, req_body.stream_id, req_body.description, req_body.timestamp, req_body.username, req_body.stream_game];
|
|
|
|
const rows = await conn.query('INSERT INTO highlights (streamer_id, stream_id, description, stream_timestamp, issuing_user, game) VALUES (?, ?, ?, ?, ?, ?)', params);
|
|
} catch (err) {
|
|
throw err;
|
|
} finally {
|
|
if (conn) {
|
|
conn.end();
|
|
}
|
|
}
|
|
}
|