【发布时间】:2012-04-05 16:38:56
【问题描述】:
文档只是说“或定义自定义架构(非杂耍),例如猫鼬。请注意,在自定义架构的情况下,所有 jugglingdb 功能当然都将被禁用。”
但是..
这个架构应该在哪里创建?
【问题讨论】:
标签: node.js express railway.js
文档只是说“或定义自定义架构(非杂耍),例如猫鼬。请注意,在自定义架构的情况下,所有 jugglingdb 功能当然都将被禁用。”
但是..
这个架构应该在哪里创建?
【问题讨论】:
标签: node.js express railway.js
我相信你仍然可以在 db/schema.js 中创建它。例如:
customSchema(function () {
var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/test');
var Schema = mongoose.Schema, ObjectId = Schema.ObjectId;
var BlogPost = new Schema({
author : ObjectId
, title : String
, body : String
, date : Date
});
var Post = mongoose.model('BlogPost', BlogPost);
Post.modelName = 'BlogPost'; // this is for some features inside railway (helpers, etc)
module.exports['BlogPost'] = Post;
});
【讨论】:
在让上面的 customSchema 示例正常工作时,我遇到了类似的问题。我认为这个小技巧可能会为其他尝试使用 railjs customchemas 的人节省大量时间。
在customSchema(function() {...}) 代码块中放入console.log("custom schema initialized"); 后,我注意到console.log 没有触发...这显然意味着我的db/schema.js 文件中的customSchema 块甚至没有得到调用。
经过多次摆弄,我意识到在您的config/database.json 文件中,您必须将驱动程序设置为“内存”(对于您希望运行 customSchema 的任何环境)。例如,如果您将其设置为 driver: "mongoose",则铁路将不会运行 customSchema 代码块。
总而言之,如果您想运行 customSchema,请确保您的 config/database.json 文件如下所示:
{
"production":
{
"driver": "memory",
},
"development":
{
"driver": "memory"
},
"test":
{
"driver": "memory"
}
}
如果你有这样的东西,你的 customSchema 将不起作用:
{
"production":
{
"driver": "mongoose",
"url": "mongodb://<user>:<pass>@localhost:<port>/<database>"
},
"development":
{
"driver": "mongoose",
"url": "mongodb://<user>:<pass>@localhost:<port>/<database>"
},
"test":
{
"driver": "memory"
}
}
至少在撰写本文时...railwayjs 仍在开发中,所以我确信事情可能会发生变化。我正在使用railwaysjs version 0.2.17-pre4
** 还有一个警告——如果你从你的 node_modules 文件夹中删除“jugglingdb”——这是与铁路打包的 ORM——那么 customSchema 将不会被调用。我相信这是因为 'jugglingdb' 紧密集成到 railsjs 框架中。我猜,基本原理是该框架的作者试图模仿 ruby-on-rails - 我们知道它与 activeRecord 紧密集成。
因此,即使您不使用 jugglingdb 并且正在运行自己的自定义架构(例如通过 mongoose 或其他 ORM),也不要删除 jugglingdb。
我为此记录了一个错误: https://github.com/1602/express-on-railway/issues/212
【讨论】: