Supercharge Your Svelte State Management with Akita
Tags: svelte, akita
Published: 2019-07-03 11:11:55

There’s a new player in the world of JS frameworks — Svelte. As its name suggests, Svelte is lean, extremely lean. In fact, Svelte’s modus operandi, of compiling code to (ideal) vanilla js during the build phase rather than interpreting it during the run phase, has had people describing it as a non-framework and its own creator [calling it a language](https://gist.github.com/Rich-Harris/0f910048478c2a6505d1c32185b61934), rather than a framework. Specifically, one created for describing reactive user interfaces.

I’ve started playing around with Svelte and so far I’ve been having a blast. The [tutorial](https://svelte.dev/tutorial) offered by creator Rich Harris functions as a very pleasant introduction. After completing it, I started to wonder how [Akita](https://netbasal.com/introducing-akita-a-new-state-management-pattern-for-angular-applications-f2f0fab5a8), the state management solution we’ve created here in Datorama, would work in conjunction with Svelte.

###Svelte + Akita = A Great Combo
If you’re already familiar with Svelte, you might be asking yourself why additional state management is even required, as Svelte comes with built-in reactive store functionality. If you have a small application, you can probably get away with using Svelte API alone. However, larger and more complex applications require a full-fledged state management solution. It’s similar to how we need Redux when we have React’s Context API.

A note on Akita’s architecture:

![Akita’s architecture](https://blobscdn.gitbook.com/v0/b/gitbook-28427.appspot.com/o/assets%2F-LDIcOEJiLYk8yWho34E%2F-LEFMbbD5BNkHxecdUde%2F-LEFMe1nMjDF-0kBdGY5%2Fakita-arc.jpg?alt=media&token=4f72cec7-063d-46f2-b231-48d475235744)

The two main components of Akita are the store and the query. You can think of the store like a table that contains data in a database, and you can perform various actions on it like inserting, updating, etc. My svelte component will reactively get the data from the query. And since I want it to remain agnostic of its data source, I won’t import the store inside the component; Instead, I’ll create a service for the component to work with.

As you can see, Akita defines a strict pattern for managing your state data. It comes with powerful Entity management, a robust set of plugins and dev-tools, and its own cli. Svelte and Akita are a great natural combo because Akita’s queries can return observables, which Svelte **supports out of the box**. So here is one example of how the two can be used in tandem:

###Incorporating Akita
To add Akita to my svelte app, I install it, and its cli, via npm. I start with a fresh [Svelte seed](https://github.com/antony/svelte-seed) and go ahead with creating the traditional “TODO” app using Svelte and Akita.

###The TODO Entity Store
Since we’re dealing with TODO items, I opt to create an Entity Store in Akita. You can think of an entity store as a table in a database where each row represents an entity. To do that I use the [Akita CLI](https://github.com/datorama/akita/tree/master/cli).

The store itself has all the built-in methods I need in order to perform CRUD operations on my entities.

{% gist https://gist.github.com/theblushingcrow/af418c858958276afdfd5f2480cac11f %}

In addition to those methods, I add a filter parameter in the store creation, representing the [UI State data](https://engineering.datorama.com/ui-state-management-made-easy-with-akita-and-angular-6e460566ec7c) used to filter the display of the TODO items.

###The TODO Service
Next, I’ll create the aforementioned service:

{% gist https://gist.github.com/theblushingcrow/ab2d41ece84c9575f32834217dcaf5a1 %}

It contains all the methods I’ll need to update/read from the server and the store.

###The TODO Entity Query
Akita Entity Query’s built-in methods can be divided into two types. A method that starts with select, for which you get an observable for the requested data, and methods that start with get, in which you get the requested data directly. It’s important to emphasize that the subscription won’t be triggered unless the value you asked is changed by reference. To those, I add the following methods:

{% gist https://gist.github.com/theblushingcrow/a404f26bf9492214e8de924f7a925b09 %}

I use `combineLatest` to subscribe to the store, get the latest version of the TODO list, and filter it according to the latest filter selection.

The next step is to create the Svelte components that interact with the Akita Entity store:

###The Filter Component
This component’s sole responsibility is to let the users determine the criteria by which they filter the TODO list:

{% gist https://gist.github.com/theblushingcrow/5c5eb130f024b867e85ce6886af47498 %}

###The Add TODO Component
This component is in charge of dispatching a todo event for the purpose of adding a TODO:

{% gist https://gist.github.com/theblushingcrow/9824e970b1ca35c3a8988165b273ada8 %}

###The TODO Component
It is used to display a single TODO item, along with a button for deleting it, and a checkbox to toggle its ‘completed’ status:

{% gist https://gist.github.com/theblushingcrow/8cef0a3029ef4b809f54d9c5c913b122 %}

###Connecting the Dots
Finally, a TODOs component will use all the previously mentioned components, along with the query and service I’ve created, to manage my list:

{% gist https://gist.github.com/theblushingcrow/c88367ed473591b04216e89bf21d99f0 %}

###The End Result
![](https://thepracticaldev.s3.amazonaws.com/i/6c3c7l7yujx239gufe13.gif)

Before we conclude, I wanted to add one last tidbit: Another thing you can get from Akita out of the box, is the ability to persist the state via local storage. I simply add in the main JS file:

{% gist https://gist.github.com/theblushingcrow/1ff39e08f7ffd7704f77e928843ea474 %}

And voila, your state is now persistent!

![](https://thepracticaldev.s3.amazonaws.com/i/orb77n9ymnm7y4lc8hw5.gif)

You can find all the files for this example [here](https://github.com/datorama/akita/tree/master/examples/svelte).

In summary: **Both Akita and Svelte offer a moderate learning curve and enable the easy creation of incredibly lean apps with super-efficient state management out of the box. I encourage you to try both, either together or separately** 👯‍♀️

*Follow me on [Medium](https://medium.com/@inbalsinai) to read more about Svelte, Akita, JS!*