From 47d7634638ef0f5c5798b7d5aff510bbfa6657dc Mon Sep 17 00:00:00 2001 From: Alex van Andel Date: Tue, 6 Jul 2021 16:10:22 +0000 Subject: [PATCH 1/3] Adds prisma helper function whereAndSelect --- lib/prisma.ts | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/lib/prisma.ts b/lib/prisma.ts index 4d3f8470..b20a1b48 100644 --- a/lib/prisma.ts +++ b/lib/prisma.ts @@ -1,9 +1,9 @@ -import { PrismaClient } from '@prisma/client'; +import { PrismaClient } from "@prisma/client"; let prisma: PrismaClient; -const globalAny:any = global; +const globalAny: any = global; -if (process.env.NODE_ENV === 'production') { +if (process.env.NODE_ENV === "production") { prisma = new PrismaClient(); } else { if (!globalAny.prisma) { @@ -12,4 +12,18 @@ if (process.env.NODE_ENV === 'production') { prisma = globalAny.prisma; } -export default prisma; \ No newline at end of file +const whereAndSelect = (modelQuery, criteria: Record, pluckedAttributes: string[]) => + modelQuery({ + where: criteria, + select: pluckedAttributes.reduce( + (select: { [string]: boolean }, attr: string) => ({ + ...select, + [attr]: true, + }), + {} + ), + }); + +export { whereAndSelect }; + +export default prisma; From e08815ce787eff0ab6d0a545c8269b840c673df2 Mon Sep 17 00:00:00 2001 From: Alex van Andel Date: Tue, 6 Jul 2021 18:20:25 +0000 Subject: [PATCH 2/3] Added support for deep selecting --- lib/prisma.ts | 23 ++++++--- test/lib/prisma.test.ts | 101 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 117 insertions(+), 7 deletions(-) create mode 100644 test/lib/prisma.test.ts diff --git a/lib/prisma.ts b/lib/prisma.ts index b20a1b48..5d75ada2 100644 --- a/lib/prisma.ts +++ b/lib/prisma.ts @@ -12,16 +12,25 @@ if (process.env.NODE_ENV === "production") { prisma = globalAny.prisma; } +const pluck = (select: Record, attr: string) => { + const parts = attr.split("."); + const alwaysAttr = parts[0]; + const pluckedValue = + parts.length > 1 + ? { + select: pluck(select[alwaysAttr] ? select[alwaysAttr].select : {}, parts.slice(1).join(".")), + } + : true; + return { + ...select, + [alwaysAttr]: pluckedValue, + }; +}; + const whereAndSelect = (modelQuery, criteria: Record, pluckedAttributes: string[]) => modelQuery({ where: criteria, - select: pluckedAttributes.reduce( - (select: { [string]: boolean }, attr: string) => ({ - ...select, - [attr]: true, - }), - {} - ), + select: pluckedAttributes.reduce(pluck, {}), }); export { whereAndSelect }; diff --git a/test/lib/prisma.test.ts b/test/lib/prisma.test.ts new file mode 100644 index 00000000..45c34398 --- /dev/null +++ b/test/lib/prisma.test.ts @@ -0,0 +1,101 @@ + +import {it, expect} from '@jest/globals'; +import {whereAndSelect} from "@lib/prisma"; + +it('can decorate using whereAndSelect', async () => { + whereAndSelect( (queryObj) => { + expect(queryObj).toStrictEqual({ where: { id: 1 }, select: { example: true } }); + }, + { id: 1 }, + [ + "example" + ]) +}); + +it('can do nested selects using . seperator', async () => { + + whereAndSelect( (queryObj) => { + expect(queryObj).toStrictEqual({ + where: { + uid: 1, + }, + select: { + description: true, + attendees: { + select: { + email: true, + name: true, + }, + }, + } + }); + }, + { uid: 1 }, + [ + "description", + "attendees.email", + "attendees.name", + ]) +}) + +it('can handle nesting deeply', async () => { + whereAndSelect( (queryObj) => { + expect(queryObj).toStrictEqual({ + where: { + uid: 1, + }, + select: { + description: true, + attendees: { + select: { + email: { + select: { + nested: true + } + }, + name: true, + }, + }, + } + }); + }, + { uid: 1 }, + [ + "description", + "attendees.email.nested", + "attendees.name", + ]) +}); + +it('can handle nesting multiple', async () => { + whereAndSelect( (queryObj) => { + expect(queryObj).toStrictEqual({ + where: { + uid: 1, + }, + select: { + description: true, + attendees: { + select: { + email: true, + name: true, + }, + }, + bookings: { + select: { + id: true, + name: true, + } + } + } + }); + }, + { uid: 1 }, + [ + "description", + "attendees.email", + "attendees.name", + "bookings.id", + "bookings.name", + ]) +}); From de74d85fac59ff7ad34101e48fc10d0991906f96 Mon Sep 17 00:00:00 2001 From: Alex van Andel Date: Tue, 6 Jul 2021 18:23:33 +0000 Subject: [PATCH 3/3] Remove --experimental-vm-modules & fix Codacy --- package.json | 2 +- test/lib/prisma.test.ts | 48 ++++++++++++++++++++++++----------------- 2 files changed, 29 insertions(+), 21 deletions(-) diff --git a/package.json b/package.json index b0ac1af3..4b8836be 100644 --- a/package.json +++ b/package.json @@ -4,7 +4,7 @@ "private": true, "scripts": { "dev": "next dev", - "test": "node --experimental-vm-modules node_modules/.bin/jest", + "test": "node node_modules/.bin/jest", "build": "next build", "start": "next start", "postinstall": "prisma generate", diff --git a/test/lib/prisma.test.ts b/test/lib/prisma.test.ts index 45c34398..b2164ffd 100644 --- a/test/lib/prisma.test.ts +++ b/test/lib/prisma.test.ts @@ -1,20 +1,23 @@ -import {it, expect} from '@jest/globals'; -import {whereAndSelect} from "@lib/prisma"; +import { it, expect } from '@jest/globals'; +import { whereAndSelect } from "@lib/prisma"; -it('can decorate using whereAndSelect', async () => { - whereAndSelect( (queryObj) => { - expect(queryObj).toStrictEqual({ where: { id: 1 }, select: { example: true } }); +it("can decorate using whereAndSelect", async () => { + whereAndSelect( + (queryObj) => { + expect(queryObj).toStrictEqual({ where: { id: 1 }, select: { example: true } }); }, { id: 1 }, [ - "example" - ]) + "example", + ] + ); }); -it('can do nested selects using . seperator', async () => { +it("can do nested selects using . seperator", async () => { - whereAndSelect( (queryObj) => { + whereAndSelect( + (queryObj) => { expect(queryObj).toStrictEqual({ where: { uid: 1, @@ -27,7 +30,7 @@ it('can do nested selects using . seperator', async () => { name: true, }, }, - } + }, }); }, { uid: 1 }, @@ -35,11 +38,13 @@ it('can do nested selects using . seperator', async () => { "description", "attendees.email", "attendees.name", - ]) -}) + ] + ); +}); -it('can handle nesting deeply', async () => { - whereAndSelect( (queryObj) => { +it("can handle nesting deeply", async () => { + whereAndSelect( + (queryObj) => { expect(queryObj).toStrictEqual({ where: { uid: 1, @@ -50,13 +55,13 @@ it('can handle nesting deeply', async () => { select: { email: { select: { - nested: true + nested: true, } }, name: true, }, }, - } + }, }); }, { uid: 1 }, @@ -64,11 +69,13 @@ it('can handle nesting deeply', async () => { "description", "attendees.email.nested", "attendees.name", - ]) + ] + ); }); -it('can handle nesting multiple', async () => { - whereAndSelect( (queryObj) => { +it("can handle nesting multiple", async () => { + whereAndSelect( + (queryObj) => { expect(queryObj).toStrictEqual({ where: { uid: 1, @@ -97,5 +104,6 @@ it('can handle nesting multiple', async () => { "attendees.name", "bookings.id", "bookings.name", - ]) + ] + ); });