@unchainedshop/utils 2.0.0-beta6 → 2.0.0-beta8
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.
|
@@ -1,22 +1,33 @@
|
|
|
1
1
|
import { log, LogLevel } from '@unchainedshop/logger';
|
|
2
2
|
const buildIndexes = (collection, indexes) => Promise.all(indexes.map(async ({ index, options }) => {
|
|
3
|
-
|
|
3
|
+
try {
|
|
4
|
+
await collection.createIndex(index, options);
|
|
5
|
+
return false;
|
|
6
|
+
}
|
|
7
|
+
catch (e) {
|
|
8
|
+
log(e, { level: LogLevel.Error });
|
|
9
|
+
return e;
|
|
10
|
+
}
|
|
4
11
|
}));
|
|
5
12
|
export const buildDbIndexes = async (collection, indexes) => {
|
|
6
13
|
let success = true;
|
|
7
14
|
const buildErrors = (await buildIndexes(collection, indexes)).filter(Boolean);
|
|
8
15
|
if (buildErrors.length) {
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
16
|
+
// try {
|
|
17
|
+
// const dropError = await collection.dropIndexes();
|
|
18
|
+
// } catch (e) {
|
|
19
|
+
// /* */
|
|
20
|
+
// }
|
|
21
|
+
// if (!dropError) {
|
|
22
|
+
const rebuildErrors = (await buildIndexes(collection, indexes)).filter(Boolean);
|
|
23
|
+
if (rebuildErrors.length) {
|
|
24
|
+
log(`Error building some indexes for ${collection.collectionName}`, {
|
|
25
|
+
level: LogLevel.Error,
|
|
26
|
+
...rebuildErrors,
|
|
27
|
+
});
|
|
28
|
+
success = false;
|
|
19
29
|
}
|
|
30
|
+
// }
|
|
20
31
|
}
|
|
21
32
|
return success;
|
|
22
33
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"build-db-indexes.js","sourceRoot":"","sources":["../../src/db/build-db-indexes.ts"],"names":[],"mappings":"AACA,OAAO,EAA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AK,EAAE,QAAQ,CAAC,KAAK;gBACrB,GAAG,aAAa;aACjB,CAAC,CAAC;YACH,OAAO,GAAG,KAAK,CAAC;SACjB;QACD,IAAI;KACL;IAED,OAAO,OAAO,CAAC;AACjB,CAAC,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@unchainedshop/utils",
|
|
3
|
-
"version": "2.0.0-
|
|
3
|
+
"version": "2.0.0-beta8",
|
|
4
4
|
"main": "lib/utils-index.js",
|
|
5
5
|
"exports": {
|
|
6
6
|
".": "./lib/utils-index.js",
|
|
@@ -29,18 +29,18 @@
|
|
|
29
29
|
},
|
|
30
30
|
"homepage": "https://github.com/unchainedshop/unchained#readme",
|
|
31
31
|
"dependencies": {
|
|
32
|
-
"@unchainedshop/logger": "^2.0.0-
|
|
32
|
+
"@unchainedshop/logger": "^2.0.0-beta8",
|
|
33
33
|
"abort-controller": "3.0.0",
|
|
34
34
|
"bson": "^4.7.0",
|
|
35
35
|
"hashids": "^2.2.10",
|
|
36
|
-
"jest": "^29.
|
|
36
|
+
"jest": "^29.3.1",
|
|
37
37
|
"locale": "0.1.0",
|
|
38
|
-
"lru-cache": "^7.14.
|
|
38
|
+
"lru-cache": "^7.14.1",
|
|
39
39
|
"simpl-schema": "^3.0.1"
|
|
40
40
|
},
|
|
41
41
|
"devDependencies": {
|
|
42
|
-
"@types/node": "^
|
|
43
|
-
"@unchainedshop/types": "^2.0.0-
|
|
44
|
-
"typescript": "^4.
|
|
42
|
+
"@types/node": "^18.11.9",
|
|
43
|
+
"@unchainedshop/types": "^2.0.0-beta8",
|
|
44
|
+
"typescript": "^4.9.3"
|
|
45
45
|
}
|
|
46
46
|
}
|
|
@@ -1,10 +1,19 @@
|
|
|
1
1
|
import { Collection, Indexes, Document } from '@unchainedshop/types/common';
|
|
2
2
|
import { log, LogLevel } from '@unchainedshop/logger';
|
|
3
3
|
|
|
4
|
-
const buildIndexes = <T>(
|
|
4
|
+
const buildIndexes = <T>(
|
|
5
|
+
collection: Collection<T>,
|
|
6
|
+
indexes: Indexes<T>,
|
|
7
|
+
): Promise<Array<false | Error>> =>
|
|
5
8
|
Promise.all(
|
|
6
9
|
indexes.map(async ({ index, options }) => {
|
|
7
|
-
|
|
10
|
+
try {
|
|
11
|
+
await collection.createIndex(index, options);
|
|
12
|
+
return false;
|
|
13
|
+
} catch (e: any) {
|
|
14
|
+
log(e, { level: LogLevel.Error });
|
|
15
|
+
return e as Error;
|
|
16
|
+
}
|
|
8
17
|
}),
|
|
9
18
|
);
|
|
10
19
|
|
|
@@ -16,19 +25,23 @@ export const buildDbIndexes = async <T extends Document>(
|
|
|
16
25
|
const buildErrors = (await buildIndexes<T>(collection, indexes)).filter(Boolean);
|
|
17
26
|
|
|
18
27
|
if (buildErrors.length) {
|
|
19
|
-
|
|
28
|
+
// try {
|
|
29
|
+
// const dropError = await collection.dropIndexes();
|
|
30
|
+
// } catch (e) {
|
|
31
|
+
// /* */
|
|
32
|
+
// }
|
|
20
33
|
|
|
21
|
-
if (!dropError) {
|
|
22
|
-
|
|
34
|
+
// if (!dropError) {
|
|
35
|
+
const rebuildErrors = (await buildIndexes<T>(collection, indexes)).filter(Boolean);
|
|
23
36
|
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
}
|
|
37
|
+
if (rebuildErrors.length) {
|
|
38
|
+
log(`Error building some indexes for ${collection.collectionName}`, {
|
|
39
|
+
level: LogLevel.Error,
|
|
40
|
+
...rebuildErrors,
|
|
41
|
+
});
|
|
42
|
+
success = false;
|
|
31
43
|
}
|
|
44
|
+
// }
|
|
32
45
|
}
|
|
33
46
|
|
|
34
47
|
return success;
|