DEV Community

Beaver Bridge
Beaver Bridge

Posted on

Sveltekit + TypeScript + TypeORM + ESModule

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 tsconfig-paths 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 -r tsconfig-paths/register ./node_modules/typeorm/cli.js --dataSource src/lib/typeorm/config.ts",
  "migration:create": "tsx -r tsconfig-paths/register ./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

Top comments (0)