-
-
Notifications
You must be signed in to change notification settings - Fork 27
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
findUnique
isn't working
#61
Comments
Oddly enough it'll work if I run the server using |
Looks like you're using Next.js, which has been known to cause some issues with hot-reloading in dev mode, could you look at this issue and see if yours behaves the same way? |
Oooohhh that seemed to fix it. Appreciate the help and thanks for open sourcing this package! For anyone else I ended up with this in import { PrismaClient } from "@prisma/client";
import { fieldEncryptionMiddleware } from "prisma-field-encryption";
const globalForPrisma = global as unknown as { prisma: PrismaClient };
declare global {
// allow global `var` declarations
// eslint-disable-next-line no-var
var prisma: PrismaClient | undefined;
}
const prisma =
globalForPrisma.prisma ||
new PrismaClient({
log: ["error"],
});
// Only add the middleware to prisma once so if it's set within the global no need to add it again
if (!globalForPrisma.prisma) {
prisma.$use(fieldEncryptionMiddleware());
}
if (process.env.NODE_ENV !== "production") {
globalForPrisma.prisma = prisma;
}
export default prisma; |
Sounds like adding a way to detect multiple uses of the middleware with the same configuration on the same Prisma client would be a good DX improvement. |
Hmmm is there any way to check if middleware has already been configured on the client? Happy to take a stab at it and put up a PR if you have any thoughts / could point me in the right direction |
I was thinking of something along the lines of a global variable in the middleware module, or a dedicated property on the Prisma client, that would keep a reference of instanciations and associated configurations (or maybe a hash of if to reduce access to sensitive data). Not sure how the CJS vs ESM loading mechanisms and hot reloading would play around such an idea though. |
If you're using NestJS, I have a solution for this issue: You can create an export for getting PrismaClient with the extension like this: export function createPrismaClientWithEncryption() {
return new PrismaClient().$extends(
fieldEncryptionExtension()
);
} I've made a PrismaModule, in this module you can create a provider with a useValue and refer to the function above: @Global()
@Module({
providers: [
{
provide: PrismaService,
useValue: createPrismaClientWithEncryption(),
}
],
exports: [PrismaService]
})
export class PrismaModule {} I hope this helps a lot. |
Here's my model
It seems to be correctly encrypting the keys e.g. if I look at the database directly I see
token
="v1.aesgcm256.b52f8efd.uujweP0t9Bi6kEur.lk9R-BR2_o..."
token_hash
="1307434863b6c28b94c625628664d5bade226b00325d684d810507c999bfeaea"
And then when I do
prisma.apiKey.findMany()
the values are correctI turned on the debugger using
DEBUG="prisma-field-encryption:*" yarn dev
and when I search for the API key bytoken
e.g.and you'll notice that the
tokenHash
is different which is why the return value is empty but I'm not sure why the hash is differentWithin my
.env
I've got justPRISMA_FIELD_ENCRYPTION_KEY
set and then I just do this withinclients/prisma.ts
Any idea where I might be going wrong? I'm pretty close and imagine it's just one setting or variable I didn't set correctly
The text was updated successfully, but these errors were encountered: