@nathapp/nestjs-prisma 3.3.0 → 4.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +10 -7
- package/.eslintrc.js +0 -8
- package/.turbo/turbo-build.log +0 -4
- package/CHANGELOG.md +0 -279
- package/docs/20260215-fix-plan-prisma.md +0 -37
- package/docs/20260215-review-prisma.md +0 -598
- package/docs/20260216-plan-prisma-a-minus.md +0 -70
- package/lib/errors/prisma-error.mapper.ts +0 -164
- package/lib/filter/prisma-exception.filter.ts +0 -67
- package/lib/index.ts +0 -12
- package/lib/pagination/index.ts +0 -1
- package/lib/pagination/paginate.ts +0 -44
- package/lib/prisma.constants.ts +0 -40
- package/lib/prisma.decorator.ts +0 -8
- package/lib/prisma.health.ts +0 -40
- package/lib/prisma.interfaces.ts +0 -51
- package/lib/prisma.module.ts +0 -281
- package/lib/prisma.service.ts +0 -179
- package/lib/repository/abstract-prisma.repository.ts +0 -138
- package/lib/repository/index.ts +0 -1
- package/lib/testing/index.ts +0 -1
- package/lib/testing/prisma-testing.utils.ts +0 -29
- package/lib/transaction/index.ts +0 -2
- package/lib/transaction/prisma-transaction-manager.ts +0 -77
- package/lib/transaction/prisma-transaction.constants.ts +0 -29
- package/test/error-mapping.spec.ts +0 -231
- package/test/jest-nax.json +0 -27
- package/test/jest-test.json +0 -29
- package/test/lifecycle.spec.ts +0 -241
- package/test/multi-db-als.spec.ts +0 -142
- package/test/multi-db-transaction.spec.ts +0 -75
- package/test/pagination/paginate.spec.ts +0 -359
- package/test/prisma-exception-filter.spec.ts +0 -213
- package/test/prisma-transaction-manager.spec.ts +0 -285
- package/test/prisma.decorator.spec.ts +0 -257
- package/test/prisma.health.spec.ts +0 -226
- package/test/prisma.module.spec.ts +0 -506
- package/test/prisma.service.spec.ts +0 -359
- package/test/repository/abstract-prisma.repository.spec.ts +0 -157
- package/test/transaction-manager-e2e.spec.ts +0 -496
- package/test/transaction.spec.ts +0 -371
- package/test/transparent-transaction.spec.ts +0 -500
- package/tsconfig.json +0 -9
|
@@ -1,142 +0,0 @@
|
|
|
1
|
-
import { Test } from '@nestjs/testing';
|
|
2
|
-
import { ITransactionManager, TRANSACTION_MANAGER } from '@nathapp/nestjs-data';
|
|
3
|
-
import { PrismaModule } from '../lib/prisma.module';
|
|
4
|
-
import { PrismaTransactionManager } from '../lib/transaction/prisma-transaction-manager';
|
|
5
|
-
import { getPrismaToken, getPrismaTransactionManagerToken } from '../lib/prisma.constants';
|
|
6
|
-
import { PRISMA_TRANSACTION_ALS, getPrismaTransactionAlsToken } from '../lib/transaction/prisma-transaction.constants';
|
|
7
|
-
|
|
8
|
-
class FakePrismaClient {
|
|
9
|
-
$connect = jest.fn().mockResolvedValue(undefined);
|
|
10
|
-
$disconnect = jest.fn().mockResolvedValue(undefined);
|
|
11
|
-
$on = jest.fn();
|
|
12
|
-
$transaction = jest.fn().mockImplementation(async (cb: any) => {
|
|
13
|
-
const txClient = { $$marker: Math.random() };
|
|
14
|
-
return cb(txClient);
|
|
15
|
-
});
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
describe('Multi-DB Prisma ALS token wiring', () => {
|
|
19
|
-
it('named connections register distinct ALS tokens', async () => {
|
|
20
|
-
const module = await Test.createTestingModule({
|
|
21
|
-
imports: [
|
|
22
|
-
PrismaModule.forRoot({
|
|
23
|
-
name: 'dbA',
|
|
24
|
-
client: FakePrismaClient as any,
|
|
25
|
-
transaction: true,
|
|
26
|
-
}),
|
|
27
|
-
PrismaModule.forRoot({
|
|
28
|
-
name: 'dbB',
|
|
29
|
-
client: FakePrismaClient as any,
|
|
30
|
-
transaction: true,
|
|
31
|
-
}),
|
|
32
|
-
],
|
|
33
|
-
}).compile();
|
|
34
|
-
|
|
35
|
-
const alsA = module.get(getPrismaTransactionAlsToken('dbA'));
|
|
36
|
-
const alsB = module.get(getPrismaTransactionAlsToken('dbB'));
|
|
37
|
-
|
|
38
|
-
expect(alsA).toBeDefined();
|
|
39
|
-
expect(alsB).toBeDefined();
|
|
40
|
-
expect(alsA).not.toBe(alsB);
|
|
41
|
-
});
|
|
42
|
-
|
|
43
|
-
it('default connection registers global PRISMA_TRANSACTION_ALS', async () => {
|
|
44
|
-
const module = await Test.createTestingModule({
|
|
45
|
-
imports: [
|
|
46
|
-
PrismaModule.forRoot({
|
|
47
|
-
client: FakePrismaClient as any,
|
|
48
|
-
transaction: true,
|
|
49
|
-
}),
|
|
50
|
-
],
|
|
51
|
-
}).compile();
|
|
52
|
-
|
|
53
|
-
const als = module.get(PRISMA_TRANSACTION_ALS);
|
|
54
|
-
expect(als).toBeDefined();
|
|
55
|
-
expect(typeof als.run).toBe('function');
|
|
56
|
-
});
|
|
57
|
-
|
|
58
|
-
it('default + named: ALS are isolated', async () => {
|
|
59
|
-
const module = await Test.createTestingModule({
|
|
60
|
-
imports: [
|
|
61
|
-
PrismaModule.forRoot({
|
|
62
|
-
client: FakePrismaClient as any,
|
|
63
|
-
transaction: true,
|
|
64
|
-
}),
|
|
65
|
-
PrismaModule.forRoot({
|
|
66
|
-
name: 'analytics',
|
|
67
|
-
client: FakePrismaClient as any,
|
|
68
|
-
transaction: true,
|
|
69
|
-
}),
|
|
70
|
-
],
|
|
71
|
-
}).compile();
|
|
72
|
-
|
|
73
|
-
const defaultAls = module.get(PRISMA_TRANSACTION_ALS);
|
|
74
|
-
const analyticsAls = module.get(getPrismaTransactionAlsToken('analytics'));
|
|
75
|
-
|
|
76
|
-
expect(defaultAls).toBeDefined();
|
|
77
|
-
expect(analyticsAls).toBeDefined();
|
|
78
|
-
expect(defaultAls).not.toBe(analyticsAls);
|
|
79
|
-
});
|
|
80
|
-
|
|
81
|
-
it('forRootAsync named connections register distinct ALS tokens', async () => {
|
|
82
|
-
const module = await Test.createTestingModule({
|
|
83
|
-
imports: [
|
|
84
|
-
PrismaModule.forRootAsync({
|
|
85
|
-
name: 'dbA',
|
|
86
|
-
useFactory: () => ({
|
|
87
|
-
name: 'dbA',
|
|
88
|
-
client: FakePrismaClient as any,
|
|
89
|
-
transaction: true,
|
|
90
|
-
}),
|
|
91
|
-
}),
|
|
92
|
-
PrismaModule.forRootAsync({
|
|
93
|
-
name: 'dbB',
|
|
94
|
-
useFactory: () => ({
|
|
95
|
-
name: 'dbB',
|
|
96
|
-
client: FakePrismaClient as any,
|
|
97
|
-
transaction: true,
|
|
98
|
-
}),
|
|
99
|
-
}),
|
|
100
|
-
],
|
|
101
|
-
}).compile();
|
|
102
|
-
|
|
103
|
-
const alsA = module.get(getPrismaTransactionAlsToken('dbA'));
|
|
104
|
-
const alsB = module.get(getPrismaTransactionAlsToken('dbB'));
|
|
105
|
-
|
|
106
|
-
expect(alsA).toBeDefined();
|
|
107
|
-
expect(alsB).toBeDefined();
|
|
108
|
-
expect(alsA).not.toBe(alsB);
|
|
109
|
-
});
|
|
110
|
-
|
|
111
|
-
it('injected PrismaTransactionManager uses correct ALS per connection', async () => {
|
|
112
|
-
const module = await Test.createTestingModule({
|
|
113
|
-
imports: [
|
|
114
|
-
PrismaModule.forRoot({
|
|
115
|
-
name: 'dbA',
|
|
116
|
-
client: FakePrismaClient as any,
|
|
117
|
-
transaction: true,
|
|
118
|
-
}),
|
|
119
|
-
PrismaModule.forRoot({
|
|
120
|
-
name: 'dbB',
|
|
121
|
-
client: FakePrismaClient as any,
|
|
122
|
-
transaction: true,
|
|
123
|
-
}),
|
|
124
|
-
],
|
|
125
|
-
}).compile();
|
|
126
|
-
|
|
127
|
-
const txA = module.get<PrismaTransactionManager>(getPrismaTransactionManagerToken('dbA'));
|
|
128
|
-
const txB = module.get<PrismaTransactionManager>(getPrismaTransactionManagerToken('dbB'));
|
|
129
|
-
|
|
130
|
-
// txA and txB should have independent ALS state
|
|
131
|
-
expect(txA.isInTransaction()).toBe(false);
|
|
132
|
-
expect(txB.isInTransaction()).toBe(false);
|
|
133
|
-
|
|
134
|
-
await txA.run(async () => {
|
|
135
|
-
expect(txA.isInTransaction()).toBe(true);
|
|
136
|
-
expect(txB.isInTransaction()).toBe(false);
|
|
137
|
-
});
|
|
138
|
-
|
|
139
|
-
expect(txA.isInTransaction()).toBe(false);
|
|
140
|
-
expect(txB.isInTransaction()).toBe(false);
|
|
141
|
-
});
|
|
142
|
-
});
|
|
@@ -1,75 +0,0 @@
|
|
|
1
|
-
import { Test } from '@nestjs/testing';
|
|
2
|
-
import { ITransactionManager, TRANSACTION_MANAGER } from '@nathapp/nestjs-data';
|
|
3
|
-
import { PrismaModule } from '../lib/prisma.module';
|
|
4
|
-
import { getPrismaTransactionManagerToken } from '../lib/prisma.constants';
|
|
5
|
-
|
|
6
|
-
class FakePrismaClient {
|
|
7
|
-
$connect = jest.fn().mockResolvedValue(undefined);
|
|
8
|
-
$disconnect = jest.fn().mockResolvedValue(undefined);
|
|
9
|
-
$on = jest.fn();
|
|
10
|
-
// Each instance gets a unique transaction client to prove isolation.
|
|
11
|
-
$transaction = jest.fn().mockImplementation(async (cb: any) => {
|
|
12
|
-
const txClient = { $$marker: Math.random() };
|
|
13
|
-
return cb(txClient);
|
|
14
|
-
});
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
describe('Multi-DB Prisma — two named connections', () => {
|
|
18
|
-
it('registers two distinct named tx managers, no collision', async () => {
|
|
19
|
-
const module = await Test.createTestingModule({
|
|
20
|
-
imports: [
|
|
21
|
-
PrismaModule.forRoot({ name: 'dbA', client: FakePrismaClient as any, transaction: true }),
|
|
22
|
-
PrismaModule.forRoot({ name: 'dbB', client: FakePrismaClient as any, transaction: true }),
|
|
23
|
-
],
|
|
24
|
-
}).compile();
|
|
25
|
-
|
|
26
|
-
const txA = module.get<ITransactionManager>(getPrismaTransactionManagerToken('dbA'));
|
|
27
|
-
const txB = module.get<ITransactionManager>(getPrismaTransactionManagerToken('dbB'));
|
|
28
|
-
|
|
29
|
-
expect(txA).not.toBe(txB);
|
|
30
|
-
expect(typeof txA.run).toBe('function');
|
|
31
|
-
expect(typeof txB.run).toBe('function');
|
|
32
|
-
|
|
33
|
-
// No global alias when neither connection is default.
|
|
34
|
-
expect(() => module.get(TRANSACTION_MANAGER)).toThrow();
|
|
35
|
-
});
|
|
36
|
-
|
|
37
|
-
it('tx.run on dbA does not affect dbB tx state', async () => {
|
|
38
|
-
const module = await Test.createTestingModule({
|
|
39
|
-
imports: [
|
|
40
|
-
PrismaModule.forRoot({ name: 'dbA', client: FakePrismaClient as any, transaction: true }),
|
|
41
|
-
PrismaModule.forRoot({ name: 'dbB', client: FakePrismaClient as any, transaction: true }),
|
|
42
|
-
],
|
|
43
|
-
}).compile();
|
|
44
|
-
|
|
45
|
-
const txA = module.get<ITransactionManager>(getPrismaTransactionManagerToken('dbA'));
|
|
46
|
-
const txB = module.get<ITransactionManager>(getPrismaTransactionManagerToken('dbB'));
|
|
47
|
-
|
|
48
|
-
expect(txA.isInTransaction()).toBe(false);
|
|
49
|
-
expect(txB.isInTransaction()).toBe(false);
|
|
50
|
-
|
|
51
|
-
await txA.run(async () => {
|
|
52
|
-
expect(txA.isInTransaction()).toBe(true);
|
|
53
|
-
expect(txB.isInTransaction()).toBe(false); // dbB is independent
|
|
54
|
-
});
|
|
55
|
-
|
|
56
|
-
expect(txA.isInTransaction()).toBe(false);
|
|
57
|
-
});
|
|
58
|
-
|
|
59
|
-
it('default + named connection: only default aliases the global token', async () => {
|
|
60
|
-
const module = await Test.createTestingModule({
|
|
61
|
-
imports: [
|
|
62
|
-
PrismaModule.forRoot({ client: FakePrismaClient as any, transaction: true }),
|
|
63
|
-
PrismaModule.forRoot({ name: 'analytics', client: FakePrismaClient as any, transaction: true }),
|
|
64
|
-
],
|
|
65
|
-
}).compile();
|
|
66
|
-
|
|
67
|
-
const txDefault = module.get<ITransactionManager>(getPrismaTransactionManagerToken());
|
|
68
|
-
const txAnalytics = module.get<ITransactionManager>(getPrismaTransactionManagerToken('analytics'));
|
|
69
|
-
const global = module.get<ITransactionManager>(TRANSACTION_MANAGER);
|
|
70
|
-
|
|
71
|
-
expect(txDefault).toBe(global); // default == global alias
|
|
72
|
-
expect(txAnalytics).not.toBe(global); // named is separate
|
|
73
|
-
expect(txAnalytics).not.toBe(txDefault);
|
|
74
|
-
});
|
|
75
|
-
});
|
|
@@ -1,359 +0,0 @@
|
|
|
1
|
-
import { Paginate } from '../../lib/pagination/paginate';
|
|
2
|
-
import { Page, PageOption } from '@nathapp/nestjs-common';
|
|
3
|
-
|
|
4
|
-
describe('Paginate', () => {
|
|
5
|
-
interface User {
|
|
6
|
-
id: number;
|
|
7
|
-
name: string;
|
|
8
|
-
email: string;
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
let mockModel: {
|
|
12
|
-
findMany: jest.Mock;
|
|
13
|
-
count: jest.Mock;
|
|
14
|
-
};
|
|
15
|
-
|
|
16
|
-
beforeEach(() => {
|
|
17
|
-
mockModel = {
|
|
18
|
-
findMany: jest.fn(),
|
|
19
|
-
count: jest.fn(),
|
|
20
|
-
};
|
|
21
|
-
});
|
|
22
|
-
|
|
23
|
-
describe('basic pagination', () => {
|
|
24
|
-
it('should paginate with correct skip/take for first page', async () => {
|
|
25
|
-
const users: User[] = [
|
|
26
|
-
{ id: 1, name: 'Alice', email: 'alice@test.com' },
|
|
27
|
-
{ id: 2, name: 'Bob', email: 'bob@test.com' },
|
|
28
|
-
];
|
|
29
|
-
|
|
30
|
-
mockModel.findMany.mockResolvedValue(users);
|
|
31
|
-
mockModel.count.mockResolvedValue(10);
|
|
32
|
-
|
|
33
|
-
const pageOption = new PageOption(1, 2);
|
|
34
|
-
const result = await Paginate(mockModel, pageOption);
|
|
35
|
-
|
|
36
|
-
expect(mockModel.findMany).toHaveBeenCalledWith({
|
|
37
|
-
skip: 0,
|
|
38
|
-
take: 2,
|
|
39
|
-
});
|
|
40
|
-
expect(result).toBeInstanceOf(Page);
|
|
41
|
-
expect(result.records).toEqual(users);
|
|
42
|
-
expect(result.total).toBe(10);
|
|
43
|
-
});
|
|
44
|
-
|
|
45
|
-
it('should paginate with correct skip/take for second page', async () => {
|
|
46
|
-
const users: User[] = [
|
|
47
|
-
{ id: 3, name: 'Charlie', email: 'charlie@test.com' },
|
|
48
|
-
{ id: 4, name: 'Diana', email: 'diana@test.com' },
|
|
49
|
-
];
|
|
50
|
-
|
|
51
|
-
mockModel.findMany.mockResolvedValue(users);
|
|
52
|
-
mockModel.count.mockResolvedValue(10);
|
|
53
|
-
|
|
54
|
-
const pageOption = new PageOption(2, 2);
|
|
55
|
-
const result = await Paginate(mockModel, pageOption);
|
|
56
|
-
|
|
57
|
-
expect(mockModel.findMany).toHaveBeenCalledWith({
|
|
58
|
-
skip: 2,
|
|
59
|
-
take: 2,
|
|
60
|
-
});
|
|
61
|
-
expect(result.records).toEqual(users);
|
|
62
|
-
});
|
|
63
|
-
|
|
64
|
-
it('should paginate with correct skip/take for larger page size', async () => {
|
|
65
|
-
mockModel.findMany.mockResolvedValue([]);
|
|
66
|
-
mockModel.count.mockResolvedValue(100);
|
|
67
|
-
|
|
68
|
-
const pageOption = new PageOption(3, 20);
|
|
69
|
-
await Paginate(mockModel, pageOption);
|
|
70
|
-
|
|
71
|
-
expect(mockModel.findMany).toHaveBeenCalledWith({
|
|
72
|
-
skip: 40,
|
|
73
|
-
take: 20,
|
|
74
|
-
});
|
|
75
|
-
});
|
|
76
|
-
});
|
|
77
|
-
|
|
78
|
-
describe('page metadata', () => {
|
|
79
|
-
it('should return correct total count', async () => {
|
|
80
|
-
mockModel.findMany.mockResolvedValue([]);
|
|
81
|
-
mockModel.count.mockResolvedValue(42);
|
|
82
|
-
|
|
83
|
-
const pageOption = new PageOption(1, 10);
|
|
84
|
-
const result = await Paginate(mockModel, pageOption);
|
|
85
|
-
|
|
86
|
-
expect(result.total).toBe(42);
|
|
87
|
-
});
|
|
88
|
-
|
|
89
|
-
it('should calculate hasNext correctly when more pages exist', async () => {
|
|
90
|
-
mockModel.findMany.mockResolvedValue([{ id: 1 }, { id: 2 }]);
|
|
91
|
-
mockModel.count.mockResolvedValue(10);
|
|
92
|
-
|
|
93
|
-
const pageOption = new PageOption(1, 2);
|
|
94
|
-
const result = await Paginate(mockModel, pageOption);
|
|
95
|
-
|
|
96
|
-
expect(result.hasNext).toBe(true);
|
|
97
|
-
});
|
|
98
|
-
|
|
99
|
-
it('should calculate hasNext correctly on last page', async () => {
|
|
100
|
-
mockModel.findMany.mockResolvedValue([{ id: 9 }, { id: 10 }]);
|
|
101
|
-
mockModel.count.mockResolvedValue(10);
|
|
102
|
-
|
|
103
|
-
const pageOption = new PageOption(5, 2);
|
|
104
|
-
const result = await Paginate(mockModel, pageOption);
|
|
105
|
-
|
|
106
|
-
expect(result.hasNext).toBe(false);
|
|
107
|
-
});
|
|
108
|
-
|
|
109
|
-
it('should calculate hasPrev correctly on first page', async () => {
|
|
110
|
-
mockModel.findMany.mockResolvedValue([{ id: 1 }, { id: 2 }]);
|
|
111
|
-
mockModel.count.mockResolvedValue(10);
|
|
112
|
-
|
|
113
|
-
const pageOption = new PageOption(1, 2);
|
|
114
|
-
const result = await Paginate(mockModel, pageOption);
|
|
115
|
-
|
|
116
|
-
expect(result.hasPrev).toBe(false);
|
|
117
|
-
});
|
|
118
|
-
|
|
119
|
-
it('should calculate hasPrev correctly on second page', async () => {
|
|
120
|
-
mockModel.findMany.mockResolvedValue([{ id: 3 }, { id: 4 }]);
|
|
121
|
-
mockModel.count.mockResolvedValue(10);
|
|
122
|
-
|
|
123
|
-
const pageOption = new PageOption(2, 2);
|
|
124
|
-
const result = await Paginate(mockModel, pageOption);
|
|
125
|
-
|
|
126
|
-
expect(result.hasPrev).toBe(true);
|
|
127
|
-
});
|
|
128
|
-
});
|
|
129
|
-
|
|
130
|
-
describe('query arguments', () => {
|
|
131
|
-
it('should pass where clause to findMany', async () => {
|
|
132
|
-
mockModel.findMany.mockResolvedValue([]);
|
|
133
|
-
mockModel.count.mockResolvedValue(0);
|
|
134
|
-
|
|
135
|
-
const pageOption = new PageOption(1, 10);
|
|
136
|
-
const where = { status: 'active' };
|
|
137
|
-
|
|
138
|
-
await Paginate(mockModel, pageOption, { where });
|
|
139
|
-
|
|
140
|
-
expect(mockModel.findMany).toHaveBeenCalledWith({
|
|
141
|
-
where,
|
|
142
|
-
skip: 0,
|
|
143
|
-
take: 10,
|
|
144
|
-
});
|
|
145
|
-
});
|
|
146
|
-
|
|
147
|
-
it('should pass where clause to count', async () => {
|
|
148
|
-
mockModel.findMany.mockResolvedValue([]);
|
|
149
|
-
mockModel.count.mockResolvedValue(0);
|
|
150
|
-
|
|
151
|
-
const pageOption = new PageOption(1, 10);
|
|
152
|
-
const where = { status: 'active' };
|
|
153
|
-
|
|
154
|
-
await Paginate(mockModel, pageOption, { where });
|
|
155
|
-
|
|
156
|
-
expect(mockModel.count).toHaveBeenCalledWith({ where });
|
|
157
|
-
});
|
|
158
|
-
|
|
159
|
-
it('should pass orderBy to findMany', async () => {
|
|
160
|
-
mockModel.findMany.mockResolvedValue([]);
|
|
161
|
-
mockModel.count.mockResolvedValue(0);
|
|
162
|
-
|
|
163
|
-
const pageOption = new PageOption(1, 10);
|
|
164
|
-
const orderBy = { createdAt: 'desc' };
|
|
165
|
-
|
|
166
|
-
await Paginate(mockModel, pageOption, { orderBy });
|
|
167
|
-
|
|
168
|
-
expect(mockModel.findMany).toHaveBeenCalledWith({
|
|
169
|
-
orderBy,
|
|
170
|
-
skip: 0,
|
|
171
|
-
take: 10,
|
|
172
|
-
});
|
|
173
|
-
});
|
|
174
|
-
|
|
175
|
-
it('should pass include to findMany', async () => {
|
|
176
|
-
mockModel.findMany.mockResolvedValue([]);
|
|
177
|
-
mockModel.count.mockResolvedValue(0);
|
|
178
|
-
|
|
179
|
-
const pageOption = new PageOption(1, 10);
|
|
180
|
-
const include = { posts: true };
|
|
181
|
-
|
|
182
|
-
await Paginate(mockModel, pageOption, { include });
|
|
183
|
-
|
|
184
|
-
expect(mockModel.findMany).toHaveBeenCalledWith({
|
|
185
|
-
include,
|
|
186
|
-
skip: 0,
|
|
187
|
-
take: 10,
|
|
188
|
-
});
|
|
189
|
-
});
|
|
190
|
-
|
|
191
|
-
it('should pass select to findMany', async () => {
|
|
192
|
-
mockModel.findMany.mockResolvedValue([]);
|
|
193
|
-
mockModel.count.mockResolvedValue(0);
|
|
194
|
-
|
|
195
|
-
const pageOption = new PageOption(1, 10);
|
|
196
|
-
const select = { id: true, name: true };
|
|
197
|
-
|
|
198
|
-
await Paginate(mockModel, pageOption, { select });
|
|
199
|
-
|
|
200
|
-
expect(mockModel.findMany).toHaveBeenCalledWith({
|
|
201
|
-
select,
|
|
202
|
-
skip: 0,
|
|
203
|
-
take: 10,
|
|
204
|
-
});
|
|
205
|
-
});
|
|
206
|
-
|
|
207
|
-
it('should pass all args combined to findMany', async () => {
|
|
208
|
-
mockModel.findMany.mockResolvedValue([]);
|
|
209
|
-
mockModel.count.mockResolvedValue(0);
|
|
210
|
-
|
|
211
|
-
const pageOption = new PageOption(2, 5);
|
|
212
|
-
const args = {
|
|
213
|
-
where: { status: 'active' },
|
|
214
|
-
orderBy: { createdAt: 'desc' },
|
|
215
|
-
include: { posts: true },
|
|
216
|
-
};
|
|
217
|
-
|
|
218
|
-
await Paginate(mockModel, pageOption, args);
|
|
219
|
-
|
|
220
|
-
expect(mockModel.findMany).toHaveBeenCalledWith({
|
|
221
|
-
...args,
|
|
222
|
-
skip: 5,
|
|
223
|
-
take: 5,
|
|
224
|
-
});
|
|
225
|
-
});
|
|
226
|
-
|
|
227
|
-
it('should not pass orderBy/include/select to count', async () => {
|
|
228
|
-
mockModel.findMany.mockResolvedValue([]);
|
|
229
|
-
mockModel.count.mockResolvedValue(0);
|
|
230
|
-
|
|
231
|
-
const pageOption = new PageOption(1, 10);
|
|
232
|
-
const args = {
|
|
233
|
-
where: { status: 'active' },
|
|
234
|
-
orderBy: { createdAt: 'desc' },
|
|
235
|
-
include: { posts: true },
|
|
236
|
-
select: { id: true },
|
|
237
|
-
};
|
|
238
|
-
|
|
239
|
-
await Paginate(mockModel, pageOption, args);
|
|
240
|
-
|
|
241
|
-
// Only where should be passed to count
|
|
242
|
-
expect(mockModel.count).toHaveBeenCalledWith({ where: args.where });
|
|
243
|
-
expect(mockModel.count).not.toHaveBeenCalledWith(
|
|
244
|
-
expect.objectContaining({ orderBy: expect.anything() }),
|
|
245
|
-
);
|
|
246
|
-
});
|
|
247
|
-
});
|
|
248
|
-
|
|
249
|
-
describe('edge cases', () => {
|
|
250
|
-
it('should handle empty results', async () => {
|
|
251
|
-
mockModel.findMany.mockResolvedValue([]);
|
|
252
|
-
mockModel.count.mockResolvedValue(0);
|
|
253
|
-
|
|
254
|
-
const pageOption = new PageOption(1, 10);
|
|
255
|
-
const result = await Paginate(mockModel, pageOption);
|
|
256
|
-
|
|
257
|
-
expect(result.records).toEqual([]);
|
|
258
|
-
expect(result.total).toBe(0);
|
|
259
|
-
expect(result.hasNext).toBe(false);
|
|
260
|
-
expect(result.hasPrev).toBe(false);
|
|
261
|
-
});
|
|
262
|
-
|
|
263
|
-
it('should handle single page of results', async () => {
|
|
264
|
-
const users = [
|
|
265
|
-
{ id: 1, name: 'Alice', email: 'alice@test.com' },
|
|
266
|
-
{ id: 2, name: 'Bob', email: 'bob@test.com' },
|
|
267
|
-
];
|
|
268
|
-
mockModel.findMany.mockResolvedValue(users);
|
|
269
|
-
mockModel.count.mockResolvedValue(2);
|
|
270
|
-
|
|
271
|
-
const pageOption = new PageOption(1, 10);
|
|
272
|
-
const result = await Paginate(mockModel, pageOption);
|
|
273
|
-
|
|
274
|
-
expect(result.records).toEqual(users);
|
|
275
|
-
expect(result.total).toBe(2);
|
|
276
|
-
expect(result.hasNext).toBe(false);
|
|
277
|
-
expect(result.hasPrev).toBe(false);
|
|
278
|
-
});
|
|
279
|
-
|
|
280
|
-
it('should handle page beyond available records', async () => {
|
|
281
|
-
mockModel.findMany.mockResolvedValue([]);
|
|
282
|
-
mockModel.count.mockResolvedValue(10);
|
|
283
|
-
|
|
284
|
-
const pageOption = new PageOption(100, 10);
|
|
285
|
-
const result = await Paginate(mockModel, pageOption);
|
|
286
|
-
|
|
287
|
-
expect(result.records).toEqual([]);
|
|
288
|
-
expect(result.total).toBe(10);
|
|
289
|
-
});
|
|
290
|
-
|
|
291
|
-
it('should work with no args parameter', async () => {
|
|
292
|
-
mockModel.findMany.mockResolvedValue([]);
|
|
293
|
-
mockModel.count.mockResolvedValue(0);
|
|
294
|
-
|
|
295
|
-
const pageOption = new PageOption(1, 10);
|
|
296
|
-
const result = await Paginate(mockModel, pageOption);
|
|
297
|
-
|
|
298
|
-
expect(mockModel.findMany).toHaveBeenCalledWith({
|
|
299
|
-
skip: 0,
|
|
300
|
-
take: 10,
|
|
301
|
-
});
|
|
302
|
-
expect(mockModel.count).toHaveBeenCalledWith({ where: undefined });
|
|
303
|
-
expect(result).toBeInstanceOf(Page);
|
|
304
|
-
});
|
|
305
|
-
|
|
306
|
-
it('should work with undefined where in args', async () => {
|
|
307
|
-
mockModel.findMany.mockResolvedValue([]);
|
|
308
|
-
mockModel.count.mockResolvedValue(0);
|
|
309
|
-
|
|
310
|
-
const pageOption = new PageOption(1, 10);
|
|
311
|
-
await Paginate(mockModel, pageOption, {});
|
|
312
|
-
|
|
313
|
-
expect(mockModel.count).toHaveBeenCalledWith({ where: undefined });
|
|
314
|
-
});
|
|
315
|
-
});
|
|
316
|
-
|
|
317
|
-
describe('concurrent execution', () => {
|
|
318
|
-
it('should call findMany and count in parallel', async () => {
|
|
319
|
-
const findManyPromise = new Promise((resolve) =>
|
|
320
|
-
setTimeout(() => resolve([]), 10),
|
|
321
|
-
);
|
|
322
|
-
const countPromise = new Promise((resolve) => setTimeout(() => resolve(5), 10));
|
|
323
|
-
|
|
324
|
-
mockModel.findMany.mockReturnValue(findManyPromise);
|
|
325
|
-
mockModel.count.mockReturnValue(countPromise);
|
|
326
|
-
|
|
327
|
-
const pageOption = new PageOption(1, 10);
|
|
328
|
-
const startTime = Date.now();
|
|
329
|
-
|
|
330
|
-
await Paginate(mockModel, pageOption);
|
|
331
|
-
|
|
332
|
-
const duration = Date.now() - startTime;
|
|
333
|
-
|
|
334
|
-
// In CI environments, timer precision may vary.
|
|
335
|
-
// We just want to ensure it's reasonably fast and definitely parallel (not 200ms+ serial)
|
|
336
|
-
expect(duration).toBeLessThan(100);
|
|
337
|
-
expect(mockModel.findMany).toHaveBeenCalled();
|
|
338
|
-
expect(mockModel.count).toHaveBeenCalled();
|
|
339
|
-
});
|
|
340
|
-
});
|
|
341
|
-
|
|
342
|
-
describe('type safety', () => {
|
|
343
|
-
it('should return correctly typed Page<T>', async () => {
|
|
344
|
-
const users: User[] = [
|
|
345
|
-
{ id: 1, name: 'Alice', email: 'alice@test.com' },
|
|
346
|
-
];
|
|
347
|
-
mockModel.findMany.mockResolvedValue(users);
|
|
348
|
-
mockModel.count.mockResolvedValue(1);
|
|
349
|
-
|
|
350
|
-
const pageOption = new PageOption(1, 10);
|
|
351
|
-
const result = await Paginate<User>(mockModel, pageOption);
|
|
352
|
-
|
|
353
|
-
expect(result).toBeInstanceOf(Page);
|
|
354
|
-
expect(result.records[0].id).toBe(1);
|
|
355
|
-
expect(result.records[0].name).toBe('Alice');
|
|
356
|
-
expect(result.records[0].email).toBe('alice@test.com');
|
|
357
|
-
});
|
|
358
|
-
});
|
|
359
|
-
});
|