@sdflc/backend-helpers 1.0.130 → 1.0.132
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.
|
@@ -9,16 +9,33 @@ import { BaseRestApiCrudControllerPropsInterface } from './interfaces';
|
|
|
9
9
|
* changes.
|
|
10
10
|
*
|
|
11
11
|
* ── Argument shapes ───────────────────────────────────────────────────────────
|
|
12
|
-
* list → core.list({ filter, params })
|
|
12
|
+
* list → core.list({ filter, params }) params: PaginationAndSorting
|
|
13
13
|
* get → core.get({ id })
|
|
14
14
|
* getMany → core.getMany({ ids })
|
|
15
|
-
* create → core.create({ params })
|
|
16
|
-
* createMany → core.createMany({ params })
|
|
17
|
-
* update → core.update({ where, params })
|
|
18
|
-
* updateMany → core.updateMany({ params })
|
|
15
|
+
* create → core.create({ params }) body: { params: {...} }
|
|
16
|
+
* createMany → core.createMany({ params }) body: { params: [{...}] }
|
|
17
|
+
* update → core.update({ where, params }) body: { params: {...} }
|
|
18
|
+
* updateMany → core.updateMany({ params }) body: { params: [{...}] }
|
|
19
19
|
* remove → core.remove({ where })
|
|
20
20
|
* removeMany → core.removeMany({ where })
|
|
21
21
|
*
|
|
22
|
+
* ── URL conventions ───────────────────────────────────────────────────────────
|
|
23
|
+
* Filter fields go flat in the query string:
|
|
24
|
+
* ?partyId=abc-123&isEnabled=true
|
|
25
|
+
*
|
|
26
|
+
* Pagination and sorting use bracket notation (parsed by qs automatically):
|
|
27
|
+
* ?pagination[page]=1&pagination[pageSize]=20
|
|
28
|
+
* ?sortBy[0][name]=orderNo&sortBy[0][order]=ASC
|
|
29
|
+
*
|
|
30
|
+
* Combined example:
|
|
31
|
+
* GET /api/v1/party-ticket-types
|
|
32
|
+
* ?partyId=abc-123
|
|
33
|
+
* &isEnabled=true
|
|
34
|
+
* &pagination[page]=1
|
|
35
|
+
* &pagination[pageSize]=20
|
|
36
|
+
* &sortBy[0][name]=orderNo
|
|
37
|
+
* &sortBy[0][order]=ASC
|
|
38
|
+
*
|
|
22
39
|
* ── Extension points ──────────────────────────────────────────────────────────
|
|
23
40
|
* buildWhere(req, ids) — override to add extra where conditions beyond id
|
|
24
41
|
* buildFilter(req) — override to customise filter extraction from QS
|
|
@@ -67,15 +84,35 @@ declare class BaseRestApiCrudController extends BaseController {
|
|
|
67
84
|
* Builds the `filter` object passed to core.list().
|
|
68
85
|
*
|
|
69
86
|
* Base implementation treats every query string key that is NOT a known
|
|
70
|
-
* pagination key as a filter field. Override to
|
|
71
|
-
* (e.g.
|
|
87
|
+
* pagination key (pagination, sortBy) as a filter field. Override to
|
|
88
|
+
* apply custom parsing (e.g. array values, renamed fields).
|
|
89
|
+
*
|
|
90
|
+
* Coercions applied at the transport boundary so gateways receive the
|
|
91
|
+
* same types whether called from GraphQL or REST:
|
|
92
|
+
* 'true' → true
|
|
93
|
+
* 'false' → false
|
|
94
|
+
* 'null' → omitted (key excluded from filter entirely)
|
|
95
|
+
* 'undefined' → omitted (key excluded from filter entirely)
|
|
96
|
+
* anything else → left as string
|
|
72
97
|
*/
|
|
73
98
|
protected buildFilter(req: any): Record<string, any>;
|
|
74
99
|
/**
|
|
75
|
-
* Builds the `params` object passed to core.list()
|
|
100
|
+
* Builds the `params` object passed to core.list().
|
|
101
|
+
*
|
|
102
|
+
* Assembles the PaginationAndSorting shape expected by cores:
|
|
103
|
+
* {
|
|
104
|
+
* pagination: { page: string, pageSize: string },
|
|
105
|
+
* sortBy: [{ name: string, order: 'ASC' | 'DESC' }]
|
|
106
|
+
* }
|
|
107
|
+
*
|
|
108
|
+
* pagination values are passed through as-is (strings from qs) —
|
|
109
|
+
* type coercion is left to the core/gateway layer.
|
|
110
|
+
*
|
|
111
|
+
* sortBy entries with a missing or empty name are silently dropped.
|
|
112
|
+
* order is normalised to 'ASC' or 'DESC', defaulting to 'ASC' for any
|
|
113
|
+
* unrecognised value — this is structural normalisation, not type coercion.
|
|
76
114
|
*
|
|
77
|
-
*
|
|
78
|
-
* string. Override to support additional or renamed pagination conventions.
|
|
115
|
+
* Override to support additional or renamed pagination conventions.
|
|
79
116
|
*/
|
|
80
117
|
protected buildParams(req: any): Record<string, any>;
|
|
81
118
|
protected resolveCore(req: any): Promise<any>;
|
|
@@ -97,8 +134,8 @@ declare class BaseRestApiCrudController extends BaseController {
|
|
|
97
134
|
* GET /
|
|
98
135
|
*
|
|
99
136
|
* Splits the query string into filter (non-pagination keys) and params
|
|
100
|
-
* (
|
|
101
|
-
* the GraphQL list resolver: core.list({ filter, params }).
|
|
137
|
+
* (PaginationAndSorting shape) via buildFilter() and buildParams().
|
|
138
|
+
* Shape matches the GraphQL list resolver: core.list({ filter, params }).
|
|
102
139
|
*/
|
|
103
140
|
protected list(req: any): Promise<any>;
|
|
104
141
|
/**
|
|
@@ -5,10 +5,15 @@ exports.BaseRestApiCrudController = void 0;
|
|
|
5
5
|
const BaseController_1 = require("./BaseController");
|
|
6
6
|
const parseIds_1 = require("./parseIds");
|
|
7
7
|
/**
|
|
8
|
-
*
|
|
8
|
+
* Query string keys that belong to `params` (PaginationAndSorting shape).
|
|
9
9
|
* Everything else in the query string is treated as a filter field.
|
|
10
|
+
*
|
|
11
|
+
* qs (used by Express) parses bracket notation automatically, so:
|
|
12
|
+
* ?pagination[page]=1&pagination[pageSize]=20&sortBy[0][name]=orderNo&sortBy[0][order]=ASC
|
|
13
|
+
* arrives in req.query as:
|
|
14
|
+
* { pagination: { page: '1', pageSize: '20' }, sortBy: [{ name: 'orderNo', order: 'ASC' }] }
|
|
10
15
|
*/
|
|
11
|
-
const PAGINATION_KEYS = new Set(['
|
|
16
|
+
const PAGINATION_KEYS = new Set(['pagination', 'sortBy']);
|
|
12
17
|
/**
|
|
13
18
|
* BaseRestApiCrudController
|
|
14
19
|
*
|
|
@@ -18,16 +23,33 @@ const PAGINATION_KEYS = new Set(['page', 'limit', 'offset', 'sortBy', 'sortDir',
|
|
|
18
23
|
* changes.
|
|
19
24
|
*
|
|
20
25
|
* ── Argument shapes ───────────────────────────────────────────────────────────
|
|
21
|
-
* list → core.list({ filter, params })
|
|
26
|
+
* list → core.list({ filter, params }) params: PaginationAndSorting
|
|
22
27
|
* get → core.get({ id })
|
|
23
28
|
* getMany → core.getMany({ ids })
|
|
24
|
-
* create → core.create({ params })
|
|
25
|
-
* createMany → core.createMany({ params })
|
|
26
|
-
* update → core.update({ where, params })
|
|
27
|
-
* updateMany → core.updateMany({ params })
|
|
29
|
+
* create → core.create({ params }) body: { params: {...} }
|
|
30
|
+
* createMany → core.createMany({ params }) body: { params: [{...}] }
|
|
31
|
+
* update → core.update({ where, params }) body: { params: {...} }
|
|
32
|
+
* updateMany → core.updateMany({ params }) body: { params: [{...}] }
|
|
28
33
|
* remove → core.remove({ where })
|
|
29
34
|
* removeMany → core.removeMany({ where })
|
|
30
35
|
*
|
|
36
|
+
* ── URL conventions ───────────────────────────────────────────────────────────
|
|
37
|
+
* Filter fields go flat in the query string:
|
|
38
|
+
* ?partyId=abc-123&isEnabled=true
|
|
39
|
+
*
|
|
40
|
+
* Pagination and sorting use bracket notation (parsed by qs automatically):
|
|
41
|
+
* ?pagination[page]=1&pagination[pageSize]=20
|
|
42
|
+
* ?sortBy[0][name]=orderNo&sortBy[0][order]=ASC
|
|
43
|
+
*
|
|
44
|
+
* Combined example:
|
|
45
|
+
* GET /api/v1/party-ticket-types
|
|
46
|
+
* ?partyId=abc-123
|
|
47
|
+
* &isEnabled=true
|
|
48
|
+
* &pagination[page]=1
|
|
49
|
+
* &pagination[pageSize]=20
|
|
50
|
+
* &sortBy[0][name]=orderNo
|
|
51
|
+
* &sortBy[0][order]=ASC
|
|
52
|
+
*
|
|
31
53
|
* ── Extension points ──────────────────────────────────────────────────────────
|
|
32
54
|
* buildWhere(req, ids) — override to add extra where conditions beyond id
|
|
33
55
|
* buildFilter(req) — override to customise filter extraction from QS
|
|
@@ -97,29 +119,84 @@ class BaseRestApiCrudController extends BaseController_1.BaseController {
|
|
|
97
119
|
* Builds the `filter` object passed to core.list().
|
|
98
120
|
*
|
|
99
121
|
* Base implementation treats every query string key that is NOT a known
|
|
100
|
-
* pagination key as a filter field. Override to
|
|
101
|
-
* (e.g.
|
|
122
|
+
* pagination key (pagination, sortBy) as a filter field. Override to
|
|
123
|
+
* apply custom parsing (e.g. array values, renamed fields).
|
|
124
|
+
*
|
|
125
|
+
* Coercions applied at the transport boundary so gateways receive the
|
|
126
|
+
* same types whether called from GraphQL or REST:
|
|
127
|
+
* 'true' → true
|
|
128
|
+
* 'false' → false
|
|
129
|
+
* 'null' → omitted (key excluded from filter entirely)
|
|
130
|
+
* 'undefined' → omitted (key excluded from filter entirely)
|
|
131
|
+
* anything else → left as string
|
|
102
132
|
*/
|
|
103
133
|
buildFilter(req) {
|
|
104
134
|
const filter = {};
|
|
105
135
|
for (const [key, value] of Object.entries(req.query ?? {})) {
|
|
106
|
-
if (
|
|
136
|
+
if (PAGINATION_KEYS.has(key)) {
|
|
137
|
+
continue;
|
|
138
|
+
}
|
|
139
|
+
if (value === 'null' || value === 'undefined') {
|
|
140
|
+
// Omit entirely — these express "no value" and should not constrain
|
|
141
|
+
// the query. Passing the string 'null' to a gateway that checks
|
|
142
|
+
// value == null would silently fail to filter.
|
|
143
|
+
continue;
|
|
144
|
+
}
|
|
145
|
+
if (value === 'true') {
|
|
146
|
+
filter[key] = true;
|
|
147
|
+
}
|
|
148
|
+
else if (value === 'false') {
|
|
149
|
+
filter[key] = false;
|
|
150
|
+
}
|
|
151
|
+
else {
|
|
107
152
|
filter[key] = value;
|
|
108
153
|
}
|
|
109
154
|
}
|
|
110
155
|
return filter;
|
|
111
156
|
}
|
|
112
157
|
/**
|
|
113
|
-
* Builds the `params` object passed to core.list()
|
|
158
|
+
* Builds the `params` object passed to core.list().
|
|
159
|
+
*
|
|
160
|
+
* Assembles the PaginationAndSorting shape expected by cores:
|
|
161
|
+
* {
|
|
162
|
+
* pagination: { page: string, pageSize: string },
|
|
163
|
+
* sortBy: [{ name: string, order: 'ASC' | 'DESC' }]
|
|
164
|
+
* }
|
|
165
|
+
*
|
|
166
|
+
* pagination values are passed through as-is (strings from qs) —
|
|
167
|
+
* type coercion is left to the core/gateway layer.
|
|
114
168
|
*
|
|
115
|
-
*
|
|
116
|
-
*
|
|
169
|
+
* sortBy entries with a missing or empty name are silently dropped.
|
|
170
|
+
* order is normalised to 'ASC' or 'DESC', defaulting to 'ASC' for any
|
|
171
|
+
* unrecognised value — this is structural normalisation, not type coercion.
|
|
172
|
+
*
|
|
173
|
+
* Override to support additional or renamed pagination conventions.
|
|
117
174
|
*/
|
|
118
175
|
buildParams(req) {
|
|
176
|
+
const query = req.query ?? {};
|
|
119
177
|
const params = {};
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
178
|
+
// ── pagination ────────────────────────────────────────────────────────────
|
|
179
|
+
// qs parses ?pagination[page]=1&pagination[pageSize]=20 into:
|
|
180
|
+
// req.query.pagination = { page: '1', pageSize: '20' }
|
|
181
|
+
// Values are passed through as-is — coercion is left to the core/gateway.
|
|
182
|
+
if (query.pagination && typeof query.pagination === 'object') {
|
|
183
|
+
params.pagination = query.pagination;
|
|
184
|
+
}
|
|
185
|
+
// ── sortBy ────────────────────────────────────────────────────────────────
|
|
186
|
+
// qs parses ?sortBy[0][name]=orderNo&sortBy[0][order]=ASC into:
|
|
187
|
+
// req.query.sortBy = [{ name: 'orderNo', order: 'ASC' }]
|
|
188
|
+
// Entries with a missing or empty name are silently dropped.
|
|
189
|
+
// order is normalised to 'ASC' or 'DESC', defaulting to 'ASC' for any
|
|
190
|
+
// unrecognised value — this is structural normalisation, not type coercion.
|
|
191
|
+
if (Array.isArray(query.sortBy)) {
|
|
192
|
+
const sortBy = query.sortBy
|
|
193
|
+
.filter((item) => item && typeof item === 'object' && item.name)
|
|
194
|
+
.map((item) => ({
|
|
195
|
+
name: String(item.name),
|
|
196
|
+
order: String(item.order ?? 'ASC').toUpperCase() === 'DESC' ? 'DESC' : 'ASC',
|
|
197
|
+
}));
|
|
198
|
+
if (sortBy.length > 0) {
|
|
199
|
+
params.sortBy = sortBy;
|
|
123
200
|
}
|
|
124
201
|
}
|
|
125
202
|
return params;
|
|
@@ -180,8 +257,8 @@ class BaseRestApiCrudController extends BaseController_1.BaseController {
|
|
|
180
257
|
* GET /
|
|
181
258
|
*
|
|
182
259
|
* Splits the query string into filter (non-pagination keys) and params
|
|
183
|
-
* (
|
|
184
|
-
* the GraphQL list resolver: core.list({ filter, params }).
|
|
260
|
+
* (PaginationAndSorting shape) via buildFilter() and buildParams().
|
|
261
|
+
* Shape matches the GraphQL list resolver: core.list({ filter, params }).
|
|
185
262
|
*/
|
|
186
263
|
async list(req) {
|
|
187
264
|
const core = await this.resolveCore(req);
|
|
@@ -9,16 +9,33 @@ import { BaseRestApiCrudControllerPropsInterface } from './interfaces';
|
|
|
9
9
|
* changes.
|
|
10
10
|
*
|
|
11
11
|
* ── Argument shapes ───────────────────────────────────────────────────────────
|
|
12
|
-
* list → core.list({ filter, params })
|
|
12
|
+
* list → core.list({ filter, params }) params: PaginationAndSorting
|
|
13
13
|
* get → core.get({ id })
|
|
14
14
|
* getMany → core.getMany({ ids })
|
|
15
|
-
* create → core.create({ params })
|
|
16
|
-
* createMany → core.createMany({ params })
|
|
17
|
-
* update → core.update({ where, params })
|
|
18
|
-
* updateMany → core.updateMany({ params })
|
|
15
|
+
* create → core.create({ params }) body: { params: {...} }
|
|
16
|
+
* createMany → core.createMany({ params }) body: { params: [{...}] }
|
|
17
|
+
* update → core.update({ where, params }) body: { params: {...} }
|
|
18
|
+
* updateMany → core.updateMany({ params }) body: { params: [{...}] }
|
|
19
19
|
* remove → core.remove({ where })
|
|
20
20
|
* removeMany → core.removeMany({ where })
|
|
21
21
|
*
|
|
22
|
+
* ── URL conventions ───────────────────────────────────────────────────────────
|
|
23
|
+
* Filter fields go flat in the query string:
|
|
24
|
+
* ?partyId=abc-123&isEnabled=true
|
|
25
|
+
*
|
|
26
|
+
* Pagination and sorting use bracket notation (parsed by qs automatically):
|
|
27
|
+
* ?pagination[page]=1&pagination[pageSize]=20
|
|
28
|
+
* ?sortBy[0][name]=orderNo&sortBy[0][order]=ASC
|
|
29
|
+
*
|
|
30
|
+
* Combined example:
|
|
31
|
+
* GET /api/v1/party-ticket-types
|
|
32
|
+
* ?partyId=abc-123
|
|
33
|
+
* &isEnabled=true
|
|
34
|
+
* &pagination[page]=1
|
|
35
|
+
* &pagination[pageSize]=20
|
|
36
|
+
* &sortBy[0][name]=orderNo
|
|
37
|
+
* &sortBy[0][order]=ASC
|
|
38
|
+
*
|
|
22
39
|
* ── Extension points ──────────────────────────────────────────────────────────
|
|
23
40
|
* buildWhere(req, ids) — override to add extra where conditions beyond id
|
|
24
41
|
* buildFilter(req) — override to customise filter extraction from QS
|
|
@@ -67,15 +84,35 @@ declare class BaseRestApiCrudController extends BaseController {
|
|
|
67
84
|
* Builds the `filter` object passed to core.list().
|
|
68
85
|
*
|
|
69
86
|
* Base implementation treats every query string key that is NOT a known
|
|
70
|
-
* pagination key as a filter field. Override to
|
|
71
|
-
* (e.g.
|
|
87
|
+
* pagination key (pagination, sortBy) as a filter field. Override to
|
|
88
|
+
* apply custom parsing (e.g. array values, renamed fields).
|
|
89
|
+
*
|
|
90
|
+
* Coercions applied at the transport boundary so gateways receive the
|
|
91
|
+
* same types whether called from GraphQL or REST:
|
|
92
|
+
* 'true' → true
|
|
93
|
+
* 'false' → false
|
|
94
|
+
* 'null' → omitted (key excluded from filter entirely)
|
|
95
|
+
* 'undefined' → omitted (key excluded from filter entirely)
|
|
96
|
+
* anything else → left as string
|
|
72
97
|
*/
|
|
73
98
|
protected buildFilter(req: any): Record<string, any>;
|
|
74
99
|
/**
|
|
75
|
-
* Builds the `params` object passed to core.list()
|
|
100
|
+
* Builds the `params` object passed to core.list().
|
|
101
|
+
*
|
|
102
|
+
* Assembles the PaginationAndSorting shape expected by cores:
|
|
103
|
+
* {
|
|
104
|
+
* pagination: { page: string, pageSize: string },
|
|
105
|
+
* sortBy: [{ name: string, order: 'ASC' | 'DESC' }]
|
|
106
|
+
* }
|
|
107
|
+
*
|
|
108
|
+
* pagination values are passed through as-is (strings from qs) —
|
|
109
|
+
* type coercion is left to the core/gateway layer.
|
|
110
|
+
*
|
|
111
|
+
* sortBy entries with a missing or empty name are silently dropped.
|
|
112
|
+
* order is normalised to 'ASC' or 'DESC', defaulting to 'ASC' for any
|
|
113
|
+
* unrecognised value — this is structural normalisation, not type coercion.
|
|
76
114
|
*
|
|
77
|
-
*
|
|
78
|
-
* string. Override to support additional or renamed pagination conventions.
|
|
115
|
+
* Override to support additional or renamed pagination conventions.
|
|
79
116
|
*/
|
|
80
117
|
protected buildParams(req: any): Record<string, any>;
|
|
81
118
|
protected resolveCore(req: any): Promise<any>;
|
|
@@ -97,8 +134,8 @@ declare class BaseRestApiCrudController extends BaseController {
|
|
|
97
134
|
* GET /
|
|
98
135
|
*
|
|
99
136
|
* Splits the query string into filter (non-pagination keys) and params
|
|
100
|
-
* (
|
|
101
|
-
* the GraphQL list resolver: core.list({ filter, params }).
|
|
137
|
+
* (PaginationAndSorting shape) via buildFilter() and buildParams().
|
|
138
|
+
* Shape matches the GraphQL list resolver: core.list({ filter, params }).
|
|
102
139
|
*/
|
|
103
140
|
protected list(req: any): Promise<any>;
|
|
104
141
|
/**
|
|
@@ -2,10 +2,15 @@
|
|
|
2
2
|
import { BaseController } from './BaseController';
|
|
3
3
|
import { parseIds } from './parseIds';
|
|
4
4
|
/**
|
|
5
|
-
*
|
|
5
|
+
* Query string keys that belong to `params` (PaginationAndSorting shape).
|
|
6
6
|
* Everything else in the query string is treated as a filter field.
|
|
7
|
+
*
|
|
8
|
+
* qs (used by Express) parses bracket notation automatically, so:
|
|
9
|
+
* ?pagination[page]=1&pagination[pageSize]=20&sortBy[0][name]=orderNo&sortBy[0][order]=ASC
|
|
10
|
+
* arrives in req.query as:
|
|
11
|
+
* { pagination: { page: '1', pageSize: '20' }, sortBy: [{ name: 'orderNo', order: 'ASC' }] }
|
|
7
12
|
*/
|
|
8
|
-
const PAGINATION_KEYS = new Set(['
|
|
13
|
+
const PAGINATION_KEYS = new Set(['pagination', 'sortBy']);
|
|
9
14
|
/**
|
|
10
15
|
* BaseRestApiCrudController
|
|
11
16
|
*
|
|
@@ -15,16 +20,33 @@ const PAGINATION_KEYS = new Set(['page', 'limit', 'offset', 'sortBy', 'sortDir',
|
|
|
15
20
|
* changes.
|
|
16
21
|
*
|
|
17
22
|
* ── Argument shapes ───────────────────────────────────────────────────────────
|
|
18
|
-
* list → core.list({ filter, params })
|
|
23
|
+
* list → core.list({ filter, params }) params: PaginationAndSorting
|
|
19
24
|
* get → core.get({ id })
|
|
20
25
|
* getMany → core.getMany({ ids })
|
|
21
|
-
* create → core.create({ params })
|
|
22
|
-
* createMany → core.createMany({ params })
|
|
23
|
-
* update → core.update({ where, params })
|
|
24
|
-
* updateMany → core.updateMany({ params })
|
|
26
|
+
* create → core.create({ params }) body: { params: {...} }
|
|
27
|
+
* createMany → core.createMany({ params }) body: { params: [{...}] }
|
|
28
|
+
* update → core.update({ where, params }) body: { params: {...} }
|
|
29
|
+
* updateMany → core.updateMany({ params }) body: { params: [{...}] }
|
|
25
30
|
* remove → core.remove({ where })
|
|
26
31
|
* removeMany → core.removeMany({ where })
|
|
27
32
|
*
|
|
33
|
+
* ── URL conventions ───────────────────────────────────────────────────────────
|
|
34
|
+
* Filter fields go flat in the query string:
|
|
35
|
+
* ?partyId=abc-123&isEnabled=true
|
|
36
|
+
*
|
|
37
|
+
* Pagination and sorting use bracket notation (parsed by qs automatically):
|
|
38
|
+
* ?pagination[page]=1&pagination[pageSize]=20
|
|
39
|
+
* ?sortBy[0][name]=orderNo&sortBy[0][order]=ASC
|
|
40
|
+
*
|
|
41
|
+
* Combined example:
|
|
42
|
+
* GET /api/v1/party-ticket-types
|
|
43
|
+
* ?partyId=abc-123
|
|
44
|
+
* &isEnabled=true
|
|
45
|
+
* &pagination[page]=1
|
|
46
|
+
* &pagination[pageSize]=20
|
|
47
|
+
* &sortBy[0][name]=orderNo
|
|
48
|
+
* &sortBy[0][order]=ASC
|
|
49
|
+
*
|
|
28
50
|
* ── Extension points ──────────────────────────────────────────────────────────
|
|
29
51
|
* buildWhere(req, ids) — override to add extra where conditions beyond id
|
|
30
52
|
* buildFilter(req) — override to customise filter extraction from QS
|
|
@@ -94,29 +116,84 @@ class BaseRestApiCrudController extends BaseController {
|
|
|
94
116
|
* Builds the `filter` object passed to core.list().
|
|
95
117
|
*
|
|
96
118
|
* Base implementation treats every query string key that is NOT a known
|
|
97
|
-
* pagination key as a filter field. Override to
|
|
98
|
-
* (e.g.
|
|
119
|
+
* pagination key (pagination, sortBy) as a filter field. Override to
|
|
120
|
+
* apply custom parsing (e.g. array values, renamed fields).
|
|
121
|
+
*
|
|
122
|
+
* Coercions applied at the transport boundary so gateways receive the
|
|
123
|
+
* same types whether called from GraphQL or REST:
|
|
124
|
+
* 'true' → true
|
|
125
|
+
* 'false' → false
|
|
126
|
+
* 'null' → omitted (key excluded from filter entirely)
|
|
127
|
+
* 'undefined' → omitted (key excluded from filter entirely)
|
|
128
|
+
* anything else → left as string
|
|
99
129
|
*/
|
|
100
130
|
buildFilter(req) {
|
|
101
131
|
const filter = {};
|
|
102
132
|
for (const [key, value] of Object.entries(req.query ?? {})) {
|
|
103
|
-
if (
|
|
133
|
+
if (PAGINATION_KEYS.has(key)) {
|
|
134
|
+
continue;
|
|
135
|
+
}
|
|
136
|
+
if (value === 'null' || value === 'undefined') {
|
|
137
|
+
// Omit entirely — these express "no value" and should not constrain
|
|
138
|
+
// the query. Passing the string 'null' to a gateway that checks
|
|
139
|
+
// value == null would silently fail to filter.
|
|
140
|
+
continue;
|
|
141
|
+
}
|
|
142
|
+
if (value === 'true') {
|
|
143
|
+
filter[key] = true;
|
|
144
|
+
}
|
|
145
|
+
else if (value === 'false') {
|
|
146
|
+
filter[key] = false;
|
|
147
|
+
}
|
|
148
|
+
else {
|
|
104
149
|
filter[key] = value;
|
|
105
150
|
}
|
|
106
151
|
}
|
|
107
152
|
return filter;
|
|
108
153
|
}
|
|
109
154
|
/**
|
|
110
|
-
* Builds the `params` object passed to core.list()
|
|
155
|
+
* Builds the `params` object passed to core.list().
|
|
156
|
+
*
|
|
157
|
+
* Assembles the PaginationAndSorting shape expected by cores:
|
|
158
|
+
* {
|
|
159
|
+
* pagination: { page: string, pageSize: string },
|
|
160
|
+
* sortBy: [{ name: string, order: 'ASC' | 'DESC' }]
|
|
161
|
+
* }
|
|
162
|
+
*
|
|
163
|
+
* pagination values are passed through as-is (strings from qs) —
|
|
164
|
+
* type coercion is left to the core/gateway layer.
|
|
111
165
|
*
|
|
112
|
-
*
|
|
113
|
-
*
|
|
166
|
+
* sortBy entries with a missing or empty name are silently dropped.
|
|
167
|
+
* order is normalised to 'ASC' or 'DESC', defaulting to 'ASC' for any
|
|
168
|
+
* unrecognised value — this is structural normalisation, not type coercion.
|
|
169
|
+
*
|
|
170
|
+
* Override to support additional or renamed pagination conventions.
|
|
114
171
|
*/
|
|
115
172
|
buildParams(req) {
|
|
173
|
+
const query = req.query ?? {};
|
|
116
174
|
const params = {};
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
175
|
+
// ── pagination ────────────────────────────────────────────────────────────
|
|
176
|
+
// qs parses ?pagination[page]=1&pagination[pageSize]=20 into:
|
|
177
|
+
// req.query.pagination = { page: '1', pageSize: '20' }
|
|
178
|
+
// Values are passed through as-is — coercion is left to the core/gateway.
|
|
179
|
+
if (query.pagination && typeof query.pagination === 'object') {
|
|
180
|
+
params.pagination = query.pagination;
|
|
181
|
+
}
|
|
182
|
+
// ── sortBy ────────────────────────────────────────────────────────────────
|
|
183
|
+
// qs parses ?sortBy[0][name]=orderNo&sortBy[0][order]=ASC into:
|
|
184
|
+
// req.query.sortBy = [{ name: 'orderNo', order: 'ASC' }]
|
|
185
|
+
// Entries with a missing or empty name are silently dropped.
|
|
186
|
+
// order is normalised to 'ASC' or 'DESC', defaulting to 'ASC' for any
|
|
187
|
+
// unrecognised value — this is structural normalisation, not type coercion.
|
|
188
|
+
if (Array.isArray(query.sortBy)) {
|
|
189
|
+
const sortBy = query.sortBy
|
|
190
|
+
.filter((item) => item && typeof item === 'object' && item.name)
|
|
191
|
+
.map((item) => ({
|
|
192
|
+
name: String(item.name),
|
|
193
|
+
order: String(item.order ?? 'ASC').toUpperCase() === 'DESC' ? 'DESC' : 'ASC',
|
|
194
|
+
}));
|
|
195
|
+
if (sortBy.length > 0) {
|
|
196
|
+
params.sortBy = sortBy;
|
|
120
197
|
}
|
|
121
198
|
}
|
|
122
199
|
return params;
|
|
@@ -177,8 +254,8 @@ class BaseRestApiCrudController extends BaseController {
|
|
|
177
254
|
* GET /
|
|
178
255
|
*
|
|
179
256
|
* Splits the query string into filter (non-pagination keys) and params
|
|
180
|
-
* (
|
|
181
|
-
* the GraphQL list resolver: core.list({ filter, params }).
|
|
257
|
+
* (PaginationAndSorting shape) via buildFilter() and buildParams().
|
|
258
|
+
* Shape matches the GraphQL list resolver: core.list({ filter, params }).
|
|
182
259
|
*/
|
|
183
260
|
async list(req) {
|
|
184
261
|
const core = await this.resolveCore(req);
|