pythonrethinkdbreql

How to generate a RethinkDB changefeed for all tables in a database


I'm testing an API which inserts or deletes data in multiple tables of a RethinkDB database. In order to monitor what is happening to the database while using the API, I would like to print the changes in all its tables.

Here is some 'pseudo-code' of what I'm trying to achieve:

import rethinkdb as r

# Prior to running this script, run "rethinkdb --port-offset 1" at the command line
conn = r.connect('localhost', 28016)
if 'test' in r.db_list().run(conn):
    r.db_drop('test').run(conn)
r.db_create('test').run(conn)

r.table_create('table1').run(conn)
r.table_create('table2').run(conn)

feed = r.table('table1' and 'table2').changes().run(conn)
for document in feed:
    print document

Prior to running this script, I would run rethinkdb --port-offset 1 to initialize the RethinkDB database.

Once this script is running, I'd like to insert data into either table1 or table2 (using, for example, the web UI at localhost:8081) and see the changes printed in the terminal running the script. This appears not to work, however, because r.table('table1' and 'table2') is probably not a valid ReQL query.

How can I monitor changes in both tables?


Solution

  • You can follow multiple changefeeds in a single query using r.union:

    r.union(
      r.table('table1').changes(),
      r.table('table2').changes()
    ).run(conn)