2022-02-08 21:22:42 +00:00
|
|
|
const { MongoClient } = require('mongodb')
|
|
|
|
|
|
|
|
global.mconn = null
|
|
|
|
global.mdb = null
|
|
|
|
module.exports = {
|
|
|
|
conn: () => {
|
|
|
|
if (mconn !== null)
|
|
|
|
return mconn
|
|
|
|
const dbhost = process.env.DB_HOST || 'db'
|
2022-02-08 22:25:55 +00:00
|
|
|
mconn = new MongoClient(`mongodb://${dbhost}:27017`, {
|
2022-02-08 21:22:42 +00:00
|
|
|
useUnifiedTopology: true,
|
|
|
|
useNewUrlParser: true
|
|
|
|
})
|
|
|
|
return mconn
|
|
|
|
},
|
|
|
|
get: () => {
|
2022-02-08 22:25:55 +00:00
|
|
|
mdb = mconn.db(process.env.DB_NAME || 'fedilove')
|
2022-02-08 21:22:42 +00:00
|
|
|
return mdb
|
|
|
|
},
|
|
|
|
table: {
|
2022-02-13 01:02:29 +00:00
|
|
|
objects: () => { return mdb.collection('objects') },
|
2022-02-08 21:22:42 +00:00
|
|
|
users: () => { return mdb.collection('u__users') },
|
2022-02-08 22:19:53 +00:00
|
|
|
sessions: () => { return mdb.collection('u__sessions') },
|
2022-02-21 22:54:59 +00:00
|
|
|
quizs: () => { return mdb.collection('u__quizs') },
|
2022-02-08 21:22:42 +00:00
|
|
|
},
|
2022-02-16 13:17:22 +00:00
|
|
|
getAPObject: async (objID, checkType) => {
|
|
|
|
const object = await module.exports.table
|
|
|
|
.objects().findOne({ id: objID })
|
|
|
|
if (object === null)
|
|
|
|
return null
|
|
|
|
if (checkType !== undefined &&
|
|
|
|
object.type !== checkType)
|
|
|
|
return null
|
|
|
|
return object
|
|
|
|
},
|
2022-02-08 21:22:42 +00:00
|
|
|
}
|