-
Notifications
You must be signed in to change notification settings - Fork 0
/
delSensorbyDate.js
59 lines (53 loc) · 1.18 KB
/
delSensorbyDate.js
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
// usage node delSensorbyDate.js 98
const config = require("./config.json");
const nano = require("nano")(config.writer);
const weatherdb = nano.use("weatherdb");
const bulk = {
docs: [],
};
var sensor, force;
force = false;
if (process.argv.length > 2) {
if (process.argv.length > 3) {
force = process.argv[3] == "-force";
sensor = process.argv[2];
} else {
if (process.argv[2] == "-force") {
force = true;
} else {
sensor = process.argv[2];
}
}
}
function bulkDelete() {
weatherdb.bulk(bulk, function (err, body) {
console.log(err, body);
});
}
function processRow(row) {
if (typeof sensor != "string" || row.doc.sensorid == sensor) {
console.log(row.doc);
bulk.docs.push({
_id: row.doc._id,
_rev: row.doc._rev,
_deleted: true,
});
}
}
weatherdb
.list({
start_key: "2021-12-11T17:50:00.000Z",
include_docs: true,
})
.then((body) => {
console.log("number of rows:", body.total_rows);
body.rows.forEach(processRow);
console.log("rows to delete:", bulk.docs.length);
if (force) {
console.log("starting delete");
bulkDelete();
} else {
console.log("use: -force to delete records");
}
})
.catch((err) => console.log("err:", err.reason));