Chore: Remove whereAndSelect and fix typescript of main PrismaClient adapter (#966)
This commit is contained in:
		
							parent
							
								
									78182db99c
								
							
						
					
					
						commit
						c01004b470
					
				
					 2 changed files with 10 additions and 129 deletions
				
			
		|  | @ -1,45 +1,18 @@ | |||
| import { PrismaClient } from "@prisma/client"; | ||||
| 
 | ||||
| let prisma: PrismaClient; | ||||
| const globalAny: any = global; | ||||
| 
 | ||||
| if (process.env.NODE_ENV === "production") { | ||||
|   prisma = new PrismaClient(); | ||||
| } else { | ||||
|   if (!globalAny.prisma) { | ||||
|     globalAny.prisma = new PrismaClient({ | ||||
|       log: ["query", "error", "warn"], | ||||
|     }); | ||||
|   } | ||||
|   prisma = globalAny.prisma; | ||||
| declare global { | ||||
|   // eslint-disable-next-line no-var
 | ||||
|   var prisma: PrismaClient | undefined; | ||||
| } | ||||
| 
 | ||||
| const pluck = (select: Record<string, boolean>, 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, | ||||
|   }; | ||||
| }; | ||||
| 
 | ||||
| /** | ||||
|  * @deprecated | ||||
|  * This function will always return `any` type, | ||||
|  * See https://github.com/calendso/calendso/pull/460 for alternative approach
 | ||||
|  */ | ||||
| const whereAndSelect = (modelQuery, criteria: Record<string, unknown>, pluckedAttributes: string[]) => | ||||
|   modelQuery({ | ||||
|     where: criteria, | ||||
|     select: pluckedAttributes.reduce(pluck, {}), | ||||
| export const prisma = | ||||
|   globalThis.prisma || | ||||
|   new PrismaClient({ | ||||
|     log: ["query", "error", "warn"], | ||||
|   }); | ||||
| 
 | ||||
| export { whereAndSelect }; | ||||
| if (process.env.NODE_ENV !== "production") { | ||||
|   globalThis.prisma = prisma; | ||||
| } | ||||
| 
 | ||||
| export default prisma; | ||||
|  |  | |||
|  | @ -1,92 +0,0 @@ | |||
| import { expect, it } 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"] | ||||
|   ); | ||||
| }); | ||||
		Loading…
	
		Reference in a new issue
	
	 Alex van Andel
						Alex van Andel