Aller au contenu principal
Version: 2.10.2 (latest)

Créer des Modèles & Requêtes

foal generate entity my-entity


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


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

export class Product extends BaseEntity {

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

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

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

await chair.remove();


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