TypeORM Integration
DolphJS provides full integration for SQL databases via TypeORM. You get auto-initialization from the dolph_config.yaml file, and direct access to the DataSource object without having to build boilerplate setup scripts.
Configuration
Section titled “Configuration”To connect to your database via TypeORM, simply declare the typeorm block in your dolph_config.yaml.
The options object provided maps exactly to TypeORM’s official DataSourceOptions. This guarantees that you can use every single TypeORM feature, such as synchronize, logging, subscribers, migrations, and more.
database: typeorm: options: type: postgres host: localhost port: 5432 username: root password: sensitive database: dolph_db synchronize: true logging: false entities: - "src/models/*.ts"When you start your DolphJS application, the DolphFactory reads this block and automatically connects the DataSource — but not before your services are constructed. See the callout below before wiring up getDataSource() in a service.
Defining the Entity
Section titled “Defining the Entity”Let’s create a standard TypeORM entity.
import { Entity, PrimaryGeneratedColumn, Column } from 'typeorm';
@Entity()export class User { @PrimaryGeneratedColumn() id: number;
@Column() firstName: string;
@Column() lastName: string;
@Column({ default: true }) isActive: boolean;}Using TypeORM in Services
Section titled “Using TypeORM in Services”Since TypeORM is globally auto-initialized by DolphJS, you can easily access the underlying DataSource anywhere inside your services using the getDataSource() function from @dolphjs/dolph/packages/typeorm.
import { DService } from '@dolphjs/dolph/decorators';import { getDataSource } from '@dolphjs/dolph/packages/typeorm';import { User } from '../models/user.entity';
@DService('userService')export class UserService { // A getter, not a field initializer — see the caution above. private get userRepo() { return getDataSource().getRepository(User); }
async createUser(firstName: string, lastName: string) { const user = new User(); user.firstName = firstName; user.lastName = lastName;
// Save to the database return await this.userRepo.save(user); }
async getAllActiveUsers() { return await this.userRepo.find({ where: { isActive: true }, }); }}Wrapping Up
Section titled “Wrapping Up”By passing configuration down via the native options block, your dolph_config.yaml becomes the central source of truth, and TypeORM fits naturally into your standard DolphJS architecture.