Skip to content

Commit 8afe584

Browse files
author
100xdevs
committed
init
0 parents  commit 8afe584

15 files changed

+415
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
node_modules
2+
dist

package.json

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"name": "sql-commands",
3+
"version": "1.0.0",
4+
"main": "index.js",
5+
"license": "MIT",
6+
"scripts": {
7+
"build": "tsc -b"
8+
},
9+
"dependencies": {
10+
"@types/pg": "^8.10.2",
11+
"pg": "^8.11.3"
12+
}
13+
}

src/create-index.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { getClient } from "./utils";
2+
3+
async function addIndex() {
4+
const client = await getClient();
5+
6+
const createIndexQuery = 'CREATE INDEX idx_todos_user_id ON todos(user_id)';
7+
await client.query(createIndexQuery);
8+
9+
console.log("Index added successfully on user_id column of todos table!");
10+
}
11+
12+
addIndex();

src/create-table.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import { getClient } from "./utils";
2+
3+
async function createTable() {
4+
const createUserTableQuery = `
5+
CREATE TABLE users (
6+
id SERIAL PRIMARY KEY,
7+
email VARCHAR(255) UNIQUE NOT NULL,
8+
password VARCHAR(255) NOT NULL
9+
);
10+
`;
11+
12+
const client = await getClient();
13+
14+
await client.query(createUserTableQuery);
15+
16+
const createTodosQuery = `
17+
CREATE TABLE todos (
18+
id SERIAL PRIMARY KEY,
19+
title TEXT NOT NULL,
20+
description TEXT,
21+
user_id INTEGER REFERENCES users(id),
22+
done BOOLEAN DEFAULT FALSE
23+
);
24+
`;
25+
26+
27+
await client.query(createTodosQuery);
28+
29+
console.log("Table created successfully!");
30+
}
31+
32+
33+
34+
createTable();

src/delete-data.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { getClient } from "./utils";
2+
3+
async function deleteTodo(todoId: number) {
4+
const client = await getClient();
5+
6+
const deleteTodoText = 'DELETE FROM todos WHERE id = $1';
7+
await client.query(deleteTodoText, [todoId]);
8+
9+
console.log(`Todo with ID ${todoId} deleted!`);
10+
}
11+
12+
const todoIdToDelete = 1;
13+
deleteTodo(todoIdToDelete);

src/get-data.ts

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import { getClient } from "./utils";
2+
3+
async function getUsers() {
4+
const client = await getClient();
5+
6+
const selectUsersText = 'SELECT * FROM users';
7+
const userRes = await client.query(selectUsersText);
8+
9+
console.log("Users:");
10+
for (let user of userRes.rows) {
11+
console.log(`ID: ${user.id}, Email: ${user.email}`);
12+
}
13+
}
14+
15+
async function getUserFromEmail(email: string) {
16+
const client = await getClient();
17+
18+
const selectUserText = 'SELECT * FROM users WHERE email = $1';
19+
const userRes = await client.query(selectUserText, [email]);
20+
21+
console.log("Single User detail:");
22+
for (let user of userRes.rows) {
23+
console.log(`ID: ${user.id}, Email: ${user.email}`);
24+
}
25+
}
26+
27+
async function getTodosForUser(userId: number) {
28+
const client = await getClient();
29+
30+
const selectTodosText = 'SELECT * FROM todos WHERE user_id = $1';
31+
const todoRes = await client.query(selectTodosText, [userId]);
32+
33+
console.log(`Todos for User ID ${userId}:`);
34+
for (let todo of todoRes.rows) {
35+
console.log(`ID: ${todo.id}, Title: ${todo.title}, Description: ${todo.description}, Done: ${todo.done}`);
36+
}
37+
}
38+
39+
getUsers();
40+
41+
getUserFromEmail("[email protected]")
42+
43+
const userIdToFetch = 1;
44+
getTodosForUser(userIdToFetch);

src/insert-data.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { getClient } from "./utils";
2+
3+
async function createEntries() {
4+
const client = await getClient();
5+
const insertUserText = 'INSERT INTO users (email, password) VALUES ($1, $2) RETURNING id';
6+
const userValues = ['[email protected]', 'hashed_password_here'];
7+
8+
let response = await client.query(insertUserText, userValues);
9+
const insertTodoText = 'INSERT INTO todos (title, description, user_id, done) VALUES ($1, $2, $3, $4) RETURNING id';
10+
const todoValues = ['Buy groceries', 'Milk, bread, and eggs', response.rows[0].id, false];
11+
await client.query(insertTodoText, todoValues);
12+
13+
console.log("Entries created!");
14+
}
15+
16+
17+
18+
createEntries();

src/joins/advance-1.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { getClient } from "../utils";
2+
3+
// Get all todos for a give user
4+
// This needs to ensure that every user comes atleast once
5+
async function getUserAndTodosWithJoin(userId: number) {
6+
const client = await getClient();
7+
8+
const joinQuery = `
9+
SELECT users.*, todos.title, todos.description, todos.done
10+
FROM users
11+
LEFT JOIN todos ON users.id = todos.user_id
12+
WHERE users.id = $1;
13+
`;
14+
15+
const res = await client.query(joinQuery, [userId]);
16+
const results = res.rows;
17+
18+
console.log("User and Todos:", results);
19+
}
20+
21+
getUserAndTodosWithJoin(1)

src/joins/advance-2.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { getClient } from "../utils";
2+
3+
// Get all todos for a give user
4+
// This shouldnt return a row if no todos exist for the user
5+
async function getUserAndTodosWithJoin(userId: number) {
6+
const client = await getClient();
7+
8+
const joinQuery = `
9+
SELECT users.*, todos.title, todos.description, todos.done
10+
FROM users
11+
JOIN todos ON users.id = todos.user_id
12+
WHERE users.id = $1;
13+
`;
14+
15+
const res = await client.query(joinQuery, [userId]);
16+
const results = res.rows;
17+
18+
console.log("User and Todos:", results);
19+
}
20+
21+
getUserAndTodosWithJoin(5)

src/joins/advance-3.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { getClient } from "../utils";
2+
3+
async function getAllTodosWithUserDetails() {
4+
const client = await getClient();
5+
6+
const joinQuery = `
7+
SELECT todos.*, users.email, users.password
8+
FROM todos
9+
JOIN users ON todos.user_id = users.id;
10+
`;
11+
12+
const res = await client.query(joinQuery);
13+
const results = res.rows;
14+
15+
console.log("Todos with User Details:", results);
16+
}
17+
18+
getAllTodosWithUserDetails();

0 commit comments

Comments
 (0)