Utils for load data from different formats. You can find many examples of the use inside models _initialData
folders.
Example:
const csvLoader = require('@unitybase/base').dataLoader
conn = session.connection;
csvLoader.loadSimpleCSVData(conn, path.join(__dirname, 'ubm_enum-CDN.csv'),
'ubm_enum', 'eGroup;code;name;sortOrder'.split(';'), [0, 1, 2, 3]
)
Example 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
loadArrayData(conn, dataArray, entityName, ettAttributes, mappingopt, transLenopt)
inner
Load data from a array (rows) of array (columns data).
Arguments:
-
conn
(SyncConnection)
 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
If argument is not passed, it defaults to all ettAttributes passed "as is".
-
[transLen=1000]
(Number)
 Maximum rows count to be inserted on the single database transaction
loadSimpleCSVData(conn, fileName, entityName, ettAttributes, mappingopt, startRowopt, delimiteropt, transLen)
inner
Load data from CSV with delimiter (";" by default)
Arguments:
-
conn
(SyncConnection)
 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 one of:
- 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
If argument is not passed, it defaults to all ettAttributes passed "as is".
-
[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
localizeEntity(session, config, locale)
inner
Perform localization of entities data based on config & locale. See *.js in models _initialData/locale
folder for usage samples.
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")
Example
const loader = require('@unitybase/base').dataLoader
let 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)
lookup(conn, entityName, attributeName, colIndex, doNotUseCacheopt) → 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
(SyncConnection)
-
entityName
(string)
-
attributeName
(string|Array.<string>)
 Attribute name or array of names
-
colIndex
(number|Array.<number>)
 Column index or indexes
-
[doNotUseCache]
(boolean)
 Option to skip using cache on lookup. Use, when entity refer itself and next rows may use previous.
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
(SyncConnection)
 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
If argument is not passed, it defaults to all ettAttributes passed "as is".
[transLen=1000]
(Number)
 Maximum rows count to be inserted on the single database transaction
conn
(SyncConnection)
 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 one of:
- 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
If argument is not passed, it defaults to all ettAttributes passed "as is".
[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
_initialData/locale
folder for usage samples.
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")
const loader = require('@unitybase/base').dataLoader
let 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)
conn
(SyncConnection)
entityName
(string)
attributeName
(string|Array.<string>)
 Attribute name or array of names
colIndex
(number|Array.<number>)
 Column index or indexes
[doNotUseCache]
(boolean)
 Option to skip using cache on lookup. Use, when entity refer itself and next rows may use previous.
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
)