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
Base properties and methods
Base properties and methods of the DataManager class are available in Table 1 and Table 2. ” article.
Table 1. The DataManager class properties
Name | Type | Description |
---|---|---|
dataStore | Object | The data collection repository. |
itemClassName | String | Name of the record class. Has the “Terrasoft.DataManagerItem” value. |
Table 2. Main methods of the DataManager class
Name | Parameters | Description |
---|---|---|
select |
config {Object} – configuration object; callback {Function} – callback function; scope {Object} – the callback function context. |
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. |
createItem |
config {Object} – configuration object; callback {Function} – callback function; scope {Object} – the callback function context. |
Creates a new record of the config.entitySchemaName type with the config.columnValues colomn values. |
addItem | item {Terrasoft.DataManagerItem} – added record. | Adds the item record to the schema data collection. |
findItem |
entitySchemaName {String} – data collection name; id {String} — record Id. |
Returns the record of the schema data collection with the entitySchemaName name and id Id. |
remove | item {Terrasoft.DataManagerItem} – deleted record. | Sets the isDeleted flag for the item record. The record will be deleted from the database after saving the changes. |
removeItem | item {Terrasoft.DataManagerItem} – deleted record. | Deletes the record from the schema data collection. |
update |
config {Object} – configuration object; callback {Function} – callback function; scope {Object} – the callback function context. |
Updates the record with the config.primaryColumnValue primary column value by the config.columnValues values. |
discardItem | item {Terrasoft.DataManagerItem} – a record with the canceled changes. | Cancels changes for the item record made in current working session with the DataManger object. |
save |
config {Object} – configuration object; callback {Function} – callback function; scope {Object} – the callback function context. |
Saves the schema data collections specified in the config.entitySchemaNames to the database. |
Base properties and methods of the DataManagerItem class are available in Table 3 and Table 4. ” article.
Table 3. The DataManagerItem class properties
Name | Type | Description |
---|---|---|
viewModel | Terrasoft.BaseViewMode | Object projection of the record in the database. |
Table 4. Main methods of the DataManagerItem class
Name | Parameters | Description |
---|---|---|
setColumnValue |
columnName {String} – column name; columnValue {String} – column value. |
Sets the new columnValue value for the columnName column. |
getColumnValue |
columnName {String} – column name; |
Returns the value of the columnName column. |
getValues | No. | Returns values of all record columns. |
remove | No. | Sets isDeleted flag to the record. |
discard | No. | Cancels changes for the record made in current working session with the DataManger object. |
save | No. | Saves changes in the database. |
getIsNew | No. | Returns the flag that the record is new. |
getIsChanged | No. | 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);