core-services-sdk 1.3.41 → 1.3.42
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 +1 -1
- package/src/mongodb/paginate.js +12 -10
- package/types/mongodb/paginate.d.ts +39 -1
package/package.json
CHANGED
package/src/mongodb/paginate.js
CHANGED
|
@@ -97,8 +97,6 @@ export async function getPaginationEdges({
|
|
|
97
97
|
}
|
|
98
98
|
}
|
|
99
99
|
|
|
100
|
-
import { ObjectId } from 'mongodb'
|
|
101
|
-
|
|
102
100
|
/**
|
|
103
101
|
* Classic page/limit pagination with total count
|
|
104
102
|
*
|
|
@@ -114,19 +112,23 @@ import { ObjectId } from 'mongodb'
|
|
|
114
112
|
export async function paginate(
|
|
115
113
|
collection,
|
|
116
114
|
{
|
|
115
|
+
sortBy = '_id',
|
|
116
|
+
page = 1,
|
|
117
|
+
limit = 10,
|
|
117
118
|
filter = {},
|
|
118
119
|
projection,
|
|
119
120
|
order = 'desc',
|
|
120
|
-
cursorField = '_id',
|
|
121
|
-
limit = 10,
|
|
122
|
-
page = 1,
|
|
123
121
|
} = {},
|
|
124
122
|
) {
|
|
125
123
|
// Validation
|
|
126
|
-
if (page < 1)
|
|
127
|
-
|
|
124
|
+
if (page < 1) {
|
|
125
|
+
page = 1
|
|
126
|
+
}
|
|
127
|
+
if (limit < 1) {
|
|
128
|
+
limit = 10
|
|
129
|
+
}
|
|
128
130
|
|
|
129
|
-
const sort = { [
|
|
131
|
+
const sort = { [sortBy]: order === 'asc' ? 1 : -1 }
|
|
130
132
|
const skip = (page - 1) * limit
|
|
131
133
|
|
|
132
134
|
const [results, totalCount] = await Promise.all([
|
|
@@ -145,11 +147,11 @@ export async function paginate(
|
|
|
145
147
|
|
|
146
148
|
return {
|
|
147
149
|
order,
|
|
150
|
+
hasNext,
|
|
148
151
|
totalCount,
|
|
149
152
|
totalPages,
|
|
150
|
-
currentPage: page,
|
|
151
|
-
hasNext,
|
|
152
153
|
hasPrevious,
|
|
153
154
|
list: results,
|
|
155
|
+
currentPage: page,
|
|
154
156
|
}
|
|
155
157
|
}
|
|
@@ -9,7 +9,7 @@
|
|
|
9
9
|
* @param {'asc'|'desc'} [options.order='asc']
|
|
10
10
|
* @param {number} [options.limit=10]
|
|
11
11
|
*/
|
|
12
|
-
export function
|
|
12
|
+
export function paginateCursor(
|
|
13
13
|
collection: import('mongodb').Collection,
|
|
14
14
|
{
|
|
15
15
|
limit,
|
|
@@ -48,4 +48,42 @@ export function getPaginationEdges({
|
|
|
48
48
|
hasNext: boolean
|
|
49
49
|
hasPrevious: boolean
|
|
50
50
|
}>
|
|
51
|
+
/**
|
|
52
|
+
* Classic page/limit pagination with total count
|
|
53
|
+
*
|
|
54
|
+
* @param {import('mongodb').Collection} collection
|
|
55
|
+
* @param {Object} options
|
|
56
|
+
* @param {Object} [options.filter={}] - MongoDB filter
|
|
57
|
+
* @param {string} [options.cursorField='_id'] - Field to sort by
|
|
58
|
+
* @param {'asc'|'desc'} [options.order='desc'] - Sort order
|
|
59
|
+
* @param {number} [options.limit=10] - Items per page
|
|
60
|
+
* @param {number} [options.page=1] - Page number (1-based)
|
|
61
|
+
* @param {Object} [options.projection] - Projection fields
|
|
62
|
+
*/
|
|
63
|
+
export function paginate(
|
|
64
|
+
collection: import('mongodb').Collection,
|
|
65
|
+
{
|
|
66
|
+
sortBy,
|
|
67
|
+
page,
|
|
68
|
+
limit,
|
|
69
|
+
filter,
|
|
70
|
+
projection,
|
|
71
|
+
order,
|
|
72
|
+
}?: {
|
|
73
|
+
filter?: any
|
|
74
|
+
cursorField?: string
|
|
75
|
+
order?: 'asc' | 'desc'
|
|
76
|
+
limit?: number
|
|
77
|
+
page?: number
|
|
78
|
+
projection?: any
|
|
79
|
+
},
|
|
80
|
+
): Promise<{
|
|
81
|
+
order: 'asc' | 'desc'
|
|
82
|
+
hasNext: boolean
|
|
83
|
+
totalCount: number
|
|
84
|
+
totalPages: number
|
|
85
|
+
hasPrevious: boolean
|
|
86
|
+
list: import('mongodb').WithId<import('bson').Document>[]
|
|
87
|
+
currentPage: number
|
|
88
|
+
}>
|
|
51
89
|
import { ObjectId } from 'mongodb'
|