aboutsummaryrefslogtreecommitdiff
path: root/database/db.js
blob: d641c2e630fd2b18b5e3d97acd990fc0e74fd981 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
let init = function(db, config){

	// Create the tables we need to store galleries and files
	db.schema.createTableIfNotExists('gallery', function (table) {
		table.increments()
		table.string('name')
		table.timestamps()
	}).then(() => {})

	db.schema.createTableIfNotExists('files', function (table) {
		table.increments()
		table.string('name')
		table.string('original')
		table.string('type')
		table.string('size')
		table.string('ip')
		table.integer('galleryid')
		table.timestamps()
	}).then(() => {})

	db.schema.createTableIfNotExists('tokens', function (table) {
		table.string('name')
		table.string('value')
		table.timestamps()
	}).then(() => {

		// == Generate a 1 time token == //
		db.table('tokens').then((tokens) => {
			if(tokens.length !== 0) return printAndSave(config, tokens[0].value, tokens[1].value)

			// This is the first launch of the app
			let clientToken = require('randomstring').generate()
			let adminToken = require('randomstring').generate()

			db.table('tokens').insert(
				[
					{ 
						name: 'client', 
						value: clientToken 
					},
					{ 
						name: 'admin', 
						value: adminToken 
					}
				]
			).then(() => {
				printAndSave(config, clientToken, adminToken)
			})

		})

	})

}

function printAndSave(config, clientToken, adminToken){
	console.log('Your client token is: ' + clientToken)
	console.log('Your admin token is: ' + adminToken)
	config.clientToken = clientToken
	config.adminToken = adminToken
}

module.exports = init