-
Notifications
You must be signed in to change notification settings - Fork 0
/
db-init.js
87 lines (85 loc) · 2.27 KB
/
db-init.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
const { credentials } = require('./config')
const { Client } = require('pg')
const { connectionString } = credentials.postgres
const client = new Client({connectionString})
const createScript = `
CREATE TABLE IF NOT EXISTS vacations (
name varchar(200) NOT NULL,
slug varchar(200) NOT NULL UNIQUE,
category varchar(50),
sku varchar(50),
description text,
location_search varchar(100) NOT NULL,
location_lat double precision,
location_lgn double precision,
price money,
tags jsonb,
in_season boolean,
available boolean,
requires_waiver boolean,
maximum_guests integer,
notes text,
packages_sold integer
);
CREATE TABLE IF NOT EXISTS vacation_in_season_listeners (
email varchar(200) NOT NULL,
sku varchar(20) NOT NULL,
PRIMARY KEY (email, sku)
);
`
const getVacationCount = async client => {
const { rows } = await client.query('SELECT COUNT(*) FROM VACATIONS')
return Number(rows[0].count)
}
const seedVacations = async client => {
const sql = `
INSERT INTO vacations(
name,
slug,
category,
sku,
description,
location_search,
price,
tags,
in_season,
available,
requires_waiver,
maximum_guests,
notes,
packages_sold
) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14)
`
await client.query(sql, [
'Hood River Day Trip',
'hood-river-day-trip',
'Day trip',
'HR199',
'Spend a day sailling on the COlumbia and enjoying craft beers in Hood River!',
'Hood River, Oregon, USA',
99.95,
`["day trip", "hood river", "sailling", "windsurfing", "breweries" ]`,
true,
true,
false,
16,
null,
0,
])
}
client.connect().then(async () => {
try {
console.log('creating database schema')
await client.query(createScript)
const vacationCount = await getVacationCount(client)
if(vacationCount === 0) {
console.log('seeding vacations')
await seedVacations(client)
}
} catch(err){
console.log('ERROR: could not initialize database')
console.log(err.message)
} finally {
client.end()
}
})