-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
156 lines (139 loc) · 4.05 KB
/
app.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
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
const { MongoClient } = require("mongodb");
const { bootstrap, parsers } = require("@kaholo/plugin-library");
const helpers = require("./helpers");
async function findMany(params) {
const {
uri,
username,
password,
database,
collection,
query,
} = params;
const newUri = await helpers.consolidateUri(uri, username, password);
const client = new MongoClient(newUri);
try {
const databased = client.db(database);
const collected = databased.collection(collection);
const parsedQuery = await parsers.object(query);
const documents = await collected.find(parsedQuery).toArray();
// you can reach this point even if database and collection do not exist.
if (documents && documents.length > 0) {
return documents;
}
throw new Error("Matching document(s) not found.");
} catch (error) {
throw new Error(error.message);
}
}
async function insertMany(params) {
const {
uri,
username,
password,
database,
collection,
documents,
} = params;
const newUri = await helpers.consolidateUri(uri, username, password);
const client = new MongoClient(newUri);
try {
const databased = client.db(database);
const collected = databased.collection(collection);
const docJsonArray = parsers.object(documents);
const inserted = await collected.insertMany(docJsonArray);
// you can reach this point even if database and collection do not exist.
if (inserted && inserted.insertedCount > 0) {
return `${inserted.insertedCount} document(s) successfully inserted.`;
}
throw new Error("No documents inserted.");
} catch (error) {
throw new Error(error.message);
}
}
async function deleteMany(params) {
const {
uri,
username,
password,
database,
collection,
query,
} = params;
const newUri = await helpers.consolidateUri(uri, username, password);
const client = new MongoClient(newUri);
try {
const databased = client.db(database);
const collected = databased.collection(collection);
const parsedQuery = await parsers.object(query);
const deleted = await collected.deleteMany(parsedQuery);
// you can reach this point even if database and collection do not exist.
if (deleted && deleted.deletedCount > 0) {
return `${deleted.deletedCount} document(s) successfully deleted.`;
}
throw new Error("No matching document(s) were found or deleted.");
} catch (error) {
throw new Error(error.message);
}
}
async function updateMany(params) {
const {
uri,
username,
password,
database,
collection,
filter,
document,
} = params;
const newUri = await helpers.consolidateUri(uri, username, password);
const client = new MongoClient(newUri);
try {
const databased = client.db(database);
const collected = databased.collection(collection);
const parsedFilter = await parsers.object(filter);
const parsedDocument = await parsers.object(document);
const modified = await collected.updateMany(parsedFilter, parsedDocument);
// you can reach this point even if database and collection do not exist.
if (modified && modified.modifiedCount > 0) {
return `${modified.modifiedCount} document(s) successfully updated.`;
}
throw new Error("No matching document(s) were found or updated.");
} catch (error) {
throw new Error(error.message);
}
}
async function dumpDatabase(params) {
const {
uri,
username,
password,
database,
archivePath,
addParams,
} = params;
const {
args,
envVars,
} = await helpers.parseConnectionStringToShellArguments(uri, username, password);
if (database) {
args.push("--db", database);
}
if (archivePath) {
args.push(`--archive=${archivePath}`, "--gzip");
}
if (addParams) {
args.push(addParams);
}
await helpers.mongodumpInstalled();
const command = `mongodump ${args.join(" ")}`;
const result = helpers.runCommand(command, envVars);
return result;
}
module.exports = bootstrap({
findMany,
insertMany,
deleteMany,
updateMany,
dumpDatabase,
}, {});