@vegan-friendly/strapi-plugin-elasticsearch 0.2.7 → 0.2.8
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/dist/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "@vegan-friendly/strapi-plugin-elasticsearch",
|
3
|
-
"version": "0.2.
|
3
|
+
"version": "0.2.8",
|
4
4
|
"description": "A Strapi plugin to enable using Elasticsearch with Strapi CMS.",
|
5
5
|
"homepage": "https://github.com/vegan-friendly/strapi-plugin-elasticsearch",
|
6
6
|
"strapi": {
|
package/dist/server/bootstrap.js
CHANGED
@@ -25,7 +25,12 @@ exports.default = async ({ strapi }) => {
|
|
25
25
|
strapi.cron.add({
|
26
26
|
elasticsearchIndexing: {
|
27
27
|
task: async ({ strapi }) => {
|
28
|
-
|
28
|
+
try {
|
29
|
+
await indexer.indexPendingData();
|
30
|
+
}
|
31
|
+
catch (err) {
|
32
|
+
strapi.log.error('Error while indexing data: ', err);
|
33
|
+
}
|
29
34
|
},
|
30
35
|
options: {
|
31
36
|
rule: pluginConfig['indexingCronSchedule'],
|
@@ -119,6 +124,15 @@ exports.default = async ({ strapi }) => {
|
|
119
124
|
const registry = strapi.service('plugin::elasticsearch.virtualCollectionsRegistry');
|
120
125
|
// Setup lifecycle hooks
|
121
126
|
const virtualCollections = registry.getAll();
|
127
|
+
// Check if indices exists, if not create them
|
128
|
+
virtualCollections.forEach(async (collection) => {
|
129
|
+
const indexName = await helper.getCurrentIndexName(collection.indexAlias);
|
130
|
+
const indexExists = await esInterface.listIndicesByPattern(indexName);
|
131
|
+
if (!indexExists.includes(indexName)) {
|
132
|
+
await esInterface.createIndex(indexName, collection.mappings);
|
133
|
+
strapi.log.info(`Created Elasticsearch index: ${indexName}`);
|
134
|
+
}
|
135
|
+
});
|
122
136
|
// Create a set of all collections that need hooks
|
123
137
|
const collectionsToHook = new Set();
|
124
138
|
virtualCollections.forEach((collection) => {
|
@@ -156,10 +156,18 @@ exports.default = ({ strapi }) => ({
|
|
156
156
|
}
|
157
157
|
},
|
158
158
|
async listIndicesByPattern(pattern = 'restaurants*') {
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
159
|
+
try {
|
160
|
+
const results = await client.cat.indices({
|
161
|
+
index: pattern,
|
162
|
+
format: 'json',
|
163
|
+
});
|
164
|
+
return results.map((index) => index.index).filter((index) => index != null);
|
165
|
+
}
|
166
|
+
catch (err) {
|
167
|
+
if (err?.message?.includes('index_not_found_exception')) {
|
168
|
+
return [];
|
169
|
+
}
|
170
|
+
throw err;
|
171
|
+
}
|
164
172
|
},
|
165
173
|
});
|
@@ -36,16 +36,17 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
36
36
|
const yup = __importStar(require("yup"));
|
37
37
|
const isFunction = () => yup.mixed().test('is-function', `must be a function`, (value) => typeof value === 'function');
|
38
38
|
const configSchema = yup.object({
|
39
|
-
indexAlias: yup.string().
|
39
|
+
indexAlias: yup.string().notRequired().nonNullable(),
|
40
40
|
collectionName: yup.string().required(),
|
41
41
|
extractData: isFunction().required(),
|
42
42
|
extractByIds: isFunction().required(),
|
43
43
|
getIndexItemId: isFunction(),
|
44
44
|
triggers: yup
|
45
45
|
.array()
|
46
|
+
.optional()
|
46
47
|
.of(yup.object({
|
47
48
|
collection: yup.string().required(),
|
48
|
-
getIdsToReindex: isFunction(),
|
49
|
+
getIdsToReindex: isFunction().required(),
|
49
50
|
alsoTriggerDelete: yup.boolean().default(false),
|
50
51
|
}))
|
51
52
|
.default([]),
|
@@ -69,9 +70,10 @@ exports.default = ({ strapi }) => {
|
|
69
70
|
if (!config) {
|
70
71
|
const helper = strapi.plugin('elasticsearch').service('helper');
|
71
72
|
const defaultConf = configSchema.getDefault();
|
72
|
-
|
73
|
-
config =
|
74
|
-
|
73
|
+
const virtualCollectionsFactories = strapi.plugin('elasticsearch').config('virtualCollections') || [];
|
74
|
+
config = virtualCollectionsFactories.map((factory) => {
|
75
|
+
let collectionConfig = factory(strapi);
|
76
|
+
collectionConfig = configSchema.validateSync(collectionConfig, { stripUnknown: true });
|
75
77
|
collectionConfig.getIndexItemId = collectionConfig.getIndexItemId || ((id) => helper.getIndexItemId({ collectionName: collectionConfig.collectionName, itemId: id }));
|
76
78
|
return { ...defaultConf, ...collectionConfig };
|
77
79
|
});
|
@@ -1,4 +1,9 @@
|
|
1
1
|
import { MappingTypeMapping } from '@elastic/elasticsearch/lib/api/types';
|
2
|
+
export type VirtualCollectionFactory = (strapi: any) => VirtualCollectionConfig;
|
3
|
+
export type ExtractByIdsFunction = (ids: number[]) => Promise<StrapiEntity[]>;
|
4
|
+
export type ExtractDataFunction = (page: number, pageSize?: number) => Promise<StrapiEntity[]>;
|
5
|
+
export type GetIndexItemIdFunction = (itemId: number, collectionName: string) => string;
|
6
|
+
export type GetIdsToIndexFunction = (event: any) => Promise<number[]>;
|
2
7
|
export type VirtualCollectionConfig = {
|
3
8
|
/**
|
4
9
|
* Optional -
|
@@ -17,8 +22,8 @@ export type VirtualCollectionConfig = {
|
|
17
22
|
* e.g 'api::restaurants.restaurants'.
|
18
23
|
*/
|
19
24
|
collectionName: string;
|
20
|
-
extractData:
|
21
|
-
extractByIds:
|
25
|
+
extractData: ExtractDataFunction;
|
26
|
+
extractByIds: ExtractByIdsFunction;
|
22
27
|
/**
|
23
28
|
* Optional -
|
24
29
|
* A function that takes an item and returns the id of the item to be used in the index.
|
@@ -28,7 +33,7 @@ export type VirtualCollectionConfig = {
|
|
28
33
|
* @param collectionName collection name. you probably want to use this to create a unique id for the item, especially if it is saved to the default index.
|
29
34
|
* @returns the id of the item to be used in the index, _id. must be unique accross the index.
|
30
35
|
*/
|
31
|
-
getIndexItemId?:
|
36
|
+
getIndexItemId?: GetIndexItemIdFunction;
|
32
37
|
triggers: Array<{
|
33
38
|
/**
|
34
39
|
* collection name to listen to for changes.
|
@@ -39,7 +44,7 @@ export type VirtualCollectionConfig = {
|
|
39
44
|
* @param event - The event object containing the data to be indexed.
|
40
45
|
* @returns ids of the items to be reindexed.
|
41
46
|
*/
|
42
|
-
getIdsToReindex:
|
47
|
+
getIdsToReindex: GetIdsToIndexFunction;
|
43
48
|
/**
|
44
49
|
* if true, and the trigger is a delete event, the item of the virtual collection will be deleted as well if the id returned from getIdsToReindex match.
|
45
50
|
* defaults to false.
|
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "@vegan-friendly/strapi-plugin-elasticsearch",
|
3
|
-
"version": "0.2.
|
3
|
+
"version": "0.2.8",
|
4
4
|
"description": "A Strapi plugin to enable using Elasticsearch with Strapi CMS.",
|
5
5
|
"homepage": "https://github.com/vegan-friendly/strapi-plugin-elasticsearch",
|
6
6
|
"strapi": {
|