Skip to content

Commit

Permalink
feat(index): transform null to boolean for some fields
Browse files Browse the repository at this point in the history
  • Loading branch information
Mickaël Depardon committed Apr 30, 2024
1 parent c333128 commit 9fb7e6a
Show file tree
Hide file tree
Showing 11 changed files with 162 additions and 69 deletions.
121 changes: 68 additions & 53 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 4 additions & 3 deletions package.json
Expand Up @@ -49,6 +49,7 @@
"@strapi/strapi": "^4.24.0",
"@strapi/utils": "^4.24.0",
"algoliasearch": "4.23.3",
"deep-reduce": "^1.0.5",
"prop-types": "^15.8.1"
},
"devDependencies": {
Expand All @@ -60,8 +61,8 @@
"@types/react-dom": "^18.0.28",
"@types/react-router-dom": "^5.3.3",
"@types/styled-components": "^5.1.34",
"@typescript-eslint/eslint-plugin": "^7.7.1",
"@typescript-eslint/parser": "^7.7.1",
"@typescript-eslint/eslint-plugin": "^7.8.0",
"@typescript-eslint/parser": "^7.8.0",
"commitlint": "^19.3.0",
"eslint": "^8.57.0",
"eslint-config": "^0.3.0",
Expand All @@ -71,7 +72,7 @@
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-router-dom": "^5.3.4",
"release-it": "^17.2.0",
"release-it": "^17.2.1",
"styled-components": "^5.3.6",
"ts-jest": "^29.1.2",
"typescript": "5.4.5"
Expand Down
4 changes: 3 additions & 1 deletion server/controllers/index-all.ts
Expand Up @@ -56,6 +56,7 @@ export default ({ strapi }: { strapi: Strapi }) => ({
idPrefix = '',
populate = '*',
hideFields = [],
transformToBooleanFields = [],
} = contentType;

const indexName = `${indexPrefix}${index ?? name}`;
Expand Down Expand Up @@ -85,7 +86,8 @@ export default ({ strapi }: { strapi: Strapi }) => ({
await strapiService.afterUpdateAndCreateAlreadyPopulate(
articles,
idPrefix,
algoliaIndex
algoliaIndex,
transformToBooleanFields
);

return ctx.send({
Expand Down
11 changes: 8 additions & 3 deletions server/services/algolia.ts
@@ -1,5 +1,6 @@
import { Strapi } from '@strapi/strapi';
import { SearchIndex } from 'algoliasearch';
import { transformNullToBoolean } from '../../utils/utils';

// eslint-disable-next-line @typescript-eslint/no-unused-vars
export default ({ strapi }: { strapi: Strapi }) => ({
Expand All @@ -13,7 +14,8 @@ export default ({ strapi }: { strapi: Strapi }) => ({
createOrDeleteObjects: async (
objectsToSave: any[],
objectsIdsToDelete: string[],
algoliaIndex: SearchIndex
algoliaIndex: SearchIndex,
transformToBooleanFields: string[] = []
) => {
const strapiAlgolia = strapi.plugin('strapi-algolia');
const utilsService = strapiAlgolia.service('utils');
Expand All @@ -29,11 +31,14 @@ export default ({ strapi }: { strapi: Strapi }) => ({
}

if (objectsToSave.length) {
const chunkedObjectsToSave =
const chunkedObjectsToSave: any[][] =
utilsService.getChunksRequests(objectsToSave);

for (const chunk of chunkedObjectsToSave) {
await algoliaIndex.saveObjects(chunk);
const cleanedChunk = chunk.map((c) =>
transformNullToBoolean(c, transformToBooleanFields)
);
await algoliaIndex.saveObjects(cleanedChunk);
}
}
},
Expand Down
3 changes: 3 additions & 0 deletions server/services/lifecycles.ts
Expand Up @@ -32,6 +32,7 @@ export default ({ strapi }: { strapi: Strapi }) => ({
idPrefix = '',
populate = '*',
hideFields = [],
transformToBooleanFields = [],
} = contentType;

if (strapi.contentTypes[name]) {
Expand All @@ -45,6 +46,7 @@ export default ({ strapi }: { strapi: Strapi }) => ({
[event],
populate,
hideFields,
transformToBooleanFields,
idPrefix,
algoliaIndex
);
Expand All @@ -54,6 +56,7 @@ export default ({ strapi }: { strapi: Strapi }) => ({
[event],
populate,
hideFields,
transformToBooleanFields,
idPrefix,
algoliaIndex
);
Expand Down
10 changes: 7 additions & 3 deletions server/services/strapi.ts
Expand Up @@ -38,6 +38,7 @@ export default ({ strapi }: { strapi: Strapi }) => ({
_events: any[],
populate: any,
hideFields: string[],
transformToBooleanFields: string[],
idPrefix: string,
algoliaIndex: SearchIndex
) => {
Expand Down Expand Up @@ -84,13 +85,15 @@ export default ({ strapi }: { strapi: Strapi }) => ({
await algoliaService.createOrDeleteObjects(
objectsToSave,
objectsIdsToDelete,
algoliaIndex
algoliaIndex,
transformToBooleanFields
);
},
afterUpdateAndCreateAlreadyPopulate: async (
articles: any[],
idPrefix: string,
algoliaIndex: SearchIndex
algoliaIndex: SearchIndex,
transformToBooleanFields: string[] = []
) => {
const strapiAlgolia = strapi.plugin('strapi-algolia');
const algoliaService = strapiAlgolia.service('algolia');
Expand Down Expand Up @@ -126,7 +129,8 @@ export default ({ strapi }: { strapi: Strapi }) => ({
await algoliaService.createOrDeleteObjects(
objectsToSave,
objectsIdsToDelete,
algoliaIndex
algoliaIndex,
transformToBooleanFields
);
},
afterDeleteOneOrMany: async (
Expand Down

0 comments on commit 9fb7e6a

Please sign in to comment.