Utils for load data from different formats. You can find many examples of the use inside models _initialData
folders.
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]
)
//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', value'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', value: row[1]});
if (!regionType){
throw new Error('Unknown region type ' + row[1]);
}
return regionType;
},
2, 2, 3
],
1, ','
)
Methods
# loadArrayData (conn: SyncConnection, dataArray: Array, entityName: String, ettAttributes: Array.<string>, mappingopt: Array.<(Number | Object | function())>, transLenopt: Number) inner
Load data from a array (rows) of array (columns data).
Arguments:
conn
: SyncConnectionConnection to UnityBase server
dataArray
: Arrayarray to load
entityName
: StringEntity 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: NumberMaximum rows count to be inserted on the single database transaction
# loadSimpleCSVData (conn: SyncConnection, fileName: String, entityName: String, ettAttributes: Array.<string>, mappingopt: Array.<(Number | Object | function())>, startRowopt: Number, delimiteropt: String, transLenopt: Number) inner
Load data from CSV with delimiter (";" by default)
Arguments:
conn
: SyncConnectionConnection to UnityBase server
fileName
: StringFull path to file
entityName
: StringEntity 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: NumberStart from this CSV file row
delimiter
= ';': StringCSV file delimiter
transLen
= 1000: NumberMaximum rows count to be inserted on the single database transaction
# localizeEntity (session: ServerSession, config: Object, locale: String) inner
Perform localization of entities data based on config & locale. See *.js in models _initialData/locale
folder for usage samples.
Arguments:
session
: ServerSessionconfig
: Objectentity
: StringEntity to localize
keyAttribute
: StringUnique 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
: StringLocale 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: 'Users'}},
{keyValue: 'UBS_MESSAGE_TYPE;system', execParams: {name: 'System'}},
{keyValue: 'UBS_MESSAGE_TYPE;warning', execParams: {name: 'Warning'}},
{keyValue: 'UBS_MESSAGE_TYPE;information', execParams: {name: 'Info'}}
]
}
loader.localizeEntity(session, localizationConfig, __filename)
# lookup (conn: SyncConnection, entityName: string, attributeName: string | Array.<string>, colIndex: number | Array.<number>, doNotUseCacheopt: boolean) → 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:
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
)