db.js 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. let init = function(db, config){
  2. // Create the tables we need to store galleries and files
  3. db.schema.createTableIfNotExists('gallery', function (table) {
  4. table.increments()
  5. table.string('name')
  6. table.timestamps()
  7. }).then(() => {})
  8. db.schema.createTableIfNotExists('files', function (table) {
  9. table.increments()
  10. table.string('file')
  11. table.string('original')
  12. table.string('type')
  13. table.string('size')
  14. table.string('ip')
  15. table.integer('galleryid')
  16. table.timestamps()
  17. }).then(() => {})
  18. db.schema.createTableIfNotExists('tokens', function (table) {
  19. table.string('name')
  20. table.string('value')
  21. table.timestamps()
  22. }).then(() => {
  23. // == Generate a 1 time token == //
  24. db.table('tokens').then((tokens) => {
  25. if(tokens.length === 0){
  26. // This is the first launch of the app
  27. let clientToken = require('randomstring').generate()
  28. let adminToken = require('randomstring').generate()
  29. db.table('tokens').insert(
  30. [
  31. {
  32. name: 'client',
  33. value: clientToken
  34. },
  35. {
  36. name: 'admin',
  37. value: adminToken
  38. }
  39. ]
  40. ).then(() => {
  41. console.log('Your client token is: ' + clientToken)
  42. console.log('Your admin token is: ' + adminToken)
  43. config.clientToken = clientToken
  44. config.adminToken = adminToken
  45. })
  46. }
  47. })
  48. })
  49. }
  50. module.exports = init