fixed creating tables is not exist
This commit is contained in:
@@ -14,6 +14,29 @@ db.connect()
|
||||
.then(() => console.log('Successfully connected to database'))
|
||||
.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
|
||||
)
|
||||
`);
|
||||
|
||||
// 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
|
||||
)
|
||||
`);
|
||||
}
|
||||
createTables()
|
||||
.catch((err) => {
|
||||
console.error('Error creating tables:', err);
|
||||
});
|
||||
|
||||
// function for checking if user exists
|
||||
async function isUserExists(username) {
|
||||
|
||||
@@ -4,28 +4,6 @@ const jwtSecret = process.env.JWT_SECRET;
|
||||
const { db } = require('./db.js');
|
||||
|
||||
function initializeSocket(server) {
|
||||
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,
|
||||
email VARCHAR(255) UNIQUE NOT NULL,
|
||||
)
|
||||
`);
|
||||
|
||||
// Create messages table
|
||||
await db.query(`
|
||||
CREATE TABLE IF NOT EXISTS messages (
|
||||
id SERIAL PRIMARY KEY,
|
||||
content TEXT NOT NULL,
|
||||
username VARCHAR(255) NOT NULL,
|
||||
recipient VARCHAR(255) NOT NULL,
|
||||
)
|
||||
`);
|
||||
|
||||
}
|
||||
|
||||
const io = new Server(server, {
|
||||
cookie: {
|
||||
httpOnly: true,
|
||||
@@ -51,10 +29,6 @@ function initializeSocket(server) {
|
||||
});
|
||||
|
||||
io.on('connection', (socket) => {
|
||||
createTables()
|
||||
.catch((err) => {
|
||||
console.error('Error creating tables:', err);
|
||||
});
|
||||
|
||||
if (!socket.user) {
|
||||
console.log('User not authenticated');
|
||||
@@ -129,7 +103,7 @@ function initializeSocket(server) {
|
||||
|
||||
if (result.rows.length > 0) {
|
||||
//const { username: sender, recipient: receiver, content: content, id: id } = result.rows;
|
||||
console.log('Sending messages:', result.rows);
|
||||
console.log('Sending historical messages:', result.rows);
|
||||
socket.emit('messages history', result.rows);
|
||||
} else {
|
||||
io.emit('no messages', );
|
||||
|
||||
12
compose.yaml
12
compose.yaml
@@ -6,6 +6,12 @@ services:
|
||||
context: .
|
||||
environment:
|
||||
NODE_ENV: production
|
||||
PG_HOST: ${PG_HOST}
|
||||
PG_USER: ${PG_USER}
|
||||
PG_DATABASE: ${PG_DATABASE}
|
||||
PG_PASSWORD: ${PG_PASSWORD}
|
||||
SESSION_SECRET: ${SESSION_SECRET}
|
||||
JWT_SECRET: ${JWT_SECRET}
|
||||
ports:
|
||||
- "3000:3000"
|
||||
depends_on:
|
||||
@@ -18,9 +24,9 @@ services:
|
||||
volumes:
|
||||
- db-data:/var/lib/postgresql/data
|
||||
environment:
|
||||
POSTGRES_USER: postgres
|
||||
POSTGRES_DB: webchat
|
||||
POSTGRES_PASSWORD: str0ngP@ssword
|
||||
POSTGRES_USER: ${PG_USER}
|
||||
POSTGRES_DB: ${PG_DATABASE}
|
||||
POSTGRES_PASSWORD: ${PG_PASSWORD}
|
||||
expose:
|
||||
- "5432"
|
||||
healthcheck:
|
||||
|
||||
Reference in New Issue
Block a user