Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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.

like image 904
André Pena Avatar asked Jul 25 '15 11:07

André Pena


People also ask

How does RethinkDB work?

RethinkDB uses a range sharding algorithm parameterized on the table's primary key to partition the data. When the user states they want a given table to use a certain number of shards, the system examines the statistics for the table and finds the optimal set of split points to break up the table evenly.

What is RethinkDB good for?

What is RethinkDB? RethinkDB is built to store JSON documents, and scale to multiple machines with very little effort. It has a pleasant query language that supports really useful queries like table joins and group by, and is easy to setup and learn.

Who uses RethinkDB?

Who uses RethinkDB? 51 companies reportedly use RethinkDB in their tech stacks, including GraphicWeave, Owlin, and Parabol Multiplayer Web App.

How do I connect to RethinkDB?

Once RethinkDB is running, you can connect to it at http://localhost:8080, assuming you've kept the default port (8080) and it's running on your local machine. By default, RethinkDB binds the web interface to localhost for security reasons.


2 Answers

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 } 
like image 129
Tholle Avatar answered Sep 22 '22 06:09

Tholle


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;         }     });   } 
like image 39
xyztdanid4 Avatar answered Sep 19 '22 06:09

xyztdanid4