@longzai-intelligence/pagination 0.0.1 → 0.0.2

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 (70) hide show
  1. package/.turbo/turbo-build.log +3 -3
  2. package/.turbo/turbo-test$colon$coverage.log +6 -6
  3. package/.turbo/turbo-test.log +7 -7
  4. package/CHANGELOG.md +7 -0
  5. package/README.md +5 -1
  6. package/coverage/coverage-final.json +6 -2
  7. package/coverage/index.html +19 -4
  8. package/coverage/src/adapters/index.html +1 -16
  9. package/coverage/src/adapters/typeorm.adapter.ts.html +1 -1
  10. package/coverage/src/index.html +16 -1
  11. package/coverage/src/index.ts.html +38 -11
  12. package/coverage/src/schemas/index.html +161 -0
  13. package/coverage/src/{adapters → schemas}/index.ts.html +61 -10
  14. package/coverage/src/schemas/pagination.schemas.ts.html +343 -0
  15. package/coverage/src/schemas/result.schemas.ts.html +724 -0
  16. package/coverage/src/schemas/sort.schemas.ts.html +544 -0
  17. package/coverage/src/typeorm.ts.html +154 -0
  18. package/coverage/src/types/index.html +1 -1
  19. package/coverage/src/types/index.ts.html +69 -3
  20. package/coverage/src/types/pagination.types.ts.html +15 -138
  21. package/coverage/src/types/result.types.ts.html +11 -299
  22. package/coverage/src/types/sort.types.ts.html +16 -139
  23. package/coverage/src/types/typeorm.d.ts.html +1 -1
  24. package/coverage/src/utils/index.html +1 -1
  25. package/coverage/src/utils/index.ts.html +1 -1
  26. package/coverage/src/utils/pagination.util.ts.html +1 -1
  27. package/coverage/src/utils/validation.util.ts.html +1 -1
  28. package/dist/index-C8FaNzej.d.mts +145 -0
  29. package/dist/index-CMiyhlfm.d.cts +145 -0
  30. package/dist/index.cjs +33 -1
  31. package/dist/index.d.cts +2 -81
  32. package/dist/index.d.mts +2 -81
  33. package/dist/index.mjs +1 -1
  34. package/dist/typeorm.cjs +129 -0
  35. package/dist/typeorm.d.cts +26 -0
  36. package/dist/typeorm.d.mts +26 -0
  37. package/dist/typeorm.mjs +1 -0
  38. package/dist/utils-CV6Ovku6.mjs +1 -0
  39. package/dist/utils-D_qD2YAX.cjs +1 -0
  40. package/docs/specs/spec-20260413-typeorm-subpath-export/checklist.md +33 -0
  41. package/docs/specs/spec-20260413-typeorm-subpath-export/spec.md +104 -0
  42. package/docs/specs/spec-20260413-typeorm-subpath-export/tasks.md +48 -0
  43. package/package.json +15 -3
  44. package/src/__tests__/index.test.ts +9 -3
  45. package/src/__tests__/pagination.util.test.ts +0 -2
  46. package/src/__tests__/result.schemas.test.ts +142 -0
  47. package/src/__tests__/sort.schemas.test.ts +157 -0
  48. package/src/__tests__/typeorm.adapter.test.ts +1 -3
  49. package/src/__tests__/validation.util.test.ts +0 -2
  50. package/src/adapters/{typeorm.adapter.ts → typeorm/adapter.ts} +10 -92
  51. package/src/adapters/typeorm/index.ts +23 -0
  52. package/src/adapters/typeorm/types.ts +95 -0
  53. package/src/index.ts +18 -9
  54. package/src/schemas/index.ts +28 -0
  55. package/src/schemas/pagination.schemas.ts +86 -0
  56. package/src/schemas/result.schemas.ts +213 -0
  57. package/src/schemas/sort.schemas.ts +153 -0
  58. package/src/typeorm.ts +23 -0
  59. package/src/types/index.ts +23 -1
  60. package/src/types/pagination.types.ts +13 -54
  61. package/src/types/result.types.ts +9 -105
  62. package/src/types/sort.types.ts +14 -55
  63. package/tsconfig/.cache/app.tsbuildinfo +1 -1
  64. package/tsconfig/.cache/node.tsbuildinfo +1 -1
  65. package/tsconfig/.cache/test.tsbuildinfo +1 -1
  66. package/tsconfig/app.json +4 -1
  67. package/tsdown.config.ts +10 -3
  68. package/src/adapters/index.ts +0 -11
  69. package/tsconfig/.cache/build.tsbuildinfo +0 -1
  70. /package/src/{types → adapters/typeorm}/typeorm.d.ts +0 -0
@@ -1,7 +1,7 @@
1
1
  /**
2
- * TypeORM 分页适配器
2
+ * TypeORM 分页适配器实现
3
3
  *
4
- * 注意:此模块需要安装 typeorm 作为 peerDependency
4
+ * 提供 TypeORM 分页查询的核心实现功能
5
5
  */
6
6
 
7
7
  import type {
@@ -11,102 +11,20 @@ import type {
11
11
  FindManyOptions,
12
12
  FindOptionsOrder,
13
13
  } from 'typeorm';
14
- import type { PaginationParams, PaginatedResult } from '../types';
14
+ import type { PaginationParams, PaginatedResult } from '@/types';
15
+ import type {
16
+ EntityMapper,
17
+ TypeOrmPaginateOptions,
18
+ TypeOrmPaginateOptionsWithMapper,
19
+ TypeOrmPaginateOptionsNoMapper,
20
+ } from './types';
15
21
  import {
16
22
  normalizePagination,
17
23
  calculateOffset,
18
24
  calculateTotalPages,
19
25
  hasNextPage,
20
26
  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>;
27
+ } from '@/utils';
110
28
 
111
29
  /**
112
30
  * 类型守卫:检查选项是否包含映射器
@@ -0,0 +1,23 @@
1
+ /**
2
+ * TypeORM 适配器入口
3
+ *
4
+ * 提供 TypeORM 分页查询功能,需要安装 typeorm 作为 peerDependency。
5
+ *
6
+ * @example
7
+ * ```typescript
8
+ * import { paginateWithRepository } from '@longzai-intelligence/pagination/typeorm';
9
+ *
10
+ * const result = await paginateWithRepository(userRepository, {
11
+ * page: 1,
12
+ * pageSize: 20,
13
+ * });
14
+ * ```
15
+ */
16
+
17
+ export type { EntityMapper, TypeOrmPaginateOptions } from './types';
18
+
19
+ export {
20
+ paginateWithRepository,
21
+ paginateWithQueryBuilder,
22
+ createPaginationQueryBuilder,
23
+ } from './adapter';
@@ -0,0 +1,95 @@
1
+ /**
2
+ * TypeORM 适配器类型定义
3
+ *
4
+ * 提供 TypeORM 分页查询相关的类型定义
5
+ */
6
+
7
+ import type { ObjectLiteral, FindManyOptions, FindOptionsOrder } from 'typeorm';
8
+
9
+ /**
10
+ * 实体映射器函数类型
11
+ *
12
+ * 将实体类型转换为 DTO 类型的映射函数
13
+ *
14
+ * @typeParam TEntity - 实体类型
15
+ * @typeParam TDto - DTO 类型
16
+ */
17
+ export type EntityMapper<TEntity, TDto> = (entity: TEntity) => TDto | Promise<TDto>;
18
+
19
+ /**
20
+ * TypeORM 分页查询选项(带映射器)
21
+ *
22
+ * 配置 TypeORM 分页查询的选项,必须提供映射函数
23
+ *
24
+ * @typeParam TEntity - 实体类型
25
+ * @typeParam TDto - DTO 类型
26
+ */
27
+ export type TypeOrmPaginateOptionsWithMapper<TEntity extends ObjectLiteral, TDto> = {
28
+ /**
29
+ * 实体到 DTO 的映射函数
30
+ *
31
+ * 必须提供映射函数以确保类型安全
32
+ */
33
+ mapper: EntityMapper<TEntity, TDto>;
34
+
35
+ /**
36
+ * 排序规则
37
+ */
38
+ order?: FindOptionsOrder<TEntity>;
39
+
40
+ /**
41
+ * 查询条件
42
+ */
43
+ where?: FindManyOptions<TEntity>['where'];
44
+
45
+ /**
46
+ * 关联关系
47
+ */
48
+ relations?: FindManyOptions<TEntity>['relations'];
49
+
50
+ /**
51
+ * 选择字段
52
+ */
53
+ select?: FindManyOptions<TEntity>['select'];
54
+ };
55
+
56
+ /**
57
+ * TypeORM 分页查询选项(无映射器)
58
+ *
59
+ * 配置 TypeORM 分页查询的选项,返回原始实体
60
+ *
61
+ * @typeParam TEntity - 实体类型
62
+ */
63
+ export type TypeOrmPaginateOptionsNoMapper<TEntity extends ObjectLiteral> = {
64
+ /**
65
+ * 排序规则
66
+ */
67
+ order?: FindOptionsOrder<TEntity>;
68
+
69
+ /**
70
+ * 查询条件
71
+ */
72
+ where?: FindManyOptions<TEntity>['where'];
73
+
74
+ /**
75
+ * 关联关系
76
+ */
77
+ relations?: FindManyOptions<TEntity>['relations'];
78
+
79
+ /**
80
+ * 选择字段
81
+ */
82
+ select?: FindManyOptions<TEntity>['select'];
83
+ };
84
+
85
+ /**
86
+ * TypeORM 分页查询选项
87
+ *
88
+ * 配置 TypeORM 分页查询的选项
89
+ *
90
+ * @typeParam TEntity - 实体类型
91
+ * @typeParam TDto - DTO 类型
92
+ */
93
+ export type TypeOrmPaginateOptions<TEntity extends ObjectLiteral, TDto = TEntity> =
94
+ | TypeOrmPaginateOptionsWithMapper<TEntity, TDto>
95
+ | TypeOrmPaginateOptionsNoMapper<TEntity>;
package/src/index.ts CHANGED
@@ -17,6 +17,24 @@ export type { SortOrder, SortParams, SortItem, MultiSortParams } from './types';
17
17
  // 常量导出
18
18
  export { PAGINATION_DEFAULTS } from './types';
19
19
 
20
+ // Zod Schemas 导出
21
+ export {
22
+ PaginationDefaultsSchema,
23
+ PaginationParamsSchema,
24
+ OffsetPaginationParamsSchema,
25
+ CursorPaginationParamsSchema,
26
+ SortOrderSchema,
27
+ SortParamsSchema,
28
+ SortItemSchema,
29
+ MultiSortParamsSchema,
30
+ createSortParamsSchema,
31
+ createSortItemSchema,
32
+ createMultiSortParamsSchema,
33
+ createPaginatedResultSchema,
34
+ createOffsetPaginatedResultSchema,
35
+ createCursorPaginatedResultSchema,
36
+ } from './types';
37
+
20
38
  // 工具函数导出
21
39
  export type {
22
40
  OffsetPaginationResult,
@@ -45,12 +63,3 @@ export {
45
63
  isValidPagination,
46
64
  isValidOffsetPagination,
47
65
  } from './utils';
48
-
49
- // 适配器导出
50
- export type { EntityMapper, TypeOrmPaginateOptions } from './adapters';
51
-
52
- export {
53
- paginateWithRepository,
54
- paginateWithQueryBuilder,
55
- createPaginationQueryBuilder,
56
- } from './adapters';
@@ -0,0 +1,28 @@
1
+ /**
2
+ * Zod Schemas 导出
3
+ *
4
+ * 类型定义请从 '../types' 导入
5
+ */
6
+
7
+ export {
8
+ PaginationDefaultsSchema,
9
+ PaginationParamsSchema,
10
+ OffsetPaginationParamsSchema,
11
+ CursorPaginationParamsSchema,
12
+ } from './pagination.schemas';
13
+
14
+ export {
15
+ SortOrderSchema,
16
+ SortParamsSchema,
17
+ SortItemSchema,
18
+ MultiSortParamsSchema,
19
+ createSortParamsSchema,
20
+ createSortItemSchema,
21
+ createMultiSortParamsSchema,
22
+ } from './sort.schemas';
23
+
24
+ export {
25
+ createPaginatedResultSchema,
26
+ createOffsetPaginatedResultSchema,
27
+ createCursorPaginatedResultSchema,
28
+ } from './result.schemas';
@@ -0,0 +1,86 @@
1
+ /**
2
+ * 分页参数 Zod Schema 定义
3
+ */
4
+
5
+ import { z } from 'zod';
6
+
7
+ /**
8
+ * 默认分页配置 Schema
9
+ */
10
+ export const PaginationDefaultsSchema = z.object({
11
+ page: z.literal(1),
12
+ pageSize: z.literal(20),
13
+ maxPageSize: z.literal(100),
14
+ minPageSize: z.literal(1),
15
+ });
16
+
17
+ /**
18
+ * 默认分页参数类型
19
+ */
20
+ export type PaginationDefaults = z.infer<typeof PaginationDefaultsSchema>;
21
+
22
+ /**
23
+ * 分页参数 Schema(page 风格,推荐使用)
24
+ *
25
+ * 适用于用户界面分页场景
26
+ */
27
+ export const PaginationParamsSchema = z.object({
28
+ /**
29
+ * 页码(从 1 开始)
30
+ */
31
+ page: z.number().int().positive().optional(),
32
+
33
+ /**
34
+ * 每页数量
35
+ */
36
+ pageSize: z.number().int().positive().optional(),
37
+ });
38
+
39
+ /**
40
+ * 分页参数类型(page 风格)
41
+ */
42
+ export type PaginationParams = z.infer<typeof PaginationParamsSchema>;
43
+
44
+ /**
45
+ * Offset 分页参数 Schema
46
+ *
47
+ * 适用于 API 调用、数据库查询等场景
48
+ */
49
+ export const OffsetPaginationParamsSchema = z.object({
50
+ /**
51
+ * 限制数量(正整数)
52
+ */
53
+ limit: z.number().int().positive().optional(),
54
+
55
+ /**
56
+ * 偏移量(非负整数)
57
+ */
58
+ offset: z.number().int().nonnegative().optional(),
59
+ });
60
+
61
+ /**
62
+ * Offset 分页参数类型
63
+ */
64
+ export type OffsetPaginationParams = z.infer<typeof OffsetPaginationParamsSchema>;
65
+
66
+ /**
67
+ * 游标分页参数 Schema
68
+ *
69
+ * 适用于无限滚动、实时数据流场景
70
+ */
71
+ export const CursorPaginationParamsSchema = z.object({
72
+ /**
73
+ * 游标
74
+ */
75
+ cursor: z.string().optional(),
76
+
77
+ /**
78
+ * 限制数量
79
+ */
80
+ limit: z.number().int().positive().optional(),
81
+ });
82
+
83
+ /**
84
+ * 游标分页参数类型
85
+ */
86
+ export type CursorPaginationParams = z.infer<typeof CursorPaginationParamsSchema>;
@@ -0,0 +1,213 @@
1
+ /**
2
+ * 分页结果 Zod Schema 定义
3
+ */
4
+
5
+ import { z } from 'zod';
6
+
7
+ /**
8
+ * 创建分页结果 Schema
9
+ *
10
+ * 包含分页数据和分页信息的完整结果
11
+ *
12
+ * @typeParam T - 数据项 Schema 类型
13
+ */
14
+ export function createPaginatedResultSchema<T extends z.ZodTypeAny>(itemSchema: T) {
15
+ return z.object({
16
+ /**
17
+ * 数据项数组
18
+ */
19
+ items: z.array(itemSchema),
20
+
21
+ /**
22
+ * 总记录数
23
+ */
24
+ total: z.number().int().nonnegative(),
25
+
26
+ /**
27
+ * 当前页码
28
+ */
29
+ page: z.number().int().positive(),
30
+
31
+ /**
32
+ * 每页数量
33
+ */
34
+ pageSize: z.number().int().positive(),
35
+
36
+ /**
37
+ * 总页数
38
+ */
39
+ totalPages: z.number().int().nonnegative(),
40
+
41
+ /**
42
+ * 是否有下一页
43
+ */
44
+ hasNextPage: z.boolean(),
45
+
46
+ /**
47
+ * 是否有上一页
48
+ */
49
+ hasPreviousPage: z.boolean(),
50
+ });
51
+ }
52
+
53
+ /**
54
+ * 分页结果类型(泛型版本)
55
+ */
56
+ export type PaginatedResult<T> = {
57
+ /**
58
+ * 数据项数组
59
+ */
60
+ items: T[];
61
+
62
+ /**
63
+ * 总记录数
64
+ */
65
+ total: number;
66
+
67
+ /**
68
+ * 当前页码
69
+ */
70
+ page: number;
71
+
72
+ /**
73
+ * 每页数量
74
+ */
75
+ pageSize: number;
76
+
77
+ /**
78
+ * 总页数
79
+ */
80
+ totalPages: number;
81
+
82
+ /**
83
+ * 是否有下一页
84
+ */
85
+ hasNextPage: boolean;
86
+
87
+ /**
88
+ * 是否有上一页
89
+ */
90
+ hasPreviousPage: boolean;
91
+ };
92
+
93
+ /**
94
+ * 创建 Offset 风格分页结果 Schema
95
+ *
96
+ * 包含偏移量分页数据和分页信息
97
+ *
98
+ * @typeParam T - 数据项 Schema 类型
99
+ */
100
+ export function createOffsetPaginatedResultSchema<T extends z.ZodTypeAny>(itemSchema: T) {
101
+ return z.object({
102
+ /**
103
+ * 数据项数组
104
+ */
105
+ items: z.array(itemSchema),
106
+
107
+ /**
108
+ * 总记录数
109
+ */
110
+ total: z.number().int().nonnegative(),
111
+
112
+ /**
113
+ * 限制数量
114
+ */
115
+ limit: z.number().int().nonnegative(),
116
+
117
+ /**
118
+ * 偏移量
119
+ */
120
+ offset: z.number().int().nonnegative(),
121
+
122
+ /**
123
+ * 是否有更多数据
124
+ */
125
+ hasMore: z.boolean(),
126
+ });
127
+ }
128
+
129
+ /**
130
+ * Offset 风格分页结果类型(泛型版本)
131
+ */
132
+ export type OffsetPaginatedResult<T> = {
133
+ /**
134
+ * 数据项数组
135
+ */
136
+ items: T[];
137
+
138
+ /**
139
+ * 总记录数
140
+ */
141
+ total: number;
142
+
143
+ /**
144
+ * 限制数量
145
+ */
146
+ limit: number;
147
+
148
+ /**
149
+ * 偏移量
150
+ */
151
+ offset: number;
152
+
153
+ /**
154
+ * 是否有更多数据
155
+ */
156
+ hasMore: boolean;
157
+ };
158
+
159
+ /**
160
+ * 创建游标分页结果 Schema
161
+ *
162
+ * 包含游标分页数据和分页信息
163
+ *
164
+ * @typeParam T - 数据项 Schema 类型
165
+ */
166
+ export function createCursorPaginatedResultSchema<T extends z.ZodTypeAny>(itemSchema: T) {
167
+ return z.object({
168
+ /**
169
+ * 数据项数组
170
+ */
171
+ items: z.array(itemSchema),
172
+
173
+ /**
174
+ * 下一页游标
175
+ */
176
+ nextCursor: z.string().nullable(),
177
+
178
+ /**
179
+ * 上一页游标
180
+ */
181
+ previousCursor: z.string().nullable(),
182
+
183
+ /**
184
+ * 是否有更多数据
185
+ */
186
+ hasMore: z.boolean(),
187
+ });
188
+ }
189
+
190
+ /**
191
+ * 游标分页结果类型(泛型版本)
192
+ */
193
+ export type CursorPaginatedResult<T> = {
194
+ /**
195
+ * 数据项数组
196
+ */
197
+ items: T[];
198
+
199
+ /**
200
+ * 下一页游标
201
+ */
202
+ nextCursor: string | null;
203
+
204
+ /**
205
+ * 上一页游标
206
+ */
207
+ previousCursor: string | null;
208
+
209
+ /**
210
+ * 是否有更多数据
211
+ */
212
+ hasMore: boolean;
213
+ };