-
Notifications
You must be signed in to change notification settings - Fork 4
/
db.js
55 lines (53 loc) · 1.44 KB
/
db.js
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
const knex = require('knex')({
client: 'sqlite3',
connection: {
filename: './db.sqlite'
},
useNullAsDefault: true
})
const initDB = async () => {
const usersExist = await knex.schema.hasTable('users')
if (!usersExist) {
await knex.schema.createTable('users', table => {
table.increments('id').primary()
table.string('name')
table.string('email')
table.string('passwd')
table.string('role')
table.string('unit') // metric // imperial
table.string('timezone')
})
}
const runsExist = await knex.schema.hasTable('runs')
if (!runsExist) {
await knex.schema.createTable('runs', table => {
table.increments('id').primary()
table.integer('user_id')
table.string('friendly_name')
table.integer('duration') // in seconds
table.timestamp('created') // utc
table.integer('distance') // meters
})
}
const totalUsers = await knex('users').select(knex.raw('count(*) as total'))
if (totalUsers[0].total === 0) {
await knex.insert({
name: 'Tulio Faria',
email: 'tuliofaria@devpleno.com',
passwd: 'abc123',
role: 'admin',
unit: 'metric',
timezone: 'America/Sao_Paulo'
}).into('users')
await knex.insert({
name: 'Zé da Silva',
email: 'ze@dominio.com',
passwd: 'abc123',
role: 'user',
unit: 'metric',
timezone: 'America/Sao_Paulo'
}).into('users')
}
}
initDB()
module.exports = knex