adapt-authoring-tags 1.3.4 → 1.3.5

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.
@@ -0,0 +1,45 @@
1
+ import { ObjectId } from 'mongodb'
2
+
3
+ const SKIP_COLLECTIONS = new Set(['migrations'])
4
+
5
+ export default function (migration) {
6
+ migration.describe('Normalise tag references to ObjectId form across all collections')
7
+ migration.runCommand(normaliseTags)
8
+ }
9
+
10
+ async function normaliseTags (db, log) {
11
+ const collections = await db.listCollections({}, { nameOnly: true }).toArray()
12
+ for (const { name } of collections) {
13
+ if (SKIP_COLLECTIONS.has(name) || name.startsWith('system.')) continue
14
+ await normaliseCollection(db.collection(name), log)
15
+ }
16
+ }
17
+
18
+ async function normaliseCollection (collection, log) {
19
+ const match = { tags: { $elemMatch: { $type: 'string' } } }
20
+ const docs = await collection.find(match, { projection: { _id: 1, tags: 1 } }).toArray()
21
+ if (!docs.length) return
22
+
23
+ const ops = []
24
+ let unconvertible = 0
25
+ for (const doc of docs) {
26
+ const next = []
27
+ for (const t of doc.tags) {
28
+ if (t instanceof ObjectId) {
29
+ next.push(t)
30
+ } else if (typeof t === 'string' && ObjectId.isValid(t)) {
31
+ next.push(new ObjectId(t))
32
+ } else {
33
+ next.push(t)
34
+ unconvertible++
35
+ }
36
+ }
37
+ ops.push({ updateOne: { filter: { _id: doc._id }, update: { $set: { tags: next } } } })
38
+ }
39
+
40
+ for (let i = 0; i < ops.length; i += 500) {
41
+ await collection.bulkWrite(ops.slice(i, i + 500), { ordered: false })
42
+ }
43
+ log('info', 'migrations', `${collection.collectionName}: normalised tag entries on ${docs.length} document(s)`)
44
+ if (unconvertible) log('warn', 'migrations', `${collection.collectionName}: ${unconvertible} tag entr(ies) could not be coerced to ObjectId and were left as-is`)
45
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "adapt-authoring-tags",
3
- "version": "1.3.4",
3
+ "version": "1.3.5",
4
4
  "description": "Module for managing tags",
5
5
  "homepage": "https://github.com/adapt-security/adapt-authoring-tags",
6
6
  "license": "GPL-3.0",