github.com/muhammedhassanm/blockchain@v0.0.0-20200120143007-697261defd4d/sawtooth-supply-chain-master/server/scripts/bootstrap_database.js (about)

     1  /**
     2   * Copyright 2017 Intel Corporation
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   *     http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   * ----------------------------------------------------------------------------
    16   */
    17  'use strict'
    18  
    19  const r = require('rethinkdb')
    20  const config = require('../system/config')
    21  
    22  const HOST = config.DB_HOST
    23  const PORT = config.DB_PORT
    24  const NAME = config.DB_NAME
    25  
    26  r.connect({host: HOST, port: PORT})
    27    .then(conn => {
    28      console.log(`Creating "${NAME}" database...`)
    29      r.dbList().contains(NAME).run(conn)
    30        .then(dbExists => {
    31          if (dbExists) throw new Error(`"${NAME}" already exists`)
    32          return r.dbCreate(NAME).run(conn)
    33        })
    34        .then(() => {
    35          console.log('Creating "users" table...')
    36          return r.db(NAME).tableCreate('users', {
    37            primaryKey: 'publicKey'
    38          }).run(conn)
    39        })
    40        .then(() => {
    41          // The usernames table is used to quickly ensure unique usernames
    42          console.log('Creating "usernames" table...')
    43          return r.db(NAME).tableCreate('usernames', {
    44            primaryKey: 'username'
    45          }).run(conn)
    46        })
    47        .then(() => {
    48          console.log('Creating "agents" table...')
    49          return r.db(NAME).tableCreate('agents').run(conn)
    50        })
    51        .then(() => {
    52          return r.db(NAME).table('agents').indexCreate('publicKey').run(conn)
    53        })
    54        .then(() => {
    55          console.log('Creating "records" table...')
    56          return r.db(NAME).tableCreate('records').run(conn)
    57        })
    58        .then(() => {
    59          r.db(NAME).table('records').indexCreate('recordId').run(conn)
    60        })
    61        .then(() => {
    62          console.log('Creating "recordTypes" table...')
    63          return r.db(NAME).tableCreate('recordTypes').run(conn)
    64        })
    65        .then(() => {
    66          return r.db(NAME).table('recordTypes').indexCreate('name').run(conn)
    67        })
    68        .then(() => {
    69          console.log('Creating "properties" table...')
    70          return r.db(NAME).tableCreate('properties').run(conn)
    71        })
    72        .then(() => {
    73          return r.db(NAME).table('properties').indexCreate('attributes', [
    74            r.row('name'),
    75            r.row('recordId')
    76          ]).run(conn)
    77        })
    78        .then(() => {
    79          console.log('Creating "propertyPages" table...')
    80          return r.db(NAME).tableCreate('propertyPages').run(conn)
    81        })
    82        .then(() => {
    83          return r.db(NAME).table('propertyPages').indexCreate('attributes', [
    84            r.row('name'),
    85            r.row('recordId'),
    86            r.row('pageNum')
    87          ]).run(conn)
    88        })
    89        .then(() => {
    90          console.log('Creating "proposals" table...')
    91          return r.db(NAME).tableCreate('proposals').run(conn)
    92        })
    93        .then(() => {
    94          return r.db(NAME).table('proposals').indexCreate('attributes', [
    95            r.row('recordId'),
    96            r.row('timestamp'),
    97            r.row('receivingAgent'),
    98            r.row('role')
    99          ]).run(conn)
   100        })
   101        .then(() => {
   102          console.log('Creating "blocks" table...')
   103          return r.db(NAME).tableCreate('blocks', {
   104            primaryKey: 'blockNum'
   105          }).run(conn)
   106        })
   107        .then(() => {
   108          console.log('Bootstrapping complete, closing connection.')
   109          return conn.close()
   110        })
   111        .catch(err => {
   112          console.log(`Unable to bootstrap "${NAME}" db: ${err.message}`)
   113          return conn.close()
   114        })
   115    })
   116    .catch(err => {
   117      console.log(`Unable to connect to db at ${HOST}:${PORT}}: ${err.message}`)
   118    })