Madul is a simple set of tools that help you craft clean functional code that's straightforward to test - and fun to write & maintain
You can find a Getting Started Guide and more here - enjoy! 🤘🏻
export const dependencies = () => ({
'+/db': ['connectAs', 'getAllMessagesBefore', 'getMessagesFrom']
})
export const $init = async ({ connectAs, username }) => await connectAs({ username })
export const getMessagesFrom = async ({
friend,
getAllMessagesBefore,
getMessagesFrom,
sentBefore,
}) => {
const allMessages = await getAllMessagesBefore({ timestamp: sentBefore })
const fromMyFriend = await getMessagesFrom({ friend })
return allMessages.filter(m => fromMyFriend.includes(m))
}
- A madul is just a plain old node module
- The
db
dependency is loaded asynchronously - Dependencies are passed as named parameters to methods
- The
$init
method is guaranteed to be executed after all dependencies have been loaded, but before the madul is available for use; so you know that thedb
will be properly setup and connected to asusername
import madul from '@bsgbryan/madul'
const receiver = await madul('+/MessageReciever', { username: 'KatherineJohnson' })
const oneHour = 1000 * 60 * 60
const sentBefore = Date.now() - oneHour
const messages = await receiver.getMessagesFrom({ friend: 'Ada', sentBefore })
console.log('My messages from Ada!', messages)
- We pass the
username
used for connecting to thedb
when creating ourmadul
- We don't call
$init
directly; that's handled for us as part ofmadul
- We don't pass the
db
dependency togetMessagesFrom
; that's handled for us