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