-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy path20260114145500-encode-sample-metadatakeys.js
More file actions
84 lines (71 loc) · 2.23 KB
/
Copy path20260114145500-encode-sample-metadatakeys.js
File metadata and controls
84 lines (71 loc) · 2.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/* eslint-disable @typescript-eslint/no-require-imports */
const {
encodeScientificMetadataKeys,
decodeScientificMetadataKeys,
} = require("../dist/common/utils");
module.exports = {
async up(db, client) {
const bulkOps = [];
for await (const sample of db
.collection("Sample")
.find({ sampleCharacteristics: { $exists: true } })) {
const metadata = sample.sampleCharacteristics;
if (!metadata || typeof metadata !== "object") continue;
let encodedMetadata;
try {
encodedMetadata = encodeScientificMetadataKeys(metadata);
} catch (err) {
console.error(
`Error encoding sampleCharacteristics for Sample (Id: ${sample._id}):`,
err,
);
continue;
}
console.log(
`Updating Sample (Id: ${sample._id}) with encoded sampleCharacteristics keys`,
);
bulkOps.push({
updateOne: {
filter: { _id: sample._id },
update: { $set: { sampleCharacteristics: encodedMetadata } },
},
});
}
if (bulkOps.length > 0) {
console.log(`Executing bulk update for ${bulkOps.length} samples`);
await db.collection("Sample").bulkWrite(bulkOps);
}
},
async down(db, client) {
const bulkOps = [];
for await (const sample of db
.collection("Sample")
.find({ sampleCharacteristics: { $exists: true } })) {
const metadata = sample.sampleCharacteristics;
if (!metadata || typeof metadata !== "object") continue;
let decodedMetadata;
try {
decodedMetadata = decodeScientificMetadataKeys(metadata);
} catch (err) {
console.error(
`Error decoding sampleCharacteristics for Sample (Id: ${sample._id}):`,
err,
);
continue;
}
console.log(
`Reverting Sample (Id: ${sample._id}) to decoded sampleCharacteristics keys`,
);
bulkOps.push({
updateOne: {
filter: { _id: sample._id },
update: { $set: { sampleCharacteristics: decodedMetadata } },
},
});
}
if (bulkOps.length > 0) {
console.log(`Executing bulk revert for ${bulkOps.length} samples`);
await db.collection("Sample").bulkWrite(bulkOps);
}
},
};