Plug & Play Context Loader
ContextLoader.create() is the ZinTrust Plug & Play surface for repeated multi-model context assembly.
It provides dependency-ordered load(...) resolution, request-scoped memoization, and optional shared batch loading without pretending that every graph becomes one SQL query.
Available runtime
ts
import { ContextLoader } from '@zintrust/core';
const loader = ContextLoader.create({ mode: 'batch' }).batch(
'profilesByUserId',
async (userIds) => {
const profiles = await Profile.whereIn('user_id', userIds).get();
return new Map(profiles.map((profile) => [String(profile.user_id), profile]));
}
);
const context = await loader
.load('user', async () => User.find(userId))
.load('profile', async ({ user }) => {
const currentUser = user as { id?: string } | null;
return currentUser?.id ? loader.fromBatch('profilesByUserId', currentUser.id) : null;
})
.load('wallet', async ({ user }) => {
const currentUser = user as { id?: string } | null;
return currentUser?.id ? Wallet.where('user_id', '=', currentUser.id).first() : null;
})
.resolve();What the core owns
ContextLoader provides:
- dependency-ordered
load(...)execution - request-scoped
resolve()memoization per plan - optional shared batch registration through
.batch(...) fromBatch(...)fan-out loading that can coalesce concurrent requests on one loader instance- consistent
nullresults for missing batch values
What the application still owns
Applications still decide:
- whether plain relationship eager loading is the better abstraction
- which models or services belong in the context graph
- how batched data is grouped and keyed
- how null or missing values should affect downstream business logic
Product-fit boundaries
ContextLoaderdoes not guarantee a single SQL queryContextLoaderdoes not replace ORM relationshipsContextLoaderis most useful when one action needs repeated multi-model coordination or shared fan-out batching
Related docs
- Use models and orm-advanced-relationships when one ORM root can express the graph directly.
- Use plug-and-play-performance for the retention rules that keep request-scoped loaders bounded.