Utils for load data from different formats. You can find many examples of the use inside models _initialData
folders.
Sample:
csvLoader = require('@unitybase/base/dataLoader'),
conn = session.connection;
csvLoader.loadSimpleCSVData(conn, __dirname + '/ubm_enum-CDN.csv',
'ubm_enum', 'eGroup;code;name;sortOrder'.split(';'), [0, 1, 2, 3]
);
Sample with data transformation - in this case we pass transformation function to mapping array instead of CSV column index:
var ukraineID = conn.lookup('cdn_country', 'ID',
{expression: 'code', condition: 'equal', values: {code: 'UKR'}}
);
if (!ukraineID) {
throw new Error('Country with code UKR not found');
}
// CSV columns: code,regionType,name,fullName
// we map:
// - parentAdminUnitID to id of Ukraine (constant)
// - regionTypeID fo function what lookup region ID using region code from CSV file
csvLoader.loadSimpleCSVData(conn, __dirname + '/cdn_region_ukraine.csv', 'cdn_region',
['parentAdminUnitID', 'code', 'regionTypeID', 'name', 'caption', 'fullName'],
[
function(){return ukraineID;},
0,
function(row){
var regionType;
regionType = conn.lookup('cdn_regiontype', 'ID', {expression: 'code', condition: 'equal', values: {code: row[1]}});
if (!regionType){
throw new Error('Unknown region type ' + row[1]);
}
return regionType;
},
2, 2, 3
],
1, ','
);
Methods
loadSimpleCSVData(conn, fileName, entityName, ettAttributes, mapping, startRowopt, delimiteropt, transLen)
inner
Load data from CSV with delimiter (";" by default)
Arguments:
-
conn
(UBConnection)
 Connection to UnityBase server
-
fileName
(String)
 Full path to file
-
entityName
(String)
 Entity code to load data into
-
ettAttributes
(Array.<string>)
 Array of attribute codes
-
mapping
(Array.<(Number|Object|function())>)
 Mapping of CSV file columns to attributes. Can be:
- either numeric (zero based) index of column is CSV file
- or lookup configuration
- or function what take a array representing current row in CSW file on input and return a attribute value to bi inserted
-
[startRow=0]
(Number)
 Start from this CSV file row
-
[delimiter=';']
(String)
 CSV file delimiter
-
transLen=1000}
(Number)
 Maximum rows count to be inserted on the single database transaction
loadArrayData(conn, dataArray, entityName, ettAttributes, mappingopt, transLenopt)
inner
Load data from a array (rows) of array (columns data).
Arguments:
-
conn
(UBConnection)
 Connection to UnityBase server
-
dataArray
(Array)
 array to load
-
entityName
(String)
 Entity code to load data into
-
ettAttributes
(Array.<string>)
 Array of attribute codes
-
[mapping]
(Array.<(Number|Object|function())>)
 Mapping of CSV file columns to attributes. Can be one of:
- numeric (zero based) index of column is CSV file
- lookup configuration
- function (currentRowAsArray, newRecordID) what take a array representing current row in CSV file & new RecordID on input and return a attribute value to be inserted
-
[transLen=1000]
(Number)
 Maximum rows count to be inserted on the single database transaction
localizeEntity(session, config, locale)
inner
Perform localization of entities data based on config & locale. See *.js in models _initialData/locale
folder for usage samples.
var
loader = require('@unitybase/base/dataLoader'),
localizationConfig = {
entity: 'ubm_enum',
keyAttribute: 'eGroup;code',
localization: [
{keyValue: 'UBS_MESSAGE_TYPE;user', execParams: {name: 'Користувачів'}},
{keyValue: 'UBS_MESSAGE_TYPE;system', execParams: {name: 'Система'}},
{keyValue: 'UBS_MESSAGE_TYPE;warning', execParams: {name: 'Попереждення'}},
{keyValue: 'UBS_MESSAGE_TYPE;information', execParams: {name: 'Інформація'}}
]
};
loader.localizeEntity(session, localizationConfig, __filename);
Arguments:
-
session
(ServerSession)
-
config
(Object)
Properties
-
entity
(String)
 Entity to localize
-
keyAttribute
(String)
 Unique key attribute (language independent) we search row for localize. If the key is a component that values should be separated by a ";"
-
localization
(Array.<Object>)
 Array of object {keyValue: valueOfKeyAttribute}, execParams: {attrToLocalize1: 'localized value', ...}} If config.keyAttribute is complex, key value must be a ; separated string
-
locale
(String)
 Locale to localize to. Either locale file name (contain file start with locale^ (uk^my_data.js)) or locale ("uk")
lookup(conn, entityName, attributeName, colIndex) → function
inner
A helper for dataLoader. Resolves code to ID.
Supports combined keys, in that case, both "attributeName" and "colIndex" parameters shall be arrays.
Arguments:
-
conn
(UBConnection)
-
entityName
(string)
-
attributeName
(string|Array.<string>)
 Attribute name or array of names
-
colIndex
(number|Array.<number>)
 Column index or indexes
Example
loader.loadArrayData(
conn,
[
['employee1@example.com', 'users'],
['employee2@example.com', 'users'],
['manager1@example.com', 'users'],
['manager2@example.com', 'users']
],
'uba_userrole',
['userID', 'roleID'],
[
lookup(conn, 'uba_user', 'name', 0),
lookup(conn, 'uba_role', 'name', 1)
],
1000
)
conn
(UBConnection)
 Connection to UnityBase server
fileName
(String)
 Full path to file
entityName
(String)
 Entity code to load data into
ettAttributes
(Array.<string>)
 Array of attribute codes
mapping
(Array.<(Number|Object|function())>)
 Mapping of CSV file columns to attributes. Can be:
- either numeric (zero based) index of column is CSV file
- or lookup configuration
- or function what take a array representing current row in CSW file on input and return a attribute value to bi inserted
[startRow=0]
(Number)
 Start from this CSV file row
[delimiter=';']
(String)
 CSV file delimiter
transLen=1000}
(Number)
 Maximum rows count to be inserted on the single database transaction
conn
(UBConnection)
 Connection to UnityBase server
dataArray
(Array)
 array to load
entityName
(String)
 Entity code to load data into
ettAttributes
(Array.<string>)
 Array of attribute codes
[mapping]
(Array.<(Number|Object|function())>)
 Mapping of CSV file columns to attributes. Can be one of:
- numeric (zero based) index of column is CSV file
- lookup configuration
- function (currentRowAsArray, newRecordID) what take a array representing current row in CSV file & new RecordID on input and return a attribute value to be inserted
[transLen=1000]
(Number)
 Maximum rows count to be inserted on the single database transaction
Perform localization of entities data based on config & locale. See *.js in models _initialData/locale
folder for usage samples.
var
loader = require('@unitybase/base/dataLoader'),
localizationConfig = {
entity: 'ubm_enum',
keyAttribute: 'eGroup;code',
localization: [
{keyValue: 'UBS_MESSAGE_TYPE;user', execParams: {name: 'Користувачів'}},
{keyValue: 'UBS_MESSAGE_TYPE;system', execParams: {name: 'Система'}},
{keyValue: 'UBS_MESSAGE_TYPE;warning', execParams: {name: 'Попереждення'}},
{keyValue: 'UBS_MESSAGE_TYPE;information', execParams: {name: 'Інформація'}}
]
};
loader.localizeEntity(session, localizationConfig, __filename);
session
(ServerSession)
config
(Object)
Properties
-
entity
(String)
 Entity to localize -
keyAttribute
(String)
 Unique key attribute (language independent) we search row for localize. If the key is a component that values should be separated by a ";" -
localization
(Array.<Object>)
 Array of object {keyValue: valueOfKeyAttribute}, execParams: {attrToLocalize1: 'localized value', ...}} If config.keyAttribute is complex, key value must be a ; separated string
locale
(String)
 Locale to localize to. Either locale file name (contain file start with locale^ (uk^my_data.js)) or locale ("uk")
conn
(UBConnection)
entityName
(string)
attributeName
(string|Array.<string>)
 Attribute name or array of names
colIndex
(number|Array.<number>)
 Column index or indexes
loader.loadArrayData(
conn,
[
['employee1@example.com', 'users'],
['employee2@example.com', 'users'],
['manager1@example.com', 'users'],
['manager2@example.com', 'users']
],
'uba_userrole',
['userID', 'roleID'],
[
lookup(conn, 'uba_user', 'name', 0),
lookup(conn, 'uba_role', 'name', 1)
],
1000
)