TypeORM
TypeORM is a TypeScript ORM. In this guide, you'll learn how to connect TypeORM to Prisma Postgres.
- Node.js version 16 or higher
- TypeScript version 4.5 or higher
Use the TypeORM CLI to generate a starter project:
bunx typeorm init --name typeorm-quickstart --database postgrespnpm dlx typeorm init --name typeorm-quickstart --database postgresyarn dlx typeorm init --name typeorm-quickstart --database postgresnpx typeorm init --name typeorm-quickstart --database postgresThis command will generate a new project with the following structure:
typeorm-quickstart
├── src
│ ├── entity
│ │ └── User.ts # Sample entity
│ ├── migration # Migrations folder
│ ├── data-source.ts # Data source configuration
│ └── index.ts # Application entry point
├── .gitignore
├── package.json
├── README.md
└── tsconfig.jsonNavigate to the project directory and install dependencies:
cd typeorm-quickstart
bun installcd typeorm-quickstart
pnpm installcd typeorm-quickstart
yarn installcd typeorm-quickstart
npm installInstall dotenv to load environment variables:
bun add dotenvpnpm add dotenvyarn add dotenvnpm install dotenvCreate a Prisma Postgres database with the create-db CLI tool:
bunx create-dbpnpm dlx create-dbyarn dlx create-dbnpx create-dbThen the CLI tool should output:
┌ 🚀 Creating a Prisma Postgres database
│
│ Provisioning a temporary database in us-east-1...
│
│ It will be automatically deleted in 24 hours, but you can claim it.
│
◇ Database created successfully!
│
│
● Database Connection
│
│
│ Connection String:
│
│ postgresql://hostname:password@db.prisma.io:5432/postgres?sslmode=require
│
│
◆ Claim Your Database
│
│ Keep your database for free:
│
│ https://create-db.prisma.io/claim?CLAIM_CODE
│
│ Database will be deleted on 11/18/2025, 1:55:39 AM if not claimed.
│
└Create a .env file and add the connection string from the output:
DATABASE_URL="postgresql://hostname:password@db.prisma.io:5432/postgres?sslmode=require"The database created is temporary and will be deleted in 24 hours unless claimed. Claiming moves the database into your Prisma Data Platform account. Visit the claim URL from the output to keep your database.
Update the src/data-source.ts file to use your Prisma Postgres connection:
import "reflect-metadata";
import "dotenv/config";
import { DataSource } from "typeorm";
import { User } from "./entity/User";
// Parse DATABASE_URL into connection parameters
function parseConnectionString(url: string) {
const parsed = new URL(url);
return {
host: parsed.hostname,
port: parseInt(parsed.port),
username: parsed.username,
password: parsed.password,
database: parsed.pathname.slice(1), // Remove leading '/'
};
}
const connectionParams = parseConnectionString(process.env.DATABASE_URL!);
export const AppDataSource = new DataSource({
type: "postgres",
host: "localhost",
port: 5432,
username: "test",
password: "test",
database: "test",
...connectionParams,
ssl: true,
synchronize: true,
logging: false,
entities: [User],
migrations: [],
subscribers: [],
});Start the application:
bun startpnpm startyarn startnpm startYou should see output indicating the connection was successful and a new user was inserted into the database:
Inserting a new user into the database...
Saved a new user with id: 1
Loading users from the database...
Loaded users: [ User { id: 1, firstName: 'Timber', lastName: 'Saw', age: 25 } ]TypeORM is now connected to Prisma Postgres. For entities, migrations, and queries, see the TypeORM documentation.