Current section

23 Versions

Jump to

Compare versions

8 files changed
+515 additions
-456 deletions
  @@ -11,9 +11,19 @@ gleam add database
11 11 ```gleam
12 12 import database
13 13 import gleam/option
14 + import gleam/dynamic/decode
14 15
15 16 type Music {
16 - Music(name: String, release_year: String)
17 + // The first value of the custom type will always
18 + // be its primary_key, so if you want a specific value,
19 + // make sure to make it the first value.
20 + Music(name: String, release_year: Int)
21 + }
22 +
23 + fn music_decoder() {
24 + use name <- database.field(0, decode.string)
25 + use release_year <- database.field(1, decode.int)
26 + decode.success(Music(name: release_year))
17 27 }
18 28
19 29 // The generated table will be entirely based on this definition,
  @@ -24,10 +34,9 @@ type Music {
24 34 const music_table_def = Music(name: "Music name", year: 0000)
25 35
26 36 pub fn main() -> Nil {
27 - // 0 indicates that the table primary_key is the 0th value (name)
28 37 let assert Ok(table) = database.create_table(
29 38 definition: music_table_def,
30 - index_at: 0)
39 + decode_with: music_decoder())
31 40
32 41 // All interactions with the table happen within a transaction
33 42 let assert Ok(Nil) = database.transaction(table, fn(ref) {
  @@ -1,25 +1,24 @@
1 - name = "database"
2 - version = "1.2.0"
3 - description = "A very gleamy embedded data storage"
4 -
5 - licences = ["MIT"]
6 - repository = { type = "github", user = "R0DR160HM", repo = "database" }
7 -
8 - # Fill out these fields if you intend to generate HTML documentation or publish
9 - # your project to the Hex package manager.
10 - #
11 - # description = ""
12 - # licences = ["Apache-2.0"]
13 - # repository = { type = "github", user = "", repo = "" }
14 - # links = [{ title = "Website", href = "" }]
15 - #
16 - # For a full reference of all the available options, you can have a look at
17 - # https://gleam.run/writing-gleam/gleam-toml/.
18 -
19 - [dependencies]
20 - gleam_stdlib = ">= 0.44.0 and < 2.0.0"
21 - gleam_erlang = ">= 0.34.0 and < 1.0.0"
22 - gleam_crypto = ">= 1.5.0 and < 2.0.0"
23 -
24 - [dev-dependencies]
25 - gleeunit = ">= 1.0.0 and < 2.0.0"
1 + name = "database"
2 + version = "2.0.0"
3 + description = "A very gleamy embedded data storage"
4 +
5 + licences = ["MIT"]
6 + repository = { type = "github", user = "R0DR160HM", repo = "database" }
7 +
8 + # Fill out these fields if you intend to generate HTML documentation or publish
9 + # your project to the Hex package manager.
10 + #
11 + # description = ""
12 + # licences = ["Apache-2.0"]
13 + # repository = { type = "github", user = "", repo = "" }
14 + # links = [{ title = "Website", href = "" }]
15 + #
16 + # For a full reference of all the available options, you can have a look at
17 + # https://gleam.run/writing-gleam/gleam-toml/.
18 +
19 + [dependencies]
20 + gleam_stdlib = ">= 0.44.0 and < 2.0.0"
21 + gleam_erlang = ">= 0.34.0 and < 1.0.0"
22 +
23 + [dev-dependencies]
24 + gleeunit = ">= 1.0.0 and < 2.0.0"
  @@ -1,6 +1,6 @@
1 1 {<<"name">>, <<"database">>}.
2 2 {<<"app">>, <<"database">>}.
3 - {<<"version">>, <<"1.2.0">>}.
3 + {<<"version">>, <<"2.0.0">>}.
4 4 {<<"description">>, <<"A very gleamy embedded data storage"/utf8>>}.
5 5 {<<"licenses">>, [<<"MIT">>]}.
6 6 {<<"build_tools">>, [<<"gleam">>]}.
  @@ -8,20 +8,15 @@
8 8 {<<"Repository">>, <<"https://github.com/R0DR160HM/database">>}
9 9 ]}.
10 10 {<<"requirements">>, [
11 - {<<"gleam_crypto">>, [
12 - {<<"app">>, <<"gleam_crypto">>},
11 + {<<"gleam_stdlib">>, [
12 + {<<"app">>, <<"gleam_stdlib">>},
13 13 {<<"optional">>, false},
14 - {<<"requirement">>, <<">= 1.5.0 and < 2.0.0">>}
14 + {<<"requirement">>, <<">= 0.44.0 and < 2.0.0">>}
15 15 ]},
16 16 {<<"gleam_erlang">>, [
17 17 {<<"app">>, <<"gleam_erlang">>},
18 18 {<<"optional">>, false},
19 19 {<<"requirement">>, <<">= 0.34.0 and < 1.0.0">>}
20 - ]},
21 - {<<"gleam_stdlib">>, [
22 - {<<"app">>, <<"gleam_stdlib">>},
23 - {<<"optional">>, false},
24 - {<<"requirement">>, <<">= 0.44.0 and < 2.0.0">>}
25 20 ]}
26 21 ]}.
27 22 {<<"files">>, [
  @@ -29,6 +24,7 @@
29 24 <<"README.md">>,
30 25 <<"gleam.toml">>,
31 26 <<"include\database_Table.hrl">>,
27 + <<"include\database_Transaction.hrl">>,
32 28 <<"src\database.app.src">>,
33 29 <<"src\database.erl">>,
34 30 <<"src\database.gleam">>
  @@ -1,5 +1,6 @@
1 1 -record(table, {
2 2 tabname :: gleam@erlang@atom:atom_(),
3 3 attributes :: list(database:table_attributes()),
4 - path :: gleam@erlang@charlist:charlist()
4 + path :: gleam@erlang@charlist:charlist(),
5 + decoder :: gleam@dynamic@decode:decoder(any())
5 6 }).
  @@ -0,0 +1,4 @@
1 + -record(transaction, {
2 + ref :: database:table_ref(any()),
3 + decoder :: gleam@dynamic@decode:decoder(any())
4 + }).
Loading more files…