Record Level Access via SQL
Overview
Profound.js provides simple APIs to access your data on any of the supported databases, without having to write a lot of SQL.
To use this SQL interface, you record some key information about each file or table in a JavaScript file called a DbDefn module. The sections below describe what the interface supports, how to create the DbDefn module, and how existing Record Level Access code picks it up.
Supported Capabilities
Read, write, update, and delete capabilities.
Fetching records sequentially in either direction.
Fetching records by key.
Multi-part keys.
Use of data structures to fetch and update data.
Use of global record format fields to fetch and update data.
Options for qualified and non-qualified access.
Key data structures.
Updating fields selectively.
Top-down transactional I/O without requiring callbacks.
Access to INFDS (Information Data Structure) values.
Prepared statements.
Immediately executed statements.
Parameter binding.
Custom connection attributes.
Remote connections.
For more information about RLA capabilities, see Record Level Access API.
Unsupported Capabilities
The following are not supported:
Override database file.
Using files with level checking.
QTEMP library/schema.
Record locking.
Fetching records by relative record number.
Commit and rollback.
Some of the INFDS (Information Data Structure) fields.
Creating a DbDefn Module
For instance, if you have a file or table in your database named product, you would:
Enable DbDefn support within the configuration.
Name the module file
product.DbDefn.js.The module file name must end with
.DbDefn.js.The first part of the name (
product) is how your Profound.js programs will reference it.
Describe the table using the properties below.
DbDefn Properties
dbObject - Required. Set this to the actual database object name. It can be the name of a table or a view, and it does not need to match the module file name.
useSQL - Optional. Set this to
trueto enable all Profound.js modules to access this file using an SQL interface. This is only required if you are using an IBM i database; all other databases force this totrue.uniqueKey - Optional. Required if the table or view does not have a primary (unique) key defined, and required if this is an SQL view. If set, it is used to correctly read one record at a time in a unique order.
dbSchema - Optional. Set this to the actual database library or schema. You would normally not need to use this.
format - Optional. The record format name of the IBM i table. This is required when converting RPG programs to Profound.js programs.
overrides - Optional. This is where you can override specific field types and values.
dbConnection - Optional. A string that refers to a database connection name. If this property is not specified, the default database connection is used.
uniqueKey Sub-Properties
field - The database column name.
ascending -
trueorfalse, indicating whether the column should be sorted in ascending order. The default istrue.
When to Use Overrides
There are several reasons you may need overrides. To name a few:
The database does not support storage of that type of field (such as with Oracle and a Time type field).
The database stores dates as a number or string, but you want the UI to show them as true dates.
The database stores two separate fields for Date and Time, but you want to put them together as a single Date and Time field.
The database does not allow storage with that same definition (such as
char(1024), where some databases may require you to make that varying).The database does not support storing packed values, but the converted module is coded with packed fields.
Example: Using Overrides
The example below shows a couple of different ways you can use these overrides:
A field named
prcatidthat is stored in the database as character, but is referenced within the Profound.js programs as decimal.A field named
prtimethat is stored in the database as character, but is referenced within the Profound.js programs as time.
product.DbDefn.js
module.exports = {
// useSQL: true routes every pjs.defineTable("product") call through the SQL
// interface. Only needed on IBM i; every other database forces this on.
useSQL: true,
// The real table or view name. Does not have to match the file name.
dbObject: "product",
overrides: {
// Stored in the database as character, referenced in Profound.js as decimal.
prcatid: {
type: "decimal",
length: 5,
decimals: 0,
dbTransforms: {
// toDb runs on the way in: 42 -> "00042"
toDb: function(value) { return value.toString().padStart(5, "0"); },
// fromDb runs on the way out: "00042" -> 42
fromDb: function(value) { return Number(value); }
}
},
// Stored in the database as character, referenced in Profound.js as time.
// Profound.js ships helper transforms for this common case.
prtime: {
type: "time",
dbTransforms: {
toDb: profound.utils.timeToCharTransform,
fromDb: profound.utils.charToTimeTransform
}
},
// Force a fixed-length (non-varying) character column.
prdesc: {
varying: false
}
}
};Using the DbDefn Module
Once this module file is created, all Profound.js programs that call pjs.defineTable(...) will use an SQL interface to access the data.
If you are using an IBM i database, you can set
useSQLtofalse. This causes your Profound.js modules to go back to using the standard IBM i record level file operations.If you delete this DbDefn module file, the modules revert to using the standard IBM i record level file operation commands.
This DbDefn module file is required in order to use any of the RLA APIs for any non-IBM i database.
Example: Reading All Records
Here is a Profound.js program that reads all the records of a table. It is just like any other Profound.js module using RLA — you do not need to change any of your logic to switch from standard RLA functions to the RLA via SQL functions. Its purpose is to show that the DbDefn module, not the program, is what selects the SQL interface.
products.js
// modules/MYAPP/products.js
//
// Reads every row of the "product" table and prints them as JSON.
// This is ordinary Record Level Access code -- nothing here is SQL-specific.
// The only thing routing it through the SQL interface is the
// product.DbDefn.js module shown above.
//
// Prerequisites:
// - product.DbDefn.js exists, with useSQL: true
// - enableDbDefn is set to true in the Profound.js configuration file
// - the table has a unique key, or uniqueKey is set in the DbDefn module,
// so records can be read one at a time in a repeatable order
function products() {
// Open "product" for input. keyed: true is required in order to use
// positionTo() below.
pjs.defineTable("product", { read: true, keyed: true });
// Position before the first record. pjs.START means "start of file" and
// works whatever data type the key uses, so it is safer than passing a
// literal such as 0, which only works for a non-negative numeric key.
product.positionTo(pjs.START);
const data = [];
// fetch() is an alias of fetchNext(); both read the next record in key order.
let record = product.fetch();
// pjs.endOfData() reports the result of the LAST operation, so test it
// before using the record that was just read. When the fetch runs past the
// last record, endOfData() is true and "record" should be discarded.
while (!pjs.endOfData()) {
data.push(record);
record = product.fetch();
}
console.log(`Read ${data.length} product records.`);
console.log(JSON.stringify(data, null, 2));
}
exports.run = products;Running the module prints the record count followed by the rows as formatted JSON:
Expected output
Read 3 product records.
[
{
"prid": 1,
"prdesc": "Widget, small",
"prcatid": 10,
"prtime": "09:15:00"
},
...
]