DEV Community

Beaver Bridge
Beaver Bridge

Posted on • Edited on

1

Sveltekit + TypeScript + TypeORM + ESM

SvelteKit Project 생성

npm create svelte@latest test1
# Skeleton project
# using TypeScript
# check ESLint, Prettier, Playwright, Vitest

cd test1 

bun i
Enter fullscreen mode Exit fullscreen mode

TypeORM 설정

참조: https://typeorm.io/#installation

bun add typeorm reflect-metadata pg

bun add @types/node tsx -d
Enter fullscreen mode Exit fullscreen mode

tsconfig.json

"compilerOptions": {
  ...
  "emitDecoratorMetadata": true,
  "experimentalDecorators": true
}
Enter fullscreen mode Exit fullscreen mode

package.json

"scripts": {
  ...
  "typeorm": "tsx ./node_modules/typeorm/cli.js --dataSource src/lib/typeorm/config.ts",
  "migration:create": "tsx ./node_modules/typeorm/cli.js migration:create src/lib/typeorm/migrations/Migration",
  "migration:generate": "npm run typeorm migration:generate src/lib/typeorm/migrations/Migration",
  "migration:run": "npm run typeorm migration:run"
}
Enter fullscreen mode Exit fullscreen mode

src/lib/typeorm/config.ts

import { DataSource } from 'typeorm';

export const AppDataSource = new DataSource({
  type: 'postgres',
  host: 'localhost',
  port: 5432,
  username: 'postgres',
  password: 'default_password',
  database: 'postgres',
  synchronize: false,
  logging: true,
  entities: ['src/lib/typeorm/entity/*.ts'],
  subscribers: [],
  migrations: ['src/lib/typeorm/migrations/*.ts']
});
Enter fullscreen mode Exit fullscreen mode

테스트

npm run migration:create
Enter fullscreen mode Exit fullscreen mode

이렇게하니 TypeORM 에서 migration이나 CRUD에서 Unknown file extension ".ts" 가 발생한다.

아무리 찾아봐도 해답은 없는 것 같고, 이렇게 하는게 최선인 것 같다.

src/lib/typeorm/config.ts


import { User } from './entity/User';
import { Company } from './entity/Company';

...
entities: [User, Company],
migrations: ['dist/lib/typeorm/migrations/*.{ts, js}']
...
Enter fullscreen mode Exit fullscreen mode

결국은 해결하지 못했다. 다른 Prisma, Sequelize, MikroORM, DrizzleORM 을 모두 확인해봤지만, TypeORM만큼 마이그레이션용으로 괜찮은 건 없는 것 같고(그나마 MikroORM이 가장 근접했다), 결국 마이그레이션은 TypeORM으로, 그후에 Drizzle의 db:pull로 가져온 후 CRUD는 Drizzle을 사용하기로 했다.


migrations: ['src/lib/typeorm/migrations/*.ts'] 이 부분은 평소에는 주석처리를 해놓고 마이그레이션 할 때만 해제해서 사용하는 걸로 사용 중이다. drizzle은 제거 했다.


config.tsconfig.migration.ts 로 나눠서, package.json 넣는 정보는 마이그레이션에서만 사용하니 config.migration.ts 로 등록하고, 실제 코드는 config.ts 를 사용하고 있다.

Postmark Image

Speedy emails, satisfied customers

Are delayed transactional emails costing you user satisfaction? Postmark delivers your emails almost instantly, keeping your customers happy and connected.

Sign up

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay