From Prisma
Add MongoDB population (eager-loading of related documents via aggregation $lookup pipelines) to an existing entity in a hexagonal architecture TypeScript project using the @efesto-cloud/mongodb-population package. Use this skill whenever the project uses MongoDB and the user says things like "populate Foo with its Bar", "add population support for FooEntity", "wire up the $lookup for Foo", "add the populate option to FooRepo", "create the QueryBuilder and Populator for Foo", "Foo needs to include its related Bar when fetched", or whenever someone needs to add optional relational data loading to an existing MongoDB repository. Trigger even if the user just says "add population" without specifying the entity — ask them. Do NOT trigger for creating entities, DTOs, or base repositories from scratch (those are handled by entity and mongodb-persistence skills). For Prisma-based projects use the prisma-population skill instead. For the generic Shape type and Populate<T> concepts, see the population skill.
How this skill is triggered — by the user, by Claude, or both
Slash command
/Prisma:mongodb-populationThe summary Claude sees in its skill listing — used to decide when to auto-load this skill
**Installation:** If not already installed, add the required packages:
Installation: If not already installed, add the required packages:
pnpm add @efesto-cloud/population (for Populate type and normalizePopulate helper)pnpm add @efesto-cloud/mongodb-population (for BasePopulator and QueryBuilder classes)Adds MongoDB population support — typed eager-loading of related documents via aggregation $lookup — to an existing entity. The entity, its DTO, document type, mapper, and repository are assumed to already exist. This skill only patches them where needed and writes the population infrastructure.
Scope: Shape type, QueryBuilder, Populator, plus targeted patches to entity, DTO, document, mapper, and repository interface/implementation.
Does not: create entities or repositories from scratch, write use cases, or manage DI container wiring.
If the user has not specified which entity to populate and/or which fields should be populated, use AskUserQuestion to ask:
Do not proceed until you have at least the entity name and one field to populate.
Before touching any file, orient yourself:
src/db/CollectionNameEnum.ts or similar. You'll need the collection name constant for $lookup.src/repo/shape/, src/repo/populate/, src/repo/query/. If any exist, read one to match the exact import style.src/entity/FooEntity.tssrc/dto/IFoo.tssrc/db/Documents/FooDocument.tssrc/mapper/FooMapper.tssrc/repo/IFooRepo.tssrc/repo/impl/FooRepoImpl.tsIf src/repo/shape/ or src/repo/populate/ directories do not yet exist, create them.
Patch each file only where something is actually missing. Do not rewrite files wholesale.
For each populated field bar on entity Foo:
bar: Bar | null (initialized to null in create())bars: Bar[] (initialized to [] in create())create() static method — if the populated field has a meaningful default, accept it as an optional param. Typically bar is not passed to create() (it starts null/empty and is filled by the mapper after aggregation).toDTO() — if the DTO has an optional bar? field, map it: bar: this.props.bar?.toDTO() ?? null.get bar(): Bar | null (or Bar[]) if missing.populateBar() mutation method needed — the mapper sets the field directly after aggregation.For each populated field bar on IFoo:
bar?: IBar | null (optional — it may or may not be present depending on query).bars?: IBar[].index.ts re-exports the DTO, no change needed there unless you added a new sub-type.For each populated field bar on FooDocument:
bar?: BarDocument | null (always optional — absent on raw stored documents, present only after $lookup).bars?: BarDocument[].bar_id: ObjectId | null) should already be present; do not add a second FK.For each populated field bar, update FooMapper.from():
// After building the base entity:
if (doc.bar) {
entity.props.bar = BarMapper.from(doc.bar);
}
// or for arrays:
if (doc.bars) {
entity.props.bars = doc.bars.map(BarMapper.from);
}
The to() direction (entity → document) should not include populated fields — they are loaded, not saved, through this path.
Read the reference files before writing:
references/query-builder-example.ts — QueryBuilder with populateWith()references/populator-example.ts — flat Populator (no nesting)references/populator-nested-example.ts — nested Populator delegating to sub-populatorpopulation skill's references/shape-example.tssrc/repo/shape/FooShape.ts// Leaf fields use `true`; fields whose related entity is also populatable use that entity's Shape type.
import type { BarShape } from './BarShape.js'; // only if Bar also has a populator
export type FooShape = {
bar: true; // 1:1, leaf — Bar has no further population
items: true; // 1:many, leaf
baz: BazShape; // 1:1, nested — Baz itself has populatable fields
};
src/repo/query/FooQueryBuilder.tsimport { normalizePopulate, type Populate } from '@efesto-cloud/population';
import { QueryBuilder } from '@efesto-cloud/mongodb-population';
import FooDocument from '~/db/Documents/FooDocument.js';
import FooPopulator from '../populate/FooPopulator.js';
import type { FooShape } from '../shape/FooShape.js';
export default class FooQueryBuilder extends QueryBuilder<FooDocument> {
populateWith(fields: Populate<FooShape> = {}): this {
const normalized = normalizePopulate(fields, FooPopulator.SHAPE);
const pipeline = FooPopulator.buildPipeline(normalized);
this.push_populate_pipeline(pipeline);
return this;
}
}
src/repo/populate/FooPopulator.tsFor each field:
bar_id): use lookup + unwind.foo_id as FK, or Foo stores an array of IDs): use lookup only, no unwind.lookup. See references/populator-nested-example.ts.import { BasePopulator } from '@efesto-cloud/mongodb-population';
import type { NormalizedPopulate } from '@efesto-cloud/population';
import CollectionNameEnum from '~/db/CollectionNameEnum.js';
import type TCollectionName from '~/db/TCollectionName.js';
import type { FooShape } from '../shape/FooShape.js';
export default class FooPopulator extends BasePopulator<FooShape, TCollectionName> {
static readonly SHAPE: FooShape = {
bar: true,
items: true,
};
private bar(): void {
if (!this.markPopulated('bar')) return;
this.addStages(
this.lookup({
from: CollectionNameEnum.bar, // collection name constant
localField: 'bar_id', // FK on Foo document
foreignField: '_id',
as: 'bar',
}),
this.unwind('bar'), // 1:1 — flatten array to single object
);
}
private items(): void {
if (!this.markPopulated('items')) return;
this.addStages(
this.lookup({
from: CollectionNameEnum.item,
localField: '_id', // Foo's own _id
foreignField: 'foo_id', // FK on Item documents
as: 'items',
}),
// No unwind — keeps the array
);
}
populate(spec: NormalizedPopulate<FooShape>): this {
if (spec.bar) this.bar();
if (spec.items) this.items();
return this;
}
static buildPipeline(spec: NormalizedPopulate<FooShape>): import('mongodb').Document[] {
return new FooPopulator().populate(spec).build();
}
}
src/repo/IFooRepo.tsAdd the Options namespace with a populate field, and add options? param to every query method (save/saveMany/delete do not need it):
import type { Populate } from '@efesto-cloud/population';
import type { FooShape } from './shape/FooShape.js';
interface IFooRepo {
search(query: IFooRepo.Search, options?: IFooRepo.Options): Promise<Foo[]>;
get(id: ObjectId, options?: IFooRepo.Options): Promise<Maybe<Foo>>;
findByIds(ids: ObjectId[], options?: IFooRepo.Options): Promise<Foo[]>;
// ... other query methods
save(entity: Foo): Promise<void>;
}
namespace IFooRepo {
export type Options = {
populate?: Populate<FooShape>;
};
}
export default IFooRepo;
src/repo/impl/FooRepoImpl.tsSwitch each query method to use FooQueryBuilder with .populateWith(options?.populate):
async get(id: ObjectId, options?: IFooRepo.Options): Promise<Maybe<Foo>> {
const pipeline = new FooQueryBuilder()
.match({ _id: id } as Filter<FooDocument>)
.populateWith(options?.populate)
.limit(1)
.build();
const results = await this.coll.aggregate<FooDocument>(
pipeline, { session: this.db.session }
).toArray();
if (results.length === 0) return Maybe.none();
return Maybe.maybe(FooMapper.from(results[0]!));
}
Methods that already use aggregate() just need .populateWith(options?.populate) inserted into the builder chain. Methods that use findOne() or find() should be converted to aggregate() with the QueryBuilder.
If Foo has a type discriminator and different variants have different populatable fields:
{ fontFile: true; rasterFile: true; vectorFile: true; }.$lookup on a non-existent FK just returns an empty array, which is then dropped by unwind or ignored.When Bar itself has a BarPopulator, you can pass a sub-pipeline into the $lookup:
private bar(nestedSpec: NormalizedPopulate<BarShape>): void {
if (!this.markPopulated('bar')) return;
const nestedPipeline = BarPopulator.buildPipeline(nestedSpec);
this.addStages(
this.lookup({
from: CollectionNameEnum.bar,
localField: 'bar_id',
foreignField: '_id',
as: 'bar',
pipeline: nestedPipeline, // <-- sub-population
}),
this.unwind('bar'),
);
}
The Shape field must then be typed as BarShape (not true), and the populate() method receives spec.bar as a NormalizedPopulate<BarShape>.
$lookup with $in (Foo stores an array of IDs)When Foo.bar_ids is an array of ObjectIds pointing to Bar documents:
this.lookup({
from: CollectionNameEnum.bar,
localField: 'bar_ids', // array field on Foo
foreignField: '_id',
as: 'bars',
})
// No unwind — result is an array matching the IDs
For bar_id: ObjectId | null, the $lookup returns an empty array when FK is null. unwind() always uses preserveNullAndEmptyArrays: true, so nullable FKs are handled safely with the standard call:
this.addStages(
this.lookup({ from: CollectionNameEnum.bar, localField: 'bar_id', foreignField: '_id', as: 'bar' }),
this.unwind('bar'),
);
Then in the mapper: entity.props.bar = doc.bar ? BarMapper.from(doc.bar) : null.
Run the typecheck command for the core package then fix any errors before considering the task done.
references/query-builder-example.ts — Full QueryBuilderreferences/populator-example.ts — Flat populator (leaf fields only)references/populator-nested-example.ts — Populator with nested sub-populationpopulation skill's references/shape-example.tsnpx claudepluginhub efesto-cloud/lib --plugin PrismaCreates, edits, and optimizes skills for Claude Code, including drafting, evaluating with test prompts, iterating on performance, and improving skill descriptions for better triggering accuracy.