mirror of
https://github.com/hedgedoc/hedgedoc.git
synced 2025-05-19 17:55:17 -04:00
Move stripNullByte and processData from models/index.ts to utils.ts
Signed-off-by: David Mehren <dmehren1@gmail.com>
This commit is contained in:
parent
03ae37055d
commit
8c662a1f41
2 changed files with 32 additions and 42 deletions
|
@ -1,12 +1,11 @@
|
||||||
import {Sequelize} from 'sequelize-typescript';
|
import {Sequelize} from 'sequelize-typescript';
|
||||||
import { Author } from './author';
|
import {Author} from './author';
|
||||||
import { Note } from './note';
|
import {Note} from './note';
|
||||||
import { Revision } from './revision';
|
import {Revision} from './revision';
|
||||||
import { Temp } from './temp';
|
import {Temp} from './temp';
|
||||||
import { User } from './user';
|
import {User} from './user';
|
||||||
var fs = require('fs')
|
|
||||||
var path = require('path');
|
const {cloneDeep} = require('lodash')
|
||||||
const { cloneDeep } = require('lodash')
|
|
||||||
|
|
||||||
// core
|
// core
|
||||||
var config = require('../config')
|
var config = require('../config')
|
||||||
|
@ -17,7 +16,7 @@ dbconfig.logging = config.debug ? (data) => {
|
||||||
logger.info(data)
|
logger.info(data)
|
||||||
} : false
|
} : false
|
||||||
|
|
||||||
var sequelize: any = null;
|
export let sequelize: any = null;
|
||||||
|
|
||||||
// Heroku specific
|
// Heroku specific
|
||||||
if (config.dbURL) {
|
if (config.dbURL) {
|
||||||
|
@ -26,38 +25,5 @@ if (config.dbURL) {
|
||||||
sequelize = new Sequelize(dbconfig.database, dbconfig.username, dbconfig.password, dbconfig)
|
sequelize = new Sequelize(dbconfig.database, dbconfig.username, dbconfig.password, dbconfig)
|
||||||
}
|
}
|
||||||
|
|
||||||
// [Postgres] Handling NULL bytes
|
|
||||||
// https://github.com/sequelize/sequelize/issues/6485
|
|
||||||
function stripNullByte (value) {
|
|
||||||
value = '' + value
|
|
||||||
// eslint-disable-next-line no-control-regex
|
|
||||||
return value ? value.replace(/\u0000/g, '') : value
|
|
||||||
}
|
|
||||||
sequelize.stripNullByte = stripNullByte
|
|
||||||
|
|
||||||
function processData (data, _default, process) {
|
|
||||||
if (data === undefined) return data
|
|
||||||
else if (process) {
|
|
||||||
if (data === null) {
|
|
||||||
return _default
|
|
||||||
} else {
|
|
||||||
return process(data)
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
if (data === null) {
|
|
||||||
return _default
|
|
||||||
} else {
|
|
||||||
return data
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
sequelize.processData = processData
|
|
||||||
|
|
||||||
var db: any = {}
|
|
||||||
|
|
||||||
sequelize.addModels([Author, Note, Revision, Temp, User]);
|
sequelize.addModels([Author, Note, Revision, Temp, User]);
|
||||||
|
|
||||||
db.sequelize = sequelize
|
|
||||||
db.Sequelize = Sequelize
|
|
||||||
|
|
||||||
module.exports = db
|
|
||||||
|
|
24
lib/utils.ts
24
lib/utils.ts
|
@ -24,6 +24,30 @@ export module Utils {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// [Postgres] Handling NULL bytes
|
||||||
|
// https://github.com/sequelize/sequelize/issues/6485
|
||||||
|
export function stripNullByte(value) {
|
||||||
|
value = '' + value
|
||||||
|
// eslint-disable-next-line no-control-regex
|
||||||
|
return value ? value.replace(/\u0000/g, '') : value
|
||||||
|
}
|
||||||
|
|
||||||
|
export function processData (data, _default, process?) {
|
||||||
|
if (data === undefined) return data
|
||||||
|
else if (process) {
|
||||||
|
if (data === null) {
|
||||||
|
return _default
|
||||||
|
} else {
|
||||||
|
return process(data)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if (data === null) {
|
||||||
|
return _default
|
||||||
|
} else {
|
||||||
|
return data
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue