filesafe/database/db.js

49 lines
1.3 KiB
JavaScript
Raw Normal View History

let init = function(db){
2017-01-14 08:50:18 +00:00
// Create the tables we need to store galleries and files
2017-01-17 22:05:00 +00:00
db.schema.createTableIfNotExists('albums', function (table) {
2017-01-14 08:50:18 +00:00
table.increments()
table.integer('userid')
2017-01-14 08:50:18 +00:00
table.string('name')
table.integer('enabled')
2017-01-18 05:40:14 +00:00
table.integer('timestamp')
2017-01-14 08:50:18 +00:00
}).then(() => {})
db.schema.createTableIfNotExists('files', function (table) {
table.increments()
table.integer('userid')
table.string('name')
table.string('original')
table.string('type')
table.string('size')
2017-01-19 06:34:48 +00:00
table.string('hash')
table.string('ip')
2017-01-17 22:05:00 +00:00
table.integer('albumid')
2017-01-18 05:40:14 +00:00
table.integer('timestamp')
2017-01-14 08:50:18 +00:00
}).then(() => {})
db.schema.createTableIfNotExists('users', function (table) {
table.increments()
table.string('username')
table.string('password')
table.string('token')
2017-01-18 05:40:14 +00:00
table.integer('timestamp')
2017-01-16 07:37:42 +00:00
}).then(() => {
db.table('users').where({username: 'root'}).then((user) => {
if(user.length > 0) return
require('bcrypt').hash('root', 10, function(err, hash) {
if(err) console.error('Error generating password hash for root')
db.table('users').insert({
username: 'root',
password: hash,
token: require('randomstring').generate(64),
timestamp: Math.floor(Date.now() / 1000)
}).then(() => {})
})
})
2017-01-16 07:37:42 +00:00
})
2017-01-16 07:45:29 +00:00
}
2017-01-14 08:50:18 +00:00
module.exports = init