Sveltekit + TypeScript + TypeORM + ESM

Beaver Bridge - Oct 21 - - Dev Community

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을 사용하기로 했다.

. . . . . . . . . . . . . . . . . . . . . . . . . . .
Terabox Video Player