-
Notifications
You must be signed in to change notification settings - Fork 130
/
Copy pathindex.js
executable file
·141 lines (121 loc) · 4.53 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
#!/usr/bin/env node
"use strict"
const opn = require('opn')
let log = console.log
const express = require('express')
const bodyParser = require('body-parser')
const compression = require('compression')
const expressHandlebars = require('express-handlebars')
const errorHandler = require('errorhandler')
const cors = require('cors')
const favicon = require('serve-favicon')
const path = require('path')
let mongoDb = require('mongodb')
let mongoskin = require('mongoskin')
let OId = require('mongoskin').ObjectId
let config = require('./config')
const port = config.api.port
let dbHostName, dbPortNumber, dbName
dbHostName = config.database.host
dbPortNumber = config.database.port
dbName = config.database.name
var app = express()
app.use(favicon(path.join(__dirname, 'public', 'img', 'favicons', 'favicon.ico')))
app.use(errorHandler())
app.use(cors({credential: false}))
app.use(bodyParser.json())
app.use(express.static(path.join(__dirname,'public')))
app.use(compression())
app.get('/api/dbs', function(req, res) {
if (!req.admin) req.admin = mongoskin.db(`mongodb://${dbHostName}:${dbPortNumber}/${dbName}`).admin()
req.admin.listDatabases(function(error, dbs) {
res.json(dbs)
})
})
app.param('dbName', function(req, res, next, dbName){
var db = mongoskin.db(`mongodb://${dbHostName}:${dbPortNumber}/${dbName}`)
req.db = db
req.admin = db.admin()
return next()
})
app.param('collectionName', function(req, res, next, collectionName){
req.collection = req.db.collection(collectionName)
return next()
})
app.get('/api/dbs/:dbName/collections', function(req, res, next) {
req.db.collections(function(e, names) {
if (!names) next(new Error('No collections'))
let collections = names.map((collection)=>{
log(collection.s.name)
return {name: collection.s.name}
})
res.json({collections: collections})
})
})
app.get('/api/dbs/:dbName/collections/:collectionName', function(req, res, next) {
let query = {}
try {
query = JSON.parse(req.query.query)
//recognize and convert any regex queries from strings into regex objects
for (var prop in query){
if ((query[prop][0] == "R" && query[prop][1] == "/") //arbitrary letter 'R' used by this app
&& (query[prop].length > 3) //avoids a few corner cases
&& ((query[prop][(query[prop].length - 1) ] == "/" ) || (query[prop][(query[prop].length - 2)] == "/") || (query[prop][query[prop].length - 3 ] == "/" )|| (query[prop][query[prop].length - 4 ] == "/" ))
){
var splitRegex = query[prop].split("/")
var makeRegex = new RegExp( splitRegex[1], splitRegex[2])
query[prop] = makeRegex
}
}
} catch (error) {
console.log('Invalid query, cannot parse it.')
query = {} // fail more gracefully.
// return next(new Error('Invalid query, cannot parse it'))
}
if (query._id) {
if (query._id['$in'] && Array.isArray(query._id.$in)) {
query._id.$in = query._id.$in.map((id)=>{
return OId(id)
})
} else query._id = OId(query._id)
}
req.collection.find(query || {}, {limit: req.query.limit || 20}).toArray(function(e, docs) {
console.log('boo', docs, query)
res.json({docs: docs})
})
})
app.post('/api/dbs/:dbName/collections/:collectionName', function(req, res) {
delete req.body._id
req.collection.insert(req.body, function(e, results) {
// console.log('boo', e, results)
res.json(results)
})
})
app.delete('/api/dbs/:dbName/collections/:collectionName/:id', function(req, res) {
if (req.body._id && req.body._id != req.params.id) return res.status(400).json({error: 'ID in the body is not matching ID in the URL'})
delete req.body._id
req.collection.remove({ _id: mongoDb.ObjectId(req.params.id)}, function(e, results) {
res.json(results)
})
})
app.patch('/api/dbs/:dbName/collections/:collectionName/:id', function(req, res) {
if (req.body._id && req.body._id != req.params.id) return res.status(400).json({error: 'ID in the body is not matching ID in the URL'})
delete req.body._id
req.collection.updateById(req.params.id, {$set: req.body}, function(e, results) {
// console.log('boo', e, results)
res.json(results)
})
})
if (require.main === module) {
app.listen(port, function(){
if (process.env.NODE_ENV && process.env.NODE_ENV=='dev') {
console.log('Mongoui API is listening on: %s', config.api.port)
} else {
console.log('Mongoui web app is listening on: %s', config.api.port)
// Opens the url in the default browser
opn(`http://localhost:${config.api.port}`)
}
})
} else {
module.exports = app
}