Files
deighton-ar/server/src/database/DB.js
2018-05-09 15:32:53 -07:00

52 lines
1.4 KiB
JavaScript

import mongoose from "mongoose"
import mongodb from "mongodb"
import Grid from "gridfs-stream"
import mergePlugin from "mongoose-doc-merge"
import autobind from "autobind-decorator"
import * as Schemas from "./schemas"
import util from "util"
@autobind
export class DB {
constructor() {
mongoose.plugin(mergePlugin)
}
async connect(mongoUri, isProduction) {
const connection = await mongoose.createConnection(mongoUri, {
promiseLibrary: Promise,
autoIndex: !isProduction,
})
this.gridfs = Grid(connection.db, mongoose.mongo)
this.gridfs.findOneAsync = util.promisify(this.gridfs.findOne)
this.gridfs.removeAsync = util.promisify(this.gridfs.remove)
this.User = connection.model("User", Schemas.userSchema)
this.WorkItem = connection.model("WorkItem", Schemas.workItemSchema)
this.Activity = connection.model("Activity", Schemas.activitySchema)
this.Team = connection.model("Team", Schemas.teamSchema)
this.Counter = connection.model("Counter", Schemas.counterSchema)
return this
}
newObjectId(s) {
// If s is undefined, then a new ObjectID is created, else s is assumed to be a parsable ObjectID
return new mongodb.ObjectID(s).toString()
}
async lookupToken(token, done) {
try {
const user = await this.User.findOne({ loginToken: token })
if (!user) {
done(null, false)
} else {
done(null, user)
}
} catch (err) {
done(err)
}
}
}