TypeORM

0.3.28 · active · verified Sat Apr 18

TypeORM is a powerful Data-Mapper ORM designed for TypeScript and ES2021+ applications, providing advanced features for database interaction. It supports a wide array of popular relational and NoSQL databases, including MySQL/MariaDB, PostgreSQL, MS SQL Server, Oracle, SAP HANA, SQLite, and MongoDB. The current stable version is 0.3.28, with frequent patch and minor releases indicating active development.

Common errors

Warnings

Install

Imports

Quickstart

This example demonstrates how to initialize a TypeORM DataSource with SQLite, define a simple User entity, create its schema, and then perform basic save and find operations. It highlights the core steps for getting started with TypeORM in a TypeScript environment.

import "reflect-metadata";
import { DataSource, Entity, PrimaryGeneratedColumn, Column, Repository } from "typeorm";

@Entity()
class User {
    @PrimaryGeneratedColumn()
    id!: number;

    @Column()
    firstName!: string;

    @Column()
    lastName!: string;
}

async function run() {
    const AppDataSource = new DataSource({
        type: "sqlite",
        database: "database.sqlite",
        synchronize: true, // For development, automatically create schema
        entities: [User],
        logging: false,
    });

    await AppDataSource.initialize();
    console.log("Data Source has been initialized!");

    const userRepository: Repository<User> = AppDataSource.getRepository(User);

    const user = new User();
    user.firstName = "John";
    user.lastName = "Doe";
    await userRepository.save(user);
    console.log("User saved:", user);

    const allUsers = await userRepository.find();
    console.log("All users:", allUsers);

    await AppDataSource.destroy();
    console.log("Data Source has been destroyed!");
}

run().catch(error => console.error("Error:", error));

view raw JSON →