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.
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.
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
}
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 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;
}
});
}
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)
run()
after r.db(db).tableList()
? –
Relaxation 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
}}
© 2022 - 2024 — McMap. All rights reserved.