|
| 1 | +# Bi-Directional Adapter |
| 2 | + |
| 3 | +[](https://www.npmjs.com/package/bidirectional-adapter) |
| 4 | +[](https://www.npmjs.com/package/bidirectional-adapter) |
| 5 | + |
| 6 | +Factory to create bi-directional adapters that can convert between two distinct data structures. |
| 7 | +Using adapters helps to decouple systems which share common data structures and may need to alter them |
| 8 | +independently without introducing conflicts in each other. |
| 9 | + |
| 10 | +## Why? |
| 11 | + |
| 12 | +You have multiple services which each operate on a single shared resource but want to represent the data |
| 13 | +differently in each service or want to isolate each service from data structure changes required by the other. |
| 14 | + |
| 15 | +With `bidirectional-adapter` you can define a simple entity which can be used to transform data between two formats. |
| 16 | + |
| 17 | +```ts |
| 18 | +const adapter = createAdapter<string, number>( |
| 19 | + (stringValue) => parseInt(stringValue, 10), |
| 20 | + (numericValue) => String(numericValue) |
| 21 | +); |
| 22 | +``` |
| 23 | + |
| 24 | +## Example |
| 25 | + |
| 26 | +```ts |
| 27 | +import axios from 'axios'; |
| 28 | +import createAdapter from '@voiceflow/bidirectional-adapter'; |
| 29 | + |
| 30 | +const accountAdapter = createAdapter( |
| 31 | + (dbAccount) => ({ |
| 32 | + id: dbAccount.user_id, |
| 33 | + name: `${dbAccount.user.firstName} ${dbAccount.user.lastName}`, |
| 34 | + email: dbAccount.user.email, |
| 35 | + }), |
| 36 | + (appAccount) => ({ |
| 37 | + user_id: appAcount.id, |
| 38 | + user: { |
| 39 | + firstName: appAccount.name.split(' ')[0], |
| 40 | + lastName: appAccount.name.split(' ')[1], |
| 41 | + }, |
| 42 | + email: appAccount.email, |
| 43 | + }) |
| 44 | +); |
| 45 | + |
| 46 | +const fetchAccount = (userID) => { |
| 47 | + // state is in the shape of propertyTwo |
| 48 | + // do reducer stuff here |
| 49 | + return axios.get(`/account/${userID}`).then((res) => accountAdapter.fromDB(res.data)); |
| 50 | +}; |
| 51 | + |
| 52 | +const updateAccount = (account) => { |
| 53 | + // state is in the shape of propertyTwo |
| 54 | + // do reducer stuff here |
| 55 | + return axios.post(`/account/${userID}`, accountAdapter.toDB(account)); |
| 56 | +}; |
| 57 | +``` |
| 58 | + |
| 59 | +## Smart Adapter Example |
| 60 | + |
| 61 | +```ts |
| 62 | +import { createSmartMultiAdapter } from '@voiceflow/bidirectional-adapter'; |
| 63 | + |
| 64 | +interface DBModel { |
| 65 | + x: number; |
| 66 | + a: number; |
| 67 | + b: string; |
| 68 | + c1: boolean; |
| 69 | +} |
| 70 | + |
| 71 | +interface Model { |
| 72 | + x: number; |
| 73 | + ab: string; |
| 74 | + c2: boolean; |
| 75 | +} |
| 76 | + |
| 77 | +type KeyMap = [['a' | 'b', 'ab'], ['c1', 'c2']]; |
| 78 | + |
| 79 | +const adapter = createSmartMultiAdapter<DBModel, Model, [], [], KeyMap>( |
| 80 | + () => ({}) as any, |
| 81 | + () => ({}) as any |
| 82 | +); |
| 83 | + |
| 84 | +adapter.fromDB({ a: 1, b: 'a', c1: false, x: 1 }); // Model |
| 85 | +adapter.fromDB({ a: 1, b: 'a', c1: false }); // Pick<Model, 'ab' | 'c2'> |
| 86 | +adapter.fromDB({ b: 'a', c1: false }); // Pick<Model, 'c2'> |
| 87 | +adapter.fromDB({ x: 1 }); // Pick<Model, 'x1'> |
| 88 | +adapter.fromDB({}); // EmptyObject |
| 89 | + |
| 90 | +adapter.mapFromDB([{ a: 1, b: 'a', c1: false, x: 1 }]); // Model[] |
| 91 | +adapter.mapFromDB([{ a: 1, b: 'a', c1: false }]); // Pick<Model, 'ab' | 'c2'>[] |
| 92 | +adapter.mapFromDB([{ b: 'a', c1: false }]); // Pick<Model, 'c2'>[] |
| 93 | +adapter.mapFromDB([{ x: 1 }]); // Pick<Model, 'x1'>[] |
| 94 | +adapter.mapFromDB([{}]); // EmptyObject[] |
| 95 | + |
| 96 | +adapter.toDB({ ab: '1', c2: false, x: 1 }); // DBModel |
| 97 | +adapter.toDB({ ab: '1', x: 1 }); // Pick<DBModel, 'a' | 'b' | 'x'> |
| 98 | +adapter.toDB({ c2: false }); // Pick<Model, 'c2'> |
| 99 | +adapter.toDB({ x: 1 }); // Pick<Model, 'x1'> |
| 100 | +adapter.toDB({}); // EmptyObject |
| 101 | + |
| 102 | +adapter.mapToDB([{ ab: '1', c2: false, x: 1 }]); // DBModel |
| 103 | +adapter.mapToDB([{ ab: '1', x: 1 }]); // Pick<DBModel, 'a' | 'b' | 'x'> |
| 104 | +adapter.mapToDB([{ c2: false }]); // Pick<Model, 'c2'> |
| 105 | +adapter.mapToDB([{ x: 1 }]); // Pick<Model, 'x1'> |
| 106 | +adapter.mapToDB([{}]); // EmptyObject |
| 107 | +``` |
| 108 | + |
| 109 | +## Installation |
| 110 | + |
| 111 | +To use `bidirectional-adapter`, install it as a dependency: |
| 112 | + |
| 113 | +```bash |
| 114 | +# If you use npm: |
| 115 | +npm install @voiceflow/bidirectional-adapter |
| 116 | + |
| 117 | +# Or if you use Yarn: |
| 118 | +yarn add @voiceflow/bidirectional-adapter |
| 119 | +``` |
| 120 | + |
| 121 | +This assumes that you’re using a package manager such as [npm](http://npmjs.com/). |
| 122 | + |
| 123 | +## License |
| 124 | + |
| 125 | +[ISC](LICENSE.md) |
0 commit comments