diff --git a/.env b/.env new file mode 100644 index 0000000..ac49206 --- /dev/null +++ b/.env @@ -0,0 +1,7 @@ +PG_HOST=db +PG_PORT=5432 +PG_USER=postgres +PG_PASSWORD=changeme +PG_DATABASE=webchat +SESSION_SECRET=changeme +JWT_SECRET=changeme diff --git a/.gitignore b/.gitignore index e72e7f3..cae0190 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,3 @@ node_modules/ .idea -package-lock.json -.env \ No newline at end of file +package-lock.json \ No newline at end of file diff --git a/backend/db.js b/backend/db.js index e08a7d4..fbc294c 100644 --- a/backend/db.js +++ b/backend/db.js @@ -15,24 +15,33 @@ db.connect() .catch((err) => console.log('Error on connecting to database: ', err)); async function createTables() { - await db.query(` - CREATE TABLE IF NOT EXISTS accounts ( - id SERIAL PRIMARY KEY, - username VARCHAR(255) UNIQUE NOT NULL, - password VARCHAR(255) NOT NULL - ) - `); + try { + // Create accounts table + await db.query(` + CREATE TABLE IF NOT EXISTS accounts ( + id SERIAL PRIMARY KEY, + username VARCHAR(255) UNIQUE NOT NULL, + password VARCHAR(255) NOT NULL + ) + `); - // Create messages table - await db.query(` - CREATE TABLE IF NOT EXISTS messages ( - id SERIAL PRIMARY KEY, - content TEXT NOT NULL, - username VARCHAR(50) NOT NULL, - recipient VARCHAR(255) NOT NULL - ) - `); + // Create messages table + await db.query(` + CREATE TABLE IF NOT EXISTS messages ( + id SERIAL PRIMARY KEY, + content TEXT NOT NULL, + username VARCHAR(50) NOT NULL, + recipient VARCHAR(255) NOT NULL + ) + `); + + console.log('Tables created successfully'); + } catch (err) { + console.error('Error creating tables:', err); + throw err; + } } + createTables() .catch((err) => { console.error('Error creating tables:', err);