Backend store manages business logic functions in Node.js backend application.
- It allows to define functions and to call them each other through the store
- It allows to use custom middleware executed before/after each function call
- It has built-in set of business logic error classes
- It has built-in few simple yet powerful plugins
What it's trying to solve:
- Simplifies code organisation
- Simplifies code testing
- Simplifies error handling
- Simplifies logging and error tracking
$ yarn add backend-store
import { Store } from 'backend-store'
import logger from 'backend-store/plugins/logger'
// create store
const store = new Store()
// use logger plugin (see console output what it can do)
store.plugin(logger)
// define method that calls other method
store.define('api/users/create', async (payload = {}, { context, errors, dispatch }) {
if (!context || !context.user || context.user.role !== 'admin') {
throw new errors.AuthorizationError('Only admin can create posts')
}
const post = await dispatch('database/users/insert', {
title: payload.title,
userId: context.user.id
})
return post
})
// define another method
store.define('database/users/insert', async ({ title, userId }) {
return {
id: 1,
title,
userId
}
})
// call (dispatch) method in store; pass payload and context
store.dispatch('api/users/create', { title: 'hello!' }, {
user: { role: 'admin' }
}).then(post => {
console.log(post)
})
More examples in docs
https://alekbarszczewski.github.io/backend-store.
- ow - label arguments (waiting for ow package to be updated)
- test plugin