|
| 1 | +// @ts-ignore |
| 2 | +import { type MongooseAdapter } from '@payloadcms/db-mongodb' |
| 3 | +import { buildConfig, getPayload } from 'payload' |
| 4 | +import { afterEach, expect, it, vi } from 'vitest' |
| 5 | + |
| 6 | +import { describe } from '../__helpers/int/vitest.js' |
| 7 | + |
| 8 | +describe( |
| 9 | + 'mongodb read path selection', |
| 10 | + { db: (adapter) => adapter === 'mongodb' || adapter === 'mongodb-atlas' }, |
| 11 | + () => { |
| 12 | + const createdIDs: (number | string)[] = [] |
| 13 | + |
| 14 | + const getPayloadInstance = async () => |
| 15 | + await getPayload({ |
| 16 | + key: '_joinsQueryPath', |
| 17 | + config: await buildConfig({ |
| 18 | + secret: '______', |
| 19 | + db: await import('../databaseAdapter.js').then((mod) => mod.databaseAdapter), |
| 20 | + collections: [ |
| 21 | + { |
| 22 | + slug: 'categories', |
| 23 | + fields: [ |
| 24 | + { name: 'title', type: 'text' }, |
| 25 | + { |
| 26 | + name: 'posts', |
| 27 | + type: 'join', |
| 28 | + collection: 'posts', |
| 29 | + on: 'category', |
| 30 | + }, |
| 31 | + ], |
| 32 | + versions: false, |
| 33 | + }, |
| 34 | + { |
| 35 | + slug: 'posts', |
| 36 | + fields: [ |
| 37 | + { |
| 38 | + name: 'category', |
| 39 | + type: 'relationship', |
| 40 | + relationTo: 'categories', |
| 41 | + }, |
| 42 | + ], |
| 43 | + versions: false, |
| 44 | + }, |
| 45 | + ], |
| 46 | + }), |
| 47 | + }) |
| 48 | + |
| 49 | + const seedCategory = async () => { |
| 50 | + const payload = await getPayloadInstance() |
| 51 | + |
| 52 | + const category = await payload.create({ |
| 53 | + collection: 'categories', |
| 54 | + // @ts-expect-error not generated |
| 55 | + data: { title: 'a' }, |
| 56 | + }) |
| 57 | + createdIDs.push(category.id) |
| 58 | + |
| 59 | + return { |
| 60 | + adapter: payload.db as unknown as MongooseAdapter, |
| 61 | + category, |
| 62 | + payload, |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + afterEach(async () => { |
| 67 | + vi.restoreAllMocks() |
| 68 | + |
| 69 | + const payload = await getPayloadInstance() |
| 70 | + |
| 71 | + for (const id of createdIDs) { |
| 72 | + await payload.delete({ collection: 'categories', id }) |
| 73 | + } |
| 74 | + |
| 75 | + createdIDs.length = 0 |
| 76 | + }) |
| 77 | + |
| 78 | + it('should use Model.paginate when a select excludes every join field', async () => { |
| 79 | + const { adapter, payload } = await seedCategory() |
| 80 | + const Model = adapter.collections.categories! |
| 81 | + |
| 82 | + const aggregateSpy = vi.spyOn(Model, 'aggregate') |
| 83 | + const paginateSpy = vi.spyOn(Model, 'paginate') |
| 84 | + |
| 85 | + await payload.find({ |
| 86 | + collection: 'categories', |
| 87 | + limit: 20, |
| 88 | + // @ts-expect-error not generated |
| 89 | + select: { title: true }, |
| 90 | + where: { title: { equals: 'a' } }, |
| 91 | + }) |
| 92 | + |
| 93 | + expect(aggregateSpy).not.toHaveBeenCalled() |
| 94 | + expect(paginateSpy).toHaveBeenCalledTimes(1) |
| 95 | + }) |
| 96 | + |
| 97 | + it('should use Model.paginate when every join is disabled individually', async () => { |
| 98 | + const { adapter, payload } = await seedCategory() |
| 99 | + const Model = adapter.collections.categories! |
| 100 | + |
| 101 | + const aggregateSpy = vi.spyOn(Model, 'aggregate') |
| 102 | + const paginateSpy = vi.spyOn(Model, 'paginate') |
| 103 | + |
| 104 | + await payload.find({ |
| 105 | + collection: 'categories', |
| 106 | + // @ts-expect-error not generated |
| 107 | + joins: { posts: false }, |
| 108 | + limit: 20, |
| 109 | + where: { title: { equals: 'a' } }, |
| 110 | + }) |
| 111 | + |
| 112 | + expect(aggregateSpy).not.toHaveBeenCalled() |
| 113 | + expect(paginateSpy).toHaveBeenCalledTimes(1) |
| 114 | + }) |
| 115 | + |
| 116 | + it('should use Model.findOne for findByID when a select excludes every join field', async () => { |
| 117 | + const { adapter, category, payload } = await seedCategory() |
| 118 | + const Model = adapter.collections.categories! |
| 119 | + |
| 120 | + const aggregateSpy = vi.spyOn(Model, 'aggregate') |
| 121 | + const findOneSpy = vi.spyOn(Model, 'findOne') |
| 122 | + |
| 123 | + await payload.findByID({ |
| 124 | + collection: 'categories', |
| 125 | + id: category.id, |
| 126 | + // @ts-expect-error not generated |
| 127 | + select: { title: true }, |
| 128 | + }) |
| 129 | + |
| 130 | + expect(aggregateSpy).not.toHaveBeenCalled() |
| 131 | + expect(findOneSpy).toHaveBeenCalledTimes(1) |
| 132 | + }) |
| 133 | + |
| 134 | + it('should still use Model.aggregate when a join field is actually selected', async () => { |
| 135 | + const { adapter, payload } = await seedCategory() |
| 136 | + const Model = adapter.collections.categories! |
| 137 | + |
| 138 | + const aggregateSpy = vi.spyOn(Model, 'aggregate') |
| 139 | + const paginateSpy = vi.spyOn(Model, 'paginate') |
| 140 | + |
| 141 | + await payload.find({ |
| 142 | + collection: 'categories', |
| 143 | + limit: 20, |
| 144 | + where: { title: { equals: 'a' } }, |
| 145 | + }) |
| 146 | + |
| 147 | + expect(aggregateSpy).toHaveBeenCalledTimes(1) |
| 148 | + expect(paginateSpy).not.toHaveBeenCalled() |
| 149 | + }) |
| 150 | + }, |
| 151 | +) |
0 commit comments