v3.0.0/database/db.js

50 lines
1.3 KiB
JavaScript
Raw Normal View History

let init = function(db){
2017-01-14 09:50:18 +01:00
// Create the tables we need to store galleries and files
2017-01-17 23:05:00 +01:00
db.schema.createTableIfNotExists('albums', function (table) {
2017-01-14 09:50:18 +01:00
table.increments()
table.integer('userid')
2017-01-14 09:50:18 +01:00
table.string('name')
2017-02-07 08:32:55 +01:00
table.string('identifier')
table.integer('enabled')
2017-01-18 06:40:14 +01:00
table.integer('timestamp')
2017-01-14 09:50:18 +01: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 07:34:48 +01:00
table.string('hash')
table.string('ip')
2017-01-17 23:05:00 +01:00
table.integer('albumid')
2017-01-18 06:40:14 +01:00
table.integer('timestamp')
2017-01-14 09:50:18 +01:00
}).then(() => {})
db.schema.createTableIfNotExists('users', function (table) {
table.increments()
table.string('username')
table.string('password')
table.string('token')
2017-01-18 06:40:14 +01:00
table.integer('timestamp')
2017-01-16 08:37:42 +01: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 08:37:42 +01:00
})
2017-01-16 08:45:29 +01:00
}
2017-01-14 09:50:18 +01:00
module.exports = init