update database layout
This commit is contained in:
@@ -4,7 +4,7 @@
|
|||||||
"description": "",
|
"description": "",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"author": "",
|
"author": "",
|
||||||
"type": "commonjs",
|
"type": "module",
|
||||||
"main": "src/index.js",
|
"main": "src/index.js",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"start": "node src/index.js"
|
"start": "node src/index.js"
|
||||||
|
|||||||
@@ -1,15 +1,15 @@
|
|||||||
// copied from one of my older projects
|
// copied from one of my older projects
|
||||||
const mysql = require('mysql2/promise');
|
import { createPool } from 'mysql2/promise';
|
||||||
|
|
||||||
function getEnvVar(name) {
|
function getEnvVar(name) {
|
||||||
const value = process.env[name];
|
const value = process.env[name];
|
||||||
if (value === null) {
|
if (value == null) {
|
||||||
throw new Error(`Environment variable ${name} is not defined`);
|
throw new Error(`Environment variable ${name} is not defined`);
|
||||||
}
|
}
|
||||||
return value;
|
return value;
|
||||||
}
|
}
|
||||||
|
|
||||||
const pool = mysql.createPool({
|
const pool = createPool({
|
||||||
host: getEnvVar('MYSQL_HOST'),
|
host: getEnvVar('MYSQL_HOST'),
|
||||||
user: getEnvVar('MYSQL_USER'),
|
user: getEnvVar('MYSQL_USER'),
|
||||||
password: getEnvVar('MYSQL_PASSWORD'),
|
password: getEnvVar('MYSQL_PASSWORD'),
|
||||||
@@ -18,13 +18,44 @@ const pool = mysql.createPool({
|
|||||||
|
|
||||||
const migrations = [
|
const migrations = [
|
||||||
{
|
{
|
||||||
name: "init_dart_table",
|
name: "init",
|
||||||
up: async (conn) => {
|
up: async (conn) => {
|
||||||
await conn.query(`
|
await conn.query(`
|
||||||
CREATE TABLE dart (
|
CREATE TABLE IF NOT EXISTS users (
|
||||||
id INT PRIMARY KEY AUTO_INCREMENT
|
id INT PRIMARY KEY AUTO_INCREMENT,
|
||||||
|
name VARCHAR(255) NOT NULL
|
||||||
)
|
)
|
||||||
`);
|
`);
|
||||||
|
|
||||||
|
await conn.query(`
|
||||||
|
CREATE TABLE IF NOT EXISTS games (
|
||||||
|
id INT PRIMARY KEY AUTO_INCREMENT,
|
||||||
|
name VARCHAR(255) NOT NULL UNIQUE
|
||||||
|
)
|
||||||
|
`);
|
||||||
|
|
||||||
|
await conn.query(`
|
||||||
|
CREATE TABLE IF NOT EXISTS scores (
|
||||||
|
id INT PRIMARY KEY AUTO_INCREMENT,
|
||||||
|
user_id INT NOT NULL,
|
||||||
|
game_id INT NOT NULL,
|
||||||
|
score INT NOT NULL,
|
||||||
|
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||||||
|
FOREIGN KEY (user_id) REFERENCES users(id),
|
||||||
|
FOREIGN KEY (game_id) REFERENCES games(id),
|
||||||
|
UNIQUE(user_id, game_id)
|
||||||
|
)
|
||||||
|
`);
|
||||||
|
|
||||||
|
await conn.query(`
|
||||||
|
CREATE INDEX IF NOT EXISTS idx_scores_game_score
|
||||||
|
ON scores(game_id, score DESC)
|
||||||
|
`);
|
||||||
|
|
||||||
|
await pool.query(
|
||||||
|
`INSERT INTO games (name) VALUES (?) ON DUPLICATE KEY UPDATE id=id`,
|
||||||
|
['darts']
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
];
|
];
|
||||||
@@ -64,7 +95,7 @@ async function initDB() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = {
|
export {
|
||||||
pool,
|
pool,
|
||||||
initDB
|
initDB
|
||||||
};
|
};
|
||||||
@@ -1,4 +1,5 @@
|
|||||||
require('dotenv').config()
|
import dotenv from 'dotenv';
|
||||||
const db = require('./db');
|
dotenv.config();
|
||||||
|
const db = await import('./db.js');
|
||||||
|
|
||||||
db.initDB();
|
db.initDB();
|
||||||
Reference in New Issue
Block a user