...
The design of your XML data model and SQL table columns are the key point for the ease of use. When XML node and table column names identical, this method maps them automatically.
Map Function
Map function is used to transform values before saving. For example; encrypting a password before saving to database. See $Database.ExportToXml for more details.
Example
Code Block | ||
---|---|---|
| ||
$Database.ImportFromXml({ TargetSchema : 'Edoksis', TargetTable : 'Accounts', XPath : 'Accounts/Account', Map : function (xml) { var pass = xml.Evaluate('Password'); // if not marked as encrypted (means user has edited the password field) encrypt it if (!pass.startsWith('Enc:')) this.Password = $Crypto.Encrypt($EncryptionPassword, this.Id, xml.Evaluate('Password')); else // otherwise just remove the mark this.Password = pass.substr(4); } }); |
...
Code Block | ||
---|---|---|
| ||
$Database.ImportFromXml({ // Save employee
Parameters : {
TargetSchema : 'HR',
TargetTable : 'Employee'
},
XPath : 'Identities/Identity', // Find rows under Identities/Identity xpath
ColumnsXPath : 'Employee', // Fetch column values from Employee. Final xpath
Map : function(employeeNode) {
$Database.Get({ // Fetch matching records from database
Parameters : {
TargetSchema : 'HR',
TargetTable : 'OrganizationUnitPositionMembers'
},
Where : {
Criteria : [
{ Name : 'Employee', Value : employeeNode.Evaluate('Id') }, // "Employee must equal to Employee/Id xpath value."
{ Name : 'RegistryNumber', Value : '%2', Comparison : 'Like', Condition : 'Or' } // Another criteria just for sample. "or RegistryNumber must ends with 2"
]
}
})
.DeleteAll() // Delete existing all rows
.CreateNew(function() { // Create a new row
this.Employee = employeeNode.Evaluate('Id'); // Set Employee column to "Employee/Id" xpath value.
this.OrganizationUnitPosition = employeeNode.Evaluate('Employee/Position'); // Set OrganizationUnitPosition column to "Employee/Position" xpath value.
})
.Save(); // Save this table.
}
}); |
...