馃 A practical web framework for Gleam
1# Wisp Example: Using a database
2
3```sh
4gleam run # Run the server
5gleam test # Run the tests
6```
7
8This example shows how to use a database, using a `Context` type to hold the
9database connection.
10
11This example is based off of the ["working with JSON" example][json], so read
12that first. The additions are detailed here and commented in the code.
13
14[json]: https://github.com/lpil/wisp/tree/main/examples/03-working-with-json
15
16### `gleam.toml` file
17
18The `tiny_database` package has been added as a dependency. In a real project
19you would like use a proper database such as Postgres or SQLite.
20
21### `app/web` module
22
23A new `Context` type has been created to hold the database connection.
24
25### `app` module
26
27The `main` function now starts by creating a database connection and passing it
28to the handler function in a `Context` record.
29
30### `app/router` module
31
32The `handle_request` function has been updated to route requests to functions in
33the new `app/web/people` module.
34
35### `app/web/people` module
36
37This module has been created to hold all the functions for working with the
38"people" feature, including their request handlers.
39
40### `app_test` module
41
42The `with_context` function has been added to create a `Context` record with a
43database connection, and to setup the database.
44
45The tests have been updated to verify that the application saves and retrieves
46the data correctly.
47
48### Other files
49
50No changes have been made to the other files.