如何 select 加入 vapor 4 中的所有列 table?

How to select all columns from a join table in vapor 4?

vapor 似乎添加了新功能 eagerLoad 并删除了 alsoDecode。对于那些拥有亲子或兄弟姐妹关系的人来说很方便。但不适合那些没有关系的人。

我想实现一个树结构,其节点不能(或者我不知道如何)参与关系。节点有一个父节点和许多也是节点的子节点。

所以我有这个结构的三个表。

Tree:

| Field       | Type            |  
| ----------- | --------------- |  
| id          | UUID?           |  
| name        | String          |  
| nodes       | [Node]          |
| paths       | [Path]          |

Nodes:
| Field         | Type                       |  
| ------------- | -------------------------- |  
| id            | UUID?                      |  
| type          | NodeType(root, leaf, node) |    
| tree          | Tree                       |

Path:
| Field        | Type      |  
| ------------ | --------- |  
| id           | UUID?     |  
| distance     | Int       |  
| ancestorID   | UUID      |  
| descendantID | UUID      |  
| tree         | Tree      |

问题是 如果我想做

SELECT Nodes.id, Nodes.type, Path.ancestorID from Nodes
INNER JOIN Path
ON Nodes.id = Path.descendantID

如何编写代码。

您也可以选择投射到 SQLDatabase。 确保也导入 SQLKit,Fluent 数据库始终可以转换为 SQLDatabase。

例如:let sqlDb = req.db as? SQLDatabase 将使您能够使用自定义查询,例如:sqlDb?.select().from("Nodes").join("Path", on: "Nodes.id = Path.descendantId").all()

有关 SQLKit 的更多信息,请参阅:https://github.com/vapor/sql-kit

参考:https://docs.vapor.codes/4.0/fluent/advanced/(任何 Fluent Database 都可以转换为 SQLDatabase。这包括 req.db、app.db、传递给 Migration 的数据库等)