TypeORM
The @dugongjs/typeorm package provides adapter implementations for TypeORM.
It has adapters for the following ports:
| Port | Adapter |
|---|---|
IDomainEventRepository | DomainEventRepositoryTypeOrm |
ISnapshotRepository | SnapshotRepositoryTypeOrm |
IConsumedMessageRepository | ConsumedMessageRepositoryTypeOrm |
ITransactionManager | TransactionManagerTypeOrm |
IMessageProducer | OutboxMessageProducerTypeOrm |
IOutboundMessageMapper | OutboxMessageMapperTypeOrm |
Installation
To get started, install the following packages:
- npm
- pnpm
- Yarn
npm install typeorm @dugongjs/typeorm
pnpm add typeorm @dugongjs/typeorm
yarn add typeorm @dugongjs/typeorm
Database drivers
The adapters are database agnostic, but the entity definitions differ between databases. Each supported database therefore has its own entity set, with a dedicated import:
| Database | Import | Driver package |
|---|---|---|
| PostgreSQL | @dugongjs/typeorm/driver/postgres | pg |
| SQLite | @dugongjs/typeorm/driver/sqlite | better-sqlite3 |
The entity classes are named the same in both, so moving between databases changes only the import path:
import { DomainEventEntity } from "@dugongjs/typeorm/driver/postgres";
// or
import { DomainEventEntity } from "@dugongjs/typeorm/driver/sqlite";
Install the driver package for your database alongside TypeORM, as you would for any TypeORM project:
- npm
- pnpm
- Yarn
npm install better-sqlite3
pnpm add better-sqlite3
yarn add better-sqlite3
Importing entities directly from @dugongjs/typeorm resolves to PostgreSQL by default.
Configuring DataSource
Follow the TypeORM documentation to get started. When you create your DataSource, add the following entities:
import { ConsumedMessageEntity, DomainEventEntity, SnapshotEntity } from "@dugongjs/typeorm/driver/postgres";
import { DataSource, type DataSourceOptions } from "typeorm";
const dataSourceOptions: DataSourceOptions = {
type: "postgres",
host: "localhost",
port: 5432,
username: "test",
password: "test",
database: "test",
entities: [
// Your other entities...
DomainEventEntity,
SnapshotEntity,
ConsumedMessageEntity
]
};
const dataSource = new DataSource(dataSourceOptions);
If you wish to use the outbox pattern, also add OutboxEntity to the list of entities.