我正在测试一个 API,它可以在 RethinkDB 数据库的多个表中插入或删除数据。为了在使用 API 时监视数据库发生的情况,我想打印所有它的表中的更改。
这是我想要实现的一些“伪代码”:
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
在运行此脚本之前,我会运行 rethinkdb --port-offset 1 来初始化 RethinkDB 数据库。
此脚本运行后,我想将数据插入 table1 或 table2(例如,使用位于 localhost 的 Web UI: 8081) 并查看在运行脚本的终端中打印的更改。这似乎不起作用,但是, 因为 r.table('table1' and 'table2') 可能不是有效的 ReQL 查询。
如何监控两个表的变化?
请您参考如下方法:
您可以使用 r.union 在单个查询中关注多个变更源:
r.union(
r.table('table1').changes(),
r.table('table2').changes()
).run(conn)
