Fuse
Fuse is a layer that sits infront of your data store and merges multiple incomplete data models into a single complete model.
For example, if you're making lots of requests via GraphQL, each request will return a different subset of fields on a particular model. Most architectures don't support merging multiple objects with a shared key into a single object. Fuse will do this for you automatically.
All you need to do is define your schema relations and call fuse.handle()
whenever you recieve new data. You can hook your data store into Fuse, so after the handle method is called, all mutations are sent to your store. Fuse will also only trigger a store mutation when there's a state change, keeping things efficient.
Installation
yarn add fuse-state
Usage
import Fuse from 'fuse-state'
const fuse = new Fuse({
schema: b => ({
book: {
author: b.object('author')
},
author: {
books: b.array('book')
}
}),
handlerFns: {
book: book => {
console.log(`Book with id ${book.id} updated`)
store.dispatch(updateBook(book))
},
books: books => {
console.log(`${Object.keys(books).length} books updated`)
store.dispatch(updateBooks(books))
}
}
})
fuse.handle({
book: {
id: 1,
name: 'My Book',
author: {
id: 2,
name: 'Darn Fish',
books: [{
id: 1,
year: 2022
}, {
id: 2,
name: 'My Book: The Sequel'
}]
}
},
books: [{
id: 2,
starRating: 5
}]
})
fuse.handle({
author: {
id: 2,
age: 20,
books: [{
id: 2,
year: 2023
}]
}
})
License
MIT