npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@botmatic/js-mapper

v1.0.0

Published

A mapping module for botmatic integrations. Maps properties from Botmatic to your own format, back and forth.

Downloads

3

Readme

JS Mapper

A mapping module for botmatic integrations. Maps properties from Botmatic to your own format, back and forth.

Basic usage

parameter | type | description --------- | ----- | --- mappings | array | An array of mappings. See the Configure Mappings section below

// Initialisation
const mapper = require('js-mapper')(mappings)

Basic example

Let's say you want to create an integration with an external service and want to synchronise its candidates table with Botmatic's Contacts

From an external API to Botmatic

Your API returns candidates as JSON objects as follow:

{
  "id_candidate": 98,
  "first_name": "John",
  "last_name": "Doe",
  "email_address": "[email protected]",
  "birth_date": "1985-04-29"
}

@botmatic/js-mapper converts it to a Botmatic compliant format on the fly:

const botmaticContact = mapper.mapTo(candidate, 'ext', 'botmatic')

botmaticContact now contains a Botmatic Contact:

{
  "id": 98,
  "firstname": "John",
  "lastname": "Doe",
  "email": "[email protected]",
  "birthdate": "1985-04-29T00:00:00.000Z"
}

From an Botmatic to an external API

Botmatic returns contacts as JSON objects as follow:

{
  "id": 98,
  "firstname": "John",
  "lastname": "Doe",
  "email": "[email protected]",
  "birthdate": "1985-04-29T00:00:00.000Z"
}

@botmatic/js-mapper converts it to a format expected by your API:

const botmaticContact = mapper.mapTo(candidate, 'botmatic', 'ext')

externalCandidate now contains a candidate:

{
  "id_candidate": 98,
  "first_name": "John",
  "last_name": "Doe",
  "email_address": "[email protected]",
  "birth_date": "1985-04-29"
}

Configure Mappings

The mappings object you pass on initialisation is an array of Mapping objects. There is Mapping object for each field you want to synchronize between Botmatic and your application.

The Mapping object

property | type | description -------- | ------- | ----------- botmatic | object | describes the field in Botmatic Contact ext | object | descrives the field in your application id | boolean | [optional] true if this is the identifying field (primary key)

Field description object

| key | type | possible values | description | ---------- | -------- | -------------------------- | ---------------------------- | name | string | any | field name | type | string | text, date, number | (optional, default="text") only for botmatic | transform | function | function(string) -> string | (optional) value transformer

Examples

// A sample of a config
// It will 
 
const config = [
  {
    // field description on Botmatic
    botmatic: { name: 'firstname' },
    // field description on your integration
    ext: { name: 'first_name' },
  },
  // ...
]
Id fields

To map the id field add the property id to the mapping, and set it to true

// A sample of a config
 
const config = [
  {
    // This is an id field
    id: true,
    // field description on Botmatic
    botmatic: { name: 'id' },
    // field description on your integration
    your_integration: { name: 'id_andidate' },
  },
  // ...
]
Dates

Botmatic uses ISO 8601 Date notation. If your application uses an other format, you should use specific transform functions

For example, if you API uses timestamps for dates, you can declare these transform functions alongside your mappings:

// From timestamp to ISO
const timestampToISO = (timestamp) => {
  return (new Date(timestamp)).toISOString()
}

// From ISO to timestamp
const ISOToTimestamp = (iso) => {
  return +(new Date(iso))
}

And the mapping object for the date field:

const mappings = [
  ...
  {
    botmatic: {
      name: "birthdate",
      type: "date",
      transform: timestampToISO
    },
    ext: {
      name: "date_of_birth",
      transform: ISOToTimestamp
    }
  }
]

Map contact properties

From botmatic to your integration

const mappedContact = mapper.mapTo(contact, 'botmatic', 'ext')

From your integration to Botmatic

const mappedContact = mapper.mapTo(contact, 'ext', 'botmatic')

Obtaining the id key

The identifying key on Botmatic's Contacts is always id. You must always have a mapping for the id key in your mappings.
To get the name of the identifying key on your external schema:

const extIdKey = mapper.getExtIdKey()
// extIdKey == "id_candidate"

Access your mappings at runtime

If for any reason you need to access your mappings at runtime

const mappings = mapper.data