Initial commit
This commit is contained in:
50
server/src/database/DB.js
Normal file
50
server/src/database/DB.js
Normal file
@@ -0,0 +1,50 @@
|
||||
import mongoose from 'mongoose'
|
||||
import mongodb from 'mongodb'
|
||||
import Grid from 'gridfs-stream'
|
||||
import merge from 'mongoose-merge-plugin'
|
||||
import autoBind from 'auto-bind2'
|
||||
import * as Schemas from './schemas'
|
||||
import util from 'util'
|
||||
|
||||
Grid.mongo = mongoose.mongo
|
||||
|
||||
export class DB {
|
||||
constructor() {
|
||||
mongoose.Promise = Promise
|
||||
mongoose.plugin(merge)
|
||||
|
||||
autoBind(this)
|
||||
}
|
||||
|
||||
connect(mongoUri, isProduction) {
|
||||
return mongoose.connect(mongoUri, { useMongoClient: true, config: { autoIndex: !isProduction } }).then((connection) => {
|
||||
this.connection = connection
|
||||
|
||||
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)
|
||||
|
||||
return Promise.resolve(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()
|
||||
}
|
||||
|
||||
lookupToken(token, done) {
|
||||
this.User.findOne({ 'loginToken': token }).then((user) => {
|
||||
if (!user) {
|
||||
done(null, false)
|
||||
} else {
|
||||
done(null, user)
|
||||
}
|
||||
}).catch((err) => {
|
||||
done(err)
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user