# typeorm

***

**1. Create a New Entity**

```typescript
@Entity()
export class User {
  @PrimaryGeneratedColumn()
  id: number;

  @Column()
  name: string;

  @Column()
  email: string;
}
```

**2. Find All Users**

```typescript
const users = await userRepository.find();
```

**3. Find a User by ID**

```typescript
const user = await userRepository.findOne(1);
```

**4. Find Users by Name**

```typescript
const users = await userRepository.findByName("John");
```

**5. Save a New User**

```typescript
const user = new User();
user.name = "John";
user.email = "john@example.com";
await userRepository.save(user);
```

**6. Update a User**

```typescript
const user = await userRepository.findOne(1);
user.name = "Jane";
await userRepository.save(user);
```

**7. Delete a User**

```typescript
await userRepository.delete(1);
```

**8. Create a Many-to-One Relationship**

```typescript
@Entity()
export class Order {
  @PrimaryGeneratedColumn()
  id: number;

  @Column()
  userId: number;

  @ManyToOne(() => User, user => user.orders)
  user: User;
}
```

**9. Create a One-to-Many Relationship**

```typescript
@Entity()
export class User {
  @PrimaryGeneratedColumn()
  id: number;

  @Column()
  name: string;

  @OneToMany(() => Order, order => order.user)
  orders: Order[];
}
```

**10. Create a Many-to-Many Relationship**

```typescript
@Entity()
export class User {
  @PrimaryGeneratedColumn()
  id: number;

  @Column()
  name: string;

  @ManyToMany(() => Group, group => group.users)
  groups: Group[];
}

@Entity()
export class Group {
  @PrimaryGeneratedColumn()
  id: number;

  @Column()
  name: string;

  @ManyToMany(() => User, user => user.groups)
  users: User[];
}
```

**11. Use a Query Builder**

```typescript
const users = await userRepository.createQueryBuilder("user")
  .where("user.name like :name", { name: "%John%" })
  .getMany();
```

**12. Use a Repository**

```typescript
const users = await userRepository.find({
  where: { name: "John" },
  order: { id: "ASC" },
  take: 10,
  skip: 0,
});
```

**13. Use a Data Transformer**

```typescript
@Entity()
export class User {
  @PrimaryGeneratedColumn()
  id: number;

  @Column()
  name: string;

  @Transformer((value) => value.toUpperCase(), (value) => value.toLowerCase())
  email: string;
}
```

**14. Use a Custom Repository**

```typescript
export class UserRepository extends Repository<User> {
  async findByName(name: string): Promise<User[]> {
    return this.find({ where: { name } });
  }
}
```

**15. Use a Middleware**

```typescript
export function myMiddleware(req: Request, res: Response, next: NextFunction) {
  // Do something before the request is processed
  next();
  // Do something after the request is processed
}
```

**16. Use a Plugin**

```typescript
export function myPlugin(connection: Connection) {
  // Do something when the connection is established
}
```

**17. Use a Custom TypeORM Driver**

```typescript
export class MyDriver implements Driver {
  // Implement the Driver interface
}
```

**18. Use a Custom TypeORM Query Runner**

```typescript
export class MyQueryRunner implements QueryRunner {
  // Implement the QueryRunner interface
}
```

**19. Use a Custom TypeORM Entity Manager**

```typescript
export class MyEntityManager implements EntityManager {
  // Implement the EntityManager interface
}
```

**20. Use a Custom TypeORM Schema Manager**

```typescript
export class MySchemaManager implements SchemaManager {
  // Implement the SchemaManager interface
}
```

**21. Use a Custom TypeORM Migration**

```typescript
export class MyMigration implements Migration {
  // Implement the Migration interface
}
```

**22. Use a Custom TypeORM Extension**

```typescript
export class MyExtension implements Extension {
  // Implement the Extension interface
}
```

**23. Use a Custom TypeORM Interceptor**

```typescript
export class MyInterceptor implements Interceptor {
  // Implement the Interceptor interface
}
```

**24. Use a Custom TypeORM Subscriber**

```typescript
export class MySubscriber implements Subscriber {
  // Implement the Subscriber interface
}
```

**25. Use a Custom TypeORM Event Listener**

```typescript
export class MyEventListener implements EventListener {
  // Implement the EventListener interface
}

```
