Page Contents
Home > @loopback/repository > DefaultCrudRepository > execute
DefaultCrudRepository.execute() method
Execute a SQL command.
**WARNING:** In general, it is always better to perform database actions through repository methods. Directly executing SQL may lead to unexpected results, corrupted data, security vulnerabilities and other issues.
Signature:
execute(command: Command, parameters: NamedParameters | PositionalParameters, options?: Options): Promise<AnyObject>;
Parameters
Parameter | Type | Description |
---|---|---|
command | Command | A parameterized SQL command or query. Check your database documentation for information on which characters to use as parameter placeholders. |
parameters | NamedParameters | PositionalParameters | List of parameter values to use. |
options | Options | Additional options, for example transaction . |
Returns:
Promise<AnyObject>
A promise which resolves to the command output as returned by the database driver. The output type (data structure) is database specific and often depends on the command executed.
Example
// MySQL
const result = await repo.execute(
'SELECT * FROM Products WHERE size > ?',
[42]
);
// PostgreSQL
const result = await repo.execute(
'SELECT * FROM Products WHERE size > $1',
[42]
);