Skip to content

Commit b056c34

Browse files
committed
chore: refactor
1 parent e2c8923 commit b056c34

3 files changed

Lines changed: 14 additions & 6 deletions

File tree

src/index.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ import { Client } from "pg";
22
import { Resource } from "sst";
33
import { handleCreate } from "./lib/create";
44
import { handleDestroy } from "./lib/destroy";
5-
import { type Event, eventSchema, type Response } from "./lib/types";
6-
import { names } from "./lib/util";
5+
import { Action, type Event, eventSchema, type Response } from "./lib/types";
6+
import { DB_PORT, names } from "./lib/util";
77

88
export async function handler(event: Event): Promise<Response> {
99
eventSchema.parse(event);
@@ -12,13 +12,18 @@ export async function handler(event: Event): Promise<Response> {
1212
const host = Resource.DatabaseHost.value;
1313
const password = Resource.DatabasePassword.value;
1414

15-
const client = new Client({ host, port: 5432, user: "postgres", password });
15+
const client = new Client({
16+
host,
17+
port: DB_PORT,
18+
user: "postgres",
19+
password,
20+
});
1621
await client.connect();
1722

1823
try {
19-
if (event.action === "create") {
24+
if (event.action === Action.Deploy) {
2025
return await handleCreate(client, host, database, username);
21-
} else if (event.action === "destroy") {
26+
} else if (event.action === Action.Remove) {
2227
return await handleDestroy(client, database, username);
2328
}
2429
return {

src/lib/create.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import format from "pg-format";
33
import { Action, type CreateResponse } from "./types";
44
import {
55
buildPostgresUrl,
6+
DB_PORT,
67
databaseExists,
78
generatePassword,
89
roleExists,
@@ -46,7 +47,7 @@ export async function handleCreate(
4647

4748
const url = buildPostgresUrl({
4849
host,
49-
port: 5432,
50+
port: DB_PORT,
5051
username,
5152
password,
5253
database,

src/lib/util.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import { randomBytes } from "node:crypto";
22
import type { Client } from "pg";
33

4+
export const DB_PORT = 5432;
5+
46
function sanitize(input: string) {
57
return input
68
.toLowerCase()

0 commit comments

Comments
 (0)