TypeORM
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
-
Cannot delete without criteria. Pass 'true' to delete all entities.
cause Attempting to delete all entities by calling `repository.delete({})` without explicitly confirming the intent to delete all. This behavior changed in 0.3.23 to prevent accidental mass deletion.fixIf you intend to delete all entities, change the call to `repository.delete(true)`. -
TypeError: Reflect.decorate is not a function
cause The `reflect-metadata` polyfill, essential for TypeScript decorators used by TypeORM, has not been installed or imported at the application's entry point.fixInstall `reflect-metadata` (`npm install reflect-metadata`) and add `import "reflect-metadata";` as the very first line in your application's entry file. -
No driver for database type "..." was found installed. Try to install the corresponding driver for your database.
cause TypeORM requires a specific database driver (e.g., `pg` for PostgreSQL, `mysql2` for MySQL) to be installed as a peer dependency, but it's missing for the configured database type.fixInstall the correct database driver for your chosen database, e.g., `npm install pg` for PostgreSQL or `npm install mysql2` for MySQL. -
No entities were found in the given directory: ...
cause The `entities` array in your `DataSource` configuration is empty, or the glob patterns/paths specified for your entities are incorrect, preventing TypeORM from discovering your entity classes.fixEnsure your `DataSource` configuration's `entities` array correctly lists your entity classes or provides accurate glob patterns to their location (e.g., `[User, Post]`, `["**/*.entity{.ts,.js}"]`). -
Cannot read properties of undefined (reading 'sync')
cause This error was a bug in previous versions of TypeORM related to an upgrade of the `glob` package.fixUpdate TypeORM to version `0.3.19` or later, as this issue was resolved in `0.3.19` and confirmed fixed in `0.3.20`.
Warnings
- breaking Calling `repository.delete({})` or `repository.update({}, data)` no longer deletes/updates all rows. Instead, it throws an error to prevent accidental mass deletion/updates.
- gotcha The `reflect-metadata` polyfill is required for TypeORM's decorators to work but must be explicitly installed and imported once at the top of your application's entry file.
- gotcha When using MySQL, TypeORM now defaults `connectionOptions.extra.stringifyObjects` to `true` to mitigate a potential security vulnerability in underlying MySQL drivers.
- deprecated For SAP HANA connections, TypeORM now utilizes the built-in connection pooling from the `@sap/hana-client` library. The external `hdb-pool` library is deprecated.
Install
-
npm install typeorm -
yarn add typeorm -
pnpm add typeorm
Imports
- DataSource
const DataSource = require('typeorm')import { DataSource } from 'typeorm'
Quickstart
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));