Entity
Entity
An entity is a plain TypeScript class representing a domain object — your business model, free of any framework or database concerns. It lives in a feature's domain/ folder. You write it by hand (SoapJS has no scaffolding CLI):
export type CharacterUniverse = 'marvel' | 'dc';
export type CharacterAlignment = 'hero' | 'villain' | 'anti-hero';
export class Character {
constructor(
public readonly id: string,
public readonly name: string,
public readonly universe: CharacterUniverse,
public readonly alignment: CharacterAlignment,
public readonly createdAt: Date,
public readonly updatedAt: Date,
) {}
}Validated factories
Keep invariants in the domain: expose a static create() that validates untrusted input and returns a Result, reserving the constructor for trusted rehydration (the mapper, seeds):
import { Result, Failure } from '@soapjs/soap';
static create(props: CharacterProps): Result<Character> {
if (!props.name?.trim()) {
return Result.withFailure(Failure.fromError(new ValidationError('name is required')));
}
return Result.withSuccess(new Character(/* ...validated fields */));
}Entities are pure — no decorators, no DB types. The database shape lives in a Model; a Mapper converts between the two. See the demo for full examples.
Last updated