core-services-sdk 1.3.66 → 1.3.67
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
CHANGED
|
@@ -1,13 +1,71 @@
|
|
|
1
1
|
import { applyFilter } from '../filters/apply-filter.js'
|
|
2
|
-
import { applyFilterSnakeCase } from '../filters/apply-filter-snake-case.js'
|
|
3
2
|
import { applyOrderBy } from '../modifiers/apply-order-by.js'
|
|
4
3
|
import { applyPagination } from '../modifiers/apply-pagination.js'
|
|
4
|
+
import { applyFilterSnakeCase } from '../filters/apply-filter-snake-case.js'
|
|
5
5
|
import { normalizeNumberOrDefault } from '../../core/normalize-premitives-types-or-default.js'
|
|
6
6
|
|
|
7
7
|
/**
|
|
8
|
-
* Executes paginated query.
|
|
8
|
+
* Executes a paginated SQL query using a Knex query builder.
|
|
9
|
+
*
|
|
10
|
+
* This helper clones the provided base query twice:
|
|
11
|
+
* one query is used to fetch the paginated list of rows,
|
|
12
|
+
* and the second query is used to calculate the total count
|
|
13
|
+
* of records matching the same filters.
|
|
14
|
+
*
|
|
15
|
+
* Filters are applied consistently to both queries.
|
|
16
|
+
* Ordering and pagination are applied only to the list query.
|
|
17
|
+
*
|
|
18
|
+
* The function supports both camelCase and snake_case filters,
|
|
19
|
+
* controlled by the `snakeCase` flag.
|
|
9
20
|
*
|
|
10
21
|
* @async
|
|
22
|
+
* @param {Object} params
|
|
23
|
+
* @param {import('knex').Knex.QueryBuilder} params.baseQuery
|
|
24
|
+
* A prepared Knex query builder representing the base query.
|
|
25
|
+
* It should not include pagination or ordering.
|
|
26
|
+
*
|
|
27
|
+
* @param {Object} [params.filter={}]
|
|
28
|
+
* An object describing filter conditions to apply.
|
|
29
|
+
* The structure is expected to be compatible with `applyFilter`
|
|
30
|
+
* or `applyFilterSnakeCase`.
|
|
31
|
+
*
|
|
32
|
+
* @param {boolean} [params.snakeCase=true]
|
|
33
|
+
* When true, applies filters assuming database columns
|
|
34
|
+
* are in snake_case. When false, camelCase is assumed.
|
|
35
|
+
*
|
|
36
|
+
* @param {Object|Array} [params.orderBy]
|
|
37
|
+
* Ordering definition passed directly to `applyOrderBy`.
|
|
38
|
+
* Can be a single order definition or an array of them.
|
|
39
|
+
*
|
|
40
|
+
* @param {number} [params.page=1]
|
|
41
|
+
* The current page number. Pages are 1-based.
|
|
42
|
+
*
|
|
43
|
+
* @param {number} [params.limit=10]
|
|
44
|
+
* The maximum number of rows per page.
|
|
45
|
+
*
|
|
46
|
+
* @param {Function} [params.mapRow]
|
|
47
|
+
* Optional mapping function applied to each returned row.
|
|
48
|
+
* Useful for transforming DB rows into domain entities.
|
|
49
|
+
*
|
|
50
|
+
* @returns {Promise<Object>} Pagination result object
|
|
51
|
+
* @returns {number} return.page
|
|
52
|
+
* The current page number.
|
|
53
|
+
*
|
|
54
|
+
* @returns {number} return.pages
|
|
55
|
+
* Total number of available pages.
|
|
56
|
+
*
|
|
57
|
+
* @returns {number} return.totalCount
|
|
58
|
+
* Total number of records matching the filters.
|
|
59
|
+
*
|
|
60
|
+
* @returns {boolean} return.hasPrevious
|
|
61
|
+
* Indicates whether a previous page exists.
|
|
62
|
+
*
|
|
63
|
+
* @returns {boolean} return.hasNext
|
|
64
|
+
* Indicates whether a next page exists.
|
|
65
|
+
*
|
|
66
|
+
* @returns {Array<Object>} return.list
|
|
67
|
+
* The list of records for the current page.
|
|
68
|
+
* Rows are mapped using `mapRow` if provided.
|
|
11
69
|
*/
|
|
12
70
|
export async function sqlPaginate({
|
|
13
71
|
mapRow,
|
|
@@ -1,7 +1,65 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Executes paginated query.
|
|
2
|
+
* Executes a paginated SQL query using a Knex query builder.
|
|
3
|
+
*
|
|
4
|
+
* This helper clones the provided base query twice:
|
|
5
|
+
* one query is used to fetch the paginated list of rows,
|
|
6
|
+
* and the second query is used to calculate the total count
|
|
7
|
+
* of records matching the same filters.
|
|
8
|
+
*
|
|
9
|
+
* Filters are applied consistently to both queries.
|
|
10
|
+
* Ordering and pagination are applied only to the list query.
|
|
11
|
+
*
|
|
12
|
+
* The function supports both camelCase and snake_case filters,
|
|
13
|
+
* controlled by the `snakeCase` flag.
|
|
3
14
|
*
|
|
4
15
|
* @async
|
|
16
|
+
* @param {Object} params
|
|
17
|
+
* @param {import('knex').Knex.QueryBuilder} params.baseQuery
|
|
18
|
+
* A prepared Knex query builder representing the base query.
|
|
19
|
+
* It should not include pagination or ordering.
|
|
20
|
+
*
|
|
21
|
+
* @param {Object} [params.filter={}]
|
|
22
|
+
* An object describing filter conditions to apply.
|
|
23
|
+
* The structure is expected to be compatible with `applyFilter`
|
|
24
|
+
* or `applyFilterSnakeCase`.
|
|
25
|
+
*
|
|
26
|
+
* @param {boolean} [params.snakeCase=true]
|
|
27
|
+
* When true, applies filters assuming database columns
|
|
28
|
+
* are in snake_case. When false, camelCase is assumed.
|
|
29
|
+
*
|
|
30
|
+
* @param {Object|Array} [params.orderBy]
|
|
31
|
+
* Ordering definition passed directly to `applyOrderBy`.
|
|
32
|
+
* Can be a single order definition or an array of them.
|
|
33
|
+
*
|
|
34
|
+
* @param {number} [params.page=1]
|
|
35
|
+
* The current page number. Pages are 1-based.
|
|
36
|
+
*
|
|
37
|
+
* @param {number} [params.limit=10]
|
|
38
|
+
* The maximum number of rows per page.
|
|
39
|
+
*
|
|
40
|
+
* @param {Function} [params.mapRow]
|
|
41
|
+
* Optional mapping function applied to each returned row.
|
|
42
|
+
* Useful for transforming DB rows into domain entities.
|
|
43
|
+
*
|
|
44
|
+
* @returns {Promise<Object>} Pagination result object
|
|
45
|
+
* @returns {number} return.page
|
|
46
|
+
* The current page number.
|
|
47
|
+
*
|
|
48
|
+
* @returns {number} return.pages
|
|
49
|
+
* Total number of available pages.
|
|
50
|
+
*
|
|
51
|
+
* @returns {number} return.totalCount
|
|
52
|
+
* Total number of records matching the filters.
|
|
53
|
+
*
|
|
54
|
+
* @returns {boolean} return.hasPrevious
|
|
55
|
+
* Indicates whether a previous page exists.
|
|
56
|
+
*
|
|
57
|
+
* @returns {boolean} return.hasNext
|
|
58
|
+
* Indicates whether a next page exists.
|
|
59
|
+
*
|
|
60
|
+
* @returns {Array<Object>} return.list
|
|
61
|
+
* The list of records for the current page.
|
|
62
|
+
* Rows are mapped using `mapRow` if provided.
|
|
5
63
|
*/
|
|
6
64
|
export function sqlPaginate({
|
|
7
65
|
mapRow,
|
|
@@ -12,18 +70,11 @@ export function sqlPaginate({
|
|
|
12
70
|
filter,
|
|
13
71
|
snakeCase,
|
|
14
72
|
}: {
|
|
15
|
-
|
|
16
|
-
|
|
73
|
+
baseQuery: import('knex').Knex.QueryBuilder
|
|
74
|
+
filter?: any
|
|
75
|
+
snakeCase?: boolean
|
|
76
|
+
orderBy?: any | any[]
|
|
17
77
|
page?: number
|
|
18
|
-
baseQuery: any
|
|
19
78
|
limit?: number
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
}): Promise<{
|
|
23
|
-
totalCount: number
|
|
24
|
-
totalPages: number
|
|
25
|
-
currentPage: number
|
|
26
|
-
hasPrevious: boolean
|
|
27
|
-
hasNext: boolean
|
|
28
|
-
list: any
|
|
29
|
-
}>
|
|
79
|
+
mapRow?: Function
|
|
80
|
+
}): Promise<any>
|