1
1.1
XJog ships with two persistence adapters, and an additional mock adapter for testing purposes. Writing new persistence adapters is not difficult either, should it be necessary. You will find a comparison of the adapters in table 1. PostgreSQL is recommended for production use.
Note that in addition to the regular persistence adapters, there are delta persistence adapters. Recording deltas is optional, and a separate adapter makes it possible to store delta data separately.
Adapter | Connection pooling | Notifications | Use cases |
---|---|---|---|
PostgreSQL | Yes | Listeners | Server-side, production |
SQLite | Yes | Polling | Local applications, experimenting |
Mock | No | Callbacks | Unit testing, development |
1.2
XJog is available as an npm package xjog
at the npm registry. In addition to the XJog
itself, you will need to install some additional packages depending on which database you are planning to
use.
Database | Required packages | Required for deltas |
---|---|---|
PostgreSQL |
|
|
SQLite |
|
|
Mock | (None) |
|
Listing 1 shows how to install XJog to your Node project translated to shell commands.
1.3
Let's create a simple machine and register it with XJog. Then we will run until we reach a final state and then clean up.
Let's start with creating the instance (listing 2). We'll be using in-memory SQLite database. This way the example is self-sufficient and ready to run.
The next step is to configure a machine using the createMachine
function, and to register it
with XJog. This machine will determine how the chart will behave. In our example we only have two states,
shy
and confident
. When our chart collects enough courage, it yelps "Hello, world!" and then
retires as a confident chart.
After registering the machine, we have to instantiate a chart (listing 4).
While we're at it, let's send the chart an event and check what the state is after that.
As a result, when running, we should also see "Hello, world!" printed in the console.
One more thing to add is the cleanup. We'll wrap the whole thing in try-finally
to make sure
it's called in both successful and failing paths.
And there we are!