code refactor of creating tables
This commit is contained in:
@@ -9,24 +9,24 @@ const client = new Client({
|
||||
port: process.env.PG_PORT,
|
||||
});
|
||||
|
||||
async function initializeDatabaseConnection() {
|
||||
await client
|
||||
.connect()
|
||||
.then(() =>
|
||||
console.log(
|
||||
`Successfully connected to database: ${process.env.PG_DATABASE}`,
|
||||
),
|
||||
)
|
||||
.catch((err) =>
|
||||
console.error(
|
||||
`Failed to connect to database: ${process.env.PG_DATABASE}, ${err}`,
|
||||
),
|
||||
client
|
||||
.connect()
|
||||
.then(() => {
|
||||
createTables()
|
||||
.then(() => console.log("Tables created successfully"))
|
||||
.catch((e) => console.error("Failed to create tables ", e));
|
||||
console.log(
|
||||
`Successfully connected to database: ${process.env.PG_DATABASE}`,
|
||||
);
|
||||
}
|
||||
})
|
||||
.catch((err) =>
|
||||
console.error(
|
||||
`Failed to connect to database: ${process.env.PG_DATABASE}, ${err}`,
|
||||
),
|
||||
);
|
||||
|
||||
async function createTables() {
|
||||
try {
|
||||
await client.query(`
|
||||
await client.query(`
|
||||
CREATE TABLE IF NOT EXISTS accounts (
|
||||
username VARCHAR(20),
|
||||
password VARCHAR(128),
|
||||
@@ -35,7 +35,7 @@ async function createTables() {
|
||||
);
|
||||
`);
|
||||
|
||||
await client.query(`
|
||||
await client.query(`
|
||||
CREATE TABLE IF NOT EXISTS messages (
|
||||
sender_id VARCHAR(128),
|
||||
receiver_id VARCHAR(128),
|
||||
@@ -43,12 +43,7 @@ async function createTables() {
|
||||
created_at VARCHAR(100)
|
||||
);
|
||||
`);
|
||||
} catch (e) {
|
||||
console.error("Failed to create tables ", e);
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
initializeDatabaseConnection,
|
||||
createTables,
|
||||
client,
|
||||
};
|
||||
|
||||
@@ -7,7 +7,7 @@ const io = new Server(server);
|
||||
require("dotenv").config();
|
||||
const PORT = process.env.SERVER_PORT;
|
||||
|
||||
const { connectDB } = require("./db/db.js");
|
||||
const { initializeDatabaseConnection, client } = require("./db/db.js");
|
||||
|
||||
app.use("/socket.io", express.static("./node_modules/socket.io/client-dist/"));
|
||||
const corsOptions = {
|
||||
@@ -18,10 +18,11 @@ app.get("/api/signup", (req, res) => {
|
||||
res.send("niger");
|
||||
});
|
||||
|
||||
app.listen(PORT, corsOptions, () => {
|
||||
console.log(`Server is running on port: ${PORT}`);
|
||||
});
|
||||
|
||||
io.on("connection", (socket) => {
|
||||
console.log(`User: ${socket.id} just connected`);
|
||||
});
|
||||
|
||||
//initializeDatabaseConnection();
|
||||
app.listen(PORT, corsOptions, () => {
|
||||
console.log(`Server is running on port: ${PORT}`);
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user