fedilove/api/src/db.js

37 lines
923 B
JavaScript
Raw Permalink Normal View History

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'
mconn = new MongoClient(`mongodb://${dbhost}:27017`, {
useUnifiedTopology: true,
useNewUrlParser: true
})
return mconn
},
get: () => {
mdb = mconn.db(process.env.DB_NAME || 'fedilove')
return mdb
},
table: {
2022-02-13 01:02:29 +00:00
objects: () => { return mdb.collection('objects') },
users: () => { return mdb.collection('u__users') },
sessions: () => { return mdb.collection('u__sessions') },
quizs: () => { return mdb.collection('u__quizs') },
},
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
},
}