Skip to content

Commit

Permalink
feat(nestjs): Add prop as optional parameter of getEnhancedPrisma zen…
Browse files Browse the repository at this point in the history
  • Loading branch information
Jason MacDonald committed Jun 27, 2024
1 parent 3e77974 commit aee73cc
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 2 deletions.
4 changes: 2 additions & 2 deletions packages/server/src/nestjs/zenstack.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export interface ZenStackModuleOptions {
/**
* A callback for getting an enhanced `PrismaClient`.
*/
getEnhancedPrisma: () => unknown;
getEnhancedPrisma: (prop?: string | symbol) => unknown;
}

/**
Expand Down Expand Up @@ -79,7 +79,7 @@ export class ZenStackModule {
{
get(_target, prop) {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const enhancedPrisma: any = getEnhancedPrisma();
const enhancedPrisma: any = getEnhancedPrisma(prop);
if (!enhancedPrisma) {
throw new Error('`getEnhancedPrisma` must return a valid Prisma client');
}
Expand Down
49 changes: 49 additions & 0 deletions packages/server/tests/adapter/nestjs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,4 +160,53 @@ describe('NestJS adapter tests', () => {
const postSvc = app.get('PostService');
await expect(postSvc.findAll()).resolves.toHaveLength(1);
});

it('pass property', async () => {
const { prisma, enhanceRaw } = await loadSchema(schema);

await prisma.user.create({
data: {
posts: {
create: [
{ title: 'post1', published: true },
{ title: 'post2', published: false },
],
},
},
});

const moduleRef = await Test.createTestingModule({
imports: [
ZenStackModule.registerAsync({
useFactory: (prismaService) => ({
getEnhancedPrisma: (prop) => {
return prop === 'post' ? prismaService : enhanceRaw(prismaService, { user: { id: 2 } });
},
}),
inject: ['PrismaService'],
extraProviders: [
{
provide: 'PrismaService',
useValue: prisma,
},
],
}),
],
providers: [
{
provide: 'PostService',
useFactory: (enhancedPrismaService) => ({
findAll: () => enhancedPrismaService.post.findMany(),
}),
inject: [ENHANCED_PRISMA],
},
],
}).compile();

const app = moduleRef.createNestApplication();
await app.init();

const postSvc = app.get('PostService');
await expect(postSvc.findAll()).resolves.toHaveLength(2);
});
});

0 comments on commit aee73cc

Please sign in to comment.