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 (connSyncConnection, dataArrayArray, entityNameString, ettAttributesArray.<string>, mappingoptArray.<(Number | Object | function())>, transLenoptNumber) 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 (connSyncConnection, fileNameString, entityNameString, ettAttributesArray.<string>, mappingoptArray.<(Number | Object | function())>, startRowoptNumber, delimiteroptString, transLenoptNumber) 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 (sessionServerSession, configObject, localeString) 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
    • entityString

      Entity to localize

    • keyAttributeString

      Unique key attribute (language independent) we search row for localize. If the key is a component that values should be separated by a ";"

    • localizationArray.<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: '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 (connSyncConnection, entityNamestring, attributeNamestring | Array.<string>, colIndexnumber | Array.<number>, doNotUseCacheoptboolean) → 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
  )