-
Notifications
You must be signed in to change notification settings - Fork 10
/
mongoDB.ts
59 lines (48 loc) · 1.49 KB
/
mongoDB.ts
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
import { Db, MongoClient} from 'mongodb'
let db: Db
let mongoClientObj: any;
const open = async (): Promise<boolean> => {
let uri = "mongodb://localhost:27017"
if (process.env.MONGODB_URI) {
uri = process.env.MONGODB_URI
}
console.info("Connecting to MongoDB at " + uri)
const status = await MongoClient.connect(uri, {
}).then(async (client) => {
mongoClientObj = client
console.info("MongoDB connected successfully!")
return true
}).catch((error) => {
console.error(error)
console.error("Error connecting to MongoDB")
return false
})
return status
}
const getDB = () => {
return db
}
const getMongoClientObj = (): MongoClient => {
return mongoClientObj
}
const getCollections = (serverID?: any) => {
if (process.env.STOCKPILER_MULTI_SERVER && process.env.STOCKPILER_MULTI_SERVER === "true") {
const db = mongoClientObj.db('stockpiler-' + serverID)
const collections = {
stockpiles: db.collection('stockpiles'),
targets: db.collection('targets'),
config: db.collection('config')
}
return collections
}
else {
const db = mongoClientObj.db('stockpiler')
const collections = {
stockpiles: db.collection('stockpiles'),
targets: db.collection('targets'),
config: db.collection('config')
}
return collections
}
}
export { open, getDB, getCollections, getMongoClientObj }