Skip to content

Commit c52ff1c

Browse files
Scaffolding for HTTP backend. Add unit tests for Set functionality
1 parent e3fa501 commit c52ff1c

18 files changed

Lines changed: 851 additions & 61 deletions

backend/.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,6 @@ lerna-debug.log*
2626

2727
# misc
2828
.DS_Store
29+
30+
# Database file
31+
*.db

backend/bot_data.db

-44 KB
Binary file not shown.

backend/package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
},
1212
"dependencies": {
1313
"@hono/node-server": "^1.19.6",
14+
"@hono/standard-validator": "^0.2.0",
1415
"@hono/zod-validator": "^0.7.5",
1516
"better-sqlite3": "^12.4.6",
1617
"hono": "^4.10.6",
@@ -23,6 +24,8 @@
2324
"@biomejs/biome": "^2.3.7",
2425
"@types/node": "^20.11.17",
2526
"tsx": "^4.7.1",
26-
"typescript": "^5.8.3"
27+
"typescript": "^5.8.3",
28+
"vite-tsconfig-paths": "^5.1.4",
29+
"vitest": "^4.0.14"
2730
}
2831
}

backend/pnpm-lock.yaml

Lines changed: 629 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

backend/src/db/init_tables.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { knexDb } from "@db/knexfile";
2-
import { SetCharacterTable, SetResultTable, SetTable, SSBUCharTable } from "@v1/set/schema";
2+
import { SetCharacterTable, SetResultTable, SetTable, SSBUCharTable } from "@v1/set/models";
33
import type { Knex } from "knex";
44

55
import pino from "pino";
@@ -9,7 +9,7 @@ const log = pino();
99
const tables = [SetTable, SetResultTable, SetCharacterTable, SSBUCharTable];
1010

1111
async function create_table_if_notexists(
12-
db: Knex,
12+
db: Knex = knexDb,
1313
tableName: string,
1414
callback: (tableBuilder: Knex.CreateTableBuilder) => void,
1515
): Promise<Knex.SchemaBuilder> {
@@ -28,17 +28,19 @@ export async function init_tables(db: Knex = knexDb) {
2828
for (const table of tables) {
2929
await create_table_if_notexists(trx, table.table_name, table.initialize);
3030
}
31+
await trx.seed.run();
3132
await trx.commit();
32-
await db.seed.run();
3333
}
3434

3535
export async function teardown(db: Knex = knexDb) {
3636
if (process.env.NODE_ENV === "production") {
3737
log.error("`teardown` was called on a production database -- no action will be performed");
3838
return;
3939
}
40+
const trx = await db.transaction();
4041
for (const table of tables) {
41-
await db.schema.dropTableIfExists(table.table_name);
42+
await trx.schema.dropTableIfExists(table.table_name);
4243
}
44+
await trx.commit();
4345
log.info("Successfully performed `teardown` on database");
4446
}

backend/src/index.ts

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,40 @@
11
import { init_tables, teardown } from "@db/init_tables";
22
import { serve } from "@hono/node-server";
3+
import set_router from "@v1/set/router";
4+
import user_router from "@v1/user/router";
35
import { Hono } from "hono";
6+
import { openAPIRouteHandler } from "hono-openapi";
7+
import {
8+
trimTrailingSlash,
9+
} from 'hono/trailing-slash'
10+
411

512
const app = new Hono();
613

714
await teardown();
815
await init_tables();
916

17+
18+
app.use(trimTrailingSlash())
19+
1020
app.get("/", (c) => {
1121
return c.text("Hello Hono!");
1222
});
1323

14-
serve(
15-
{
16-
fetch: app.fetch,
17-
port: 3000,
18-
},
19-
(info) => {
20-
console.log(`Server is running on http://localhost:${info.port}`);
21-
},
24+
app.get(
25+
"/docs",
26+
openAPIRouteHandler(app, {
27+
documentation: {
28+
info: {
29+
title: "Grindcord REST API",
30+
version: "0.0.1",
31+
description: "Grindcord OpenAPI Reference",
32+
},
33+
servers: [{ url: "http://localhost:3000", description: "Local Server" }],
34+
},
35+
}),
2236
);
37+
38+
serve({ fetch: app.fetch, port: 3000 }, (info) => {
39+
console.log(`Server is running on http://localhost:${info.port}`);
40+
});

backend/src/tests/common.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@ import knex, { type Knex } from "knex";
33
export const test_config = {
44
client: "better-sqlite3",
55
connection: {
6-
filename: "./test.db",
6+
filename: ":memory:",
7+
},
8+
seeds: {
9+
director: "./seeds/"
710
},
811
useNullAsDefault: true,
912
};

backend/src/tests/report.test.ts

Lines changed: 0 additions & 17 deletions
This file was deleted.
Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,19 @@ export const SetTable = {
55
initialize(table) {
66
table.increments("set_id");
77
table.string("guild_id").index("guild_id_idx");
8-
table.timestamp("created_at").defaultTo(knexDb.fn.now());
8+
// https://github.com/knex/knex/issues/6283
9+
table.timestamp("created_at").defaultTo(new Date().toISOString());
910
},
1011
};
1112

1213
export const SetResultTable = {
1314
table_name: "SetResult",
1415
initialize(table) {
1516
table.primary(["set_id", "user_id"]);
16-
table.unique(["set_id", "is_win"], {
17-
indexName: "set_winner_idx",
18-
});
1917

2018
table.integer("set_id").unsigned();
2119
table.string("user_id");
2220
table.integer("game_count");
23-
table.boolean("is_win");
2421

2522
table.foreign("set_id").references("set_id").inTable("Set");
2623
},

backend/src/v1/set/router.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { Hono } from "hono";
2+
import { describeRoute, resolver, validator } from "hono-openapi";
3+
import type { SetRecord, SetReport } from "@v1/set/schemas";
4+
5+
6+
const app = new Hono();
7+
8+
export default app;

0 commit comments

Comments
 (0)