我正在使用rethinkdbdash和koa,并试图找出在应用程序首次启动时如何执行数据库设置。我试图完成类似于Instagram教程的Segphalt的猫,但使用koa。
Module Module1
Sub Main()
Dim result = GetData()
End Sub
Public Class MakeStyle
Public Property Id As Integer
End Class
Public Class ModelStyle
Public Property Id As String
End Class
Public Class EngineStyle
Public Property Id As String
Public Property RotationsPerMinute As EngineRotationsPerMinuteStyle
End Class
Public Class EngineRotationsPerMinuteStyle
Public Property HorsePower As Integer
Public Property Torque As Integer
End Class
Public Class Payload
Public Property Make As MakeStyle
Public Property Model As ModelStyle
Public Property Engine As EngineStyle
Public Property Colors As IEnumerable(Of ColorStyle)
End Class
Public Class ColorStyleOptionItem
Public Property Id As String
Public Property Name As String
End Class
Public Class ColorStyle
Public Property Category As String
Public Property Options As IEnumerable(Of ColorStyleOptionItem)
End Class
Public Class DataItem
Public Property Category As String
Public Property Id As String
Public Property Name As String
End Class
Public Function GetData() As IEnumerable(Of DataItem)
Dim payloadString As String = GetDemoData() ' json serialized data
Dim serializer As System.Web.Script.Serialization.JavaScriptSerializer = New System.Web.Script.Serialization.JavaScriptSerializer()
Dim payload = serializer.Deserialize(Of Payload)(payloadString) ' sets Property Values of Payload to 'Nothing' when the json fields are not present in the json string.
Dim data As IEnumerable(Of DataItem) = payload.Colors.SelectMany(Function(item) item.Options.Select(Function(opt) New DataItem With {.Category = item.Category, .Id = opt.Id, .Name = opt.Name}))
GetData = data
End Function
Public Function GetDemoData() As String
Dim serializer As System.Web.Script.Serialization.JavaScriptSerializer = New System.Web.Script.Serialization.JavaScriptSerializer()
GetDemoData = serializer.Serialize(New Payload() With {
.Make = New MakeStyle() With {.Id = 200001444},
.Model = New ModelStyle() With {.Id = "Honda_Pilot"},
.Engine = New EngineStyle With {.Id = "200058127", .RotationsPerMinute = New EngineRotationsPerMinuteStyle() With {.HorsePower = 5700, .Torque = 4800}},
.Colors = New ColorStyle() {
New ColorStyle() With {
.Category = "Interior",
.Options = New ColorStyleOptionItem() {
New ColorStyleOptionItem() With {.Id = "200058068", .Name = "Blue"}
}
},
New ColorStyle() With {
.Category = "Exterior",
.Options = New ColorStyleOptionItem() {
New ColorStyleOptionItem() With {.Id = "200058073", .Name = "Crystal Black Pearl"},
New ColorStyleOptionItem() With {.Id = "200058072", .Name = "Polished Metal Metallic"}
}
}
}
})
End Function
End Module
我试图屈服而不是链接承诺,但由于语法无效而引发错误。
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);
});
执行此类设置的最佳方法是什么,包括执行在koa中运行.changes()的查询,以便它只在服务器启动时执行,而不是在每个请求周期中执行。或者在koa有更好的方法来实现这一目标吗?
答案 0 :(得分:1)
您只能在生成器函数内使用yield
关键字(签名中带星号的关键字)。通常,如果要使用yield
等待promise的完成,则可以使用基于生成器的协程包装整组操作。
您可以使用两个库:
Co是Koa在其所有中间件处理程序中使用的内容,但我个人更喜欢Bluebird,因为根据我的经验,它似乎有更好的错误处理。
您可以将设置代码包装在Bluebird协程中,然后将其调用到位:
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);
}
})();