Skip to content

Commit 256af3a

Browse files
committed
Test date plugin
1 parent b728f27 commit 256af3a

3 files changed

Lines changed: 53 additions & 23 deletions

File tree

src/storage/db/date-plugin.ts

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import type {
2+
KyselyPlugin,
3+
PluginTransformQueryArgs,
4+
PluginTransformResultArgs,
5+
QueryResult,
6+
RootOperationNode,
7+
UnknownRow,
8+
} from "kysely";
9+
10+
import log from "@log";
11+
12+
export default {
13+
transformQuery(args: PluginTransformQueryArgs): RootOperationNode {
14+
return args.node;
15+
},
16+
async transformResult(args: PluginTransformResultArgs): Promise<QueryResult<UnknownRow>> {
17+
return {
18+
...args.result,
19+
rows: args.result.rows.map(transformRow),
20+
};
21+
},
22+
} satisfies KyselyPlugin;
23+
24+
const sqlitePattern = /^\d\d\d\d-\d\d-\d\d \d\d:\d\d:\d\d$/;
25+
const iso8601Pattern = /^\d\d\d\d-\d\d-\d\dT\d\d:\d\d:\d\d.\\\d\d\d/; // not matching till the end, so we also match `+00:00` timestamps
26+
function transformRow(row: UnknownRow): UnknownRow {
27+
// See: https://github.com/NullDev/CSZ-Bot/issues/504
28+
29+
const res = { ...row }; // spread all, so we can keep the same hidden class
30+
31+
for (const [k, v] of Object.entries(row)) {
32+
if (typeof v !== "string") {
33+
continue;
34+
}
35+
36+
if (
37+
(k.endsWith("At") || k.endsWith("Until")) &&
38+
(sqlitePattern.test(v) || iso8601Pattern.test(v))
39+
) {
40+
log.info(`Would do: "${v}" -> ${new Date(v)}`);
41+
// res[k] = new Date(v);
42+
}
43+
}
44+
45+
return res;
46+
}

src/storage/db/db.ts

Lines changed: 3 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,47 +3,27 @@ import { fileURLToPath } from "node:url";
33
import * as fs from "node:fs/promises";
44
import * as path from "node:path";
55

6-
import {
7-
type KyselyPlugin,
8-
FileMigrationProvider,
9-
Kysely,
10-
Migrator,
11-
type PluginTransformResultArgs,
12-
type QueryResult,
13-
type UnknownRow,
14-
type PluginTransformQueryArgs,
15-
type RootOperationNode,
16-
} from "kysely";
6+
import { FileMigrationProvider, Kysely, Migrator } from "kysely";
177
import { captureException, startInactiveSpan } from "@sentry/bun";
188
import { BunSqliteDialect } from "kysely-bun-sqlite";
199

2010
import type { Database } from "./model.js";
11+
import datePlugin from "./date-plugin.js";
2112
import assertNever from "@/utils/assertNever.js";
2213
import log from "@log";
2314

2415
let kysely: Kysely<Database>;
2516

2617
export default () => kysely;
2718

28-
const plugin = {
29-
transformQuery(args: PluginTransformQueryArgs): RootOperationNode {
30-
return args.node;
31-
},
32-
async transformResult(args: PluginTransformResultArgs): Promise<QueryResult<UnknownRow>> {
33-
const r = args.result.rows;
34-
log.info(r[0], "Row");
35-
return args.result;
36-
},
37-
} satisfies KyselyPlugin;
38-
3919
export async function connectToDb(databasePath: string) {
4020
if (kysely) {
4121
return;
4222
}
4323

4424
const nativeDb = new SqliteDatabase(databasePath);
4525
const db = new Kysely<Database>({
46-
plugins: [plugin],
26+
plugins: [datePlugin],
4727
dialect: new BunSqliteDialect({
4828
database: nativeDb,
4929
}),

src/storage/db/model.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ import type { ColumnType, Generated, GeneratedAlways, Insertable, Selectable } f
33

44
export type Date = ColumnType<string, string, string>; // TODO: Date is not supported by DB Driver
55

6+
// !! IMPORTANT !!
7+
// When handling dates, use `At` suffix to make sure the sqlite converter can convert it to a Date object
8+
// !! / IMPORTANT !!
9+
610
export interface Database {
711
birthdays: BirthdayTable;
812
stempels: StempelTable;

0 commit comments

Comments
 (0)