Skip to content

Commit 223e4fb

Browse files
committed
refactor: replace tsup with tsdown, add database migrations and logging, normalize route formatting
1 parent fddb01a commit 223e4fb

File tree

19 files changed

+1062
-488
lines changed

19 files changed

+1062
-488
lines changed

.vscode/settings.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@
7575
"**/tmp": true,
7676
// "**/node_modules": true,
7777
"**/bower_components": true,
78-
"**/dist": true
78+
// "**/dist": true
7979
},
8080
"files.watcherExclude": {
8181
"**/.git/objects/**": true,

apps/api/package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
"private": true,
55
"scripts": {
66
"start": "node ./dist/index.js",
7-
"dev": "tsx watch src/index.ts",
8-
"build": "tsup",
7+
"dev": "npx tsdown --watch src/index.ts",
8+
"build": "npx tsdown",
99
"clean": "rm -rf ../../build/api",
1010
"typecheck": "tsc --noEmit",
1111
"lint": "pnpm exec eslint src/",
@@ -46,6 +46,7 @@
4646
"drizzle-kit": "^0.30.4",
4747
"pino-pretty": "^13.0.0",
4848
"supertest": "^7.0.0",
49+
"tsdown": "^0.15.6",
4950
"tsup": "^8.2.4",
5051
"typescript": "5.5.4",
5152
"vitest": "^3.0.5"

apps/api/src/server.ts

Lines changed: 63 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { checkConnection } from "@taskcord/database";
1+
import { checkConnection, runMigrations } from "@taskcord/database";
22
import * as dotenv from "dotenv";
33
import type { FastifyInstance, FastifyServerOptions } from "fastify";
44
import Fastify from "fastify";
@@ -11,79 +11,82 @@ import { CreateRedisClient } from "./utils/redis";
1111
dotenv.config();
1212

1313
declare module "fastify" {
14-
interface FastifyRequest {
15-
cacheDb: Redis;
16-
}
17-
interface FastifyInstance {
18-
cacheDb: Redis;
19-
}
14+
interface FastifyRequest {
15+
cacheDb: Redis;
16+
}
17+
interface FastifyInstance {
18+
cacheDb: Redis;
19+
}
2020
}
2121

2222
export default class TaskcordServer {
23-
private app: FastifyInstance | null = null;
24-
private serverOptions: FastifyServerOptions;
23+
private app: FastifyInstance | null = null;
24+
private serverOptions: FastifyServerOptions;
2525

26-
constructor() {
27-
this.serverOptions = {
28-
logger: {
29-
level: "info",
30-
transport: {
31-
target: "pino-pretty",
32-
options: {
33-
translateTime: "HH:MM:ss Z",
34-
ignore: "pid,hostname",
35-
singleLine: true,
36-
},
37-
},
38-
},
39-
};
40-
}
26+
constructor() {
27+
this.serverOptions = {
28+
logger: {
29+
level: "info",
30+
transport: {
31+
target: "pino-pretty",
32+
options: {
33+
translateTime: "HH:MM:ss Z",
34+
ignore: "pid,hostname",
35+
singleLine: true,
36+
},
37+
},
38+
},
39+
};
40+
}
4141

42-
public async initialize(): Promise<void> {
43-
const redisUrl = GlobalUtils.getRedisUrl();
44-
const globalCacheDb = CreateRedisClient(redisUrl);
45-
this.app = Fastify(this.serverOptions) as FastifyInstance;
46-
this.app.decorate("cacheDb", globalCacheDb);
42+
public async initialize(): Promise<void> {
43+
const redisUrl = GlobalUtils.getRedisUrl();
44+
const globalCacheDb = CreateRedisClient(redisUrl);
45+
this.app = Fastify(this.serverOptions) as FastifyInstance;
46+
this.app.decorate("cacheDb", globalCacheDb);
4747

48-
await this.app.register(plugins);
49-
await this.app.register(modules, { prefix: "/api" });
50-
}
48+
await this.app.register(plugins);
49+
await this.app.register(modules, { prefix: "/api" });
50+
}
5151

52-
public getApp(): FastifyInstance {
53-
if (!this.app) {
54-
throw new Error("Server not initialized. Call initialize() first.");
52+
public getApp(): FastifyInstance {
53+
if (!this.app) {
54+
throw new Error("Server not initialized. Call initialize() first.");
55+
}
56+
return this.app;
5557
}
56-
return this.app;
57-
}
5858
}
5959

6060
export const startServer = async (): Promise<FastifyInstance> => {
61-
const port = process.env.PORT || 5001;
62-
const buildStart = performance.now();
61+
const port = process.env.PORT || 5001;
62+
const buildStart = performance.now();
6363

64-
const serverInstance = new TaskcordServer();
65-
await serverInstance.initialize();
66-
const fastifyServer: FastifyInstance = serverInstance.getApp();
64+
const serverInstance = new TaskcordServer();
65+
await serverInstance.initialize();
66+
const fastifyServer: FastifyInstance = serverInstance.getApp();
6767

68-
try {
69-
console.log("✨".repeat(10), "STARTING SERVER", "✨".repeat(10));
70-
// check if cache & postgres are connected
71-
await fastifyServer.cacheDb.ping();
68+
try {
69+
console.log("✨".repeat(10), "STARTING SERVER", "✨".repeat(10));
70+
// check if cache & postgres are connected
71+
await fastifyServer.cacheDb.ping();
7272

73-
await checkConnection();
73+
await checkConnection();
74+
await runMigrations();
7475

75-
await fastifyServer.listen({ port: Number(port), host: "0.0.0.0" });
76+
await fastifyServer.listen({ port: Number(port), host: "0.0.0.0" });
7677

77-
const buildEnd = performance.now();
78-
console.log(`→ 📚 Check out API docs at http://localhost:${port}/api/docs`);
79-
console.log(
80-
`🚀🚀 Server is ready to accept requests in ${(
81-
buildEnd - buildStart
82-
).toFixed(2)} ms`,
83-
);
84-
return fastifyServer;
85-
} catch (e) {
86-
console.error("🛑 Error occured while building fastify");
87-
throw e;
88-
}
78+
const buildEnd = performance.now();
79+
console.log(
80+
`→ 📚 Check out API docs at http://localhost:${port}/api/docs`
81+
);
82+
console.log(
83+
`🚀🚀 Server is ready to accept requests in ${(
84+
buildEnd - buildStart
85+
).toFixed(2)} ms`
86+
);
87+
return fastifyServer;
88+
} catch (e) {
89+
console.error("🛑 Error occured while building fastify");
90+
throw e;
91+
}
8992
};

apps/api/tsdown.config.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// import { copyFile } from "node:fs/promises";
2+
// import path from "node:path";
3+
import { defineConfig, type Options } from "tsup";
4+
5+
export default defineConfig((options: Options) => ({
6+
entryPoints: ["src/index.ts"],
7+
outDir: "dist",
8+
target: "es2020",
9+
clean: true,
10+
sourcemap: true,
11+
format: ["cjs"],
12+
...options,
13+
// async onSuccess() {
14+
// // Copy .env file
15+
// await copyFile(
16+
// path.join(__dirname, ".env"),
17+
// path.join(__dirname, "../../build/api/.env")
18+
// ).catch(() => {
19+
// console.log("No .env file found to copy");
20+
// });
21+
22+
// // Copy package.json
23+
// await copyFile(
24+
// path.join(__dirname, "package.json"),
25+
// path.join(__dirname, "../../build/api/package.json")
26+
// );
27+
// },
28+
}));

apps/api/tsup.config.ts

Lines changed: 0 additions & 29 deletions
This file was deleted.

0 commit comments

Comments
 (0)