Skip to content
This repository was archived by the owner on Apr 19, 2023. It is now read-only.

Commit c1c0970

Browse files
✨ Add mysql helpers
1 parent e4c8cab commit c1c0970

File tree

5 files changed

+47
-27
lines changed

5 files changed

+47
-27
lines changed

src/config.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
import { config } from "dotenv";
22
config();
33

4+
// Server
5+
export const PORT = process.env.PORT || 7008;
6+
47
// Database
58
export const DB_HOST = process.env.DB_HOST || "localhost";
69
export const DB_PORT = process.env.DB_PORT

src/helpers/crud.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import { query } from "./mysql";
2+
import { User } from "../interfaces/tables/user";
3+
4+
export const listAllUsers = async () => {
5+
return <User[]>await query("SELECT * from users");
6+
};

src/helpers/mysql.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import { createPool, PoolConnection } from "mysql";
2+
import {
3+
DB_HOST,
4+
DB_USERNAME,
5+
DB_PORT,
6+
DB_PASSWORD,
7+
DB_DATABASE
8+
} from "../config";
9+
10+
export const pool = createPool({
11+
host: DB_HOST,
12+
port: DB_PORT,
13+
user: DB_USERNAME,
14+
password: DB_PASSWORD,
15+
database: DB_DATABASE
16+
});
17+
18+
export const query = (
19+
queryString: string,
20+
values?: (string | number | boolean | Date)[]
21+
) =>
22+
new Promise((resolve, reject) => {
23+
pool.getConnection((error, connection) => {
24+
if (error) return reject(error);
25+
connection.query(queryString, values, (error, result) => {
26+
connection.destroy();
27+
if (error) return reject(error);
28+
resolve(result);
29+
});
30+
});
31+
});

src/index.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
11
import express from "express";
2-
// import { create } from "./helpers/crud";
2+
import { PORT } from "./config";
3+
import { listAllUsers } from "./helpers/crud";
34

45
const app = express();
56

67
app.get("/", (req, res) => res.json({ hello: "world" }));
78
app.get("/create-account", async (req, res) => {
89
try {
9-
// res.json({ success: true, data: await create() });
10-
res.json({ success: true });
10+
const users = await listAllUsers();
11+
res.json({ success: true, users });
1112
} catch (error) {
1213
console.log("Error", error);
1314
res.json({ success: false });
1415
}
1516
});
1617

17-
app.listen(process.env.PORT || 7007, () => console.log("App running"));
18+
app.listen(PORT, () => console.log("App running"));

src/interfaces/tables/user.ts

Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,16 @@
1-
import { QueryTable, NumberColumn, StringColumn, DateColumn, BooleanColumn } from "type-sql";
2-
31
export interface User {
42
id: number;
53
name: string;
64
nickname: string;
75
primaryEmail?: number;
86
password: string;
97
twoFactorEnabled?: boolean;
10-
twoFactorSecret?: boolean;
11-
country?: string;
8+
twoFactorSecret?: string;
9+
countryCode?: string;
1210
timezone?: string;
1311
notificationEmails?: 1 | 2 | 3 | 4;
1412
preferredLanguage?: string;
1513
prefersReducedMotion?: boolean;
1614
createdAt?: Date;
1715
updatedAt?: Date;
1816
}
19-
20-
export class UserTable extends QueryTable<User, number> {
21-
id = new NumberColumn(this, "id");
22-
name = new StringColumn(this, "name");
23-
nickname = new StringColumn(this, "nickname");
24-
primaryEmail = new NumberColumn(this, "primaryEmail");
25-
password = new StringColumn(this, "password");
26-
twoFactorEnabled = new BooleanColumn(this, "twoFactorEnabled");
27-
twoFactorSecret = new StringColumn(this, "twoFactorSecret");
28-
country = new StringColumn(this, "country");
29-
timezone = new StringColumn(this, "timezone");
30-
notificationEmails = new NumberColumn(this, "notificationEmails");
31-
preferredLanguage = new StringColumn(this, "preferredLanguage");
32-
prefersReducedMotion = new BooleanColumn(this, "prefersReducedMotion");
33-
createdAt = new DateColumn(this, "createdAt");
34-
updatedAt = new DateColumn(this, "updatedAt");
35-
}
36-
37-
export const USER = new UserTable("User");

0 commit comments

Comments
 (0)