This document describes how to use TypeORM, an ORM that can run in NodeJS, Browser, Cordova, PhoneGap, Ionic, React Native, NativeScript, Expo, and Electron platforms and can be used with TypeScript and JavaScript (ES2021), with YugabetyDB.

Prerequisites

Before you can start using TypeORM, ensure that you have the following available:

npm install pg --save

Using TypeORM

To start using TypeORM with YugabyteDB, first generate the starter project from the CLI. This project creates a table user and inserts a row in it, which can be seen in the index.ts file of the project.

npx typeorm init --name MyProject --database postgres

The next step is to install new project dependencies:

cd MyProject
npm install

After you have all dependencies installed, edit the data-source.ts file and put the following there:

import "reflect-metadata"
import { DataSource } from "typeorm"
import { User } from "./entity/User"

export const AppDataSource = new DataSource({
    type: "postgres",
    host: "localhost",
    port: 5433,
    username: "yugabyte",
    password: "yugabyte",
    database: "yugabyte",
    synchronize: true,
    logging: false,
    entities: [User],
    migrations: [],
    subscribers: [],
})

To run the application, execute the following command:

npm start

Testing the code

You can verify the code execution by looking for the changes inside the database, as follows:

  • Navigate to your YugabyteDB installation directory by running the following command:

    cd /<path-to-yugabytedb>
    
  • Run the ysqlsh client by executing the following command:

    ./bin/ysqlsh
    
  • Obtain the list of all the tables in the database by executing the following command:

    \dt
    
  • Check if rows have been inserted into the table by executing the following:

    SELECT * FROM public."user;"
    

    The output should be as follows:

     id | firstName | lastName | age 
     ----+-----------+----------+-----
     1 | Timber    | Saw      |  25
     (1 row)