Skip to content

Commit d659ec8

Browse files
committed
feat(db): get all children of specified taxonomy
So if you have taxonomy countries and other taxonomy cities, you can link them both by specifying parent. So if you ask - get all cities from all countries, this is right method for you. Other feature like you want to get population of city (UK -> London -> popu
1 parent 346f279 commit d659ec8

File tree

3 files changed

+52
-1
lines changed

3 files changed

+52
-1
lines changed

src/components/db.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -658,3 +658,46 @@ export async function getTerms({
658658
result: response.result,
659659
};
660660
}
661+
662+
export async function getChildrenOfTerms({
663+
taxonomyName,
664+
language,
665+
pageNumber,
666+
pageSize,
667+
sort,
668+
filter,
669+
projection,
670+
excludeCulture,
671+
cluster,
672+
}) {
673+
const response = await server.loadJson(
674+
`${Config.apiUrl}${Endpoints.PROJECT.DATABASE.TAXONOMY.TERM.GET_CHILDREN(
675+
taxonomyName
676+
)}`,
677+
{
678+
method: 'POST',
679+
headers: {
680+
'X-CM-ProjectId': Config.projectId,
681+
'X-CM-Cluster': cluster,
682+
Authorization: `Bearer ${Config.secretKey}`,
683+
Accept: 'application/json',
684+
'Content-Type': 'application/json',
685+
'Accept-Language': language || 'en',
686+
},
687+
body: JSON.stringify({
688+
pageSize: pageSize || Config.tablePageSize,
689+
pageNumber: pageNumber || 0,
690+
projection: objectOrStringToString(projection),
691+
filter: objectOrStringToString(filter),
692+
sort: objectOrStringToString(sort),
693+
excludeCulture,
694+
}),
695+
}
696+
);
697+
698+
if (!response) return null;
699+
return {
700+
totalCount: response.totalCount,
701+
result: response.result,
702+
};
703+
}

src/routes.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ export const CONFIG = {
4141
TERM: {
4242
GET: (id) => `/v2/db/terms/${id}`,
4343
GET_ALL: (taxonomyName) => `/v2/db/taxonomies/${taxonomyName}/terms`,
44+
GET_CHILDREN: (taxonomyName) =>
45+
`/db/taxonomies/${taxonomyName}/terms/children`,
4446
},
4547
SYSTEM: {
4648
GET_TERMS: (taxonomyName) => `/v2/taxonomies/${taxonomyName}/terms`,

tests/db/collections.test.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {getRecords} from 'Components/db';
1+
import {getRecords, getChildrenOfTerms} from 'Components/db';
22
import config from 'Config';
33

44
require('dotenv').config();
@@ -19,4 +19,10 @@ describe('db tests', () => {
1919
const response = await getRecords(request);
2020
expect(response.totalCount).toBe(2);
2121
});
22+
23+
test('get all children of specified taxonomy', async () => {
24+
const request = {taxonomyName: 'countries'};
25+
const response = await getChildrenOfTerms(request);
26+
expect(response.totalCount).toBe(3);
27+
});
2228
});

0 commit comments

Comments
 (0)