52 lines
1.4 KiB
JavaScript
52 lines
1.4 KiB
JavaScript
import mongoose from "mongoose"
|
|
import mongodb from "mongodb"
|
|
import Grid from "gridfs-stream"
|
|
import merge from "mongoose-merge-plugin"
|
|
import autobind from "autobind-decorator"
|
|
import * as Schemas from "./schemas"
|
|
import util from "util"
|
|
|
|
Grid.mongo = mongoose.mongo
|
|
|
|
@autobind
|
|
export class DB {
|
|
constructor() {
|
|
mongoose.plugin(merge)
|
|
}
|
|
|
|
async connect(mongoUri, isProduction) {
|
|
const connection = await mongoose.createConnection(mongoUri, {
|
|
promiseLibrary: Promise,
|
|
autoIndex: !isProduction,
|
|
})
|
|
|
|
this.gridfs = Grid(connection.db)
|
|
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)
|
|
}
|
|
|
|
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)
|
|
}
|
|
}
|
|
}
|