2020-04-10 16:06:12 -04:00
|
|
|
import {Sequelize} from 'sequelize-typescript';
|
|
|
|
import { Author } from './author';
|
|
|
|
import { Note } from './note';
|
|
|
|
import { Revision } from './revision';
|
|
|
|
import { Temp } from './temp';
|
|
|
|
import { User } from './user';
|
2017-03-08 05:45:51 -05:00
|
|
|
var fs = require('fs')
|
2020-04-10 16:06:12 -04:00
|
|
|
var path = require('path');
|
2019-05-30 18:27:56 -04:00
|
|
|
const { cloneDeep } = require('lodash')
|
2016-04-20 06:03:55 -04:00
|
|
|
|
|
|
|
// core
|
2017-04-12 12:20:28 -04:00
|
|
|
var config = require('../config')
|
|
|
|
var logger = require('../logger')
|
2016-04-20 06:03:55 -04:00
|
|
|
|
2017-04-12 13:57:55 -04:00
|
|
|
var dbconfig = cloneDeep(config.db)
|
2019-04-12 00:05:32 -04:00
|
|
|
dbconfig.logging = config.debug ? (data) => {
|
|
|
|
logger.info(data)
|
|
|
|
} : false
|
2016-12-11 21:18:12 -05:00
|
|
|
|
2020-04-10 16:06:12 -04:00
|
|
|
var sequelize: any = null;
|
2016-12-22 00:23:17 -05:00
|
|
|
|
2016-12-22 06:42:00 -05:00
|
|
|
// Heroku specific
|
2018-03-07 09:17:35 -05:00
|
|
|
if (config.dbURL) {
|
|
|
|
sequelize = new Sequelize(config.dbURL, dbconfig)
|
2017-03-08 05:45:51 -05:00
|
|
|
} else {
|
|
|
|
sequelize = new Sequelize(dbconfig.database, dbconfig.username, dbconfig.password, dbconfig)
|
|
|
|
}
|
2016-04-20 06:03:55 -04:00
|
|
|
|
2017-01-01 22:05:05 -05:00
|
|
|
// [Postgres] Handling NULL bytes
|
|
|
|
// https://github.com/sequelize/sequelize/issues/6485
|
2017-03-08 05:45:51 -05:00
|
|
|
function stripNullByte (value) {
|
2017-03-15 10:12:24 -04:00
|
|
|
value = '' + value
|
2018-11-14 08:10:14 -05:00
|
|
|
// eslint-disable-next-line no-control-regex
|
2017-03-08 05:45:51 -05:00
|
|
|
return value ? value.replace(/\u0000/g, '') : value
|
2017-01-01 22:05:05 -05:00
|
|
|
}
|
2017-03-08 05:45:51 -05:00
|
|
|
sequelize.stripNullByte = stripNullByte
|
2017-01-01 22:05:05 -05:00
|
|
|
|
2017-03-08 05:45:51 -05:00
|
|
|
function processData (data, _default, process) {
|
|
|
|
if (data === undefined) return data
|
2020-04-10 16:06:12 -04:00
|
|
|
else if (process) {
|
|
|
|
if (data === null) {
|
|
|
|
return _default
|
|
|
|
} else {
|
|
|
|
return process(data)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (data === null) {
|
|
|
|
return _default
|
|
|
|
} else {
|
|
|
|
return data
|
|
|
|
}
|
|
|
|
}
|
2017-01-01 22:05:36 -05:00
|
|
|
}
|
2017-03-08 05:45:51 -05:00
|
|
|
sequelize.processData = processData
|
2017-01-01 22:05:36 -05:00
|
|
|
|
2020-04-10 16:06:12 -04:00
|
|
|
var db: any = {}
|
2016-04-20 06:03:55 -04:00
|
|
|
|
2020-04-10 16:06:12 -04:00
|
|
|
sequelize.addModels([Author, Note, Revision, Temp, User]);
|
2016-04-20 06:03:55 -04:00
|
|
|
|
2017-03-08 05:45:51 -05:00
|
|
|
db.sequelize = sequelize
|
|
|
|
db.Sequelize = Sequelize
|
2016-04-20 06:03:55 -04:00
|
|
|
|
2017-03-08 05:45:51 -05:00
|
|
|
module.exports = db
|