Version: 2.0.0 (latest)

Create Models & Queries

You are reading the documentation for version 2 of FoalTS. Instructions for upgrading to this version are available here. The old documentation can be found here.

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 })