TypeORM for JavaScript (Node.js): Complete Guide
\n\n1. What is it and why is it needed
\n\n TypeORM is a powerful Object-Relational Mapping (ORM) library designed for working with databases in the Node.js environment. It supports both TypeScript and JavaScript, providing developers with a convenient way to interact with relational databases (PostgreSQL, MySQL, MariaDB, SQLite, Microsoft SQL Server, Oracle, and CockroachDB) through an object-oriented approach. Instead of writing raw SQL queries, you work with entities — regular JavaScript classes that are automatically mapped to database tables.\n
\n\n Why is TypeORM needed? First, it significantly speeds up development: you don't need to manually write SQL queries for CRUD operations (Create, Read, Update, Delete). Second, it provides type safety (if you use TypeScript) and protection against SQL injections through the use of parameterized queries. Third, TypeORM provides a powerful Query Builder for complex queries, migrations for managing the database schema, and support for relationships between tables (one-to-one, one-to-many, many-to-many). This makes it an ideal choice for building scalable and maintainable applications.\n
\n\n Unlike some other ORMs, TypeORM actively uses decorators (in TypeScript) or annotations (in JavaScript via experimental features), making the code declarative and intuitive. The library also supports Active Record and Data Mapper patterns, allowing the developer to choose the most suitable working style. Thanks to its flexibility and rich functionality, TypeORM is one of the most popular ORMs in the Node.js ecosystem.\n
\n2. Installation
\n\n To install TypeORM in your Node.js project, use the npm package manager. You will also need to install a driver for your database. Below are the commands for the most popular DBMS.\n
\n# Install TypeORM\nnpm install typeorm --save\n\n# Install driver for PostgreSQL\nnpm install pg --save\n\n# Install driver for MySQL / MariaDB\nnpm install mysql2 --save\n\n# Install driver for SQLite\nnpm install sqlite3 --save\n\n# Install driver for Microsoft SQL Server\nnpm install mssql --save\n\n# Install driver for CockroachDB\nnpm install pg --save\n\n# For TypeScript also install reflect-metadata and types\nnpm install reflect-metadata --save\nnpm install @types/node --save-dev\n \n After installation, make sure the following settings are enabled in your tsconfig.json (if using TypeScript):\n
{\n "emitDecoratorMetadata": true,\n "experimentalDecorators": true\n}\n \n For JavaScript (Node.js), these settings are not required, but you can use experimental decorators if you wish.\n
\n3. Quick Start — Minimal Working Example
\n\n Let's create a simple application that connects to an SQLite database (a file-based DB that does not require a server installation) and performs basic operations.\n
\n\n Create a file index.js and add the following code:\n
const { createConnection, EntitySchema } = require("typeorm");\n\n// Define an entity using EntitySchema\nconst UserSchema = new EntitySchema({\n name: "User",\n tableName: "users",\n columns: {\n id: {\n primary: true,\n type: "int",\n generated: true\n },\n firstName: {\n