且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在 RethinkDB 中,检查数据库或表是否存在的最简单方法是什么?

更新时间:2021-08-09 07:01:24

如果你想在数据库不存在的情况下创建它,或者如果它存在就得到一个类似数据库已经存在"的值,你可以这样做以下内容:

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
}

如果它已经存在:

{
  "dbs_created": 0
}