Skip to content

Instantly share code, notes, and snippets.

@mirhec
Created January 17, 2015 06:43
Show Gist options
  • Save mirhec/c273feb045b29dbca972 to your computer and use it in GitHub Desktop.
Save mirhec/c273feb045b29dbca972 to your computer and use it in GitHub Desktop.
###
## Check if database exists. If it does not exist
## create it and create all tables and indices.
## The names of the Db, tables and indices are
## specified in config.coffee
###
createDbIfNotExists: () ->
r.connect config.rethinkdb, (err, connection) =>
if err
console.log "Could not open a connection to initialize the database"
console.log err.message
process.exit 1
# save connection object
@conn = connection
# check if a database exists
r.dbList().run @conn, (err, result) =>
# check length of result
if result.length == 0
# create db
@createDb () =>
# create all tables
for table in config.rethinkdb.tables
@createTable table
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment