Structure a shared backend library
A library of cross-cutting infrastructure (@org/infra-*) that every backend service imports, so the
fleet is consistent and DRY. This is the where & how it's packaged; the framework primitives inside
it are in write-cross-cutting-code and design-an-error-model. Frontend equivalent:
structure-a-shared-ui-lib.
When to use
You're starting or reorganizing the shared lib behind a fleet of services, deciding which package a
new primitive goes in, or pulling duplicated infra out of services into one place.
1. Split into focused packages by dependency weight
▸ Other stacks: a Go internal/ module set, a Python namespace package, a Java multi-module
artifact. Principle: partition by concern + dependency direction; the foundational package has the
fewest deps and is imported by the rest, never the reverse.
2. One barrel per package; import from the package root
- Each package exposes a single
index.ts (barrel) that re-exports its public surface. Services import
from the package root (@org/infra-common), never deep paths (@org/infra-common/src/...) —
so internals can move without breaking consumers.// infra-common/src/index.ts
export * from './typeorm'; export * from './pipes'; export * from './dto'; export * from './utils'; /* … */
// in a service:
import { BaseEntity, BaseQueryDto, Nullable } from '@org/infra-common'; // root, not a deep path
▸ Other stacks: a package's public API file / __init__.py / exported module list. Principle: one
published surface per package; internals are private.
3. Version, publish, and depend on the framework as a peer
- Publish as versioned packages (a private registry or a workspace monorepo); services pin a
version and upgrade deliberately. A breaking change to a shared contract (error body, base DTO) is a
major bump — it ripples to every service.
- The framework itself is a
peerDependency, not a bundled dep — so the lib uses the service's
framework version and you don't ship two copies. Keep the lib's own runtime deps minimal.
▸ Other stacks: semver + a lockfile; peer/provided scope (Maven provided, Go module replace).
Principle: explicit versions, framework as peer, treat shared contracts as a public API.
4. What belongs in the lib vs a service
- In the lib: cross-cutting concerns reused by ≥2 services and stable contracts — the error model,
base entity, pagination/response DTOs, auth guards/decorators, messaging clients, pipes,
interceptors, middleware, common utils (decimal/date/PII-mask/chunk), config helpers.
- In a service: domain entities, feature handlers, domain events, anything that changes per
product. Don't push volatile business logic into the lib — every change there forces a fleet-wide
bump. (DRY parallel flows still applies within a service; promote to the lib only once it's stable
and genuinely shared.)
5. Canonical primitives the lib should provide
So every service is consistent, the lib ships the building blocks services extend:
- A base entity — a uuid primary key +
createdAt/updatedAt + a soft-delete flag, with audit
columns select: false (excluded from default reads, fetched only when asked). Services extend it
and add domain columns; soft-delete and timestamps come for free.export abstract class IdentityEntity { @PrimaryGeneratedColumn('uuid') id!: string; }
export abstract class BaseEntity extends IdentityEntity {
@CreateDateColumn({ select: false }) createdAt!: Date;
@UpdateDateColumn({ select: false }) updatedAt!: Date;
@Column({ type: 'boolean', default: false, select: false }) isDeleted!: boolean;
}
- Base pagination + response DTOs — a
BaseQueryDto (pageIndex/pageSize with offset/limit
getters) and a PaginationResponse (total/pageIndex/pageSize) so every list endpoint paginates
and shapes results identically. An IdUUIDParams for :id routes.
- Column transformers — decimal (string ↔ number with fixed scale), boolean, and a PII-masking
transformer, applied at the DB boundary so money/flags/secrets are handled the same everywhere.
- Type helpers —
Nullable<T> = T | null (the agreed "absent" value, see write-service-code §3),
Optional<T, K> for partial shapes.
▸ Other stacks: a base model/ActiveRecord with timestamps + soft-delete, a shared pagination struct,
value-object converters. Principle: the lib provides the canonical base types so services don't
reinvent (and drift on) them.
Verification
- The lib is several concern-focused packages, the foundational one (errors/types) has zero
framework deps, and dependencies point one way (no cycles).
- Each package has one barrel; services import from the package root, not deep paths.
- The framework is a peer dep; packages are versioned; shared-contract changes are major bumps.
- Only cross-cutting + stable code lives in the lib; volatile domain logic stays in services.
- Services extend the lib's base entity + base DTOs rather than redefining timestamps/soft-delete/pagination.
Related
write-cross-cutting-code — the pipes/guards/interceptors/decorators that live in the lib.
design-an-error-model — the infra-exception package's content (the error contract).
structure-a-backend-service — a service that consumes this lib (and its libs/ section).
structure-a-shared-ui-lib — the frontend twin (a shared UI/design-system lib).
write-service-code (§3 nullability, §5 transformers) · code-conventions (DRY parallel flows).
1---2name: structure-a-shared-backend-lib3description: Use when organizing a shared backend infrastructure library that many services depend on (e.g. @org/infra-*) — how to split it into focused packages by dependency weight, expose one barrel per package, avoid dependency cycles with peer deps, version/publish it, decide what belongs in the lib vs a service, and the canonical primitives it should provide (a base entity with soft-delete + audit columns, base pagination/response DTOs, column transformers, type helpers). NestJS/TypeORM reference, framework-flexible. The backend twin of structure-a-shared-ui-lib.4---56# Structure a shared backend library78A library of cross-cutting infrastructure (`@org/infra-*`) that every backend service imports, so the9fleet is consistent and DRY. This is the *where & how it's packaged*; the framework primitives inside10it are in `write-cross-cutting-code` and `design-an-error-model`. Frontend equivalent:11`structure-a-shared-ui-lib`.1213## When to use14You're starting or reorganizing the shared lib behind a fleet of services, deciding which package a15new primitive goes in, or pulling duplicated infra out of services into one place.1617## 1. Split into focused packages by dependency weight18- **Several small packages, not one mega-package** — each owns one concern and pulls only the deps it19 needs, so a service that wants typed errors doesn't drag in the whole AWS/cache stack.20- **Order packages from zero-dep core → heavier**, and let the light ones be depended on by the heavy21 ones (never the reverse), so there are **no cycles**:22 - `infra-exception` / `infra-types` — **zero framework deps**; the error model + shared types. Anything23 can import it.24 - `infra-auth` — guards/decorators; depends on the error package (peer), nothing heavier.25 - `infra-cqrs` — base command/query/event classes; orthogonal, used by event-driven services.26 - `infra-common` — the workhorse (pipes, interceptors, middleware, DTOs, base entity, utils, cache,27 messaging clients). Highest reuse; may depend on the lighter packages.28 ```29 @org/infra-exception (0 deps) ←─ @org/infra-auth ←─┐30 @org/infra-types (0 deps) ←───────────────────┼─ @org/infra-common31 @org/infra-cqrs (framework only) ←────────────┘32 ```33▸ *Other stacks:* a Go `internal/` module set, a Python namespace package, a Java multi-module34artifact. Principle: **partition by concern + dependency direction; the foundational package has the35fewest deps and is imported by the rest, never the reverse.**3637## 2. One barrel per package; import from the package root38- Each package exposes a single `index.ts` (barrel) that re-exports its public surface. Services import39 from the **package root** (`@org/infra-common`), **never deep paths** (`@org/infra-common/src/...`) —40 so internals can move without breaking consumers.41 ```ts42 // infra-common/src/index.ts43 export * from './typeorm'; export * from './pipes'; export * from './dto'; export * from './utils'; /* … */44 // in a service:45 import { BaseEntity, BaseQueryDto, Nullable } from '@org/infra-common'; // root, not a deep path46 ```47▸ *Other stacks:* a package's public API file / `__init__.py` / exported module list. Principle: one48published surface per package; internals are private.4950## 3. Version, publish, and depend on the framework as a peer51- **Publish as versioned packages** (a private registry or a workspace monorepo); services pin a52 version and upgrade deliberately. A breaking change to a shared contract (error body, base DTO) is a53 **major** bump — it ripples to every service.54- **The framework itself is a `peerDependency`**, not a bundled dep — so the lib uses the *service's*55 framework version and you don't ship two copies. Keep the lib's own runtime deps minimal.56▸ *Other stacks:* semver + a lockfile; peer/provided scope (Maven `provided`, Go module replace).57Principle: explicit versions, framework as peer, treat shared contracts as a public API.5859## 4. What belongs in the lib vs a service60- **In the lib:** cross-cutting concerns reused by ≥2 services and stable contracts — the error model,61 base entity, pagination/response DTOs, auth guards/decorators, messaging clients, pipes,62 interceptors, middleware, common utils (decimal/date/PII-mask/chunk), config helpers.63- **In a service:** domain entities, feature handlers, domain events, anything that changes per64 product. **Don't push volatile business logic into the lib** — every change there forces a fleet-wide65 bump. (DRY parallel flows still applies *within* a service; promote to the lib only once it's stable66 and genuinely shared.)6768## 5. Canonical primitives the lib should provide69So every service is consistent, the lib ships the building blocks services extend:70- **A base entity** — a uuid primary key + `createdAt`/`updatedAt` + a soft-delete flag, with audit71 columns `select: false` (excluded from default reads, fetched only when asked). Services extend it72 and add domain columns; soft-delete and timestamps come for free.73 ```ts74 export abstract class IdentityEntity { @PrimaryGeneratedColumn('uuid') id!: string; }75 export abstract class BaseEntity extends IdentityEntity {76 @CreateDateColumn({ select: false }) createdAt!: Date;77 @UpdateDateColumn({ select: false }) updatedAt!: Date;78 @Column({ type: 'boolean', default: false, select: false }) isDeleted!: boolean;79 }80 ```81- **Base pagination + response DTOs** — a `BaseQueryDto` (`pageIndex`/`pageSize` with `offset`/`limit`82 getters) and a `PaginationResponse` (`total`/`pageIndex`/`pageSize`) so every list endpoint paginates83 and shapes results identically. An `IdUUIDParams` for `:id` routes.84- **Column transformers** — decimal (string ↔ number with fixed scale), boolean, and a PII-masking85 transformer, applied at the DB boundary so money/flags/secrets are handled the same everywhere.86- **Type helpers** — `Nullable<T> = T | null` (the agreed "absent" value, see `write-service-code` §3),87 `Optional<T, K>` for partial shapes.88▸ *Other stacks:* a base model/ActiveRecord with timestamps + soft-delete, a shared pagination struct,89value-object converters. Principle: the lib provides the canonical base types so services don't90reinvent (and drift on) them.9192## Verification93- The lib is **several concern-focused packages**, the foundational one (errors/types) has **zero94 framework deps**, and dependencies point one way (no cycles).95- Each package has **one barrel**; services import from the **package root**, not deep paths.96- The framework is a **peer dep**; packages are **versioned**; shared-contract changes are major bumps.97- Only **cross-cutting + stable** code lives in the lib; volatile domain logic stays in services.98- Services **extend the lib's base entity + base DTOs** rather than redefining timestamps/soft-delete/pagination.99100## Related101- `write-cross-cutting-code` — the pipes/guards/interceptors/decorators that live in the lib.102- `design-an-error-model` — the `infra-exception` package's content (the error contract).103- `structure-a-backend-service` — a service that *consumes* this lib (and its `libs/` section).104- `structure-a-shared-ui-lib` — the frontend twin (a shared UI/design-system lib).105- `write-service-code` (§3 nullability, §5 transformers) · `code-conventions` (DRY parallel flows).