Logo

dev-resources.site

for different kinds of informations.

Sveltekit + TypeScript + TypeORM + ESM

Published at
10/21/2024
Categories
sveltekit
typescript
typeorm
Author
__aa3e4bc832ba7032bfa3
Categories
3 categories in total
sveltekit
open
typescript
open
typeorm
open
Author
22 person written this
__aa3e4bc832ba7032bfa3
open
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.ts์™€ config.migration.ts ๋กœ ๋‚˜๋ˆ ์„œ, package.json ๋„ฃ๋Š” ์ •๋ณด๋Š” ๋งˆ์ด๊ทธ๋ ˆ์ด์…˜์—์„œ๋งŒ ์‚ฌ์šฉํ•˜๋‹ˆ config.migration.ts ๋กœ ๋“ฑ๋กํ•˜๊ณ , ์‹ค์ œ ์ฝ”๋“œ๋Š” config.ts ๋ฅผ ์‚ฌ์šฉํ•˜๊ณ  ์žˆ๋‹ค.

typeorm Article's
30 articles in total
Favicon
Creating Typescript app with decorator-based dependency injection ๐Ÿ’‰
Favicon
ORM and Migrating/Adding Data to MySql Database from MongoDb using TypeOrm in javaScript
Favicon
๐ŸŒŸ NestJS + Databases: Making Snake Case Seamless!๐Ÿ
Favicon
NestJS TypeORM and Multi-Tenancy
Favicon
Nx + TypeORM + NestJS + Migrations
Favicon
Handling TypeORM migrations in Electron apps
Favicon
How to manage multiple environments with dotenv and Databases config in NestJS
Favicon
Sveltekit + TypeScript + TypeORM + ESM
Favicon
Using TypeORM with TSX: A Smoother Development Experience
Favicon
Prisma or TypeORM ?
Favicon
Mock TypeORM Package
Favicon
Connecting a Serverless PostgreSQL Database (Neon) to NestJS Using the Config Service
Favicon
NestJS and TypeORMโ€Šโ€”โ€ŠEfficient Schema-Level Multi-Tenancy with Auto Generated Migrations: A DXย Approach
Favicon
Getting Started with NestJS and TypeORM: A Beginner's Guide
Favicon
Migration - Module query with TypeORM version 0.3.x
Favicon
Double bind the foreign key to avoid unnecessary JOIN in TypeORM
Favicon
Handling Migrations on NestJS with TypeORM
Favicon
match all conditions in the first array and at least one condition for the second array typeorm
Favicon
Taming cross-service database transactions in NestJS with AsyncLocalStorage
Favicon
Announcing Version 2.0 of nestjs-DbValidator
Favicon
Optimizing SQL Queries by 23x!!!
Favicon
Building my own PostgresGUI with TypeORM+TypeGraphQl class generaion
Favicon
TypeORM | Query Builder
Favicon
NestJs์—์„œ TypeORM์„ ์‚ฌ์šฉํ•˜์—ฌ MySQL ์—ฐ๋™, 2024-01-25
Favicon
Defining Custom Many-to-many Relationship in NestJS TypeORM.
Favicon
4. Building an Abstract Repository
Favicon
3. Building a Common Repository for Nest.js Microservices
Favicon
TypeORM - remove children with orphanedRowAction
Favicon
Migrating NestJS project with TypeORM to Prisma
Favicon
Authentication part 2 using NestJS

Featured ones: