-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* build: ts-rest와 zod 버전 업데이트 * build: kysely 추가 * feat: db 타입 생성 및 kysely 연결 * feat: 타입 안전한 date 함수 래퍼 * refactor: `v2/reviews`에 kysely 적용 --------- Co-authored-by: nocontribute <>
- Loading branch information
Showing
11 changed files
with
631 additions
and
118 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,286 @@ | ||
import type { ColumnType, SqlBool } from "kysely"; | ||
|
||
export type Generated<T> = T extends ColumnType<infer S, infer I, infer U> | ||
? ColumnType<S, I | undefined, U> | ||
: ColumnType<T, T | undefined, T>; | ||
|
||
export interface Book { | ||
id: Generated<number>; | ||
donator: Generated<string | null>; | ||
callSign: string; | ||
status: number; | ||
createdAt: Generated<Date>; | ||
infoId: number; | ||
updatedAt: Generated<Date>; | ||
donatorId: Generated<number | null>; | ||
} | ||
|
||
export interface BookInfo { | ||
id: Generated<number>; | ||
title: string; | ||
author: string; | ||
publisher: string; | ||
isbn: Generated<string | null>; | ||
image: Generated<string | null>; | ||
publishedAt: Generated<Date | null>; | ||
createdAt: Generated<Date>; | ||
updatedAt: Generated<Date>; | ||
categoryId: number; | ||
} | ||
|
||
export interface Category { | ||
id: Generated<number>; | ||
name: string; | ||
} | ||
|
||
export interface Lending { | ||
id: Generated<number>; | ||
lendingLibrarianId: number; | ||
lendingCondition: string; | ||
returningLibrarianId: Generated<number | null>; | ||
returningCondition: Generated<string | null>; | ||
returnedAt: Generated<Date | null>; | ||
createdAt: Generated<Date>; | ||
updatedAt: Generated<Date>; | ||
bookId: number; | ||
userId: number; | ||
} | ||
|
||
export interface Likes { | ||
id: Generated<number>; | ||
userId: number; | ||
bookInfoId: number; | ||
isDeleted: Generated<SqlBool>; | ||
} | ||
|
||
export interface Reservation { | ||
id: Generated<number>; | ||
endAt: Generated<Date | null>; | ||
createdAt: Generated<Date>; | ||
updatedAt: Generated<Date>; | ||
status: Generated<number>; | ||
bookInfoId: number; | ||
bookId: Generated<number | null>; | ||
userId: number; | ||
} | ||
|
||
export interface Reviews { | ||
id: Generated<number>; | ||
userId: number; | ||
bookInfoId: number; | ||
createdAt: Generated<Date>; | ||
updatedAt: Generated<Date>; | ||
updateUserId: number; | ||
isDeleted: Generated<SqlBool>; | ||
deleteUserId: Generated<number | null>; | ||
content: string; | ||
disabled: Generated<SqlBool>; | ||
disabledUserId: Generated<number | null>; | ||
} | ||
|
||
export interface SubTag { | ||
id: Generated<number>; | ||
userId: number; | ||
superTagId: number; | ||
createdAt: Generated<Date>; | ||
updatedAt: Generated<Date>; | ||
isDeleted: Generated<SqlBool>; | ||
updateUserId: number; | ||
content: string; | ||
isPublic: Generated<number>; | ||
} | ||
|
||
export interface SuperTag { | ||
id: Generated<number>; | ||
userId: number; | ||
bookInfoId: number; | ||
createdAt: Generated<Date>; | ||
updatedAt: Generated<Date>; | ||
isDeleted: Generated<SqlBool>; | ||
updateUserId: number; | ||
content: string; | ||
} | ||
|
||
export interface TypeormMetadata { | ||
type: string; | ||
database: Generated<string | null>; | ||
schema: Generated<string | null>; | ||
table: Generated<string | null>; | ||
name: Generated<string | null>; | ||
value: Generated<string | null>; | ||
} | ||
|
||
export interface User { | ||
id: Generated<number>; | ||
email: string; | ||
password: string; | ||
nickname: Generated<string | null>; | ||
intraId: Generated<number | null>; | ||
slack: Generated<string | null>; | ||
penaltyEndDate: Generated<Date>; | ||
role: Generated<number>; | ||
createdAt: Generated<Date>; | ||
updatedAt: Generated<Date>; | ||
} | ||
|
||
export interface UserReservation { | ||
reservationId: Generated<number>; | ||
endAt: Generated<Date | null>; | ||
reservationDate: Generated<Date>; | ||
reservedBookInfoId: number; | ||
userId: number; | ||
title: string | null; | ||
author: string | null; | ||
image: Generated<string | null>; | ||
ranking: Generated<number | null>; | ||
} | ||
|
||
export interface VHistories { | ||
id: Generated<number>; | ||
returningCondition: Generated<string | null>; | ||
login: Generated<string | null>; | ||
callSign: string; | ||
bookInfoId: Generated<number | null>; | ||
title: string | null; | ||
image: Generated<string | null>; | ||
lendingCondition: string; | ||
updatedAt: Generated<Date>; | ||
penaltyDays: Generated<number | null>; | ||
createdAt: Generated<string | null>; | ||
returnedAt: Generated<string | null>; | ||
dueDate: Generated<string | null>; | ||
lendingLibrarianNickName: Generated<string | null>; | ||
returningLibrarianNickname: Generated<string | null>; | ||
} | ||
|
||
export interface VLending { | ||
id: Generated<number>; | ||
lendingCondition: string; | ||
login: Generated<string | null>; | ||
bookId: Generated<number | null>; | ||
callSign: string | null; | ||
title: string | null; | ||
image: Generated<string | null>; | ||
penaltyDays: Generated<number | null>; | ||
createdAt: Generated<string | null>; | ||
returnedAt: Generated<string | null>; | ||
dueDate: Generated<string | null>; | ||
} | ||
|
||
export interface VLendingForSearchUser { | ||
lendingCondition: string; | ||
lendDate: Generated<Date>; | ||
userId: Generated<number>; | ||
bookInfoId: Generated<number | null>; | ||
title: string | null; | ||
author: string | null; | ||
image: Generated<string | null>; | ||
duedate: Generated<Date | null>; | ||
overDueDay: Generated<number | null>; | ||
reservedNum: Generated<number | null>; | ||
} | ||
|
||
export interface VSearchBook { | ||
bookId: Generated<number>; | ||
donator: Generated<string | null>; | ||
callSign: string; | ||
status: number; | ||
bookInfoId: number; | ||
title: string | null; | ||
author: string | null; | ||
publisher: string | null; | ||
isbn: Generated<string | null>; | ||
image: Generated<string | null>; | ||
categoryId: number | null; | ||
category: string | null; | ||
publishedAt: Generated<string | null>; | ||
isLendable: Generated<number | null>; | ||
} | ||
|
||
export interface VSearchBookByTag { | ||
id: Generated<number>; | ||
title: string; | ||
author: string; | ||
isbn: Generated<string | null>; | ||
image: Generated<string | null>; | ||
publishedAt: Generated<Date | null>; | ||
createdAt: Generated<Date>; | ||
updatedAt: Generated<Date>; | ||
category: string; | ||
superTagContent: string; | ||
subTagContent: string | null; | ||
lendingCnt: Generated<number | null>; | ||
} | ||
|
||
export interface VStock { | ||
bookId: Generated<number>; | ||
donator: Generated<string | null>; | ||
callSign: string; | ||
status: number; | ||
bookInfoId: number; | ||
title: string | null; | ||
author: string | null; | ||
publisher: string | null; | ||
isbn: Generated<string | null>; | ||
image: Generated<string | null>; | ||
categoryId: number | null; | ||
category: string | null; | ||
publishedAt: Generated<string | null>; | ||
updatedAt: Generated<string | null>; | ||
} | ||
|
||
export interface VTagsSubDefault { | ||
bookInfoId: number; | ||
title: string; | ||
id: Generated<number>; | ||
createdAt: Generated<string | null>; | ||
login: Generated<string | null>; | ||
content: string; | ||
superTagId: Generated<number>; | ||
superContent: string; | ||
isPublic: Generated<number>; | ||
isDeleted: Generated<number>; | ||
visibility: Generated<string>; | ||
} | ||
|
||
export interface VTagsSuperDefault { | ||
content: Generated<string>; | ||
count: Generated<number | null>; | ||
type: Generated<string>; | ||
createdAt: Generated<string | null>; | ||
} | ||
|
||
export interface VUserLending { | ||
lendingCondition: string; | ||
userId: number; | ||
bookInfoId: Generated<number | null>; | ||
title: string | null; | ||
image: Generated<string | null>; | ||
lendDate: Generated<string | null>; | ||
duedate: Generated<string | null>; | ||
overDueDay: Generated<number | null>; | ||
} | ||
|
||
export interface DB { | ||
book: Book; | ||
book_info: BookInfo; | ||
category: Category; | ||
lending: Lending; | ||
likes: Likes; | ||
reservation: Reservation; | ||
reviews: Reviews; | ||
sub_tag: SubTag; | ||
super_tag: SuperTag; | ||
typeorm_metadata: TypeormMetadata; | ||
user: User; | ||
user_reservation: UserReservation; | ||
v_histories: VHistories; | ||
v_lending: VLending; | ||
v_lending_for_search_user: VLendingForSearchUser; | ||
v_search_book: VSearchBook; | ||
v_search_book_by_tag: VSearchBookByTag; | ||
v_stock: VStock; | ||
v_tags_sub_default: VTagsSubDefault; | ||
v_tags_super_default: VTagsSuperDefault; | ||
v_user_lending: VUserLending; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { Kysely, MysqlDialect } from 'kysely'; | ||
import { createPool } from 'mysql2'; | ||
import type { DB } from './generated.ts'; | ||
import { connectOption } from '~/config/index.ts'; | ||
|
||
const { database, host, password, username: user } = connectOption; | ||
|
||
const dialect = new MysqlDialect({ | ||
pool: createPool({ | ||
port: 3306, | ||
connectionLimit: 10, | ||
host, | ||
database, | ||
user, | ||
password, | ||
}), | ||
}); | ||
|
||
export const db = new Kysely<DB>({ | ||
dialect, | ||
log: event => console.log('kysely:', event.query.sql, event.query.parameters), | ||
}); | ||
|
||
export type Database = typeof db; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { z } from 'zod'; | ||
|
||
/** 반환값이 조건을 만족하지 않으면 오류 throw */ | ||
const throwIf = <T>(value: T, ok: (v: T) => boolean) => { | ||
if (ok(value)) { | ||
return value; | ||
} | ||
throw new Error(`값이 예상과 달리 ${value}입니다`); | ||
}; | ||
|
||
export type Visibility = 'public' | 'private' | 'all' | ||
const roles = ['user', 'cadet', 'librarian', 'staff'] as const; | ||
export type Role = typeof roles[number] | ||
|
||
const fromEnum = (role: number): Role => | ||
throwIf(roles[role], (v) => v === undefined); | ||
|
||
export const toRole = (role: Role): number => | ||
throwIf(roles.indexOf(role), (v) => v === -1); | ||
|
||
export const roleSchema = z.number().int().min(0).max(3) | ||
.transform(fromEnum); |
Oops, something went wrong.