Home > Javascript Server-side Reference > SQL
Module Identifier
erbix/query
- Overview
- Query Objects
- JavaScript Database Connectivity Package
- Obtaining Database Connections
- Executing Statements With execute Method
- Retrieving Results With queryForList Method
Overview
The erbix/query module is a DRAFT API. It is not standardized and currently it is Erbix-specific.
The erbix/query module is used in order to interact with the account's database. Each Erbix account is linked to a dedicated database instance powered by an SQL engine.
back to summaryQuery Objects
Erbix uses query objects to identify connections to the database.
Each time you want to send an SQL command to the database server or to get back results, you'll need a query object. It can be obtained using the newQuery() function:
var myQuery = require("erbix/query").newQuery();
back to summary
JavaScript Database Connectivity (JSDBC)
The jsdbc is an accessor for the JavaScript Database Connectivity class package. It should be used to access static fields.
require("erbix/query").jsdbc.Statement.RETURN_GENERATED_KEYS;
back to summary
Obtaining Database Connections
The getConnection function returns the Connection object to the account's PostgreSQL database. See JavaScript Database Connectivity documentation for more details.
var conn = require("erbix/query").getConnection();
-OR-
var query = require("erbix/query").newQuery();
var conn = query.getConnection();
back to summary
Executing Statements With execute Method
execute(sql, params) executes a prepared statement.
- parameter sql: a string that may contain any number of '?' parameter placeholders.
- parameter params (optional): an array of objects in equal number as the '?' placeholders in sql parameter.
- return: null in case of success or a string with the error message in case of failure
Here's an example for executing an insert:
var query = require("erbix/query");
var myQuery = query.newQuery();
myQuery.execute(
"INSERT INTO table_name(name, email, message) VALUES(?, ?, ?);",
["Simon", "simon@gmail.com", "Hello!"]
);
back to summary
Retrieving Results With queryForList Method
queryForList(sql, params) executes a prepared statement that returns a single result set.
- parameter sql: a string that may contain any number of '?' parameter placeholders.
- parameter params (optional): an array of objects in equal number as the '?' placeholders in sql parameter.
- return: 1) if successful: an array of hashes (one hash per row result) having as keys the column names and as values the data contained in each row result column. 2) on error: a string with the error message.
var query = require("erbix/query");
var myQuery = query.newQuery();
var result = myQuery.queryForList(
"SELECT id, name FROM persons WHERE age > ? AND surname = ?",
[20, 'Mark']);
