@efesto-cloud/prisma-population
v0.0.2
Published
Prisma adapter for @efesto-cloud/population
Readme
@efesto-cloud/prisma-population
Prisma adapter for the @efesto-cloud/population spec types. Turns a normalized populate spec into a Prisma include argument.
Installation
pnpm add @efesto-cloud/prisma-population @efesto-cloud/population @prisma/clientExports
toPrismaInclude(spec)— pure function: converts aNormalizedPopulate<S>into a Prismaincludeobject (orundefinedwhen empty).BasePrismaPopulator<TShape>— base class for entity-specific populators. Overrideshape()and optionallyfield()to customise per-relation behaviour.PrismaInclude— type alias for the recursiveincludeshape.
toPrismaInclude
import { toPrismaInclude } from "@efesto-cloud/prisma-population";
import { normalizePopulate } from "@efesto-cloud/population";
type PostShape = { author: true; comments: { author: true } };
const normalized = normalizePopulate(
{ author: true, comments: { author: true } },
{ author: true, comments: { author: true } } satisfies PostShape,
);
const include = toPrismaInclude(normalized);
// { author: true, comments: { include: { author: true } } }
await prisma.post.findUnique({ where: { id }, include });true leaves stay true; nested specs become { include: { ... } }. Entries set to false or undefined are dropped.
BasePrismaPopulator
Use this when you want a single place to decide which relations an entity exposes and how each one is loaded.
import BasePrismaPopulator from "@efesto-cloud/prisma-population/BasePrismaPopulator";
import type { Populate } from "@efesto-cloud/population";
type PostShape = { author: true; comments: { author: true } };
export default class PostPopulator extends BasePrismaPopulator<PostShape> {
static readonly SHAPE: PostShape = { author: true, comments: { author: true } };
protected shape() {
return PostPopulator.SHAPE;
}
static buildInclude(spec: Populate<PostShape> | undefined) {
return new PostPopulator().build(spec);
}
}
const include = PostPopulator.buildInclude({ comments: { author: true } });
await prisma.post.findMany({ include });Override field() to inject per-relation options (where, orderBy, take, …) when true/plain include isn't enough.
Related
@efesto-cloud/population— spec types andnormalizePopulate.@efesto-cloud/prisma-database-context— transactional Prisma client wrapper.
