Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

typeorm

Package Overview
Dependencies
Maintainers
3
Versions
840
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

typeorm

Data-Mapper ORM for TypeScript, ES7, ES6, ES5. Supports MySQL, PostgreSQL, MariaDB, SQLite, MS SQL Server, Oracle, MongoDB databases.

  • 0.3.20
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
2.1M
decreased by-9.46%
Maintainers
3
Weekly downloads
 
Created

What is typeorm?

TypeORM is an Object-Relational Mapper (ORM) for Node.js that can be used with TypeScript or JavaScript (ES5, ES6, ES7, ES8). It supports both Active Record and Data Mapper patterns, unlike other JavaScript ORMs. You can use it with TypeScript and JavaScript (ES5, ES6, ES7, ES8). It's highly influenced by other ORMs, such as Hibernate, Doctrine, and Entity Framework.

What are typeorm's main functionalities?

Entity Definition

This code sample demonstrates how to define an entity in TypeORM. Entities are the equivalent of tables in a database, and each instance of an entity represents a row in the table.

import {Entity, PrimaryGeneratedColumn, Column} from 'typeorm';

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

    @Column()
    firstName: string;

    @Column()
    lastName: string;

    @Column()
    isActive: boolean;
}

CRUD Operations

This code sample shows how to perform CRUD (Create, Read, Update, Delete) operations using the repository API provided by TypeORM.

import {getRepository} from 'typeorm';
import {User} from './entity/User';

async function createUser() {
    const userRepository = getRepository(User);
    const user = new User();
    user.firstName = 'Timber';
    user.lastName = 'Saw';
    user.isActive = true;
    await userRepository.save(user);
}

async function findUserById(id) {
    const userRepository = getRepository(User);
    return await userRepository.findOne(id);
}

Data Querying

This code sample illustrates how to query data using the QueryBuilder API, which allows for building SQL queries in a programmatic and type-safe manner.

import {getConnection} from 'typeorm';

async function getUsersByName(firstName) {
    const users = await getConnection()
        .getRepository(User)
        .createQueryBuilder('user')
        .where('user.firstName = :firstName', { firstName })
        .getMany();
    return users;
}

Transactions

This code sample demonstrates how to perform transactions, ensuring that all operations within the transaction block either complete successfully together or are all rolled back.

import {getConnection} from 'typeorm';

async function updateUserLastName(id, lastName) {
    await getConnection().transaction(async transactionalEntityManager => {
        const user = await transactionalEntityManager.findOne(User, id);
        user.lastName = lastName;
        await transactionalEntityManager.save(user);
    });
}

Migrations

This code sample shows how to define a migration class in TypeORM. Migrations are a way to manage database schema changes and can be automatically generated or manually written.

import {MigrationInterface, QueryRunner} from 'typeorm';

export class CreateUserTable1588102412341 implements MigrationInterface {
    public async up(queryRunner: QueryRunner): Promise<void> {
        await queryRunner.query(`CREATE TABLE user (id int PRIMARY KEY, firstName varchar(255), lastName varchar(255))`);
    }

    public async down(queryRunner: QueryRunner): Promise<void> {
        await queryRunner.query('DROP TABLE user');
    }
}

Other packages similar to typeorm

FAQs

Package last updated on 26 Jan 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc