如何使用node-orm2使用Express建模两个或更多数据库?

时间:2022-05-22 21:43:50

I'm trying to use node-orm2 as middleware with Express the documentation only shows how you can connect to one database only.

我正在尝试使用node-orm2作为Express的中间件,文档仅显示如何仅连接到一个数据库。

I have tried getting Express to use two different middleware layers, but no luck. For instance,

我试过让Express使用两个不同的中间件层,但没有运气。例如,

app.use(orm.express('sqlite://test.db', define: { /* define table1 */ }));
app.use(orm.express('sqlite://test2.db', define: { /* define table2 */ }));

I've routed everything correctly to this get handler:

我已将所有内容正确地路由到此get处理程序:

app.get("/", function (req, res) {
   var t1 = req.db.models.table1.find(...);
   var t2 = req.db.models.table2.find(...);
   res.send([t1,t2]);
});

Since I app.used table1's database first, the second of the find invocations will yield

因为我首先使用table1的数据库,所以第二个查找调用将会产生

TypeError: Cannot call method 'find' of undefined

... which means that the two define: blocks haven't been merged, which is what we'd like.

...这意味着两个定义:块没有合并,这是我们想要的。

How would I access two databases with node-orm2?

如何使用node-orm2访问两个数据库?

1 个解决方案

#1


1  

Your code seems fine. I think the problem is that you're using "req.db.models" instead of just "req.models". From the documentation:

你的代码似乎很好。我认为问题是你使用的是“req.db.models”而不仅仅是“req.models”。从文档:

app.get("/", function (req, res) {
    // req.models is a reference to models used above in define()
    req.models.person.find(...);
});

And also:

You can call orm.express more than once to have multiple database connections. Models defined across connections will be joined together in req.models. Don't forget to use it before app.use(app.router), preferably right after your assets public folder(s).

您可以多次调用orm.express来拥有多个数据库连接。跨连接定义的模型将在req.models中连接在一起。不要忘记在app.use(app.router)之前使用它,最好是在资产公用文件夹之后。

Everything just gets mushed together under "models," there's no differentiator between databases. This means you can't name your models the same thing across multiple DBs, but c'est la guerre.

在“模型”下,所有东西都被混合在一起,数据库之间没有区别。这意味着你不能在多个数据库中为你的模型命名相同的东西,但是c'est la guerre。

#1


1  

Your code seems fine. I think the problem is that you're using "req.db.models" instead of just "req.models". From the documentation:

你的代码似乎很好。我认为问题是你使用的是“req.db.models”而不仅仅是“req.models”。从文档:

app.get("/", function (req, res) {
    // req.models is a reference to models used above in define()
    req.models.person.find(...);
});

And also:

You can call orm.express more than once to have multiple database connections. Models defined across connections will be joined together in req.models. Don't forget to use it before app.use(app.router), preferably right after your assets public folder(s).

您可以多次调用orm.express来拥有多个数据库连接。跨连接定义的模型将在req.models中连接在一起。不要忘记在app.use(app.router)之前使用它,最好是在资产公用文件夹之后。

Everything just gets mushed together under "models," there's no differentiator between databases. This means you can't name your models the same thing across multiple DBs, but c'est la guerre.

在“模型”下,所有东西都被混合在一起,数据库之间没有区别。这意味着你不能在多个数据库中为你的模型命名相同的东西,但是c'est la guerre。