可以使用 QueryFile 添加动态 WHERE 子句吗?

ber*_*rgy 5 postgresql node.js pg-promise

我有一个复杂的查询存储在 SQL 文件中,我想将其重用于各种路由,但根据路由更改 WHERE 子句。这将代替在多个文件中进行大型复杂查询,唯一的区别是 WHERE 语句。

使用QueryFile时是否可以动态添加WHERE?简化示例如下:

SELECT "id", "userId", "locationId", "title", "body",
  (
    SELECT row_to_json(sqUser)
    FROM (
      SELECT "id", "firstname", "lastname"
      FROM "users"
          WHERE "users"."id" = "todos"."userId"
      ) sqUser
  ) as "user"
FROM "todos"
Run Code Online (Sandbox Code Playgroud)
const queryIndex = new pgp.QueryFile('sql/todos/index.pgsql', queryOptions);
// 1. Use as is to get a list of all todos
// 2. OR Append WHERE "locationId" = $1 to get list filtered by location
// 3. OR Append WHERE "id" = $1 to get a specific item
// without having three separate SQL files?
Run Code Online (Sandbox Code Playgroud)

看起来(也许?)您可以在查询文件中添加以下内容,但这仍然感觉受到限制(仍然需要两个文件,=并且LIKE它仍然仅限于一个 WHERE 条件)。执行类似 WHERE 1 = 1 的操作来返回所有记录也感觉很奇怪。

const queryIndex = new pgp.QueryFile('sql/todos/index.pgsql', queryOptions);
// 1. Use as is to get a list of all todos
// 2. OR Append WHERE "locationId" = $1 to get list filtered by location
// 3. OR Append WHERE "id" = $1 to get a specific item
// without having three separate SQL files?
Run Code Online (Sandbox Code Playgroud)

我很想听听人们对此的想法或者是否有更好的方法。

vit*_*y-t 5

您可以将动态条件作为原始文本注入查询文件中:

SELECT "id", "userId", "locationId", "title", "body",
  (
    SELECT row_to_json(sqUser)
    FROM (
      SELECT "id", "firstname", "lastname"
      FROM "users"
          ${condition:raw}
      ) sqUser
  ) as "user"
FROM "todos"
Run Code Online (Sandbox Code Playgroud)

根据条件预先格式化参数:

// Generate a condition, based on the business logic:
const condition = pgp.as.format('WHERE col_1 = $1 AND col_2 = $2', [111, 222]);
Run Code Online (Sandbox Code Playgroud)

执行您的查询文件:

await db.any(myQueryFile, {condition});
Run Code Online (Sandbox Code Playgroud)

先进的

以上是当您想要在代码中生成一个简单的动态条件时的场景。但有时您可能需要改变复杂的静态条件。在这种情况下,您可以让主查询文件引用从属查询文件中的条件(开箱即用地支持嵌套查询文件)。在这种情况下,您甚至不需要使用:raw过滤器,因为默认情况下查询文件会作为原始文本注入:

主查询:

SELECT * FROM table ${condition}
Run Code Online (Sandbox Code Playgroud)

加载具有复杂条件的从属查询文件(当应用程序启动时):

const conditionQueryFile1 = new QueryFile(...);
const conditionQueryFile2 = new QueryFile(...);
Run Code Online (Sandbox Code Playgroud)

根据业务逻辑选择正确的从属查询:

const condition = conditionQueryFile1; // some business logic here;
Run Code Online (Sandbox Code Playgroud)

使用从站作为参数执行主站查询:

await db.any(myQueryFile, {condition});
Run Code Online (Sandbox Code Playgroud)