Skip to main content Link Menu Expand (external link) Document Search Copy Copied

Importer

Easily embed our CSV importer within your app.

Table of contents

  1. The Basics
  2. Install
  3. Usage
    1. Template
    2. Schema
  4. Transform
    1. Mutate
      1. Mutate record
      2. Batch mutate records
    2. Validate
      1. Error
      2. Warn
  5. Schema Header Fields
    1. TEXT
    2. EMAIL
    3. CHOICE
    4. BOOLEAN
    5. NUMBER
    6. DATETIME

The Basics

There are two ways to integrate an importer. You can load an importer by using a template key corresponding to a pre-made template created in builder, or by utilizing a schema key and manually coding out custom column headers with rules and validations for your importer.

View Schema Demo

Install

npm:

npm i @mightymerge/client --save

browser:

<script src="https://unpkg.com/@mightymerge/client/dist/mightymerge-client.min.js"></script>

Usage

Template

import mightymergeClient from '@mightymerge/client'

var importer = mightymergeClient({
  templateKey: 'TEMPLATE_KEY',
})

importer.open({
  chunk: function (chunk) {
    for (var i = 0; i < chunk.records.length; i++) {
      if (chunk.records[i].valid == true) {
        // do something with valid record
      }
    }
  },
  complete: function () {
    // do something on complete
  },
})

Schema

import mightymergeClient from '@mightymerge/client'

var importer = mightymergeClient({
  schemaKey: 'SCHEMA_KEY',
})

importer.open({
  schema: {
    headers: [
      {
        text: {
          label: 'name',
          display: 'Name',
          maxLength: 55,
          required: true,
        },
      },
      // ...etc
    ],
  },
  chunk: function (chunk) {
    for (var i = 0; i < chunk.records.length; i++) {
      if (chunk.records[i].valid == true) {
        // do something with valid record
      }
    }
  },
  complete: function () {
    // do something on complete
  },
})

Transform

Mutate

Mutate record

importer.open({
  ...
  mutateRecord: function (record) {
    record.data["fullname"] = `${record.data["first"]} ${record.data["last"]}`
    return record
  },
})

Batch mutate records

importer.open({
  ...
  chunkSize: 1000, // Default: 100; Max: 1000
  mutate: function (chunk) {
    return chunk.records.map(record => {
      record.data["fullname"] = `${record.data["first"]} ${record.data["last"]}`
    })    
  },
})

Validate

Error

importer.open({
  ...
  mutateRecord: function (record) {
    if (record.data["last"] == '') {
      record.error["last"] = "Last name is required"
    }
    return record
  },
})

Warn

importer.open({
  ...
  mutateRecord: function (record) {
    if (record.data["last"] == '') {
      record.warn["last"] = "Missing last name"
    }
    return record
  },
  ...
})

Schema Header Fields

TEXT

{
  // Accepts any text input
  text: { ... }
}
Name Value Default Description
label string null header label of final output
display string (same as label) Display to user
description string null Adds a description tipsy to the header column
maxLength number null Max length of input
minLength number null Min length of input
required boolean false Is field required
regex object {} Validate on regex {match: "/example/i", message: "Value must include 'example'"}

EMAIL

{
  // Validates against email format
  email: { ... }
} 
Name Value Default Description
label string null header label of final output
display string (same as label) Display to user
description string null Adds a description tipsy to the header column
maxLength number null Max length of input
minLength number null Min length of input
required boolean false Is field required
regex object {} Validate on regex {match: "/example/i", message: "Value must include 'example'"}

CHOICE

{ 
  // Validates against exclusive value options
  choice: { ... }
}
Name Value Default Description
label string null Key header label of final output
display string (same as label) Display to user
description string null Adds a description tipsy to the header column
values array [] List of available options
required boolean false Is field required

BOOLEAN

{ 
  // Validates against having a proper boolean value
  boolean: { ... }
}
Name Value Default Description
label string null header label of final output
display string (same as label) Display to user
description string null Adds a description tipsy to the header column
trueOutput string ‘TRUE’ Output when input value is true
falseOutput string ‘FALSE’ Output when input value is false
required boolean false Is field required

NUMBER

{
  // Validates against having correct number value
  number: { ... }
}
Name Value Default Description
label string null header label of final output
display string (same as label) Display to user
description string null Adds a description tipsy to the header column
maxValue number null Max number value of input
minValue number null Min number value of input
required boolean false Is field required

DATETIME

{
  // Validates against having a valid datetime
  datetime: { ... }
}
Name Value Default Description
label string null Header label of final output
display string (same as label) Display to user
description string null Adds a description tipsy to the header column
format string ‘YYYY-MM-DD HH:mm:ss a’ Auto formats date output
required boolean false Is field required