Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 34 Next »

The pjs.define() API is used within Profound.js modules to declare fields, arrays, and data structures with a strong data type. All IBM i data types are available.

To declare fields that are loosely typed, use the var clause in JavaScript instead.

Parameters
  1. Field name (String)
  2. Config (Object)
Config object

A config object is required when defining fields. It is what gives your field attributes, like type, length, and decimal places. The following config properties are available:

Property nameProperty typeDescription
typeString

The following data types can be specified:

  • "packed decimal" or "packed" (both variations have the same meaning)
  • "decimal" or "zoned" or "zoned decimal" or "numeric" (all variations have the same meaning)
  • "integer"
  • "unsigned integer"
  • "char"
  • "boolean"
  • "date"
  • "time"
  • "timestamp"
  • "pointer"
  • "data structure"

Some config properties only apply to certain types.

lengthNumber

Specifies the field length. It is required for most data types; exceptions are:

  • date
  • time
  • timestamp
  • pointer
  • data structure
decimalsNumber

Specifies the number of decimal positions. It required for packed and zoned fields.

varyingBooleanSpecifies that a character field is of varying length; otherwise, the field will be a fixed length field padded with spaces.
initValueString/Number/Boolean/ArraySpecified the field's initial value.
dimNumberThis property turns the definition into an array. When declared this way, arrays start with an index of 1, similar to RPG.
orderByString

Specifies the default sort order for arrays declared with the dim property. The orderBy property is used by array-related API, such as pjs.lookup().

Possible values are:

  • "ASCEND"
  • "DESCEND"
elementsObjectSpecifies an object of subfields for a data structure. Each property within the object is the subfield name, while the property value is another config object.
qualifiedBoolean

Identifies a data structure as qualified, meaning that all references to its subfields must be qualified with the data structure name first; for example: MyDS.mysubfield = "myvalue". This makes the data structure similar to a JavaScript object.

If qualified is not specified, the subfields can be referenced directly; for example: mysubfield = "myvalue".

specialString

Identifies a special subfield and can only be specified on a data structure subfield configuration with the elements property.

Possible values are:

For a Status Data Structure (statusDS):

  • "*status" - Status Code (commonly used to identify errors)
  • "*proc" - Module Name
  • "*parms" - Number of parameters passed
  • "*routine" - Name of the routine in which an exception or an error occurred

For an Information Data Structure (infDS):

  • "*file" - The first 8 characters of a file name
  • "*record" - The first 8 characters of a record format name being processed
  • "*opcode" - Operation
  • "*status" - Status Code
  •  "*routine" - The first 8 characters of the name of a routine in which a file operation was done
nullableBoolean / String

Use this property to create null-capable fields.

  • Use true to just make the field null-capable. This option allows you to assign a null value to the field.
  • Use a string to specify a reference a Boolean field to use as the null-indicator and to make the field null-capable.
likeRecObject

Specified on a data structure Available when using a data structure type. This object requires two elements.

  • record - internal record format name from a previously defined table.
  • fields - optional String value of "*key". If "*key" is specified, only key fields will be defined.

When likeRec is specified, the data structure becomes qualified automatically.

likeDSStringUsed on a data structure to specify the name of another data structure from which the configuration is derived.
dataAreaString

Specifies a data area object to be associated with the declared field. The name of the data area may include a library. For example:

  • "MYDTAARA" (data area will be searched for using the pathlist / IBM i library list because the library name was not specified)
  • "PRODLIB/MYDTAATA" (the library name is explicitly specified here)
statusDSBooleanIdentifies a Status Data Structure. A Status Data Structure is automatically populated with information about the current Profound.js module and job. It is an easy way to retrieve information, such as the current IBM i job name, the current user id, and more.
Examples
Example 1: packed decimal
pjs.define("Num", { type: 'packed decimal', length: 3, decimals: 2 });
Num = 5;
Example 2: date with initValue
pjs.define("loandate", { type: 'date', initValue: pjs.date('2000-01-01') });
Example 3: char field with pointer to that field
pjs.define("stg", { type: 'char', length: 4 });
pjs.define("ptr", { type: 'pointer', initValue: pjs.getBuffer("stg") });
Example 4: char field array with initValue

This does mean that every element in this array will be initilized with the initValue.

pjs.define("arr", { type: 'char', length: 10, dim: 3, initValue: '0123456789' });

Example 5: Data structure with two subfields (non-qualified)

pjs.define("someDS", { type: 'data structure', elements: {
  "Field1": { type: 'char', length: 10, initValue: '1' },
  "Field2": { type: 'char', length: 10 }
}});
 
Field2 = '2';
Example 6: Data structure array, qualified and two subfields
pjs.define("dsarr", { type: 'data structure', qualified: true, dim: 2, elements: {
  "field1": { type: 'char', length: 1 },
  "field2": { type: 'char', length: 1 }
}});
 
dsarr[1].field1 = '1';
dsarr[1].field2 = '2';
dsarr[2].field1 = '3';
dsarr[2].field2 = '4';
Example 7: Null-capable field with null assignment
pjs.define("myNumber", { 
  type: 'packed decimal',
  length: 10,
  decimals: 0,
  nullable: true 
});

//Set value to 5
myNumber = 5;

//Set null-indicator to true
myNumber = null;

//Set value to 10, sets the null-indicator to false
myNumber = 10;

//Set null-indicator to true
pjs.nullInd(myNumber, true);
  • No labels