Quick Start
Quick steps to help you build something with jerni in your local machine
Last updated
Quick steps to help you build something with jerni in your local machine
Last updated
In this example, we will try to build an API server for a new referral campaign where:
unregistered users will sign up with or without a referral code
newly signed up users will have an unique referral code to invite other users
if a user's referral code is used when signing up, that user gets a point.
To complete the following guide, you will need
Node version 8 or newer (to install Node, please go to https://nodejs.org/en/download/)
MongoDB version 3 or newer (to install MongoDB locally, please go to https://docs.mongodb.com/manual/installation/#tutorials)
You are going to create a new folder and create a Node application within it.
After running these commands, you will find a package.json
file in your newly created referral_journey
directory. So far so good!
jerni
and jerni-dev
You need to install jerni
and jerni-dev
package from npm with the following command:
For yarn users:
jerni-dev
is a toolkit to improve developers' productivity and experience when developing ajerni
app. It should never be used in production server.With
jerni-dev
developers don't have to worry about setting up and maintaining an events queue service. It also enables development only features like hot-reloading your event handlers and time-traveling your events history. Read more aboutjerni-dev
at https://github.com/tungv/jerni/blob/master/packages/jerni-dev/readme.md
Then open package.json
file for edit. You need to add a subscribe-dev
script to scripts
path.
In this step, we're going to tell jerni
what it should do when an event arrives. Base on the requirements, we need to react to event "USER_REGISTERED"
. And the reaction is to insert a new document to mongodb describing the new user.
in
jerni
an event describes something happened in the past. Events always have a unique and incrementalid
, atype
and an optionalpayload
. It's designed very similarly to a Flux Standard ActionSince it's about something that happened, the type should be in a past participle form, eg.
USER_REGISTERED
orUSER_DEACTIVATED
. Note that ALL_CAPS convention is not a requirement but more a personal reference.
To be able to work with mongodb in the opinionated way of jerni
we need to install @jerni/store-mongo
using npm:
Create a JavaScript file at models/users.js
from the current working directory with the following content:
Model is where you handle your events as they arrive. transform
property accepts a pure function to transform an event into one or many mongodb operations such as insertOne
/insertMany
, updateOne
/updateMany
, deleteOne
/deleteMany
, for more details please read https://github.com/tungv/jerni/tree/master/packages/store-mongo
mapEvents
is a utility function to make it more readable. It's entirely optional but recommended.
We are going to use createJourney
function from jerni
package to complete a journey. Please create index.js
file from the current working directory with the following content:
This code will create a journey by connecting an events queue service to your mongodb store. You can have more than one stores but only one events queue is allowed.