Skip to content

FourDModel Class

Julio Carneiro edited this page Nov 21, 2017 · 2 revisions

This service class is the Data Model base class. It provides the ability to retrieve, create, update or delete records from a 4D Database backend.

This class builds upon the Data Model functionality, as described in the Data Model wiki page.

All 4D table's Data Models extend this FourDModel class, describing each of the table's fields and providing getter/setter for each field.

Instances of this class, and its extensions, represent a record in the database.

This class provides the following public properties:

  • tableName: the 4D Table name
  • tableNumber: the 4D Table number
  • primaryKey_: the table's primary key field name
  • fields: an object array describing all fields in the table; the properties in each field description can be found in the Data Model documentation
  • fourdSaveCallbackMethod_: the name of a 4D method that will be called before a record is saved on 4D side, after an insert or update action; the method is called after all the fields in the record have been populated
  • fourdDeleteCallbackMethod_: the name of a 4D method that will be called before a record is deleted on 4D side; the record to be deleted will already be loaded and available; the method must return a boolean true/false to indicate if the record can be deleted

The FourDModel class provides the following public functions:

get() and set() Functions

The get() and set() functions are used to get/set records properties (fields) contents.

Those functions should be called in each of the table's fields getter/setter functions, as in:

    get SeasonName(): string { return this.get('SeasonName'); }
    set SeasonName(v: string) { this.set('SeasonName', v); }

Those functions allow the class to identify fields that have been modified during a session, which allows it to optimize updateRecord() calls, as only fields that have been indeed modified are sent in an update request.

getRecord() Function

Use this function to retrieve the contents of a 4D Record and fill in all the properties of a FourDModel instance. It sends a REST_LoadData request to 4D, and uses the response to populate the instance fields.

The function call takes up to 3 arguments, all optional, but at least one must be present and not be null:

  • recordNumber: the 4D Database Record Number, that identifies the record to retrieve
  • recordID: the value for the desired _record's Primary Key, which will be used to query the record to retrieve
  • query: a Query String to retrieve the desired record; if more than one record fulfills the query criteria, the first record retrieved by 4D will be used

The function returns a Promise, which the caller can subscribe to in order to perform some action after the record is received and data is ready.

refresh() Function

This function will simply reload the data for the current record in a FourDModel instance.

The function takes no arguments, and assumes a record has been previously loaded via getRecord(). It'll call 4D and retrieve the contents for the same record.

The function returns a Promise, which the caller can subscribe to in order to perform some action after the record is received and data is ready.

insertRecord() Function

This is the function that creates new records in a 4D Database. It sends a REST_PostData request, with an insert action.

The function call does not take any arguments and assumes the record instance has already been populated, including all mandatory fields.

The function returns a Promise, which the caller can subscribe to in order to perform some action after the record is added to the database. If the record has been accepted by 4D and has been added to the database, the instance's Primary Key field will have been populated, along with its Record Number.

updateRecord() Function

This is the function that updates a record in a 4D Database. It sends a REST_PostData request, with an update action.

The function call does not take any arguments and assumes the record instance has already been populated, and some fields have been modified.

The request sent to 4D will only include data for fields that have been modified. Fields that have not been touched will not be sent, thus saving on network traffic.

The function returns a Promise, which the caller can subscribe to in order to perform some action after the record is saved to the database.

deleteRecord() Function

This is the function to delete records from a 4D Database. It sends a REST_PostData request, with a delete action.

The function call takes one optional argument:

  • cascade: a boolean, if set to true it indicates a cascade delete, that is the selected record and any subordinates are to be deleted; optional argument, the default is false; (please use with care)

If a record has any subordinate records, that is records in other tables that relate to the record being deleted, the delete operation will fail. Unless the cascade argument is set to true, in which case all subordinate records will also be deleted.

The function returns a Promise, which the caller can subscribe to in order to perform some action after the record is deleted from the database.

getRecords() Function

Use this function to retrieve a list of records from a 4D Database, returning a FourDCollection. It sends a REST_GetRecords request to 4D, and uses the response to populate the records array.

The function call takes up to 6 arguments, all optional, except for the first one:

  • query: a Query String with the criteria for the desired record selection to retrieve
  • columns: an Array listing the fields to retrieve for each record; if present only those fields will be retrieved, not the entire record
  • startRec: the index of the first record from the record selection to return; default is 0, the first record
  • numOfRecords: the maximum number of records to retrieve; used for paging; default is -1, meaning no maximum set, retrieve all selected records (from the startRec point)
  • filterOptions: an additional Query String, to further reduce the resulting record selection; if set, the resulting record selection will have to meet both the query and the filterOptions criteria
  • orderBy: optional Order By clause to retrieve records in a desired sort order.
    • >table.field : to sort records by table.field in ascending order
    • <table.field : to sort records by table.field in descending order

The function returns a Promise, which the caller can subscribe to in order to perform some action after the record set is received. The Promise result is a FourDCollection holding the selected record set.


The FourDModel class also provides a set of utility functions.

recordNumber()

Function returns 4D Database record number, for the instance's current record.

isRecordLoaded()

Function returns true if a record is indeed loaded.

clearRecord()

This function will clear the record loaded, thus leaving the instance with no actual data.

recordIsDirty()

Function returns true if any field has been modified in the record instance, since it was last loaded.

clearRecordDirtyFlag()

This function will clear the record dirty flag.

getFieldProperties()

Function takes a field name argument, and returns the field description, as set in the fields Array.