-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
175 lines (141 loc) · 5.65 KB
/
index.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
// setup all configs
require('dotenv').config();
const { exec } = require("child_process");
const { MongoClient } = require('mongodb');
const app = require('express')();
const port = process.env.PORT;
let entrypoint = process.env.ENTRYPOINT;
const host = process.env.HOST;
if (entrypoint[entrypoint.length - 1] == '/')
entrypoint = entrypoint.substring(0, entrypoint.length - 1);
// list db
// list col
/*
(async () => {
const client = new MongoClient(`mongodb://apiAdmin:admin@${host}/`);
await client.connect();
console.log(JSON.stringify(await client.db('test').collection('orders').aggregate([
{ $lookup:
{
from: 'products',
localField: 'product_id',
foreignField: '_id',
as: 'orderdetails'
}
}
]).toArray()));
})()
*/
app.post(`${entrypoint}/exec`, async (req, res) => {
try {
if (req.headers['x-token'] == null)
res.send('Error: Missing headers');
else {
const token = req.headers['x-token'];
const [usr, pwd] = token.split('.');
// url adds on
let db = req.headers['x-database'] || '';
let options = req.headers['x-options'] || '';
if (options != '')
options = `?${options}`;
const client = new MongoClient(`mongodb://${usr}:${pwd}@${host}/${db}${options}`);
await client.connect();
// run the command
if (req.headers['x-command'] == null)
res.send('Error: Missing command')
else
exec(`mongosh --eval "printjson(${req.headers['x-command']})"`, (error, stdout, stderr) => {
if (error)
res.send(`Error: Mongosh says, ${error}`)
else if (stderr)
res.send(`Error: Mongosh says, ${stderr}`)
else
res.send(stdout.split('\n').splice(24).join('\n'))
});
}
} catch(e) {
console.log(e)
res.send('Error: Something went wrong..');
}
})
app.get(`${entrypoint}/api/:database/:collection`, async (req, res) => {
try {
if (req.headers['x-token'] == null)
res.send('Error: Missing headers');
else {
const token = req.headers['x-token'];
const [usr, pwd] = token.split('.');
// url adds on
let db = req.headers['x-database'] || '';
let options = req.headers['x-options'] || '';
if (options != '')
options = `?${options}`;
const client = new MongoClient(`mongodb://${usr}:${pwd}@${host}/${db}${options}`);
await client.connect();
// run the query
let query = JSON.parse(req.headers['x-query']) || {};
let query_options = JSON.parse(req.headers['x-query-options']) || {};
let sort = JSON.parse(req.headers['x-query-sort']) || {};
let limit = JSON.parse(req.headers['x-query-limit']) || {};
res.send(JSON.stringify(client.db(req.params.database).collection(req.params.collection).find(query, query_options).sort(sort).limit(limit).toArray()));
}
} catch {
res.send('Error: Something went wrong..');
}
})
app.post(`${entrypoint}/api/:database/:collection`, async (req, res) => {
try {
if (req.headers['x-token'] == null)
res.send('Error: Missing headers');
else {
const token = req.headers['x-token'];
const [usr, pwd] = token.split('.');
// url adds on
let db = req.headers['x-database'] || '';
let options = req.headers['x-options'] || '';
if (options != '')
options = `?${options}`;
const client = new MongoClient(`mongodb://${usr}:${pwd}@${host}/${db}${options}`);
await client.connect();
// insert data
let data = JSON.parse(req.headers['x-data']) || {};
if (data.length != null)
client.db(req.params.database).collection(req.params.collection).insertMany(data)
else
client.db(req.params.database).collection(req.params.collection).insertOne(data)
res.send('Successfully added the data!');
}
} catch {
res.send('Error: Something went wrong..');
}
})
app.delete(`${entrypoint}/api/:database/:collection`, async (req, res) => {
try {
if (req.headers['x-token'] == null)
res.send('Error: Missing headers');
else {
const token = req.headers['x-token'];
const [usr, pwd] = token.split('.');
// url adds on
let db = req.headers['x-database'] || '';
let options = req.headers['x-options'] || '';
if (options != '')
options = `?${options}`;
const client = new MongoClient(`mongodb://${usr}:${pwd}@${host}/${db}${options}`);
await client.connect();
// delete data
let query = JSON.parse(req.headers['x-query']) || {}
let type = JSON.parse(req.headers['x-query-type']) || 'single'
if (type == 'single')
client.db(req.params.database).collection(req.params.collection).deleteOne(query)
else
client.db(req.params.database).collection(req.params.collection).deleteMany(query)
res.send('Successfully deleted the data!');
}
} catch {
res.send('Error: Something went wrong..');
}
})
app.listen(port, () => {
console.log(`🥭 Mongo API listening on port ${port}`);
})