# Fastify के साथ TypeORM — Fastify

Source: https://www.geekswithgeeks.com/hi/fastify/ff-typeorm

> Type-safe database access के लिए TypeORM को integrate करें।

## TypeORM क्यों?

TypeORM type-safe queries, migrations, और relations provide करता है। एक robust, scalable backend के लिए इसे Fastify के साथ जोड़ें।

## TypeORM setup करना

Data source को initialize करें और connection को manage करने के लिए plugin बनाएँ।

```javascript
import { DataSource } from 'typeorm';
import { User } from './entities/User.js';

const AppDataSource = new DataSource({
  type: 'postgres',
  host: 'localhost',
  port: 5432,
  database: 'mydb',
  entities: [User],
  synchronize: true,
});

export default async function typeormPlugin(fastify) {
  await AppDataSource.initialize();
  fastify.decorate('db', AppDataSource);
  
  fastify.addHook('onClose', () => AppDataSource.destroy());
}
```
