Import file contents as arrays of objects

Installation

npm install nxus-data-loader --save

Options

All responses accept an options argument, in addition to any parser-specific options you can indicate:

  • mapping: object of {field: newField} name mappings
  • identityFields: for importing to models, array of fields to be used for createOrUpdate query
  • truncate: for importing to models, true/false for deleting existing collection data before import. Ignored if identityFields is provided.
  • strict: defaults to true. Only import columns/data that matches the attribute names for the model. Set to false to import everything.

Events

You can modify the records during import with the following specific events:

  • records.type: e.g. app.get('data-loader').after('records.csv', (results) => {})
  • record.type: e.g. app.get('data-loader).after('record.csv', (one) => {})
  • models.identity: e.g. app.get('data-loader').after('models.user', (results) => {})
  • model.identity: e.g. app.get('data-loader).after('model.user', (user) => {})

record events occur after parsing and name mapping model events occur after record events and before models are created/updated.

API


Provide a parser for a particular type (file extension)

parser(type: string, handler: function)
Parameters
type (string) The type (e.g. 'html') this renderer should handle
handler (function) Function to receive (content, options) and return parsed array of result objects

Provide an exporter for a particular type (file extension)

exporter(type: string, handler: function)
Parameters
type (string) The type (e.g. 'html') this exporter creates
handler (function) Function to receive (content, options) and return formatted output content

Request formattted output based on type

export(type: string, records: [object], opts: object): Promise
Parameters
type (string) The type (e.g. 'html') of the output content
records ([object]) The records to export
opts (object) Options for the exporter context
Returns
Promise: String of formatted output

Request parsed results based on type

import(type: string, content: string, opts: object): Promise
Parameters
type (string) The type (e.g. 'html') of the content
content (string) The contents to parse
opts (object) Options for the parser context
Returns
Promise: Array of parsed result objects

importFile

src/index.js

Request parsed results from a file path

importFile(filename: string, opts: object): Promise
Parameters
filename (string) The filename to read and parse
opts (object) Options for the parser context
Returns
Promise: Array of parsed result objects

importToModel

src/index.js

Import string contents to a model

importToModel(model: string, type: string, content: string, opts: object): Promise
Parameters
model (string) The identity of the model to populate
type (string) The type (e.g. 'html') of the content
content (string) The contents to parse
opts (object) Options for the parser context
Returns
Promise: Array of instances

importFileToModel

src/index.js

Import file contents to a model

importFileToModel(model: string, filename: string, opts: object): Promise
Parameters
model (string) The identity of the model to populate
filename (string) The filename to read and parse
opts (object) Options for the parser context
Returns
Promise: Array of instances

_storeResultsWithModel

src/index.js

Simple do-storage function to help mock up storage for tests, other applications.

_storeResultsWithModel(model: [type], values: [type], uniqueCriteria: [type]): [type]
Parameters
model ([type]) [description]
values ([type]) [description]
uniqueCriteria ([type]) [description]
Returns
[type]: [description]

Import a data file as fixture data. Can specify environment option to only load for e.g. test

fixture(modelId: string, path: string, options: object)
Parameters
modelId (string) The identity of the model to import
path (string) The path to a file
options (object) Options to pass to data-loader.importFile