In RethinkDB, what is the easiest way to check if a database or a table exists?
Asked Answered
G

4

24

One way I know I can do it is by listing throughdbList() and tableList() and then looking for what I want in the results.

Is there an easier way?

EDIT

My goal is to create a table in case it doesn't exist.

Growth answered 25/7, 2015 at 11:18 Comment(4)
for example you can try to create table, if it's already exists, you will see error: RqlRuntimeError: Table already existsLobo
What are you trying to achieve? Do you want to create a database if it doesn't exist, or simply check if it exists?Thorny
@Suvitruf, thanks for the reply. I wasn't really interested in throwing an error.. I just wanted to check whether it existed.Growth
@Tholle, yes. I want to create a table in case it doesn't exist.Growth
T
35

If you want to create a database if it does not exists, or get a value like "database already exists" if it does exist, you could do something like the following:

r.dbList().contains('example_database')
  .do(function(databaseExists) {
    return r.branch(
      databaseExists,
      { dbs_created: 0 },
      r.dbCreate('example_database')
    );
  }).run();

It will return the following if it is created:

{
  "config_changes": [
    {
      "new_val": {
        "id": "1ee7ddb4-6e2c-43bb-a0f5-64ef6a6211a8",
        "name": "example_database"
      },
      "old_val": null
    }
  ],
  "dbs_created": 1
}

And this if it already exists:

{
  "dbs_created": 0
}
Thorny answered 26/7, 2015 at 3:22 Comment(1)
@Thorny why so complicated? my version with python: for table in self.tables: if not rdb.db(self.db).table_list().contains(table).run(): rdb.db(self.db).table_create(table).run()Picklock
B
7

For the table existing checking I have found the following solution:

r.tableList().run(connection); //['people']

this will give you back an array of the tables which are defined on the default DB for example: ['people']. (if you want to set it do this: connection.use('test');)

then we can check if the array is containing our table name to create.

_.some(tableNames, tableName)

put it all together:

  if (!_.isNil(tableName) && _.isString(tableName) && !_.isNil(connection)) {
    r.tableList().run(connection).then(function(tableNames) {
      if (_.includes(tableNames, tableName)) {
        return r.tableCreate(tableName).run(connection);
      } else {
        return;
        }
    });
  }
Bronwen answered 25/9, 2016 at 19:11 Comment(0)
P
2

In JavaScript, given an array of table names, the shortest way is

const tables = ['table1Name', 'table2Name', ...]
const db = 'myDb'

r(tables)
    .difference(r.db(db).tableList())
    .forEach(table => r.db(db).tableCreate(table))
    .run(connection)
Production answered 29/11, 2017 at 18:20 Comment(2)
Don't you need a run() after r.db(db).tableList() ?Relaxation
No you only need to call run at the end of your entire queryProduction
A
0

For anyone coming from VB.net then solution would be like this.. one liner

Dim RDBDatabase as String = "dbName" 
Dim RDBTable as String = "tableName"  
   
Dim CheckDB = R.DbList().Contains(RDBDatabase).Do_(Function(databaseExists) R.Branch(databaseExists, "db done", R.DbCreate(RDBDatabase))).And(R.Db(RDBDatabase).TableList().Contains(RDBTable).Do_(Function(tableExists) R.Branch(tableExists, "tb done", R.Db(RDBDatabase).TableCreate(RDBTable)))).Run(conn)

This will create DB if it doesn't exists and will create the table if it doesn't exist

If neither exists then it will return values from the last created which is the table

{{
  "config_changes": [
    {
      "new_val": {
        "db": "dbName",
        "durability": "hard",
        "id": "0f72a570-7998-49f7-affc-96cbdd1ea086",
        "indexes": [],
        "name": "tableName",
        "primary_key": "id",
        "shards": [
          {
            "nonvoting_replicas": [],
            "primary_replica": "replicasssss_iu1",
            "replicas": [
              "replicasssss_iu1"
            ]
          }
        ],
        "write_acks": "majority"
      },
      "old_val": null
    }
  ],
  "tables_created": 1
}}
Alissaalistair answered 17/5, 2021 at 15:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.