This repository has been archived by the owner on Feb 1, 2024. It is now read-only.
/
bootstrap_database.js
67 lines (64 loc) · 2.23 KB
/
bootstrap_database.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
/**
* Copyright 2017 Intel Corporation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* ----------------------------------------------------------------------------
*/
'use strict'
const r = require('rethinkdb')
const HOST = process.env.DB_HOST || 'localhost'
const PORT = process.env.DB_PORT || 28015
const NAME = process.env.DB_NAME || 'tnt'
r.connect({host: HOST, port: PORT})
.then(conn => {
console.log(`Creating "${NAME}" database...`)
r.dbList().contains(NAME).run(conn)
.then(dbExists => {
if (dbExists) throw new Error(`"${NAME}" already exists`)
return r.dbCreate(NAME).run(conn)
})
.then(res => {
console.log('Creating "users" table...')
return r.db(NAME).tableCreate('users', {
primaryKey: 'publicKey'
}).run(conn)
})
.then(res => {
// The usernames table is used to quickly ensure unique usernames
console.log('Creating "usernames" table...')
return r.db(NAME).tableCreate('usernames', {
primaryKey: 'username'
}).run(conn)
})
.then(res => {
console.log('Creating and populating "state" table...')
return r.db(NAME).tableCreate('state').run(conn)
})
.then(res => {
return r.db(NAME).table('state').insert({
name: 'message',
value: 'Hello Track and Trade!'
}).run(conn)
})
.then(res => {
console.log('Bootstrapping complete, closing connection.')
return conn.close()
})
.catch(err => {
console.log(`Unable to bootstrap "${NAME}" db: ${err.message}`)
return conn.close()
})
})
.catch(err => {
console.log(`Unable to connect to db at ${HOST}:${PORT}}: ${err.message}`)
})