-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy pathdb.js
52 lines (44 loc) · 1.63 KB
/
db.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
const { MongoClient } = require('mongodb')
const settings = require('../config')
const api = {
init
}
async function init () {
const collections = ['flows', 'nodes', 'users', 'tags', 'events', 'ratings', 'categories']
const client = new MongoClient(settings.mongo.url)
await client.connect()
const db = client.db()
await db.command({ ping: 1 })
api.close = async function () {
return client.close()
}
collections.forEach(col => {
api[col] = db[col] = db.collection(col)
})
await db.flows.createIndex({ updated_at: -1 })
await db.flows.createIndex({ keywords: 1 })
await db.flows.createIndex({ 'maintainers.name': 1 })
await db.flows.createIndex({ npmOwners: 1 })
await db.flows.createIndex({ gitOwners: 1 })
await db.flows.createIndex({ 'rating.score': -1, 'rating.count': -1 })
await db.flows.createIndex({ 'downloads.week': -1 })
await db.ratings.createIndex({ module: 1 })
await db.ratings.createIndex({ user: 1, module: 1 })
}
// if (process.env.FLOW_ENV !== "PRODUCTION") {
// collections.forEach(col => {
// var collection = db[col];
// for (var x in collection) {
// if (typeof collection[x] === 'function' && !/^_/.test(x)) {
// db[col]["__"+x] = db[col][x];
// let origFunc = db[col][x];
// let signature = col+"."+x;
// db[col][x] = function() {
// console.log(" ",signature);//arguments[0]);
// return origFunc.apply(db[col],arguments);
// }
// }
// }
// })
// }
module.exports = api