如何使用RethinkDB在Koa中执行初始设置 [英] How to perform initial setup in koa with rethinkdb

查看:77
本文介绍了如何使用RethinkDB在Koa中执行初始设置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将rethinkdbdash与koa结合使用,并试图弄清楚在应用程序首次启动时如何执行数据库设置.我正在尝试完成类似于Segphalt的instagram教程的猫,但使用了koa.

Im using rethinkdbdash with koa and trying to figure out how to perform a set up of the database when the app first starts. Im trying to accomplish something similar to Segphalt's cats of instagram tutorial but using koa.

var conn;
r.connect(config.database).then(function(c) {
  conn = c;
  return r.dbCreate(config.database.db).run(conn);
})
.then(function() {
  return r.tableCreate("instacat").run(conn);
})
.then(function() {
  return q.all([
    r.table("instacat").indexCreate("time").run(conn),
    r.table("instacat").indexCreate("place", {geo: true}).run(conn)
  ]);
})
.error(function(err) {
  if (err.msg.indexOf("already exists") == -1)
    console.log(err);
})
.finally(function() {
  r.table("instacat").changes().run(conn)
  .then(function(cursor) {
    cursor.each(function(err, item) {
      if (item && item.new_val)
        io.sockets.emit("cat", item.new_val);
    });
  })
  .error(function(err) {
    console.log("Error:", err);
  });

我试图只屈服而不是链接promise,但是由于语法无效而抛出错误.

I've tried to just yield instead of chaining the promises but that throws an error due to invalid syntax.

 try {
   yield r.dbCreate(config.database.db).run();
   yield r.tableCreate('user').run();
 }
 catch (err) {
   if (err.message.indexOf("already exists") == -1)
      console.log(err.message);
 }

app.listen(port);

执行这种类型的设置的最佳方法是什么,包括执行在koa中运行.changes()的查询,以便它仅在服务器启动后执行,而不在每个请求周期内执行.还是有更好的方法可以在koa中实现这一目标?

What is the best way to perform this type of setup including performing a query that runs the .changes() in koa, so that it only executes once the server starts up and not on every request cycle that comes through. Or is there a better way to accomplish this in koa?

推荐答案

您只能在生成器函数(签名中带有星号的函数)内部使用yield关键字.通常,如果要使用yield等待诺言完成,则可以使用基于生成器的协程包装整个操作集.

You can only use the yield keyword inside of a generator function (one with an asterisk in the signature). Typically, if you want to use yield to wait for completion of a promise, you would wrap the entire set of operations with a generator-based coroutine.

您可以使用两个库:

  • Co: https://www.npmjs.com/package/co
  • Bluebird: https://www.npmjs.com/package/bluebird

Coa是Koa在其所有中间件处理程序中使用的底层语言,但我个人更喜欢Bluebird,因为根据我的经验,它似乎具有更好的错误处理能力.

Co is what Koa uses under the hood in all of its middleware handlers, but I personally prefer Bluebird because it seems to have slightly better error handling in my experience.

您可以将设置代码包装在Bluebird协程中,然后就地调用它:

You can wrap your setup code in a Bluebird coroutine and then call it in place:

bluebird.coroutine(function *() {
  try {
    yield r.dbCreate(config.database.db).run();
    yield r.tableCreate('user').run();
  }
  catch (err) {
    if (err.message.indexOf("already exists") == -1)
      console.log(err.message);
  }
})();

这篇关于如何使用RethinkDB在Koa中执行初始设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆