nodebb-plugin-dbsearch 6.2.18 → 6.2.19

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.
Files changed (2) hide show
  1. package/lib/mongo.js +43 -8
  2. package/package.json +1 -1
package/lib/mongo.js CHANGED
@@ -4,6 +4,36 @@ const nconf = require.main.require('nconf');
4
4
 
5
5
  const db = require.main.require('./src/database');
6
6
 
7
+ function generateIndexName(indexSpec) {
8
+ return Object.entries(indexSpec)
9
+ .map(([key, value]) => `${key}_${value}`)
10
+ .join('_');
11
+ }
12
+
13
+ async function safeCreateIndex(collection, indexSpec, options) {
14
+ try {
15
+ await db.client.collection(collection).createIndex(indexSpec, options);
16
+ } catch (err) {
17
+ if (err.code === 85) { // index options conflict, retry by dropping the index
18
+ await safeDropIndex(collection, generateIndexName(indexSpec));
19
+ await safeCreateIndex(collection, indexSpec, options);
20
+ return;
21
+ }
22
+ throw err;
23
+ }
24
+ }
25
+
26
+ async function safeDropIndex(collection, indexName) {
27
+ try {
28
+ await db.client.collection(collection).dropIndex(indexName);
29
+ } catch (err) {
30
+ // Ignore "index not found (27)" error
31
+ if (err.code !== 27) {
32
+ throw err;
33
+ }
34
+ }
35
+ };
36
+
7
37
  exports.createIndices = async function (language) {
8
38
  const options = { background: true };
9
39
  if (language && language !== 'en') {
@@ -11,11 +41,13 @@ exports.createIndices = async function (language) {
11
41
  }
12
42
 
13
43
  if (nconf.get('isPrimary') && !nconf.get('jobsDisabled')) {
14
- await db.client.collection('searchtopic').createIndex({ content: 'text', uid: 1, cid: 1 }, options);
15
- await db.client.collection('searchtopic').createIndex({ ts: 1 }, { background: true });
16
- await db.client.collection('searchpost').createIndex({ content: 'text', uid: 1, cid: 1 }, options);
17
- await db.client.collection('searchpost').createIndex({ ts: 1 }, { background: true });
18
- await db.client.collection('searchchat').createIndex({ content: 'text', roomId: 1, uid: 1 }, options);
44
+ await safeCreateIndex('searchtopic', { content: 'text', uid: 1, cid: 1 }, options);
45
+ await safeCreateIndex('searchtopic', { ts: 1 }, { background: true });
46
+
47
+ await safeCreateIndex('searchpost', { content: 'text', uid: 1, cid: 1 }, options);
48
+ await safeCreateIndex('searchpost', { ts: 1 }, { background: true });
49
+
50
+ await safeCreateIndex('searchchat', { content: 'text', roomId: 1, uid: 1 }, options);
19
51
  }
20
52
  };
21
53
 
@@ -25,11 +57,14 @@ exports.changeIndexLanguage = async function (language) {
25
57
  if (language !== 'en') {
26
58
  options.default_language = language;
27
59
  }
28
- await db.client.collection('searchtopic').dropIndex('content_text_uid_1_cid_1');
60
+
61
+ await safeDropIndex('searchtopic', 'content_text_uid_1_cid_1');
29
62
  await db.client.collection('searchtopic').createIndex(indexSpec, options);
30
- await db.client.collection('searchpost').dropIndex('content_text_uid_1_cid_1');
63
+
64
+ await safeDropIndex('searchpost', 'content_text_uid_1_cid_1');
31
65
  await db.client.collection('searchpost').createIndex(indexSpec, options);
32
- await db.client.collection('searchchat').dropIndex('content_text_roomId_1_uid_1');
66
+
67
+ await safeDropIndex('searchchat', 'content_text_roomId_1_uid_1');
33
68
  await db.client.collection('searchchat').createIndex({ content: 'text', roomId: 1, uid: 1 }, options);
34
69
  };
35
70
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nodebb-plugin-dbsearch",
3
- "version": "6.2.18",
3
+ "version": "6.2.19",
4
4
  "description": "A Plugin that lets users search posts and topics",
5
5
  "main": "index.js",
6
6
  "repository": {