Skip to content

Commit

Permalink
rafactor: prisma -> kysely 마이그레이션
Browse files Browse the repository at this point in the history
  • Loading branch information
leemhoon00 committed Nov 17, 2024
1 parent f9427bc commit f4692ee
Show file tree
Hide file tree
Showing 9 changed files with 101 additions and 42 deletions.
1 change: 0 additions & 1 deletion .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ jobs:
npm install
npx tsc -p tsconfig.json
npm install --omit=dev
npx prisma generate
mv node_modules dist/
cd dist
zip -r ../dist.zip .
Expand Down
7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,15 @@
"devDependencies": {
"@types/aws-lambda": "^8.10.145",
"@types/dotenv": "^6.1.1",
"@types/pg": "^8.11.10",
"prisma": "^5.22.0",
"prisma-kysely": "^1.8.0",
"ts-node": "^10.9.2",
"typescript": "^5.6.3"
},
"dependencies": {
"@prisma/client": "^5.21.1",
"dotenv": "^16.4.5",
"prisma": "^5.21.1"
"kysely": "^0.27.4",
"pg": "^8.13.1"
}
}
11 changes: 4 additions & 7 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
// generator client {
// provider = "prisma-client-js"
// }

generator client {
provider = "prisma-client-js"
binaryTargets = ["native", "linux-arm64-openssl-1.0.x"]
generator kysely {
provider = "prisma-kysely"
output = "../src/db"
fileName = "types.ts"
}

datasource db {
Expand Down
14 changes: 6 additions & 8 deletions src/course/course.service.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import type { PrismaClient } from '@prisma/client';
import type { Database } from 'src/db/database';

export class CourseService {
key: string;
url: string;
prisma: PrismaClient;
constructor(prisma: PrismaClient) {
db: Database;
constructor(db: Database) {
this.key = process.env.KEY!;
this.url =
'http://apis.data.go.kr/B551014/SRVC_OD_API_FACIL_COURSE/todz_api_facil_course_i';
this.prisma = prisma;
this.db = db;
}

async getLoopCount() {
Expand All @@ -26,7 +26,7 @@ export class CourseService {
}

async saveAllCourse() {
await this.prisma.course.deleteMany();
await this.db.deleteFrom('Course').execute();
const loopCount = await this.getLoopCount();

for (let i = 1; i <= loopCount; i++) {
Expand Down Expand Up @@ -101,9 +101,7 @@ export class CourseService {
};
});

await this.prisma.course.createMany({
data: courses,
});
await this.db.insertInto('Course').values(courses).execute();
}
}
}
Expand Down
14 changes: 6 additions & 8 deletions src/course/special-course.service.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import type { PrismaClient } from '@prisma/client';
import type { Database } from 'src/db/database';

export class SpecialCourseService {
key: string;
url: string;
prisma: PrismaClient;
constructor(prisma: PrismaClient) {
db: Database;
constructor(db: Database) {
this.key = process.env.KEY!;
this.url =
'http://apis.data.go.kr/B551014/SRVC_DVOUCHER_FACI_COURSE/TODZ_DVOUCHER_FACI_COURSE';
this.prisma = prisma;
this.db = db;
}

async getLoopCount() {
Expand All @@ -27,7 +27,7 @@ export class SpecialCourseService {
}

async saveAllCourse() {
await this.prisma.specialCourse.deleteMany();
await this.db.deleteFrom('SpecialCourse').execute();
const loopCount = await this.getLoopCount();

for (let i = 1; i <= loopCount; i++) {
Expand Down Expand Up @@ -101,9 +101,7 @@ export class SpecialCourseService {
});
}

await this.prisma.specialCourse.createMany({
data: courses,
});
await this.db.insertInto('SpecialCourse').values(courses).execute();
}
}
}
Expand Down
20 changes: 20 additions & 0 deletions src/db/database.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { DB } from './types';
import { Pool } from 'pg';
import { Kysely, PostgresDialect } from 'kysely';

const dialect = new PostgresDialect({
pool: new Pool({
database: process.env.DATABASE_NAME,
host: process.env.DATABASE_HOST,
user: process.env.DATABASE_USER,
password: process.env.DATABASE_PASSWORD,
port: Number(process.env.DATABASE_PORT),
max: Number(process.env.DATABASE_MAX),
}),
});

export const db = new Kysely<DB>({
dialect,
});

export type Database = typeof db;
48 changes: 48 additions & 0 deletions src/db/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import type { ColumnType } 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 type Timestamp = ColumnType<Date, Date | string, Date | string>;

export type Course = {
businessId: string;
facilitySerialNumber: string;
courseId: string;
courseName: string;
itemCode: string;
itemName: string;
instructor: string | null;
startTime: string;
endTime: string;
workday: string;
price: number;
};
export type Facility = {
businessId: string;
serialNumber: string;
name: string;
cityCode: string;
cityName: string;
localCode: string;
localName: string;
address: string;
detailAddress: string | null;
owner: string;
phone: string | null;
};
export type SpecialCourse = {
businessId: string;
courseId: string;
courseName: string;
itemName: string;
startTime: string;
endTime: string;
workday: string;
price: number;
type: string;
};
export type DB = {
Course: Course;
Facility: Facility;
SpecialCourse: SpecialCourse;
};
15 changes: 7 additions & 8 deletions src/facility/facility.service.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { PrismaClient } from '@prisma/client';
import type { Database } from 'src/db/database';

export class FacilityService {
key: string;
url: string;
prisma: PrismaClient;
constructor(prisma: PrismaClient) {
db: Database;
constructor(db: Database) {
this.key = process.env.KEY!;
this.url =
'http://apis.data.go.kr/B551014/SRVC_OD_API_FACIL_MNG/todz_api_facil_mng_i';
this.prisma = prisma;
this.db = db;
}

async getLoopCount() {
Expand All @@ -25,7 +25,7 @@ export class FacilityService {
}

async saveAllFacility() {
await this.prisma.facility.deleteMany();
await this.db.deleteFrom('Facility').execute();
const loopCount = await this.getLoopCount();

for (let i = 1; i <= loopCount; i++) {
Expand Down Expand Up @@ -72,9 +72,8 @@ export class FacilityService {
detailAddress: item.faci_daddr || null,
});
}
await this.prisma.facility.createMany({
data: facilities,
});

await this.db.insertInto('Facility').values(facilities).execute();
}
}

Expand Down
13 changes: 5 additions & 8 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
import * as dotenv from 'dotenv';
import { PrismaClient } from '@prisma/client';
dotenv.config();
import { FacilityService } from './facility/facility.service';
import { CourseService } from './course/course.service';
import { SpecialCourseService } from './course/special-course.service';
import { db } from './db/database';
import { Handler } from 'aws-lambda';

dotenv.config();

export const handler: Handler = async () => {
const prisma = new PrismaClient();

const facilityService = new FacilityService(prisma);
const courseService = new CourseService(prisma);
const specialCourseService = new SpecialCourseService(prisma);
const facilityService = new FacilityService(db);
const courseService = new CourseService(db);
const specialCourseService = new SpecialCourseService(db);

await Promise.all([
facilityService.saveAllFacility(),
Expand Down

0 comments on commit f4692ee

Please sign in to comment.