我正在测试一个API,它在reinstdb数据库的多个表中插入或删除数据。为了在使用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
来初始化rewingdb数据库。在
一旦这个脚本运行,我想将数据插入table1
或table2
(例如,使用localhost:8081
的web UI),并查看运行脚本的终端中打印的更改。然而,这似乎行不通,
因为r.table('table1' and 'table2')
可能不是有效的ReQL查询。在
如何监视两个表中的更改?在
您可以使用
r.union
在一个查询中跟踪多个changefeeds:最后,我在一个单独的线程中为每个表运行changefeeds:
注意,我在for循环中重新定义了
conn
连接对象,因为这些对象不是线程安全的。在为了测试该方法,我打开了位于
localhost:8081
的web UI,并使用了以下insert
命令:在Sublime runner中,每次按下“Run”按钮,我都会看到添加的更改:
当我在
insert
命令中选择table1
或table2
时,这两种方法都有效。在相关问题 更多 >
编程相关推荐