Skip to content

Guide · Open source

How to use TypeORM: setup, entities and repositories

Updated · written with AI from the project's own documentation and checked against our catalogue data

TypeORM is an ORM (object-relational mapper) for Node.js that lets you work with database tables as TypeScript or JavaScript classes instead of writing raw SQL for every operation. This guide explains what it does, how entities and repositories fit together, and which database and pattern choices you will face. After reading it you will understand the core model and be ready to set up a project with a supported database.

TypeORMtypeorm/typeorm · ★ 36.7k · MIT

Quick start

  1. 1Check prerequisites. Confirm you have Node.js and a TypeScript or JavaScript project, plus a supported database such as PostgreSQL, MySQL, or SQLite.
  2. 2Get the source. Clone the repository with git clone https://github.com/typeorm/typeorm, as the README describes.
  3. 3Define an entity. Create a class decorated with @Entity() and add @PrimaryGeneratedColumn() and @Column() fields.
  4. 4Create a data source. Configure a data source (for example MyDataSource) that connects to your chosen database.
  5. 5Get a repository. Call getRepository on the data source with your entity to get a repository object.
  6. 6Save and find data. Use save, find and findOneBy on the repository to write and read rows.
  7. 7Pick a pattern. Decide between Data Mapper repositories and Active Record by extending BaseEntity.

What TypeORM is and who it is for

TypeORM runs in Node.js, Browser, Cordova, Ionic, React Native, NativeScript, Expo, and Electron, and can be used with TypeScript and JavaScript (ES2023). It is aimed at developers building apps backed by a relational database, and at teams that want one data layer able to work with several database types.

It is a poor fit if you prefer writing raw SQL directly, since TypeORM adds an abstraction layer you have to learn. Beginners with no database background should learn basic SQL concepts first.

  • Good fit: Node.js or TypeScript developers with relational databases
  • Good fit: projects needing migrations, relations and transactions from a library
  • Poor fit: non-JavaScript projects
  • Poor fit: people who want to write raw SQL directly

Prerequisites before you start

You need Node.js, a TypeScript or JavaScript project, and a supported database such as PostgreSQL, MySQL, or SQLite. The README also lists MariaDB, SQL Server, Oracle, SAP HANA, Google Spanner, and MongoDB among supported databases.

  • Node.js
  • A TypeScript or JavaScript project
  • A supported database such as PostgreSQL, MySQL, or SQLite

Get the TypeORM source

The README's install instruction is to clone the repository. The source page does not list a package-manager install command, so check the project's documentation for the exact steps to add the library to your project.

git clone https://github.com/typeorm/typeorm

Define an entity with decorators

An entity is a class that maps to a database table. Decorators such as @Entity, @PrimaryGeneratedColumn and @Column tell TypeORM how to build the table and its columns. Each property of the class becomes a column.

  • @Entity() marks the class as a table
  • @PrimaryGeneratedColumn() marks the primary key
  • @Column() marks a regular column
import { Entity, PrimaryGeneratedColumn, Column } from "typeorm" @Entity() export class User { @PrimaryGeneratedColumn() id: number @Column() firstName: string @Column() lastName: string @Column() age: number }

Save and query data with a repository (Data Mapper style)

With the Data Mapper style, you get a repository from a data source and use it to save, find and remove entities. The README shows a data source named MyDataSource that returns a repository for the User entity.

  • save() stores a new or updated entity
  • find() returns all matching rows
  • findOneBy() finds one row by fields such as id or firstName
  • remove() deletes an entity
const userRepository = MyDataSource.getRepository(User) const user = new User() user.firstName = "Timber" user.lastName = "Saw" user.age = 25 await userRepository.save(user) const allUsers = await userRepository.find() const timber = await userRepository.findOneBy({ firstName: "Timber", lastName: "Saw", }) await userRepository.remove(timber)

Choose Active Record or Data Mapper

TypeORM supports both Active Record and Data Mapper patterns. In Active Record, the entity extends BaseEntity and handles its own saving and finding. In Data Mapper, a separate repository performs those operations, which keeps entities loosely coupled from persistence code. You can pick the style that suits your project.

  • Active Record: entity extends BaseEntity and calls methods like user.save()
  • Data Mapper: use a repository such as getRepository(User)
@Entity() export class User extends BaseEntity { @PrimaryGeneratedColumn() id: number @Column() firstName: string }

Explore the rest of the feature set

Beyond basic CRUD, the README lists associations (relations), eager and lazy loading, transactions, migrations, connection pooling, replication, a QueryBuilder, and query caching. The project also links sample repositories for Express, Koa, React Native, Electron and other setups, and extensions such as model generation from an existing database.

  • Relations: unidirectional, bidirectional and self-referenced
  • Transactions and migrations, including automatic migration generation
  • QueryBuilder with left and inner joins
  • Sample repositories for Express, Koa, Electron and more

What people use it for

  • Building an Express or Koa API backed by PostgreSQL or MySQL
  • Creating a desktop app with Electron that stores data in SQLite
  • Mobile or hybrid apps using React Native, Ionic or Cordova with a local database
  • Generating entities from an existing database with an extension such as typeorm-model-generator
  • Drawing an ER diagram from entities with typeorm-uml

Frequently asked questions

Is TypeORM free to use?

TypeORM is released under the MIT licence, as shown in the README badge and project metadata. You can use it in commercial and personal projects under those terms.

What databases does TypeORM support?

The README lists Google Spanner, Microsoft SQL Server, MySQL/MariaDB, MongoDB, Oracle, Postgres, SAP HANA and SQLite, along with derived databases. The features list also mentions CockroachDB and sql.js.

What is the difference between Active Record and Data Mapper in TypeORM?

Active Record lets an entity save and find itself through methods on the entity, after it extends BaseEntity. Data Mapper uses separate repository objects for those operations, keeping entities independent of persistence code. TypeORM supports both, so you choose the one you prefer.

Does TypeORM support migrations?

Yes. The README lists migrations and automatic migrations generation among its features. Check the project documentation for the commands used to run them.

How does TypeORM compare to Prisma, Sequelize, MikroORM or Drizzle ORM?

The README's own claim is that TypeORM supports more databases than any other JS/TS ORM and both Active Record and Data Mapper patterns. This guide does not benchmark it against those projects, so compare them on the features you need.

Can I use TypeORM without TypeScript?

Yes. The README says it can be used with TypeScript and JavaScript (ES2023), and the features list includes ESM and CommonJS support.

New agent skills, repos and AI tools — every week

The best new skills and open-source projects, the fastest-growing repos, and one free prompt. Five minutes to read. Unsubscribe anytime.

Our verdict

TypeORM suits Node.js and TypeScript developers who want a single data layer across several relational databases and a choice between Active Record and Data Mapper styles. If you prefer writing raw SQL, are not working in JavaScript, or are new to databases, learn SQL basics first or choose a simpler approach.

Install options & downloads