Guides
By design, importOK will try to match the specified fields to the columns included in the uploaded file. This is done by calculating the Levenshtein distance between each field and header name.
While this approach is quite efficient, you might want to apply your own mapping strategy. For example, you could perform a heuristic analysis and try to match the fields to the headers based on the uploaded data itself. Alternatively, you could connect to your API and do the mapping on the server side.
1{2 mapper: function(fields, headers, records, hasNumericColumns) {3 ...4 }5}
The mapper can be an asynchronous or synchronous function that accepts the following arguments and returns the best possible mapping.
fields
Description | An array including all the field names defined in the Fields Schema |
---|---|
Type | string[] |
headers
Description | An array including all the header names included in the uploaded file |
---|---|
Type | string[] |
records
Description | An array including all the records to be imported, before any transformations or validations rules are applied. |
---|---|
Type | ImportRecord[] |
hasNumericColumns
Description | Indicates whether or not the uploaded file has numeric columns. |
---|---|
Type | boolean |
The mapper should return an object where the keys are the field names and the values are the corresponding header names. If a field cannot be mapped, the value must be null
.
Here is a more detailed example on how you can define a custom mapping strategy that maps headers to fields using an exact match approach:
1{ 2 mapper: (fields, headers, records, hasNumericHeaders) => { 3 if (hasNumericHeaders) { 4 return {}; 5 } 6 7 const mapping = {}; 8 fields.forEach((field) => { 9 const index = headers.indexOf(field);10 mapping[field] = index !== -1 ? headers[index] : null;11 });12 13 return mapping;14 }15}