PostgreSQL
PostgreSQL
The PostgreSQL module is responsible for connecting to a PostgreSQL DBMS, performing queries and handling the results inside the robot.
Methods
connection
Method responsible for creating a connection with a PostgreSQL database.
const connection = await Database.PostgreSQL.connection["v1_0_0"]({
connectionString: `postgres://userRoberty:robertyBD123@localhost:5432/robertyBD`,
connectTimeout: 10,
queryTimeout: 60,
})
Required parameters
connectionString: String - expects a string with the connection parameters. The standard must be followed as in the example shown earlier.
Optional parameters
connectTimeout: Number - maximum time, in seconds, to wait for the server to accept the connection.queryTimeout: Number - default time limit, in seconds, for every query run on this connection.0= no limit.
Return
Returns the open connection, which must be stored in a variable (like connection in the example) and provided in the connection parameter of the other methods.
Version v2_0_0 of this method receives, instead of the connectionString, the connectionId of a connection configured in the workspace (Workspace → Connections), besides the same connectTimeout and queryTimeout:
const connection = await Database.PostgreSQL.connection["v2_0_0"]({
connectionId: "id-of-the-connection-configured-in-the-workspace",
connectTimeout: 10,
queryTimeout: 60,
})
close
Method responsible for closing an open connection with a PostgreSQL database.
await Database.PostgreSQL.close["v1_0_0"]({
connection: connection,
})
Required parameters
connection: Return of the connection action - expects the connection obtained through the connection action.
Optional parameters
This action has no optional parameters.
Return
This action has no returns.
transactionBegin
Method responsible for creating a transaction with the PostgreSQL DBMS.
await Database.PostgreSQL.transactionBegin["v1_0_0"]({
connection: connection,
})
Required parameters
connection: Return of the connection action - expects the connection obtained through the connection action.
Optional parameters
This action has no optional parameters.
Return
This action has no returns.
transactionCommit
Method responsible for committing a transaction opened by the transactionBegin action.
await Database.PostgreSQL.transactionCommit["v1_0_0"]({
connection: connection,
})
Required parameters
connection: Return of the connection action - expects the connection obtained through the connection action.
Optional parameters
This action has no optional parameters.
Return
This action has no returns.
transactionRollback
Method responsible for rolling back a transaction opened by the transactionBegin action.
await Database.PostgreSQL.transactionRollback["v1_0_0"]({
connection: connection,
})
Required parameters
connection: Return of the connection action - expects the connection obtained through the connection action.
Optional parameters
This action has no optional parameters.
Return
This action has no returns.
query
Method responsible for performing a query directly in the database through the connection obtained by the connection action.
const query = await Database.PostgreSQL.query["v1_0_0"]({
query: `SELECT * FROM tabela;`,
connection: connection,
timeout: 120
})
Required parameters
connection: Return of the connection action - expects the connection obtained through the connection action.query: String - expects a string with the query to be performed.
Optional parameters
timeout: Number - time limit, in seconds, for this query. When omitted, the connection'squeryTimeoutapplies.0= no limit. When the limit is reached, the promise rejects with an error whose message starts withTimeout. When neithertimeoutnor the connection'squeryTimeoutis provided, the limit is120seconds; in that case, and whenever the connection was opened withoutconnectTimeoutandqueryTimeout, the robot stops waiting but the query is not canceled on the server.
Return
The constant or variable created, such as the query of the example shown earlier, receives a value of the Object type. The object returned has the following properties:
query.query: String - returns the query that was used.query.results: Array - returns every result obtained with the query.query.resultsCount: Number - returns the number of rows obtained in the result.