Aller au contenu principal
Version: 1.x

Create Models & Queries

foal generate entity my-entity


Simple models are called entities in TypeORM. You can generate one with the above command.


import { Column, Entity, PrimaryGeneratedColumn } from 'typeorm';

export class Product {

id: number;

name: string;

price: number;


The class Product represents the database table and its instances represent the table rows.

In FoalTS, entity files should always be named with the extension .entity.ts. This way the CLI can find the entities when automatically generating migrations.

Using Entities

import { getRepository } from 'typeorm';

const repository = getRepository(Product);

const product = new Product(); = 'chair';
product.price = 60;

const products = await repository.find();
// find by id:
const firstProduct = await repository.findOne(1);
const chair = await repository.findOne({ name: 'chair' });

await repository.remove(chair);


import { getRepository } from 'typeorm';

const firstProduct = await getRepository(Product)
.where(' = :id', { id: 1 })