Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
106 changes: 106 additions & 0 deletions Week1/assignment/exer1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
const { Client } = require("pg");

const DB_USER = "hyfuser";
const DB_PASSWORD = "hyfpassword";
const DB_HOST = "localhost";
const DB_PORT = 5432;

const baseConfig = {
host: DB_HOST,
port: DB_PORT,
user: DB_USER,
password: DB_PASSWORD,
};

async function withClient(database, handler) {
const client = new Client({ ...baseConfig, database });
await client.connect();

try {
return await handler(client);
} finally {
await client.end();
}
}

async function setupMeetup() {
try {
// 1) Drop & recreate database meetup
await withClient("postgres", async (client) => {
console.log("Recreating database meetup...");

await client.query("DROP DATABASE IF EXISTS meetup;");
await client.query("CREATE DATABASE meetup;");
});


await withClient("meetup", async (db) => {
console.log("Creating tables...");

await db.query(`
CREATE TABLE Invitee (
invitee_no SERIAL PRIMARY KEY,
invitee_name VARCHAR(80) NOT NULL,
invited_by VARCHAR(80) NOT NULL
);
`);

await db.query(`
CREATE TABLE Room (
room_no SERIAL PRIMARY KEY,
room_name VARCHAR(80) NOT NULL,
floor_number INTEGER NOT NULL
);
`);

await db.query(`
CREATE TABLE Meeting (
meeting_no SERIAL PRIMARY KEY,
meeting_title VARCHAR(120) NOT NULL,
starting_time TIMESTAMP NOT NULL,
ending_time TIMESTAMP NOT NULL,
room_no INTEGER REFERENCES Room(room_no)
);
`);

console.log("Inserting sample data into Invitee...");
await db.query(`
INSERT INTO Invitee (invitee_name, invited_by) VALUES
('Karim', 'Majd'),
('Rim', 'Karim'),
('Alaa', 'Karim'),
('Ivan', 'Majd'),
('Dima', 'Ivan');
`);

console.log("Inserting sample data into Room...");
await db.query(`
INSERT INTO Room (room_name, floor_number) VALUES
('Alfa Room', 1),
('Atlas Room', 1),
('Nova Room', 2),
('Fox Room', 2),
('Sky Room', 3);
`);

console.log("Inserting sample data into Meeting...");
await db.query(`
INSERT INTO Meeting (meeting_title, starting_time, ending_time, room_no) VALUES
('Intro to Databases', '2025-11-20 09:00', '2025-11-20 10:00', 1),
('SQL Practice', '2025-11-20 10:30', '2025-11-20 11:30', 2),
('Node & Postgres', '2025-11-21 09:00', '2025-11-21 10:30', 3),
('Security Session', '2025-11-21 11:00', '2025-11-21 12:00', 4),
('Retrospective Meetup', '2025-11-22 15:00', '2025-11-22 16:00', 5);
`);
});

console.log("Meetup database created and filled with data.");
} catch (err) {
console.error("Error while setting up meetup database:", err.message);
}
}

setupMeetup().catch((err) => {
console.error("Unexpected error:", err);
});

127 changes: 127 additions & 0 deletions Week1/assignment/exer2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
const { Client } = require("pg");

const DB_USER = "hyfuser";
const DB_PASSWORD = "hyfpassword";
const DB_HOST = "localhost";
const DB_PORT = 5432;

const client = new Client({
user: DB_USER,
password: DB_PASSWORD,
host: DB_HOST,
port: DB_PORT,
database: "world",
});

async function runQuery(label, sql) {
console.log(`\n=== ${label} ===`);

const result = await client.query(sql);
console.table(result.rows);
}

async function runWorldQueries() {

try {

await client.connect();
console.log("Connected to world database");

await runQuery("1) Countries with population greater than 8 million",
`
SELECT name, population
FROM country
WHERE population > 8000000
ORDER BY population DESC;
`);

await runQuery('2) Countries that have "land" in their names',
`
SELECT name
FROM country
WHERE name ILIKE '%land%'
ORDER BY name;
`);

await runQuery("3) Cities with population between 500,000 and 1,000,000",
`
SELECT name, population
FROM city
WHERE population BETWEEN 500000 AND 1000000
ORDER BY population;
`);

await runQuery("4) Countries on the continent 'Europe'",
`
SELECT name
FROM country
WHERE continent = 'Europe'
ORDER BY name;
`);

await runQuery("5) Countries ordered by surface area (descending)",
`
SELECT name, surfacearea
FROM country
ORDER BY surfacearea DESC;
`);

await runQuery("6) Cities in the Netherlands",
`
SELECT c.name
FROM city AS c
JOIN country AS co ON c.countrycode = co.code
WHERE co.name = 'Netherlands'
ORDER BY c.name;
`);

await runQuery("7) Population of Rotterdam",
`
SELECT population
FROM city
WHERE name = 'Rotterdam';
`);

await runQuery("8) Top 10 countries by surface area",
`
SELECT name, surfacearea
FROM country
ORDER BY surfacearea DESC
LIMIT 10;
`);

await runQuery(
"9) Top 10 most populated cities",
`
SELECT name, population
FROM city
ORDER BY population DESC
LIMIT 10;
`);

await runQuery(
"10) Total world population",
`
SELECT SUM(population) AS world_population
FROM country;
`);


console.log("\n All queries executed successfully.");

} catch (err) {

console.error("Error while running world queries:", err.message);

} finally {

await client.end();

console.log("Connection closed.");
}
}


runWorldQueries().catch((err) => {
console.error("Unexpected error:", err);
});
13 changes: 13 additions & 0 deletions Week2/assignment/db.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import pg from "pg";

const { Client } = pg;

export function createClient() {
return new Client({
host: process.env.DB_HOST ?? "localhost",
user: process.env.DB_USER ?? "hyfuser",
password: process.env.DB_PASSWORD ?? "hyfpassword",
database: process.env.DB_NAME ?? "research_w2",
port: Number(process.env.DB_PORT ?? 5432),
});
}
43 changes: 43 additions & 0 deletions Week2/assignment/ex1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import { createClient } from "./db.js";

async function run() {
const client = createClient();

try {
await client.connect();

await client.query("DROP TABLE IF EXISTS authors CASCADE;");
await client.query(`
CREATE TABLE authors (
author_id SERIAL PRIMARY KEY,
full_name VARCHAR(120) NOT NULL,
university VARCHAR(120),
date_of_birth DATE NOT NULL,
h_index INTEGER,
gender CHAR(1) NOT NULL CHECK (gender IN ('M', 'F'))
);
`);

await client.query(`
ALTER TABLE authors
ADD COLUMN mentor INTEGER;
`);

await client.query(`
ALTER TABLE authors
ADD CONSTRAINT fk_authors_mentor
FOREIGN KEY (mentor)
REFERENCES authors (author_id)
ON DELETE SET NULL;
`);

console.log("Exercise 1: authors table + mentor FK created");
} catch (err) {
console.error("Error in ex1-keys:", err.message);
} finally {
await client.end();
console.log("Connection closed");
}
}

run();
Loading