Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shall we rename prop to model instead so it's meaning is clearer? Also, I think in this case it's always typed as string. Maybe remove the | symbol part so the user can have a simpler callback signature?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good, I'll update this in this tomorrow and push it.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!

Copy link
Contributor Author

@jasonmacdonald jasonmacdonald Jul 10, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So it looks like not including symbol causes a type error.

 Error: packages/server build: src/nestjs/zenstack.module.ts(82,83): error TS2345: Argument of type 'string | symbol' is not assignable to parameter of type 'string | undefined'.

https://github.com/zenstackhq/zenstack/actions/runs/9877836393/job/27280399900?pr=1538

This is likely because the prop from Proxy is typed as string | symbol, which is why I think I had set that earlier. I'll revert that change for now.

}

/**
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);
});
});