- 
          
- 
                Notifications
    You must be signed in to change notification settings 
- Fork 2
client.query()
        Oxford Harrison edited this page Nov 15, 2024 
        ·
        25 revisions
      
    DOCS • API • Client API
Run an arbitrary query.
client.query(
    query: string | Statement,
    values?: any[],
    options?: QueryOptions
): Promise<QueryResult>;client.query(
    query: string | Statement, 
    arg2?: {
        values?: any[];
    } & QueryOptions
): Promise<QueryResult>;client.query(
    arg: {
        query: string | Statement;
        values?: any[];
    } & QueryOptions
): Promise<QueryResult>;| Param | Interfaces | Description | 
|---|---|---|
| query | Statement | An SQL statement (string) or a query instance. | 
| values? | - | For non-DDL operations, optional values for parameters in the query. | 
| options? | QueryOptions | Optional extra parameters for the query. | 
| arg2? | - | Optional argument for a two-parameter call pattern. | 
| arg? | Statement | Optional argument for a single-parameter call pattern. | 
type Statement = 
    | SelectStatement 
    | InsertStatement 
    | UpsertStatement 
    | UpdateStatement 
    | DeleteStatement 
    | CreateDatabase 
    | RenameDatabase 
    | AlterDatabase 
    | DropDatabase
    | CreateTable 
    | RenameTable 
    | AlterTable 
    | DropTable;| Interface | Description | 
|---|---|
| SelectStatement | A SELECTstatement interface. | 
| InsertStatement | An INSERTstatement interface. | 
| UpsertStatement | An UPSERTstatement interface. | 
| UpdateStatement | An UPDATEstatement interface. | 
| DeleteStatement | A DELETEstatement interface. | 
| CreateDatabase | A CREATE DATABASEinterface. | 
| RenameDatabase | A RENAME DATABASEinterface. | 
| AlterDatabase | An ALTER DATABASEinterface. | 
| DropDatabase | A DROP DATABASEinterface. | 
| CreateTable | A CREATE TABLEinterface. | 
| RenameTable | A RENAME TABLEinterface. | 
| AlterTable | An ALTER TABLEinterface. | 
| DropTable | A DROP TABLEinterface. | 
interface QueryOptions {
    desc?: string;
    inspect?: boolean;
}| Param | Interfaces | Description | 
|---|---|---|
| desc? | - | Applicable to DDL operations; an optional commit description. Required if the Linked DB's config.require_commit_descsis active. | 
| inspect? | - | An optional directive to log details of the query to the console. | 
type QueryResult = Array<object> | number | DDLResult | null;| Type | Interfaces | Description | 
|---|---|---|
| Array<object> | - | An array of objects—the standard result type for DQL operations ( SELECT), and DML operations (INSERT,UPDATE,DELETE) with aRETURNINGclause. | 
| number | - | A number indicating total number of rows processed by the query for DML operations ( INSERT,UPDATE,DELETE) without aRETURNINGclause. | 
| DDLResult | DDLResult | The result type for DDL operations ( CREATE,ALTER,DROP,RENAME). | 
| Null | - | The default result type for all other types of query. | 
type DDLResult = boolean | SchemaInterface | Savepoint | null;| Type | Interfaces | Description | 
|---|---|---|
| boolean | - | The boolean true-the default result type for DDL operations without aRETURNINGclause. | 
| SchemaInterface | - | For an operation with a RETURNINGclause set toSCHEMA-the corresponding schema instance of the DB object manipulated by the operation. | 
| Savepoint | null | Savepoint | For an operation with a RETURNINGclause set toSAVEPOINT-theSavepointinstance associated with the DDL operation;nullwhen savepoint creation has been disabled at the server level. | 
Three-parameter call pattern:
// Each parameter passed distinctly
await client.query(
    `SELECT * FROM users WHERE name = $1`,
    ['John'],
    options
);Two-parameter call pattern:
// Values passed via second parameter
await client.query(
    `SELECT * FROM users WHERE name = $1`,
    { values: ['John'], ...options }
);Single-parameter call pattern:
// Everything in an object
await client.query({
    query: `SELECT * FROM users WHERE name = $1`,
    values: ['John'],
    ...options
});Pass relevant additional options to a query:
// Inspect query in the console
const rows = await client.query(
    `ALTER TABLE users 
    MODIFY COLUMN id int`,
    { desc: 'Query description', inspect: true }
);Run a DML operation (CREATE, ALTER, DROP, RENAME) and get back a reference to the savepoint associated with it (See ➞ Automatic-Schema-Versioning):
// Savepoint as return type
const savepoint = await client.query(
    `ALTER TABLE users
        RENAME TO accounts
    RETURNING SAVEPOINT`
);
console.log(savepoint.versionTag()); // number
await savepoint.rollback(); // trueor a DQL operation (SELECT), and get back a result set:
// Array as return type
const rows = await client.query(
    `SELECT * FROM users
    WHERE id = 4`
);
console.log(rows.length); // 1or a DML operation (INSERT, UPDATE, DELETE) with a RETURNING clause, and get back a result set:
// Array as return type
const rows = await client.query(
    `INSERT INTO users
    SET name = 'John Doe'
    RETURNING id`
);
console.log(rows.length); // 1or same DML operation without a RETURNING clause, and get back a number indicating the number of rows processed by the query:
// Number as return type
const rowCount = await client.query(
    `INSERT INTO users
    SET name = 'John Doe'`
);
console.log(rowCount); // 1