bi-directional adapter factory, used to decouple systems across shared data structures

Usage no npm install needed!

<script type="module">
  import bidirectionalAdapter from '';


Bi-Directional Adapter

npm version npm downloads

Factory to create bi-directional adapters that can convert between two distinct data structures. Using adapters helps to decouple systems which share common data structures and may need to alter them independently without introducing conflicts in each other.


You have multiple services which each operate on a single shared resource but want to represent the data differently in each service or want to isolate each service from data structure changes required by the other.

With bidirectional-adapter you can define a simple entity which can be used to transform data between two formats.

const adapter = createAdapter<string, number>(
    (stringValue) => parseInt(stringValue, 10),
    (numericValue) => String(numericValue)


import axios from 'axios';
import createAdapter from 'bidirectional-adapter';

const accountAdapter = createAdapter(
    (dbAccount) => ({
        id: dbAccount.user_id,
        name: `${dbAccount.user.firstName} ${dbAccount.user.lastName}`,
    (appAccount) => ({
        user: {
            firstName:' ')[0],
            lastName:' ')[1]

const fetchAccount = (userID) => {
    // state is in the shape of propertyTwo
    // do reducer stuff here
    return axios.get(`/account/${userID}`).then(res => accountAdapter.fromDB(;

const updateAccount = (account) => {
    // state is in the shape of propertyTwo
    // do reducer stuff here
    return`/account/${userID}`, accountAdapter.toDB(account));


To use bidirectional-adapter, install it as a dependency:

# If you use npm:
npm install bidirectional-adapter

# Or if you use Yarn:
yarn add bidirectional-adapter

This assumes that you’re using a package manager such as npm.