@longzai-intelligence/pagination 0.0.1 → 0.0.3

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.
Files changed (74) hide show
  1. package/README.md +36 -203
  2. package/dist/hono.cjs +1 -0
  3. package/dist/hono.d.cts +15 -0
  4. package/dist/hono.d.mts +15 -0
  5. package/dist/hono.mjs +1 -0
  6. package/dist/index-C4IwIpFc.d.mts +199 -0
  7. package/dist/index-DpbbjAwO.d.cts +199 -0
  8. package/dist/index.cjs +1 -1
  9. package/dist/index.d.cts +79 -80
  10. package/dist/index.d.mts +79 -80
  11. package/dist/index.mjs +1 -1
  12. package/dist/typeorm.cjs +1 -0
  13. package/dist/typeorm.d.cts +26 -0
  14. package/dist/typeorm.d.mts +26 -0
  15. package/dist/typeorm.mjs +1 -0
  16. package/dist/utils-B200OLT-.cjs +1 -0
  17. package/dist/utils-DsM7-PZL.mjs +1 -0
  18. package/package.json +29 -5
  19. package/.turbo/turbo-build.log +0 -22
  20. package/.turbo/turbo-lint.log +0 -1
  21. package/.turbo/turbo-test$colon$coverage.log +0 -36
  22. package/.turbo/turbo-test.log +0 -14
  23. package/.turbo/turbo-typecheck.log +0 -4
  24. package/CHANGELOG.md +0 -11
  25. package/coverage/base.css +0 -224
  26. package/coverage/block-navigation.js +0 -87
  27. package/coverage/coverage-final.json +0 -12
  28. package/coverage/favicon.png +0 -0
  29. package/coverage/index.html +0 -161
  30. package/coverage/prettify.css +0 -1
  31. package/coverage/prettify.js +0 -2
  32. package/coverage/sort-arrow-sprite.png +0 -0
  33. package/coverage/sorter.js +0 -210
  34. package/coverage/src/adapters/index.html +0 -131
  35. package/coverage/src/adapters/index.ts.html +0 -118
  36. package/coverage/src/adapters/typeorm.adapter.ts.html +0 -940
  37. package/coverage/src/index.html +0 -116
  38. package/coverage/src/index.ts.html +0 -253
  39. package/coverage/src/types/index.html +0 -176
  40. package/coverage/src/types/index.ts.html +0 -133
  41. package/coverage/src/types/pagination.types.ts.html +0 -292
  42. package/coverage/src/types/result.types.ts.html +0 -415
  43. package/coverage/src/types/sort.types.ts.html +0 -265
  44. package/coverage/src/types/typeorm.d.ts.html +0 -196
  45. package/coverage/src/utils/index.html +0 -146
  46. package/coverage/src/utils/index.ts.html +0 -178
  47. package/coverage/src/utils/pagination.util.ts.html +0 -703
  48. package/coverage/src/utils/validation.util.ts.html +0 -535
  49. package/eslint.config.ts +0 -3
  50. package/src/__tests__/index.test.ts +0 -66
  51. package/src/__tests__/pagination.util.test.ts +0 -253
  52. package/src/__tests__/typeorm.adapter.test.ts +0 -214
  53. package/src/__tests__/validation.util.test.ts +0 -122
  54. package/src/adapters/index.ts +0 -11
  55. package/src/adapters/typeorm.adapter.ts +0 -285
  56. package/src/index.ts +0 -56
  57. package/src/types/index.ts +0 -16
  58. package/src/types/pagination.types.ts +0 -69
  59. package/src/types/result.types.ts +0 -110
  60. package/src/types/sort.types.ts +0 -60
  61. package/src/types/typeorm.d.ts +0 -37
  62. package/src/utils/index.ts +0 -31
  63. package/src/utils/pagination.util.ts +0 -206
  64. package/src/utils/validation.util.ts +0 -150
  65. package/tsconfig/.cache/app.tsbuildinfo +0 -1
  66. package/tsconfig/.cache/build.tsbuildinfo +0 -1
  67. package/tsconfig/.cache/node.tsbuildinfo +0 -1
  68. package/tsconfig/.cache/test.tsbuildinfo +0 -1
  69. package/tsconfig/app.json +0 -12
  70. package/tsconfig/node.json +0 -11
  71. package/tsconfig/test.json +0 -14
  72. package/tsconfig.json +0 -9
  73. package/tsdown.config.ts +0 -6
  74. package/vitest.config.ts +0 -3
@@ -1,285 +0,0 @@
1
- /**
2
- * TypeORM 分页适配器
3
- *
4
- * 注意:此模块需要安装 typeorm 作为 peerDependency
5
- */
6
-
7
- import type {
8
- Repository,
9
- SelectQueryBuilder,
10
- ObjectLiteral,
11
- FindManyOptions,
12
- FindOptionsOrder,
13
- } from 'typeorm';
14
- import type { PaginationParams, PaginatedResult } from '../types';
15
- import {
16
- normalizePagination,
17
- calculateOffset,
18
- calculateTotalPages,
19
- hasNextPage,
20
- hasPreviousPage,
21
- } from '../utils';
22
-
23
- /**
24
- * 实体映射器函数类型
25
- *
26
- * 将实体类型转换为 DTO 类型的映射函数
27
- *
28
- * @typeParam TEntity - 实体类型
29
- * @typeParam TDto - DTO 类型
30
- */
31
- export type EntityMapper<TEntity, TDto> = (entity: TEntity) => TDto | Promise<TDto>;
32
-
33
- /**
34
- * TypeORM 分页查询选项(带映射器)
35
- *
36
- * 配置 TypeORM 分页查询的选项,必须提供映射函数
37
- *
38
- * @typeParam TEntity - 实体类型
39
- * @typeParam TDto - DTO 类型
40
- */
41
- export type TypeOrmPaginateOptionsWithMapper<TEntity extends ObjectLiteral, TDto> = {
42
- /**
43
- * 实体到 DTO 的映射函数
44
- *
45
- * 必须提供映射函数以确保类型安全
46
- */
47
- mapper: EntityMapper<TEntity, TDto>;
48
-
49
- /**
50
- * 排序规则
51
- */
52
- order?: FindOptionsOrder<TEntity>;
53
-
54
- /**
55
- * 查询条件
56
- */
57
- where?: FindManyOptions<TEntity>['where'];
58
-
59
- /**
60
- * 关联关系
61
- */
62
- relations?: FindManyOptions<TEntity>['relations'];
63
-
64
- /**
65
- * 选择字段
66
- */
67
- select?: FindManyOptions<TEntity>['select'];
68
- };
69
-
70
- /**
71
- * TypeORM 分页查询选项(无映射器)
72
- *
73
- * 配置 TypeORM 分页查询的选项,返回原始实体
74
- *
75
- * @typeParam TEntity - 实体类型
76
- */
77
- export type TypeOrmPaginateOptionsNoMapper<TEntity extends ObjectLiteral> = {
78
- /**
79
- * 排序规则
80
- */
81
- order?: FindOptionsOrder<TEntity>;
82
-
83
- /**
84
- * 查询条件
85
- */
86
- where?: FindManyOptions<TEntity>['where'];
87
-
88
- /**
89
- * 关联关系
90
- */
91
- relations?: FindManyOptions<TEntity>['relations'];
92
-
93
- /**
94
- * 选择字段
95
- */
96
- select?: FindManyOptions<TEntity>['select'];
97
- };
98
-
99
- /**
100
- * TypeORM 分页查询选项
101
- *
102
- * 配置 TypeORM 分页查询的选项
103
- *
104
- * @typeParam TEntity - 实体类型
105
- * @typeParam TDto - DTO 类型
106
- */
107
- export type TypeOrmPaginateOptions<TEntity extends ObjectLiteral, TDto = TEntity> =
108
- | TypeOrmPaginateOptionsWithMapper<TEntity, TDto>
109
- | TypeOrmPaginateOptionsNoMapper<TEntity>;
110
-
111
- /**
112
- * 类型守卫:检查选项是否包含映射器
113
- *
114
- * @typeParam TEntity - 实体类型
115
- * @typeParam TDto - DTO 类型
116
- * @param options - 查询选项
117
- * @returns 是否包含映射器
118
- */
119
- function hasMapper<TEntity extends ObjectLiteral, TDto>(
120
- options:
121
- | TypeOrmPaginateOptionsWithMapper<TEntity, TDto>
122
- | TypeOrmPaginateOptionsNoMapper<TEntity>
123
- | undefined,
124
- ): options is TypeOrmPaginateOptionsWithMapper<TEntity, TDto> {
125
- return options !== undefined && 'mapper' in options;
126
- }
127
-
128
- /**
129
- * 构建分页结果
130
- *
131
- * 内部辅助函数,构建分页结果对象
132
- *
133
- * @typeParam TDto - DTO 类型
134
- * @param items - 数据项数组
135
- * @param total - 总数
136
- * @param page - 当前页
137
- * @param pageSize - 每页大小
138
- * @returns 分页结果
139
- */
140
- function buildPaginatedResult<TDto>(
141
- items: TDto[],
142
- total: number,
143
- page: number,
144
- pageSize: number,
145
- ): PaginatedResult<TDto> {
146
- /**
147
- * 计算得到的总页数
148
- */
149
- const totalPages = calculateTotalPages(total, pageSize);
150
- return {
151
- items,
152
- total,
153
- page,
154
- pageSize,
155
- totalPages,
156
- hasNextPage: hasNextPage(page, totalPages),
157
- hasPreviousPage: hasPreviousPage(page),
158
- };
159
- }
160
-
161
- /**
162
- * 使用 Repository 进行分页查询
163
- *
164
- * 通过 TypeORM Repository 进行分页查询,支持实体到 DTO 的映射
165
- *
166
- * 当提供 mapper 时返回映射后的 DTO,否则返回原始实体
167
- *
168
- * @typeParam TEntity - 实体类型
169
- * @typeParam TDto - DTO 类型
170
- * @param repository - TypeORM 仓库
171
- * @param params - 分页参数
172
- * @param options - 查询选项
173
- * @returns 分页结果
174
- */
175
- export async function paginateWithRepository<TEntity extends ObjectLiteral, TDto = TEntity>(
176
- repository: Repository<TEntity>,
177
- params: PaginationParams,
178
- options?: TypeOrmPaginateOptions<TEntity, TDto>,
179
- ): Promise<PaginatedResult<TEntity | TDto>> {
180
- /**
181
- * 规范化后的分页参数
182
- */
183
- const { page, pageSize } = normalizePagination(params);
184
-
185
- /**
186
- * 计算得到的偏移量
187
- */
188
- const offset = calculateOffset(page, pageSize);
189
-
190
- /**
191
- * 默认排序规则
192
- */
193
- const defaultOrder: FindOptionsOrder<TEntity> = { createdAt: 'DESC' };
194
-
195
- /**
196
- * TypeORM 查询选项配置
197
- */
198
- const findOptions: FindManyOptions<TEntity> = {
199
- skip: offset,
200
- take: pageSize,
201
- where: options?.where,
202
- relations: options?.relations,
203
- select: options?.select,
204
- order: options?.order ?? defaultOrder,
205
- };
206
-
207
- /**
208
- * 查询结果:实体数组和总数
209
- */
210
- const [entities, total] = await repository.findAndCount(findOptions);
211
-
212
- if (hasMapper(options)) {
213
- /**
214
- * 映射后的 DTO 数组
215
- */
216
- const items = await Promise.all(entities.map(options.mapper));
217
- return buildPaginatedResult(items, total, page, pageSize);
218
- }
219
-
220
- return buildPaginatedResult(entities, total, page, pageSize);
221
- }
222
-
223
- /**
224
- * 使用 QueryBuilder 进行分页查询
225
- *
226
- * 通过 TypeORM QueryBuilder 进行分页查询,支持实体到 DTO 的映射
227
- *
228
- * 当提供 mapper 时返回映射后的 DTO,否则返回原始实体
229
- *
230
- * @typeParam TEntity - 实体类型
231
- * @typeParam TDto - DTO 类型
232
- * @param queryBuilder - TypeORM QueryBuilder
233
- * @param params - 分页参数
234
- * @param mapper - 实体到 DTO 的映射函数(可选)
235
- * @returns 分页结果
236
- */
237
- export async function paginateWithQueryBuilder<TEntity extends ObjectLiteral, TDto = TEntity>(
238
- queryBuilder: SelectQueryBuilder<TEntity>,
239
- params: PaginationParams,
240
- mapper?: EntityMapper<TEntity, TDto>,
241
- ): Promise<PaginatedResult<TEntity | TDto>> {
242
- /**
243
- * 规范化后的分页参数
244
- */
245
- const { page, pageSize } = normalizePagination(params);
246
-
247
- /**
248
- * 计算得到的偏移量
249
- */
250
- const offset = calculateOffset(page, pageSize);
251
-
252
- queryBuilder.skip(offset).take(pageSize);
253
-
254
- /**
255
- * 查询结果:实体数组和总数
256
- */
257
- const [entities, total] = await queryBuilder.getManyAndCount();
258
-
259
- if (mapper) {
260
- /**
261
- * 映射后的 DTO 数组
262
- */
263
- const items = await Promise.all(entities.map(mapper));
264
- return buildPaginatedResult(items, total, page, pageSize);
265
- }
266
-
267
- return buildPaginatedResult(entities, total, page, pageSize);
268
- }
269
-
270
- /**
271
- * 创建分页查询构建器
272
- *
273
- * 创建一个用于分页查询的 TypeORM QueryBuilder
274
- *
275
- * @typeParam TEntity - 实体类型
276
- * @param repository - TypeORM 仓库
277
- * @param alias - 查询别名
278
- * @returns QueryBuilder
279
- */
280
- export function createPaginationQueryBuilder<TEntity extends ObjectLiteral>(
281
- repository: Repository<TEntity>,
282
- alias?: string,
283
- ): SelectQueryBuilder<TEntity> {
284
- return repository.createQueryBuilder(alias ?? repository.metadata.name.toLowerCase());
285
- }
package/src/index.ts DELETED
@@ -1,56 +0,0 @@
1
- /**
2
- * 分页包主入口
3
- */
4
-
5
- // 类型导出
6
- export type {
7
- PaginationParams,
8
- OffsetPaginationParams,
9
- CursorPaginationParams,
10
- PaginationDefaults,
11
- } from './types';
12
-
13
- export type { PaginatedResult, OffsetPaginatedResult, CursorPaginatedResult } from './types';
14
-
15
- export type { SortOrder, SortParams, SortItem, MultiSortParams } from './types';
16
-
17
- // 常量导出
18
- export { PAGINATION_DEFAULTS } from './types';
19
-
20
- // 工具函数导出
21
- export type {
22
- OffsetPaginationResult,
23
- PagePaginationResult,
24
- PaginationValidationResult,
25
- OffsetPaginationValidationResult,
26
- } from './utils';
27
-
28
- export {
29
- normalizePagination,
30
- normalizeOffsetPagination,
31
- calculateOffset,
32
- calculateTotalPages,
33
- hasNextPage,
34
- hasPreviousPage,
35
- offsetToPage,
36
- pageToOffset,
37
- createPaginatedResult,
38
- } from './utils';
39
-
40
- export {
41
- isValidPage,
42
- isValidPageSize,
43
- isValidOffset,
44
- isValidLimit,
45
- isValidPagination,
46
- isValidOffsetPagination,
47
- } from './utils';
48
-
49
- // 适配器导出
50
- export type { EntityMapper, TypeOrmPaginateOptions } from './adapters';
51
-
52
- export {
53
- paginateWithRepository,
54
- paginateWithQueryBuilder,
55
- createPaginationQueryBuilder,
56
- } from './adapters';
@@ -1,16 +0,0 @@
1
- /**
2
- * 类型导出入口
3
- */
4
-
5
- export type {
6
- PaginationParams,
7
- OffsetPaginationParams,
8
- CursorPaginationParams,
9
- PaginationDefaults,
10
- } from './pagination.types';
11
-
12
- export { PAGINATION_DEFAULTS } from './pagination.types';
13
-
14
- export type { PaginatedResult, OffsetPaginatedResult, CursorPaginatedResult } from './result.types';
15
-
16
- export type { SortOrder, SortParams, SortItem, MultiSortParams } from './sort.types';
@@ -1,69 +0,0 @@
1
- /**
2
- * 分页参数类型定义
3
- */
4
-
5
- /**
6
- * 分页参数(page 风格,推荐使用)
7
- *
8
- * 适用于用户界面分页场景
9
- */
10
- export type PaginationParams = {
11
- /**
12
- * 页码(从 1 开始)
13
- */
14
- page?: number;
15
-
16
- /**
17
- * 每页数量
18
- */
19
- pageSize?: number;
20
- };
21
-
22
- /**
23
- * 分页参数(offset 风格)
24
- *
25
- * 适用于 API 调用、数据库查询等场景
26
- */
27
- export type OffsetPaginationParams = {
28
- /**
29
- * 限制数量
30
- */
31
- limit?: number;
32
-
33
- /**
34
- * 偏移量
35
- */
36
- offset?: number;
37
- };
38
-
39
- /**
40
- * 游标分页参数
41
- *
42
- * 适用于无限滚动、实时数据流场景
43
- */
44
- export type CursorPaginationParams = {
45
- /**
46
- * 游标
47
- */
48
- cursor?: string;
49
-
50
- /**
51
- * 限制数量
52
- */
53
- limit?: number;
54
- };
55
-
56
- /**
57
- * 默认分页配置
58
- */
59
- export const PAGINATION_DEFAULTS = {
60
- page: 1,
61
- pageSize: 20,
62
- maxPageSize: 100,
63
- minPageSize: 1,
64
- } as const;
65
-
66
- /**
67
- * 默认分页参数类型
68
- */
69
- export type PaginationDefaults = typeof PAGINATION_DEFAULTS;
@@ -1,110 +0,0 @@
1
- /**
2
- * 分页结果类型定义
3
- */
4
-
5
- /**
6
- * 分页结果
7
- *
8
- * 包含分页数据和分页信息的完整结果
9
- *
10
- * @typeParam T - 数据项类型
11
- */
12
- export type PaginatedResult<T> = {
13
- /**
14
- * 数据项数组
15
- */
16
- items: T[];
17
-
18
- /**
19
- * 总记录数
20
- */
21
- total: number;
22
-
23
- /**
24
- * 当前页码
25
- */
26
- page: number;
27
-
28
- /**
29
- * 每页数量
30
- */
31
- pageSize: number;
32
-
33
- /**
34
- * 总页数
35
- */
36
- totalPages: number;
37
-
38
- /**
39
- * 是否有下一页
40
- */
41
- hasNextPage: boolean;
42
-
43
- /**
44
- * 是否有上一页
45
- */
46
- hasPreviousPage: boolean;
47
- };
48
-
49
- /**
50
- * Offset 风格分页结果
51
- *
52
- * 包含偏移量分页数据和分页信息
53
- *
54
- * @typeParam T - 数据项类型
55
- */
56
- export type OffsetPaginatedResult<T> = {
57
- /**
58
- * 数据项数组
59
- */
60
- items: T[];
61
-
62
- /**
63
- * 总记录数
64
- */
65
- total: number;
66
-
67
- /**
68
- * 限制数量
69
- */
70
- limit: number;
71
-
72
- /**
73
- * 偏移量
74
- */
75
- offset: number;
76
-
77
- /**
78
- * 是否有更多数据
79
- */
80
- hasMore: boolean;
81
- };
82
-
83
- /**
84
- * 游标分页结果
85
- *
86
- * 包含游标分页数据和分页信息
87
- *
88
- * @typeParam T - 数据项类型
89
- */
90
- export type CursorPaginatedResult<T> = {
91
- /**
92
- * 数据项数组
93
- */
94
- items: T[];
95
-
96
- /**
97
- * 下一页游标
98
- */
99
- nextCursor: string | null;
100
-
101
- /**
102
- * 上一页游标
103
- */
104
- previousCursor: string | null;
105
-
106
- /**
107
- * 是否有更多数据
108
- */
109
- hasMore: boolean;
110
- };
@@ -1,60 +0,0 @@
1
- /**
2
- * 排序类型定义
3
- */
4
-
5
- /**
6
- * 排序方向
7
- */
8
- export type SortOrder = 'asc' | 'desc';
9
-
10
- /**
11
- * 排序参数
12
- *
13
- * 单字段排序参数
14
- *
15
- * @typeParam T - 排序字段类型
16
- */
17
- export type SortParams<T extends string = string> = {
18
- /**
19
- * 排序字段
20
- */
21
- sortBy?: T;
22
-
23
- /**
24
- * 排序方向
25
- */
26
- sortOrder?: SortOrder;
27
- };
28
-
29
- /**
30
- * 多字段排序项
31
- *
32
- * 单个排序项定义
33
- *
34
- * @typeParam T - 排序字段类型
35
- */
36
- export type SortItem<T extends string = string> = {
37
- /**
38
- * 排序字段
39
- */
40
- field: T;
41
-
42
- /**
43
- * 排序方向
44
- */
45
- order: SortOrder;
46
- };
47
-
48
- /**
49
- * 多字段排序参数
50
- *
51
- * 支持多个字段的排序
52
- *
53
- * @typeParam T - 排序字段类型
54
- */
55
- export type MultiSortParams<T extends string = string> = {
56
- /**
57
- * 排序项数组
58
- */
59
- sort?: SortItem<T>[];
60
- };
@@ -1,37 +0,0 @@
1
- /**
2
- * @file typeorm.d.ts
3
- * TypeORM 类型声明
4
- * 当 typeorm 未安装时提供类型支持
5
- * @author Longzai Intelligence
6
- */
7
-
8
- declare module 'typeorm' {
9
- export interface ObjectLiteral {
10
- [key: string]: unknown;
11
- }
12
-
13
- export interface Repository<TEntity extends ObjectLiteral> {
14
- findAndCount(options?: FindManyOptions<TEntity>): Promise<[TEntity[], number]>;
15
- createQueryBuilder(alias?: string): SelectQueryBuilder<TEntity>;
16
- metadata: { name: string };
17
- }
18
-
19
- export interface SelectQueryBuilder<TEntity extends ObjectLiteral> {
20
- skip(offset: number): this;
21
- take(limit: number): this;
22
- getManyAndCount(): Promise<[TEntity[], number]>;
23
- }
24
-
25
- export interface FindManyOptions<TEntity = unknown> {
26
- skip?: number;
27
- take?: number;
28
- where?: unknown;
29
- relations?: unknown;
30
- select?: unknown;
31
- order?: FindOptionsOrder<TEntity>;
32
- }
33
-
34
- export type FindOptionsOrder<TEntity> = {
35
- [P in keyof TEntity]?: 'ASC' | 'DESC' | 1 | -1;
36
- };
37
- }
@@ -1,31 +0,0 @@
1
- /**
2
- * 工具函数导出入口
3
- */
4
-
5
- export type { OffsetPaginationResult, PagePaginationResult } from './pagination.util';
6
-
7
- export type {
8
- PaginationValidationResult,
9
- OffsetPaginationValidationResult,
10
- } from './validation.util';
11
-
12
- export {
13
- normalizePagination,
14
- normalizeOffsetPagination,
15
- calculateOffset,
16
- calculateTotalPages,
17
- hasNextPage,
18
- hasPreviousPage,
19
- offsetToPage,
20
- pageToOffset,
21
- createPaginatedResult,
22
- } from './pagination.util';
23
-
24
- export {
25
- isValidPage,
26
- isValidPageSize,
27
- isValidOffset,
28
- isValidLimit,
29
- isValidPagination,
30
- isValidOffsetPagination,
31
- } from './validation.util';