|
| 1 | +import type { Params } from '@feathersjs/feathers' |
| 2 | +import { feathers } from '@feathersjs/feathers' |
| 3 | +import { MemoryService } from '@feathersjs/memory' |
| 4 | +import { chunkFind } from './chunk-find.util.js' |
| 5 | + |
| 6 | +type User = { |
| 7 | + id: number |
| 8 | + name: string |
| 9 | +} |
| 10 | + |
| 11 | +const setup = async () => { |
| 12 | + const app = feathers<{ |
| 13 | + users: MemoryService< |
| 14 | + User, |
| 15 | + Partial<User>, |
| 16 | + Params<{ id: number; name: string }> |
| 17 | + > |
| 18 | + }>() |
| 19 | + |
| 20 | + app.use( |
| 21 | + 'users', |
| 22 | + new MemoryService({ |
| 23 | + id: 'id', |
| 24 | + startId: 1, |
| 25 | + multi: true, |
| 26 | + paginate: { |
| 27 | + default: 10, |
| 28 | + max: 100, |
| 29 | + }, |
| 30 | + }), |
| 31 | + ) |
| 32 | + |
| 33 | + const usersService = app.service('users') |
| 34 | + |
| 35 | + for (let i = 1; i <= 100; i++) { |
| 36 | + await usersService.create({ name: `test${i}` }) |
| 37 | + } |
| 38 | + |
| 39 | + return { app, usersService } |
| 40 | +} |
| 41 | + |
| 42 | +describe('chunkFind', function () { |
| 43 | + it('basic usage', async function () { |
| 44 | + const { app } = await setup() |
| 45 | + |
| 46 | + const chunks = [] |
| 47 | + |
| 48 | + for await (const chunk of chunkFind(app, 'users')) { |
| 49 | + chunks.push(chunk) |
| 50 | + } |
| 51 | + |
| 52 | + expect(chunks).toHaveLength(10) |
| 53 | + expect(chunks[0]).toHaveLength(10) |
| 54 | + expect(chunks[0]![0]!.name).toBe('test1') |
| 55 | + expect(chunks[9]![9]!.name).toBe('test100') |
| 56 | + }) |
| 57 | + |
| 58 | + it('can skip items', async function () { |
| 59 | + const { app } = await setup() |
| 60 | + |
| 61 | + const chunks = [] |
| 62 | + |
| 63 | + for await (const chunk of chunkFind(app, 'users', { |
| 64 | + params: { query: { $skip: 20 } }, |
| 65 | + })) { |
| 66 | + chunks.push(chunk) |
| 67 | + } |
| 68 | + |
| 69 | + expect(chunks).toHaveLength(8) |
| 70 | + expect(chunks[0]![0]!.name).toBe('test21') |
| 71 | + }) |
| 72 | + |
| 73 | + it('can set chunk size via $limit', async function () { |
| 74 | + const { app } = await setup() |
| 75 | + |
| 76 | + const chunks = [] |
| 77 | + |
| 78 | + for await (const chunk of chunkFind(app, 'users', { |
| 79 | + params: { query: { $limit: 25 } }, |
| 80 | + })) { |
| 81 | + chunks.push(chunk) |
| 82 | + } |
| 83 | + |
| 84 | + expect(chunks).toHaveLength(4) |
| 85 | + expect(chunks[0]).toHaveLength(25) |
| 86 | + expect(chunks[3]).toHaveLength(25) |
| 87 | + }) |
| 88 | + |
| 89 | + it('can query for items', async function () { |
| 90 | + const { app } = await setup() |
| 91 | + |
| 92 | + const chunks = [] |
| 93 | + |
| 94 | + for await (const chunk of chunkFind(app, 'users', { |
| 95 | + params: { query: { name: 'test1' } }, |
| 96 | + })) { |
| 97 | + chunks.push(chunk) |
| 98 | + } |
| 99 | + |
| 100 | + expect(chunks).toHaveLength(1) |
| 101 | + expect(chunks[0]).toEqual([expect.objectContaining({ name: 'test1' })]) |
| 102 | + }) |
| 103 | + |
| 104 | + it("ignores paginate:false and always paginates", async function () { |
| 105 | + const { app } = await setup() |
| 106 | + |
| 107 | + const chunks = [] |
| 108 | + |
| 109 | + for await (const chunk of chunkFind(app, 'users', { |
| 110 | + params: { query: { name: 'test1' }, paginate: false }, |
| 111 | + })) { |
| 112 | + chunks.push(chunk) |
| 113 | + } |
| 114 | + |
| 115 | + expect(chunks).toHaveLength(1) |
| 116 | + expect(chunks[0]).toEqual([expect.objectContaining({ name: 'test1' })]) |
| 117 | + }); |
| 118 | +}) |
0 commit comments