strapi-plugin-meilisearch 0.8.2 → 0.9.2

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/README.md CHANGED
@@ -7,7 +7,7 @@
7
7
  <h4 align="center">
8
8
  <a href="https://github.com/meilisearch/meilisearch">Meilisearch</a> |
9
9
  <a href="https://docs.meilisearch.com">Documentation</a> |
10
- <a href="https://slack.meilisearch.com">Slack</a> |
10
+ <a href="https://discord.meilisearch.com">Discord</a> |
11
11
  <a href="https://roadmap.meilisearch.com/tabs/1-under-consideration">Roadmap</a> |
12
12
  <a href="https://www.meilisearch.com">Website</a> |
13
13
  <a href="https://docs.meilisearch.com/faq">FAQ</a>
@@ -530,7 +530,7 @@ If you are using [Strapi v3](https://github.com/strapi/strapi/tree/v3.6.9), plea
530
530
 
531
531
  **Supported Meilisearch versions**:
532
532
 
533
- This package only guarantees the compatibility with the [version v0.30.0 of Meilisearch](https://github.com/meilisearch/meilisearch/releases/tag/v0.30.0).
533
+ This package guarantees compatibility with [version v1.x of Meilisearch](https://github.com/meilisearch/meilisearch/releases/latest), but some features may not be present. Please check the [issues](https://github.com/meilisearch/strapi-plugin-meilisearch/issues?q=is%3Aissue+is%3Aopen+label%3A%22good+first+issue%22+label%3Aenhancement) for more info.
534
534
 
535
535
  **Node / NPM versions**:
536
536
 
@@ -73,7 +73,7 @@ export function useCollection() {
73
73
  refetchCollection()
74
74
  handleNotification({
75
75
  type: 'success',
76
- message: 'Request to delete content-type is succesfull',
76
+ message: 'Request to delete content-type is successful',
77
77
  blockTransition: false,
78
78
  })
79
79
  }
@@ -96,7 +96,7 @@ export function useCollection() {
96
96
  refetchCollection()
97
97
  handleNotification({
98
98
  type: 'success',
99
- message: 'Request to add a content-type is succesfull',
99
+ message: 'Request to add a content-type is successful',
100
100
  blockTransition: false,
101
101
  })
102
102
  }
@@ -119,7 +119,7 @@ export function useCollection() {
119
119
  refetchCollection()
120
120
  handleNotification({
121
121
  type: 'success',
122
- message: 'Request to update content-type is succesfull',
122
+ message: 'Request to update content-type is successful',
123
123
  blockTransition: false,
124
124
  })
125
125
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "strapi-plugin-meilisearch",
3
- "version": "0.8.2",
3
+ "version": "0.9.2",
4
4
  "description": "Synchronise and search in your Strapi content-types with Meilisearch",
5
5
  "scripts": {
6
6
  "playground:dev": "yarn --cwd ./playground && yarn --cwd ./playground dev",
@@ -25,7 +25,7 @@
25
25
  "README.md"
26
26
  ],
27
27
  "dependencies": {
28
- "@strapi/utils": "^4.5.2",
28
+ "@strapi/utils": "^4.5.4",
29
29
  "meilisearch": "^0.30.0"
30
30
  },
31
31
  "peerDependencies": {},
@@ -109,4 +109,63 @@ describe('Tests content types', () => {
109
109
  },
110
110
  })
111
111
  })
112
+
113
+ test('sanitizes the private fields from the entries', async () => {
114
+ const pluginMock = jest.fn(() => ({
115
+ // This rewrites only the needed methods to reach the system under test (removeSensitiveFields)
116
+ service: jest.fn().mockImplementation(() => {
117
+ return {
118
+ async actionInBatches({ contentType = 'restaurant', callback }) {
119
+ await callback({
120
+ entries: [
121
+ { id: 1, title: 'title', secret: '123' },
122
+ { id: 2, title: 'abc', secret: '234' },
123
+ ],
124
+ contentType,
125
+ })
126
+ },
127
+ getCollectionName: ({ contentType }) => contentType,
128
+ addIndexedContentType: jest.fn(),
129
+ subscribeContentType: jest.fn(),
130
+ getCredentials: () => ({}),
131
+ }
132
+ }),
133
+ }))
134
+
135
+ const service = createMeilisearchService({
136
+ strapi: {
137
+ plugin: pluginMock,
138
+ contentTypes: {
139
+ restaurant: {
140
+ attributes: {
141
+ id: { private: false },
142
+ title: { private: false },
143
+ secret: { private: true },
144
+ },
145
+ },
146
+ },
147
+ config: { get: jest.fn(() => ({ restaurant: jest.fn() })) },
148
+ },
149
+ })
150
+
151
+ await service.addContentTypeInMeiliSearch({ contentType: 'restaurant' })
152
+
153
+ expect(Meilisearch().index).toHaveBeenCalledWith('restaurant')
154
+ expect(Meilisearch().index().addDocuments).toHaveBeenNthCalledWith(
155
+ 1,
156
+ [
157
+ {
158
+ _meilisearch_id: 'restaurant-1',
159
+ id: 1,
160
+ title: 'title',
161
+ },
162
+ {
163
+ _meilisearch_id: 'restaurant-2',
164
+ id: 2,
165
+ title: 'abc',
166
+ },
167
+ ],
168
+ { primaryKey: '_meilisearch_id' }
169
+ )
170
+ })
112
171
  })
@@ -37,7 +37,7 @@ module.exports = ({ strapi }) => {
37
37
  addCollectionNamePrefix: function ({ contentType, entries }) {
38
38
  return entries.map(entry => ({
39
39
  ...entry,
40
- id: this.addCollectionNamePrefixToId({
40
+ _meilisearch_id: this.addCollectionNamePrefixToId({
41
41
  entryId: entry.id,
42
42
  contentType,
43
43
  }),
@@ -12,5 +12,5 @@ const packageJson = require('../../../package.json')
12
12
  module.exports = config =>
13
13
  new Meilisearch({
14
14
  ...config,
15
- clientAgents: [`Meilisearch Strapi ${packageJson.version}`],
15
+ clientAgents: [`Meilisearch Strapi (v${packageJson.version})`],
16
16
  })
@@ -188,10 +188,16 @@ module.exports = ({ strapi }) => {
188
188
  *
189
189
  * @return {Array<Object>} - Entries
190
190
  */
191
- removeSensitiveFields: function ({ entries }) {
191
+ removeSensitiveFields: function ({ contentType, entries }) {
192
+ // TODO: should be persisted somewhere to make it more performant
193
+ const attrs = strapi.contentTypes[contentType].attributes
194
+ const privateFields = Object.entries(attrs).map(([field, schema]) =>
195
+ schema.private ? field : false
196
+ )
197
+
192
198
  return entries.map(entry => {
193
- delete entry.createdBy
194
- delete entry.updatedBy
199
+ privateFields.forEach(attr => delete entry[attr])
200
+
195
201
  return entry
196
202
  })
197
203
  },
@@ -37,8 +37,11 @@ const sanitizeEntries = async function ({
37
37
  entries,
38
38
  })
39
39
 
40
- // Remove nested
41
- entries = await config.removeSensitiveFields({ entries })
40
+ // Remove sensitive fields (private = true)
41
+ entries = await config.removeSensitiveFields({
42
+ contentType,
43
+ entries,
44
+ })
42
45
 
43
46
  // Apply transformEntry plugin config.
44
47
  entries = await config.transformEntries({
@@ -130,7 +133,9 @@ module.exports = ({ strapi, adapter, config }) => {
130
133
  })
131
134
  )
132
135
  } else {
133
- return client.index(indexUid).updateDocuments(sanitized)
136
+ return client
137
+ .index(indexUid)
138
+ .updateDocuments(sanitized, { primaryKey: '_meilisearch_id' })
134
139
  }
135
140
  })
136
141
  },
@@ -251,7 +256,9 @@ module.exports = ({ strapi, adapter, config }) => {
251
256
  adapter,
252
257
  })
253
258
 
254
- const task = await client.index(indexUid).addDocuments(documents)
259
+ const task = await client
260
+ .index(indexUid)
261
+ .addDocuments(documents, { primaryKey: '_meilisearch_id' })
255
262
  await store.addIndexedContentType({ contentType })
256
263
 
257
264
  return task
@@ -285,7 +292,9 @@ module.exports = ({ strapi, adapter, config }) => {
285
292
  })
286
293
 
287
294
  // Add documents in Meilisearch
288
- const task = await client.index(indexUid).addDocuments(documents)
295
+ const task = await client
296
+ .index(indexUid)
297
+ .addDocuments(documents, { primaryKey: '_meilisearch_id' })
289
298
 
290
299
  return task.taskUid
291
300
  }
@@ -314,9 +323,9 @@ module.exports = ({ strapi, adapter, config }) => {
314
323
  const indexUid = config.getIndexNameOfContentType({ contentType })
315
324
 
316
325
  // Fetch contentTypes that has the same indexName as the provided contentType
317
- const contentTypesWithSameIndex = await config.listContentTypesWithCustomIndexName(
318
- { indexUid }
319
- )
326
+ const contentTypesWithSameIndex = await config
327
+ .listContentTypesWithCustomIndexName({ indexName: indexUid })
328
+ .map(contentTypeName => `api::${contentTypeName}.${contentTypeName}`)
320
329
 
321
330
  // get all contentTypes (not indexes) indexed in Meilisearch.
322
331
  const indexedContentTypes = await store.getIndexedContentTypes()
@@ -343,7 +352,7 @@ module.exports = ({ strapi, adapter, config }) => {
343
352
  }
344
353
  )
345
354
  if (indexedContentTypesWithSameIndex.length > 1) {
346
- const deleteEntries = async (entries, contentType) => {
355
+ const deleteEntries = async ({ entries, contentType }) => {
347
356
  await this.deleteEntriesFromMeiliSearch({
348
357
  contentType,
349
358
  entriesId: entries.map(entry => entry.id),