|
| 1 | +import type { Payload } from 'payload' |
| 2 | + |
| 3 | +/* eslint-disable jest/require-top-level-describe */ |
| 4 | +import path from 'path' |
| 5 | +import { fileURLToPath } from 'url' |
| 6 | + |
| 7 | +import { initPayloadInt } from '../helpers/initPayloadInt.js' |
| 8 | + |
| 9 | +const filename = fileURLToPath(import.meta.url) |
| 10 | +const dirname = path.dirname(filename) |
| 11 | + |
| 12 | +const describeSqlite = process.env.PAYLOAD_DATABASE?.startsWith('sqlite') ? describe : describe.skip |
| 13 | + |
| 14 | +let payload: Payload |
| 15 | + |
| 16 | +describeSqlite('database - sqlite bound parameters limit', () => { |
| 17 | + beforeAll(async () => { |
| 18 | + ;({ payload } = await initPayloadInt(dirname)) |
| 19 | + }) |
| 20 | + |
| 21 | + afterAll(async () => { |
| 22 | + await payload.destroy() |
| 23 | + }) |
| 24 | + |
| 25 | + it('should not use bound parameters for where querying on ID with IN if limitedBoundParameters: true', async () => { |
| 26 | + const defaultExecute = payload.db.drizzle.$client.execute.bind(payload.db.drizzle.$client) |
| 27 | + |
| 28 | + // Limit bounds parameters length |
| 29 | + payload.db.drizzle.$client.execute = async function execute(...args) { |
| 30 | + const res = await defaultExecute(...args) |
| 31 | + const [{ args: boundParameters }] = args as [{ args: any[] }] |
| 32 | + |
| 33 | + // eslint-disable-next-line jest/no-conditional-in-test |
| 34 | + if (boundParameters.length > 100) { |
| 35 | + throw new Error('Exceeded limit of bound parameters!') |
| 36 | + } |
| 37 | + return res |
| 38 | + } |
| 39 | + |
| 40 | + payload.db.limitedBoundParameters = false |
| 41 | + |
| 42 | + const IN = Array.from({ length: 300 }, (_, i) => i) |
| 43 | + |
| 44 | + // Should fail here because too the length exceeds the limit |
| 45 | + await expect( |
| 46 | + payload.find({ |
| 47 | + collection: 'simple', |
| 48 | + pagination: false, |
| 49 | + where: { id: { in: IN } }, |
| 50 | + }), |
| 51 | + ).rejects.toBeTruthy() |
| 52 | + |
| 53 | + // Should fail here because too the length exceeds the limit |
| 54 | + await expect( |
| 55 | + payload.find({ |
| 56 | + collection: 'simple', |
| 57 | + pagination: false, |
| 58 | + where: { id: { not_in: IN } }, |
| 59 | + }), |
| 60 | + ).rejects.toBeTruthy() |
| 61 | + |
| 62 | + payload.db.limitedBoundParameters = true |
| 63 | + |
| 64 | + // Should not fail because limitedBoundParameters: true |
| 65 | + await expect( |
| 66 | + payload.find({ |
| 67 | + collection: 'simple', |
| 68 | + pagination: false, |
| 69 | + where: { id: { in: IN } }, |
| 70 | + }), |
| 71 | + ).resolves.toBeTruthy() |
| 72 | + |
| 73 | + // Should not fail because limitedBoundParameters: true |
| 74 | + await expect( |
| 75 | + payload.find({ |
| 76 | + collection: 'simple', |
| 77 | + pagination: false, |
| 78 | + where: { id: { not_in: IN } }, |
| 79 | + }), |
| 80 | + ).resolves.toBeTruthy() |
| 81 | + |
| 82 | + // Verify that "in" still works properly |
| 83 | + |
| 84 | + const docs = await Promise.all( |
| 85 | + Array.from({ length: 300 }, () => payload.create({ collection: 'simple', data: {} })), |
| 86 | + ) |
| 87 | + |
| 88 | + const res = await payload.find({ |
| 89 | + collection: 'simple', |
| 90 | + pagination: false, |
| 91 | + where: { id: { in: docs.map((e) => e.id) } }, |
| 92 | + }) |
| 93 | + |
| 94 | + expect(res.totalDocs).toBe(300) |
| 95 | + for (const docInRes of res.docs) { |
| 96 | + expect(docs.some((doc) => doc.id === docInRes.id)).toBeTruthy() |
| 97 | + } |
| 98 | + }) |
| 99 | +}) |
0 commit comments