Create `lib/helpdesk/repo.ex` with the following contents. `AshSqlite.Repo` is a thin wrapper around `Ecto.Repo`, so see their documentation for how to use it if you need to use it directly. For standard Ash usage, all you will need to do is configure your resources to use your repo.
```elixir
# in lib/helpdesk/repo.ex
defmodule Helpdesk.Repo do
use AshSqlite.Repo, otp_app: :helpdesk
end
```
Next we will need to create configuration files for various environments. Run the following to create the configuration files we need.
```bash
mkdir -p config
touch config/config.exs
touch config/dev.exs
touch config/runtime.exs
touch config/test.exs
```
Place the following contents in those files, ensuring that the credentials match the user you created for your database. For most conventional installations this will work out of the box. If you've followed other guides before this one, they may have had you create these files already, so just make sure these contents are there.
```elixir
# in config/config.exs
import Config
# This should already have been added in the first
# getting started guide
config :helpdesk,
ash_apis: [Helpdesk.Support]
config :helpdesk,
ecto_repos: [Helpdesk.Repo]
# Import environment specific config. This must remain at the bottom
# of this file so it overrides the configuration defined above.
# Starts a worker by calling: Helpdesk.Worker.start_link(arg)
# {Helpdesk.Worker, arg}
Helpdesk.Repo
]
...
```
### Add AshSqlite to our resources
Now we can add the data layer to our resources. The basic configuration for a resource requires the `d:AshSqlite.sqlite|table` and the `d:AshSqlite.sqlite|repo`.
If you are unfamiliar with database migrations, it is a good idea to get a rough idea of what they are and how they work. See the links at the bottom of this guide for more. A rough overview of how migrations work is that each time you need to make changes to your database, they are saved as small, reproducible scripts that can be applied in order. This is necessary both for clean deploys as well as working with multiple developers making changes to the structure of a single database.
Typically, you need to write these by hand. AshSqlite, however, will store snapshots each time you run the command to generate migrations and will figure out what migrations need to be created.
You should always look at the generated migrations to ensure that they look correct. Do so now by looking at the generated file in `priv/repo/migrations`.
Finally, we will create the local database and apply the generated migrations:
```bash
mix ash_sqlite.create
mix ash_sqlite.migrate
```
### Try it out
And now we're ready to try it out! Run the following in iex:
Lets create some data. We'll make a representative and give them some open and some closed tickets.
And, naturally, now that we are storing this in sqlite, this database is persisted even if we stop/start our application. The nice thing, however, is that this was the _exact_ same code that we ran against our resources when they were backed by ETS.
### Calculations
Calculations can be pushed down into SQL using expressions.
For example, we can determine the percentage of tickets that are open:
```elixir
# in lib/helpdesk/support/resources/representative.ex
Take a look at the DSL documentation for more information on what you can configure. You can add check constraints, configure the behavior of foreign keys and more!
When deploying, you will need to ensure that the file you are using in production is persisted in some way (probably, unless you want it to disappear whenever your deployed system restarts). For example with fly.io this might mean adding a volume to your deployment.
- Check out the data layer docs: `AshSqlite.DataLayer`
- [Ecto's documentation](https://hexdocs.pm/ecto/Ecto.html). AshSqlite (and much of Ash itself) is made possible by the amazing Ecto. If you find yourself looking for escape hatches when using Ash or ways to work directly with your database, you will want to know how Ecto works. Ash and AshSqlite intentionally do not hide Ecto, and in fact encourages its use whenever you need an escape hatch.
- [Ecto's Migration documentation](https://hexdocs.pm/ecto_sql/Ecto.Migration.html) read more about migrations. Even with the ash_sqlite migration generator, you will very likely need to modify your own migrations some day.