Registry / database / typeorm

typeorm

JSON →
library0.3.28jsnpmunverified

TypeORM is a highly capable Data-Mapper ORM for TypeScript and ES2021+ applications, providing robust features for interacting with a wide range of databases including MySQL/MariaDB, PostgreSQL, MS SQL Server, Oracle, SAP HANA, SQLite, and MongoDB. The project is under active development, with its current stable version being 0.3.28 and frequent patch releases. TypeORM distinguishes itself through deep TypeScript integration, allowing developers to define entities using decorators and leverage compile-time type safety throughout the data layer. Its data-mapper pattern offers granular control over database interactions, supporting complex relationships, migrations, and custom repositories, making it a powerful alternative to active record ORMs. It requires Node.js >= 16.13.0 and relies on specific database drivers as peer dependencies.

npm install typeorm
INSTALL
IMPORT
SIG · TYPEORM
T
typeorm
databasejavascriptv0.3.28
Install
—
Import
—
Disk
—
Pass rate
0/ 6
Env Coverage0 / 6
glibc
18–22
musl
18–22
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18–226 runs
build_error
glibc
node 18–226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

DataSource
✓ import { DataSource } from 'typeorm';
✗ const DataSource = require('typeorm').DataSource;
DataSource is the primary class for establishing and managing database connections since v0.3.x. Prefer it over createConnection.
Entity
✓ import { Entity, PrimaryGeneratedColumn, Column, Repository } from 'typeorm';
✗ import { Entity } from 'typeorm/decorator/Entity';
Commonly imported decorators and classes for defining database entities and interacting with them.
reflect-metadata
✓ import 'reflect-metadata';
This import must be placed once at the very top of your application's entry file to enable TypeScript decorators. It provides a polyfill.

Demonstrates initializing a SQLite DataSource, defining a User entity with decorators, saving a new user, and fetching all users.

import 'reflect-metadata'; import { DataSource, Entity, PrimaryGeneratedColumn, Column, Repository } from 'typeorm'; @Entity() export class User { @PrimaryGeneratedColumn() id!: number; @Column() firstName!: string; @Column() lastName!: string; @Column() age!: number; } async function bootstrap() { const AppDataSource = new DataSource({ type: 'sqlite', database: 'database.sqlite', entities: [User], synchronize: true, logging: false, }); await AppDataSource.initialize(); console.log('Data Source has been initialized!'); const userRepository: Repository<User> = AppDataSource.getRepository(User); const newUser = new User(); newUser.firstName = 'John'; newUser.lastName = 'Doe'; newUser.age = 30; await userRepository.save(newUser); console.log('User saved:', newUser); const allUsers = await userRepository.find(); console.log('All users:', allUsers); await AppDataSource.destroy(); console.log('Data Source has been destroyed.'); } bootstrap().catch(error => console.error(error));
typeorm --version
Debug
Known issues
breakingThe `delete({})` and `update({}, { ... })` methods on `EntityManager` and `Repository` APIs no longer delete or update all rows when an empty object is provided as criteria. Instead, they will now throw an error.
fix
Explicitly define a WHERE clause for delete/update operations (e.g., `repository.delete({ id: Not(IsNull()) })`) or use `createQueryBuilder().delete().execute()` for full table operations.
affects: >=0.3.23
gotcha`reflect-metadata` is a peer dependency and must be imported once at the very top of your application's entry point to enable TypeScript decorator metadata. Forgetting this will lead to errors like 'No metadata for X entity was found'.
fix
Add `import 'reflect-metadata';` as the first line in your main application file (e.g., `main.ts` or `index.ts`).
affects: >=0.3.21
breakingWhen using MySQL, TypeORM now defaults `connectionOptions.extra.stringifyObjects` to `true` to mitigate a potential security vulnerability in the underlying `mysql` / `mysql2` client libraries. This can change how objects are serialized.
fix
If you relied on the old behavior, you can revert it by explicitly setting `connectionOptions.extra.stringifyObjects = false` in your `DataSource` configuration. Review object serialization behavior carefully.
affects: >=0.3.26
breakingFor SAP HANA connections, TypeORM now utilizes the built-in connection pool of the `@sap/hana-client` library. The `hdb-pool` library is no longer necessary and should be removed from your project.
fix
Remove `hdb-pool` from your project dependencies and update your connection configuration to rely on the `@sap/hana-client` internal pooling.
affects: >=0.3.26
gotchaTypeORM uses CommonJS modules internally in some cases or might cause issues when used in projects with `"type": "module"` in `package.json` leading to 'Cannot use import statement outside a module' errors.
fix
Ensure your TypeScript `tsconfig.json` `module` option is compatible with your runtime environment (e.g., `CommonJS` for Node.js, `ESNext` for bundlers) or configure your build system to correctly transpile TypeORM dependencies.
affects: >=0.3.0
Errors
Common errors & fixes
Error: No metadata for "YourEntityName" entity was found.
The `reflect-metadata` polyfill was not imported or your entity file was not correctly registered with the DataSource.
fix
Ensure `import 'reflect-metadata';` is the very first line in your application's entry file. Also, verify that your entities array in `DataSource` configuration correctly lists your entity classes or glob patterns.
TypeORMError: Connection "default" was not found.
The `DataSource` was not correctly initialized with `await AppDataSource.initialize()` before attempting to use it, or you're trying to get a connection by a name that doesn't exist.
fix
Always call `AppDataSource.initialize()` and `await` its result before performing any database operations. Check your `DataSource` configuration for the correct `name` if you're using multiple connections.
Cannot use import statement outside a module
Mixing CommonJS `require()` and ES module `import` syntax, or running an ES module (`.ts` / `.mjs`) directly with Node.js without proper configuration (e.g., `"type": "module"` in `package.json`).
fix
If using Node.js with ES modules, set `"type": "module"` in your `package.json` and use `import` statements. If using CommonJS, ensure your `tsconfig.json` outputs `CommonJS` and use `require()` where appropriate, or transpile your code before running.
QueryFailedError: SQLITE_CONSTRAINT: UNIQUE constraint failed: entity.column
Attempting to insert or update data that violates a unique constraint defined on a database column, e.g., trying to insert a user with an email that already exists if the email column is unique.
fix
Before inserting or updating, check if an entry with the conflicting unique value already exists. Handle the error gracefully or use `upsert` functionality if applicable.
Upgrade
Version history
0.3.28latest on npm
Audit
Dependencies
reflect-metadatarequiredRequired for decorator support in TypeScript. Must be imported once at the application entry point.
pgoptionalRequired for PostgreSQL database support.
mysql2optionalRequired for MySQL/MariaDB database support.
mongodboptionalRequired for MongoDB database support.
sqlite3optionalRequired for SQLite database support.
better-sqlite3optionalAlternative driver for SQLite, often preferred for performance.
Agent activity
25 hits · last 30 days
node
22
OpenAI (training)
1
Resources