Skip to content

Commit 5929ccf

Browse files
committed
Share a single database instance
1 parent 66c8906 commit 5929ccf

File tree

10 files changed

+24
-48
lines changed

10 files changed

+24
-48
lines changed

Diff for: .github/workflows/ci.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,4 @@ jobs:
1313
- run: docker compose run api pnpm kysely-test migrate:latest
1414

1515
- run: docker compose run api pnpm prettier --check .
16-
- run: docker compose run api pnpm vitest run
16+
- run: docker compose run api pnpm test run

Diff for: README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,5 @@
2020
docker compose run api pnpm kysely-codegen
2121
docker compose run api pnpm prettier --check .
2222
docker compose run api pnpm prettier --write .
23-
docker compose run api pnpm vitest
23+
docker compose run api pnpm test
2424
```

Diff for: kysely.config.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { defineConfig } from "kysely-ctl";
2-
import { newDialect } from "./src/database.js";
2+
import { database } from "./src/database.js";
33

44
export default defineConfig({
5-
dialect: newDialect(),
5+
kysely: database,
66
});

Diff for: package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
"scripts": {
55
"dev": "tsx watch src/index.ts",
66
"kysely-codegen": "kysely-codegen --env-file kysely-codegen.config.env --out-file kysely-codegen.ts",
7-
"kysely-test": "SQUIGLINK_POSTGRES_DATABASE=\"$SQUIGLINK_POSTGRES_TEST_DATABASE\" kysely"
7+
"kysely-test": "SQUIGLINK_POSTGRES_DATABASE=\"$SQUIGLINK_POSTGRES_TEST_DATABASE\" kysely",
8+
"test": "SQUIGLINK_POSTGRES_DATABASE=\"$SQUIGLINK_POSTGRES_TEST_DATABASE\" vitest"
89
},
910
"dependencies": {
1011
"@hono/node-server": "^1.13.7",

Diff for: src/database.ts

+12-20
Original file line numberDiff line numberDiff line change
@@ -3,26 +3,18 @@ import { Kysely, PostgresDialect } from "kysely";
33
import pg from "pg";
44
import type { Database } from "./types.js";
55

6-
const int8TypeId = 20;
7-
pg.types.setTypeParser(int8TypeId, (value) => parseInt(value, 10));
8-
96
const { Pool } = pg;
107

11-
export function newDatabase(): Kysely<Database> {
12-
return new Kysely<Database>({ dialect: newDialect() });
13-
}
14-
15-
export function newDialect(): PostgresDialect {
16-
return new PostgresDialect({
17-
pool: newPool(),
18-
});
19-
}
8+
const int8TypeId = 20;
9+
pg.types.setTypeParser(int8TypeId, (value) => parseInt(value, 10));
2010

21-
export function newPool(): pg.Pool {
22-
return new Pool({
23-
database: env.SQUIGLINK_POSTGRES_DATABASE,
24-
host: env.SQUIGLINK_POSTGRES_HOST,
25-
password: env.SQUIGLINK_POSTGRES_PASSWORD,
26-
user: env.SQUIGLINK_POSTGRES_USER,
27-
});
28-
}
11+
export const database = new Kysely<Database>({
12+
dialect: new PostgresDialect({
13+
pool: new Pool({
14+
database: env.SQUIGLINK_POSTGRES_DATABASE,
15+
host: env.SQUIGLINK_POSTGRES_HOST,
16+
password: env.SQUIGLINK_POSTGRES_PASSWORD,
17+
user: env.SQUIGLINK_POSTGRES_USER,
18+
}),
19+
}),
20+
});

Diff for: src/middlewares/database_middleware.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
import { createMiddleware } from "hono/factory";
2+
import { database } from "../database.js";
23
import { Kysely } from "kysely";
3-
import { newDatabase } from "../database.js";
44
import type { Database } from "../types.js";
55

66
export const databaseMiddleware = createMiddleware<{
77
Variables: {
88
database: Kysely<Database>;
99
};
1010
}>(async (context, next) => {
11-
const database = newDatabase();
1211
context.set("database", database);
1312
await next();
1413
});

Diff for: src/routes/brands.test.ts

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
import { application } from "../application.js";
2+
import { database } from "../database.js";
23
import { describe, expect, test } from "vitest";
3-
import { newDatabase } from "../database.js";
44

55
describe("GET /brands", () => {
66
test("it works", async () => {
7-
const database = newDatabase();
8-
97
let brandIds = [];
108

119
for (let brandIndex = 1; brandIndex <= 11; brandIndex++) {

Diff for: src/routes/databases.test.ts

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
import { application } from "../application.js";
2+
import { database } from "../database.js";
23
import { describe, expect, test } from "vitest";
3-
import { newDatabase } from "../database.js";
44

55
describe("GET /databases", () => {
66
test("it works", async () => {
7-
const database = newDatabase();
8-
97
let userIds = [];
108
let databaseIds = [];
119

Diff for: src/routes/models.test.ts

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
import { application } from "../application.js";
2+
import { database } from "../database.js";
23
import { describe, expect, test } from "vitest";
3-
import { newDatabase } from "../database.js";
44

55
describe("GET /models", () => {
66
test("it works", async () => {
7-
const database = newDatabase();
8-
97
let brandIds = [];
108
let modelIds = [];
119

Diff for: src/test_helper.ts

+2-12
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,8 @@
1-
import { beforeAll, beforeEach, vi } from "vitest";
2-
import { env } from "process";
3-
import { newDatabase } from "./database.js";
1+
import { beforeEach } from "vitest";
2+
import { database } from "./database.js";
43
import { sql } from "kysely";
54

6-
beforeAll(() => {
7-
vi.stubEnv(
8-
"SQUIGLINK_POSTGRES_DATABASE",
9-
env.SQUIGLINK_POSTGRES_TEST_DATABASE,
10-
);
11-
});
12-
135
beforeEach(async () => {
14-
const database = newDatabase();
15-
166
await sql`truncate table ${sql.table("brands")} cascade`.execute(database);
177
await sql`truncate table ${sql.table("databases")} cascade`.execute(database);
188
await sql`truncate table ${sql.table("models")} cascade`.execute(database);

0 commit comments

Comments
 (0)