The DataManager class
Glossary Item Box
Introduction
Sometimes It may be necessary to create, modify and delete entity data without saving these changes to the database in the process of working with the client part of the application. Saving changes to the database must take place when the save method is explicitly called. These functions are implemented in the DataManager and DataManagerItem classes.
The DataManager class is a singleton available through the Terrasoft global object. This class provides the dataStore repository. The contents of one or more database tables can be loaded into the repository. Example:
dataStore: { SysModule: sysModuleCollection, SysModuleEntity: sysModuleEntityCollection }
sysModuleCollection and sysModuleEntityCollection are the data collections of the DataManagerItem type of the SysModule and SysModuleEntity schemas. Each collection record is a record of the corresponding database table.
DataManager and DataManagerItem class diagram is available on Fig. 1.
Fig. 1 Class diagram
The “Terrasoft.manager.DataManager” class
Properties
Table 1. Primary properties of the DataManager class
- dataStore
- Object
-
The data collection repository.
- itemClassName
- String
-
Name of the record class. Has the Terrasoft.DataManagerItem value.
Methods
Table 2. Primary methods of the DataManager class
- select
-
If there is no data with the config.entitySchemaName name in the dataStore, then the method forms and executes the request to the database and returns the received data, or the method will return the data collection from the dataStore.
Parameters:
- config {Object} — configuration object;
- callback {Function} — callback function;
- scope {Object} — the callback function context.
- createItem
-
Creates a new record of the config.entitySchemaName type with the config.columnValues colomn values.
Parameters:
- config {Object} — configuration object;
- callback {Function} — callback function;
- scope {Object} — the callback function context.
- addItem
-
Adds the item record to the schema data collection.
Параметры:
- item {Terrasoft.DataManagerItem} — added record.
- findItem
-
Returns the record of the schema data collection with the entitySchemaName name and id Id.
Parameters:
- entitySchemaName {String} — data collection name;
- id {String} — record Id.
- remove
-
Sets the isDeleted flag for the item record. The record will be deleted from the database after saving the changes.
Parameters:
- item {Terrasoft.DataManagerItem} — deleted record.
- removeItem
-
Deletes the record from the schema data collection.
Parameters:
- item {Terrasoft.DataManagerItem} — deleted record.
- update
-
Updates the record with the config.primaryColumnValue primary column value by the config.columnValues values.
Parameters:
- config {Object} — configuration object;
- callback {Function} — callback function;
- scope {Object} — the callback function context.
- discardItem
-
Cancels changes for the item record made in current working session with the DataManger object.
Parameters:
- item {Terrasoft.DataManagerItem} — a record with the canceled changes.
- save
-
Saves the schema data collections specified in the config.entitySchemaNames to the database.
Parameters:
- config {Object} — configuration object;
- callback {Function} — callback function;
- scope {Object} — the callback function context.
The “Terrasoft.DataManagerItem” class
Properties
Table 3. Primary properties of the DataManagerItem class
- viewModel
- Terrasoft.BaseViewMode
-
Object projection of the record in the database.
Methods
Table 4. Primary methods of the DataManagerItem class
- setColumnValue
-
Sets the new columnValue value for the columnName column.
Parameters:
- columnName {String} — column name;
- columnValue {String} — column value.
- getColumnValue
-
Returns the value of the columnName column.
Parameters:
- columnName {String} — column name.
- getValues
-
Returns values of all record columns.
- remove
-
Sets isDeleted flag to the record.
- discard
-
Cancels changes for the record made in current working session with the DataManger object.
- save
-
Saves changes in the database.
- getIsNew
-
Returns the flag that the record is new.
- getIsChanged
-
Returns the flag that the record was modified.
Examples
Getting records from the [Contact] table:
// Definition of the configuration object. var config = { //Entity Schema Name. entitySchemaName: "Contact", // Remove duplicates in the resulting dataset. isDistinct: true }; // Receiving data. Terrasoft.DataManager.select(config, function (collection) { // Saving received records to local storage. collection.each(function (item) { Terrasoft.DataManager.addItem(item); }); }, this);
Adding new record to the DataManager object:
// Definition of the configuration object. var config = { // Entity Schema Name. entitySchemaName: "Contact", // Column values. columnValues: { Id: "00000000-0000-0000-0000-000000000001", Name: "Name1" } }; // Creating a new record. Terrasoft.DataManager.createItem(config, function (item) { Terrasoft.DataManager.addItem(item); }, this);
Getting the record and changing the column value:
// Getting a record. var item = Terrasoft.DataManager.findItem("Contact", "00000000-0000-0000-0000-000000000001"); // Setting a new value for "Name2" to the [Name] column. item.setColumnValue("Name", "Name2");
Deleting the record from the DataManager object:
// Definition of the configuration object. var config = { // Entity Schema Name. entitySchemaName: "Contact", // Primary column value. primaryColumnValue: "00000000-0000-0000-0000-000000000001" }; // Sets the isDeleted attribute for item. Terrasoft.DataManager.remove(config, function () { }, this);
Cancels changes made in current working session with the DataManger object.
// Getting a record. var item = Terrasoft.DataManager.findItem("Contact", "00000000-0000-0000-0000-000000000001"); // Undo changes for writing. Terrasoft.DataManager.discardItem(item);
Saves changes in the database.
// Definition of the configuration object. var config = { // Entity Schema Name. entitySchemaNames: ["Contact"] }; // Saving changes to the database. Terrasoft.DataManager.save(config, function () { }, this);