bchavez / RethinkDb.Driver

:headphones: A NoSQL C#/.NET RethinkDB database driver with 100% ReQL API coverage.
http://rethinkdb.com/api/java
Other
384 stars 134 forks source link

check table existence #16

Closed eze1981 closed 8 years ago

eze1981 commented 8 years ago

What is the preferred way to check if a table exists in a database? Using .tableList() ?

Thanks

bchavez commented 8 years ago

Hello @eze1981 , sure, you can do .tableList() and check if it exists. .tableList() is the easiest to understand:

var result = r.db(DbName).tableList().runAtom<List<string>>(conn);
if( result.Contains("test") )
{
    //exists
}
else
{
    //doesnt exist
}

Alternatively, you can have the server check and create a table if it doesn't exist. It's a bit more advanced, but this also works:

var result = r.db("mydb").tableList().contains("newTable")
    .do_(tableExists =>
        {
            return r.branch(
                tableExists, /* The test */
                new {tables_created = 0}, /* What to do if true */
                r.db("mydb").tableCreate("newTable") /* what to do if false */
                );
        }).runResult(conn);

result.TablesCreated will be 1 or 0, depending if the table existed or not. If it didn't exist, the server will create one. If you want the config_changes use .run<JObject> instead and probe though the config values you're looking for.

I hope you don't mind, I'm going to close this issue since it's not particularly a problem with the C# driver code... but feel free to comment further if you need more help understanding the examples above. A great source to get help with questions about ReQL is the freenode #rethinkdb community channel. Currently, there's about 114 users online right now. Many are idle, but someone usually answers questions after a few minutes. I'm also in this channel. If you're using windows, a good IRC freenode client is HexChat.

https://github.com/rethinkdb/rethinkdb#need-help

bchavez commented 8 years ago

Also, forgot to mention, we have a Gitter.IM channel, like IRC, but specifically for this driver: https://gitter.im/bchavez/RethinkDb.Driver