-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpostgres_crud.ts
89 lines (79 loc) · 2.58 KB
/
postgres_crud.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import { Client } from "pg";
import { getEnvVar } from "../env_vars/env_vars_utils";
const INIT_DB_SQL = `
CREATE TABLE IF NOT EXISTS crypto_coins (
id SERIAL PRIMARY KEY,
ticker VARCHAR(8) NOT NULL UNIQUE,
name VARCHAR(30) NOT NULL,
launched SMALLINT NOT NULL
);
`;
const DB_SEED_SQL = `
INSERT INTO crypto_coins (ticker, name, launched) VALUES
('BTC', 'Bitcoin', 2009),
('ETH', 'Ethereum', 2015),
('SOL', 'Solana', 2020)
ON CONFLICT (ticker) DO NOTHING;
`;
interface CryptoCoin {
id: number;
ticker: string;
name: string;
launched: number;
}
const initClient = async () => {
const isCI = getEnvVar("CI");
const client = new Client({
connectionString: `postgres://postgres:pgpass@${isCI ? "postgres" : "localhost"}:5432`,
});
client.on("error", (err) => {
console.error("Postgres client error");
throw err;
});
await client.connect();
await client.query(INIT_DB_SQL);
await client.query(DB_SEED_SQL);
return client;
};
export const getItemByTicker = async (ticker: string) => {
const client = await initClient();
const result = await client.query<CryptoCoin>("SELECT * FROM crypto_coins WHERE ticker = $1 LIMIT 1", [ticker]);
await client.end();
return result.rows;
};
export const getItemsAfterLaunchYear = async (launchYear: number) => {
const client = await initClient();
const result = await client.query<CryptoCoin>("SELECT * FROM crypto_coins WHERE launched > $1", [launchYear]);
await client.end();
return result.rows;
};
export const getAllItems = async () => {
const client = await initClient();
const result = await client.query<CryptoCoin>("SELECT * FROM crypto_coins ORDER BY launched DESC");
await client.end();
return result.rows;
};
export const addItem = async (coin: Omit<CryptoCoin, "id">) => {
const client = await initClient();
await client.query(
"INSERT INTO crypto_coins (ticker, name, launched) VALUES($1, $2, $3) ON CONFLICT (ticker) DO NOTHING",
[coin.ticker, coin.name, coin.launched],
);
await client.end();
return "ok";
};
export const removeItem = async (ticker: string) => {
const client = await initClient();
const result = await client.query<CryptoCoin>("DELETE FROM crypto_coins WHERE ticker = $1 RETURNING *", [ticker]);
await client.end();
return result.rows;
};
export const updateItem = async (coin: Omit<CryptoCoin, "id">) => {
const client = await initClient();
const result = await client.query<CryptoCoin>(
"UPDATE crypto_coins SET name = $1, launched = $2 WHERE ticker = $3 RETURNING *",
[coin.name, coin.launched, coin.ticker],
);
await client.end();
return result.rows;
};