-
Notifications
You must be signed in to change notification settings - Fork 1
/
kysely.ts
44 lines (35 loc) · 1 KB
/
kysely.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import { ColumnType, Generated, Selectable } from 'kysely';
import { createPool } from 'mysql2';
import { Kysely, MysqlDialect } from 'kysely';
import { mySqlConfig } from '../config';
interface Database {
users: UserTable;
}
interface UserTable {
id: Generated<number>;
username: string;
email: string;
password: string;
name: string;
created_at: ColumnType<Date, string | undefined, never>;
updated_at: ColumnType<Date, string | undefined, never>;
deleted_at: ColumnType<Date, string | undefined, never> | null;
}
type User = Selectable<UserTable>;
const { connectionLimit, ...config } = mySqlConfig;
if (connectionLimit) {
Object.assign(config, { connectionLimit });
}
const dialect = new MysqlDialect({
pool: createPool(config),
});
const db = new Kysely<Database>({
dialect,
});
export const kyselyMySqlGetUser = async (id: number) =>
await db
.selectFrom('users')
.selectAll()
.where('id', '=', id)
.executeTakeFirst();
export const kyselyClose = () => db.destroy();