forked from devpleno/minhas-series-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.js
53 lines (47 loc) · 1.31 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
const knex = require('knex')({
client: 'sqlite3',
connection: {
filename: './db.sqlite'
},
useNullAsDefault: true
})
const initDB = async () => {
const seriesExist = await knex.schema.hasTable('series')
if (!seriesExist) {
await knex.schema.createTable('series', table => {
table.increments('id').primary()
table.string('name')
table.string('status')
table.integer('genre_id')
table.string('comments')
table.string('poster')
table.string('background')
})
}
const genresExist = await knex.schema.hasTable('genres')
if (!genresExist) {
await knex.schema.createTable('genres', table => {
table.increments('id').primary()
table.integer('name')
})
}
const totalGenres = await knex('genres').select(knex.raw('count(*) as total'))
if (totalGenres[0].total === 0) {
await knex.insert({
name: 'Ação'
}).into('genres')
await knex.insert({
name: 'Comédia'
}).into('genres')
await knex.insert({
name: 'La casa de papel',
status: 'WATCHED',
genre_id: 1,
comments: '',
poster: '//image.tmdb.org/t/p/original/yVUAfbrP5HDJugXraB7KQS0yz6Z.jpg',
background: '//image.tmdb.org/t/p/original/piuRhGiQBYWgW668eSNJ2ug5uAO.jpg'
}).into('series')
}
}
initDB()
module.exports = knex