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

前端 未结 3 469
庸人自扰
庸人自扰 2021-02-05 05:04

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 wa

3条回答
  •  春和景丽
    2021-02-05 05:23

    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;
            }
        });
      }
    

提交回复
热议问题