• API
  • pg.Result

每次成功查询都会返回 pg.Result 形状。

¥The pg.Result shape is returned for every successful query.

note: you cannot instantiate this directly

properties

result.rows: Array<any>

每个结果都有一个行数组。如果没有返回任何行,则数组将为空。否则,数组将包含查询返回的每一行的一个项目。默认情况下,node-postgres 会创建一个从名称到每列值的映射,为每一行返回一个类似 json 的对象。

¥Every result will have a rows array. If no rows are returned the array will be empty. Otherwise the array will contain one item for each row returned from the query. By default node-postgres creates a map from the name to value of each column, giving you a json-like object back for each row.

result.fields: Array<FieldInfo>

每个结果都有一个字段数组。此数组包含结果中每个字段的 namedataTypeID。如果你使用 arrayMode 进行查询,这些字段的顺序与列的顺序相同:

¥Every result will have a fields array. This array contains the name and dataTypeID of each field in the result. These fields are ordered in the same order as the columns if you are using arrayMode for the query:

import pg from 'pg'
const { Pool } = pg
 
const pool = new Pool()
 
const client = await pool.connect()
const result = await client.query({
  rowMode: 'array',
  text: 'SELECT 1 as one, 2 as two;',
})
console.log(result.fields[0].name) // one
console.log(result.fields[1].name) // two
console.log(result.rows) // [ [ 1, 2 ] ]
await client.end()

result.command: string

最后执行的命令类型:INSERT UPDATE CREATE SELECT 等。

¥The command type last executed: INSERT UPDATE CREATE SELECT etc.

result.rowCount: int | null

最后一条命令处理的行数。对于从不影响行的命令,例如 LOCK 命令,可以是 null。更具体地说,某些命令(包括 LOCK)仅返回形式为 COMMAND 的命令标签,而没有任何要解析的 [ROWS] 字段。对于此类命令,rowCount 将是 null

¥The number of rows processed by the last command. Can be null for commands that never affect rows, such as the LOCK-command. More specifically, some commands, including LOCK, only return a command tag of the form COMMAND, without any [ROWS]-field to parse. For such commands rowCount will be null.

注意:这不反映查询返回的行数。例如更新语句可以更新许多行(因此 result.rowCount 值很高),但 result.rows.length 将为零。要检查 SELECT 查询上的空查询响应,请使用 result.rows.length === 0

¥note: this does not reflect the number of rows returned from a query. e.g. an update statement could update many rows (so high result.rowCount value) but result.rows.length would be zero. To check for an empty query response on a SELECT query use result.rows.length === 0.

@sehrope 有一个很好的解释:

¥@sehrope has a good explanation:

rowCount 由 PostgreSQL 服务器提供的命令标记填充。它通常采用以下形式:COMMAND [OID] [ROWS]

¥The rowCount is populated from the command tag supplied by the PostgreSQL server. It's generally of the form: COMMAND [OID] [ROWS]

对于 DML 命令(INSERT、UPDATE 等),它反映了服务器为处理该命令修改了多少行。对于 SELECT 或 COPY 命令,它反映了检索或复制了多少行。有关具体信息的更多信息,请点击此处:https://www.postgresql.org/docs/current/protocol-message-formats.html(搜索 CommandComplete 以获取消息类型)

¥For DML commands (INSERT, UPDATE, etc), it reflects how many rows the server modified to process the command. For SELECT or COPY commands it reflects how many rows were retrieved or copied. More info on the specifics here: https://www.postgresql.org/docs/current/protocol-message-formats.html (search for CommandComplete for the message type)

文档中关于差异的注释是因为该值由服务器控制。非标准服务器(例如:PostgreSQL fork)或未来的服务器版本在某些情况下可能会提供不同的信息,因此最好不要依赖它来假设行数组长度与 rowCount 匹配。不过,将它用于 DML 计数是可以的。

¥The note in the docs about the difference is because that value is controlled by the server. It's possible for a non-standard server (ex: PostgreSQL fork) or a server version in the future to provide different information in some situations so it'd be best not to rely on it to assume that the rows array length matches the rowCount. It's fine to use it for DML counts though.

Last updated on August 24, 2024