-
-
Notifications
You must be signed in to change notification settings - Fork 2
client.query()
Oxford Harrison edited this page Nov 11, 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>;Description
Param Interfaces Description queryStatementAn SQL statement (string) or a query instance. values?- For non-DDL operations, optional values for parameters in the query. options?QueryOptionsOptional extra parameters for the query. arg2?- Optional argument for a two-parameter call pattern. arg?StatementOptional argument for a single-parameter call pattern.
type Statement =
| SelectStatement
| InsertStatement
| UpsertStatement
| UpdateStatement
| DeleteStatement
| CreateDatabase
| RenameDatabase
| AlterDatabase
| DropDatabase
| CreateTable
| RenameTable
| AlterTable
| DropTable;Description
Interface Description SelectStatementA SELECTstatement interface.InsertStatementAn INSERTstatement interface.UpsertStatementAn UPSERTstatement interface.UpdateStatementAn UPDATEstatement interface.DeleteStatementA DELETEstatement interface.CreateDatabaseA CREATE DATABASEinterface.RenameDatabaseA RENAME DATABASEinterface.AlterDatabaseAn ALTER DATABASEinterface.DropDatabaseA DROP DATABASEinterface.CreateTableA CREATE TABLEinterface.RenameTableA RENAME TABLEinterface.AlterTableAn ALTER TABLEinterface.DropTableA DROP TABLEinterface.
└ QueryOptions
interface QueryOptions {
desc?: string;
ref?: string;
noCreateSavepoint?: boolean;
inspect?: boolean;
}Description
Param Interfaces Description desc- Applicable to DDL operations; the commit description. desc- Applicable to DDL operations; the commit reference. noCreateSavepoint- Applicable to DDL operations; a directive to disable savepoint creation. inspect- A directive to log details of the query to the console.
type QueryResult = Array<object> | number | Savepoint | boolean | null;Description
Type Interfaces Description Array<object>- An array of objects—the standard result type when it's a DQL operation ( SELECT), or when it's a DML operation (INSERT,UPDATE,DELETE) with aRETURNINGclause.number- A number indicating total number of rows processed by the query when it's a DML operation without a RETURNINGclause.Savepoint
|booleanSavepointThe default result type when it's a DDL operation ( CREATE,ALTER,DROP,RENAME); the booleantruewhen savepoint creation has been disabled viaoptions.noCreateSavepointNull- The default result type for all other types of query.
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`
);
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