node.js 如何在 Sails.js 和 Waterline 中执行 SQL 连接和关系?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17683015/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 15:02:53  来源:igfitidea点击:

How to perform SQL Joins and Relations in Sails.js and Waterline?

node.jsormsails.jswaterline

提问by Brieuce Wayne

Can anyone guide me on how to setup relational schema & performs joins in sails.js?

任何人都可以指导我如何设置关系模式并在sails.js 中执行连接?

回答by particlebanana

Associations are officially Supported in Waterline

协会在 Waterline 中得到官方支持

Overview

概述

From the docs:

从文档

With Sails and Waterline, you can associate models across multiple data stores. This means that even if your users live in PostgreSQL and their photos live in MongoDB, you can interact with the data as if they lived together in the same database. You can also have associations that span different connections (i.e. datastores/databases) using the same adapter. This comes in handy if, for example, your app needs to access/update legacy recipe data stored in a MySQL database in your company's data center, but also store/retrieve ingredient data from a brand new MySQL database in the cloud.

使用 Sails 和 Waterline,您可以跨多个数据存储关联模型。这意味着即使您的用户生活在 PostgreSQL 中并且他们的照片生活在 MongoDB 中,您也可以与数据进行交互,就好像他们生活在同一个数据库中一样。您还可以使用相同的适配器建立跨越不同连接(即数据存储/数据库)的关联。例如,如果您的应用程序需要访问/更新存储在您公司数据中心的 MySQL 数据库中的旧配方数据,但还需要从云中全新的 MySQL 数据库存储/检索成分数据,这将派上用场。

Supported Association Types

支持的关联类型

Planned Association Types

计划的关联类型



Original Post

原帖

I'm the author of Waterline, the ORM used in Sails. Waterline is brand new and we are adding features all the time. Currently we don't have support for associations but it's next on the roadmap. We worked out an API for associations that I think most people will really like. You can view the work in progress and the proposed API at: [Proposed Sails Associations API][1].

We are going to tackle Associations and Transactions next and hope to have them ready in the next month or so.

In the mean time if you are using the MySQL or PostgreSQL adapters they both expose a raw .query()method that allows you to pass in a hand built sql query and have it executed. I totally realize this isn't ideal but should allow you to continue building your app while we get support for associations and joins.

The function signature for the query method is:

Model.query(<sql query>, <optional data>, callback);

我是 Waterline 的作者,它是 Sails 中使用的 ORM。Waterline 是全新的,我们一直在添加功能。目前我们不支持关联,但它是路线图的下一个。我们为协会制定了一个 API,我认为大多数人会非常喜欢。您可以在以下位置查看正在进行的工作和提议的 API:[Proposed Sails Associations API][1]。

我们接下来将处理关联和交易,并希望在下个月左右准备好它们。

同时,如果您使用 MySQL 或 PostgreSQL 适配器,它们都公开了一个原始.query()方法,允许您传入手动构建的 sql 查询并执行它。我完全意识到这并不理想,但应该允许您在我们获得关联和加入支持的同时继续构建您的应用程序。

查询方法的函数签名是:

Model.query(<sql query>, <optional data>, callback);

回答by Bruce Kim

The example from particle banana works but should actually use "new" like "var instance = new User._model(values)". I'm using the following code and it works.

来自粒子香蕉的示例有效,但实际上应该使用“new”,例如“var instance = new User._model(values)”。我正在使用以下代码并且它有效。

Accounts.query(query, function(err, accounts) {
  if (err)
    return fn(err);

  accounts = _.map(accounts, function(account) {
    return new Accounts._model(account);
  });

  fn(null, accounts);
});