@unitybase/base/dataLoader

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, ','
         );
Author:
  • pavel.mash

Methods

loadSimpleCSVData(conn, fileName, entityName, ettAttributes, mapping, startRowopt, delimiteropt, transLen) inner

Load data from CSV with delimiter (";" by default)
Arguments:
  1. conn (UBConnection)  Connection to UnityBase server
  2. fileName (String)  Full path to file
  3. entityName (String)  Entity code to load data into
  4. ettAttributes (Array.<string>)  Array of attribute codes
  5. 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
  6. [startRow=0] (Number)  Start from this CSV file row
  7. [delimiter=';'] (String)  CSV file delimiter
  8. 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:
  1. conn (UBConnection)  Connection to UnityBase server
  2. dataArray (Array)  array to load
  3. entityName (String)  Entity code to load data into
  4. ettAttributes (Array.<string>)  Array of attribute codes
  5. [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
  6. [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:
  1. session (ServerSession)
  2. config (Object)
    Properties
    1. entity (String)  Entity to localize
    2. 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 ";"
    3. localization (Array.<Object>)  Array of object {keyValue: valueOfKeyAttribute}, execParams: {attrToLocalize1: 'localized value', ...}} If config.keyAttribute is complex, key value must be a ; separated string
  3. 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:
  1. conn (UBConnection)
  2. entityName (string)
  3. attributeName (string|Array.<string>)  Attribute name or array of names
  4. 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
  )