🧚 A practical web framework for Gleam

Tiny example database

+154 -4
+5
examples/utilities/tiny_database/.gitignore
··· 1 + *.beam 2 + *.ez 3 + build 4 + erl_crash.dump 5 + tmp
+6
examples/utilities/tiny_database/README.md
··· 1 + # Tiny Database 2 + 3 + An example database library that the Wisp examples may use. 4 + 5 + A real application likely uses a proper database such as PostgreSQL, MySQL, 6 + MariaDB, or SQLite.
+13
examples/utilities/tiny_database/gleam.toml
··· 1 + name = "tiny_database" 2 + version = "1.0.0" 3 + description = "A silly little database to be used in the examples" 4 + licences = ["Apache-2.0"] 5 + 6 + [dependencies] 7 + gleam_stdlib = "~> 0.30" 8 + simplifile = "~> 0.1" 9 + ids = "~> 0.8" 10 + gleam_json = "~> 0.6" 11 + 12 + [dev-dependencies] 13 + gleeunit = "~> 0.10"
+20
examples/utilities/tiny_database/manifest.toml
··· 1 + # This file was generated by Gleam 2 + # You typically do not need to edit this file 3 + 4 + packages = [ 5 + { name = "gleam_erlang", version = "0.20.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_erlang", source = "hex", outer_checksum = "F216A80C8FDFF774447B494D5E08AE4E9A911E971727B9A78FEBF5F300914584" }, 6 + { name = "gleam_json", version = "0.6.0", build_tools = ["gleam"], requirements = ["thoas", "gleam_stdlib"], otp_app = "gleam_json", source = "hex", outer_checksum = "C6CC5BEECA525117E97D0905013AB3F8836537455645DDDD10FE31A511B195EF" }, 7 + { name = "gleam_otp", version = "0.6.0", build_tools = ["gleam"], requirements = ["gleam_erlang", "gleam_stdlib"], otp_app = "gleam_otp", source = "hex", outer_checksum = "E31B158857E3D2AF946FE6E90E0CB21699AF226F4630E93FBEAC5DB4515F8920" }, 8 + { name = "gleam_stdlib", version = "0.30.1", build_tools = ["gleam"], requirements = [], otp_app = "gleam_stdlib", source = "hex", outer_checksum = "704258528887F95075FFED7AAE1CCF836A9B88E3AADA2F69F9DA15815F94A4F9" }, 9 + { name = "gleeunit", version = "0.11.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleeunit", source = "hex", outer_checksum = "1397E5C4AC4108769EE979939AC39BF7870659C5AFB714630DEEEE16B8272AD5" }, 10 + { name = "ids", version = "0.8.0", build_tools = ["gleam"], requirements = ["gleam_stdlib", "gleam_erlang", "gleam_otp"], otp_app = "ids", source = "hex", outer_checksum = "7A378014D40E848326FBEE8AC0C9B35EB9C8094DC4414D89F9A5AA99397A6042" }, 11 + { name = "simplifile", version = "0.1.10", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "simplifile", source = "hex", outer_checksum = "263B7C7F4B29263555DEA2D30BA918425A27120CDD1E1352744EAB4D56CE01CE" }, 12 + { name = "thoas", version = "0.4.1", build_tools = ["rebar3"], requirements = [], otp_app = "thoas", source = "hex", outer_checksum = "4918D50026C073C4AB1388437132C77A6F6F7C8AC43C60C13758CC0ADCE2134E" }, 13 + ] 14 + 15 + [requirements] 16 + gleam_json = { version = "~> 0.6" } 17 + gleam_stdlib = { version = "~> 0.30" } 18 + gleeunit = { version = "~> 0.10" } 19 + ids = { version = "~> 0.8" } 20 + simplifile = { version = "~> 0.1" }
+80
examples/utilities/tiny_database/src/tiny_database.gleam
··· 1 + import gleam/result 2 + import gleam/dynamic 3 + import ids/nanoid 4 + import gleam/json 5 + import gleam/list 6 + import gleam/map.{Map} 7 + import simplifile 8 + 9 + pub opaque type Connection { 10 + Connection(root: String) 11 + } 12 + 13 + pub fn connect(root: String) -> Connection { 14 + let assert Ok(_) = simplifile.create_directory_all(root) 15 + Connection(root) 16 + } 17 + 18 + pub fn disconnect(_connection: Connection) -> Nil { 19 + // Here we do nothing, but a real database would close the connection or do 20 + // some other teardown. 21 + Nil 22 + } 23 + 24 + pub fn with_connection(root: String, f: fn(Connection) -> t) -> t { 25 + let connection = connect(root) 26 + let result = f(connection) 27 + disconnect(connection) 28 + result 29 + } 30 + 31 + pub fn truncate(connection: Connection) -> Result(Nil, Nil) { 32 + let assert Ok(_) = simplifile.delete(connection.root) 33 + Ok(Nil) 34 + } 35 + 36 + pub fn list(connection: Connection) -> Result(List(String), Nil) { 37 + let assert Ok(_) = simplifile.create_directory_all(connection.root) 38 + simplifile.list_contents(connection.root) 39 + |> result.nil_error 40 + } 41 + 42 + pub fn insert( 43 + connection: Connection, 44 + values: Map(String, String), 45 + ) -> Result(String, Nil) { 46 + let id = nanoid.generate() 47 + let values = 48 + values 49 + |> map.to_list 50 + |> list.map(fn(pair) { #(pair.0, json.string(pair.1)) }) 51 + let json = json.to_string(json.object(values)) 52 + use _ <- result.try( 53 + simplifile.write(json, file_path(connection, id)) 54 + |> result.nil_error, 55 + ) 56 + Ok(id) 57 + } 58 + 59 + pub fn read( 60 + connection: Connection, 61 + id: String, 62 + ) -> Result(Map(String, String), Nil) { 63 + use data <- result.try( 64 + simplifile.read(file_path(connection, id)) 65 + |> result.nil_error, 66 + ) 67 + 68 + let decoder = dynamic.map(dynamic.string, dynamic.string) 69 + 70 + use data <- result.try( 71 + json.decode(data, decoder) 72 + |> result.nil_error, 73 + ) 74 + 75 + Ok(data) 76 + } 77 + 78 + fn file_path(connection: Connection, id: String) -> String { 79 + connection.root <> "/" <> id 80 + }
+26
examples/utilities/tiny_database/test/tiny_database_test.gleam
··· 1 + import gleeunit 2 + import gleeunit/should 3 + import tiny_database 4 + import gleam/map 5 + 6 + pub fn main() { 7 + gleeunit.main() 8 + } 9 + 10 + pub fn insert_read_test() { 11 + let connection = tiny_database.connect("tmp/data") 12 + 13 + let data = map.from_list([#("name", "Alice"), #("profession", "Programmer")]) 14 + 15 + let assert Ok(Nil) = tiny_database.truncate(connection) 16 + let assert Ok([]) = tiny_database.list(connection) 17 + let assert Ok(id) = tiny_database.insert(connection, data) 18 + 19 + let assert Ok(read) = tiny_database.read(connection, id) 20 + read 21 + |> should.equal(data) 22 + 23 + let assert Ok([single]) = tiny_database.list(connection) 24 + single 25 + |> should.equal(id) 26 + }
+4 -4
manifest.toml
··· 3 3 4 4 packages = [ 5 5 { name = "exception", version = "1.0.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "exception", source = "hex", outer_checksum = "71F00057D38ADB03BBCCD0E3B07AB2C236BD49DBA7E7611A9DADBD1E26C9F53D" }, 6 - { name = "gleam_bitwise", version = "1.2.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_bitwise", source = "hex", outer_checksum = "6064699EFBABB1CA392DCB193D0E8B402FB042B4B46857B01E6875E643B57F54" }, 7 - { name = "gleam_crypto", version = "0.4.0", build_tools = ["gleam"], requirements = ["gleam_stdlib", "gleam_bitwise"], otp_app = "gleam_crypto", source = "hex", outer_checksum = "42429CED0F838B40014E1C017B0495C46C311D08035D2C2D43B749B91A4374F6" }, 6 + { name = "gleam_bitwise", version = "1.3.0", build_tools = ["gleam"], requirements = [], otp_app = "gleam_bitwise", source = "hex", outer_checksum = "E2A46EE42E5E9110DAD67E0F71E7358CBE54D5EC22C526DD48CBBA3223025792" }, 7 + { name = "gleam_crypto", version = "0.4.0", build_tools = ["gleam"], requirements = ["gleam_bitwise", "gleam_stdlib"], otp_app = "gleam_crypto", source = "hex", outer_checksum = "42429CED0F838B40014E1C017B0495C46C311D08035D2C2D43B749B91A4374F6" }, 8 8 { name = "gleam_erlang", version = "0.20.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_erlang", source = "hex", outer_checksum = "F216A80C8FDFF774447B494D5E08AE4E9A911E971727B9A78FEBF5F300914584" }, 9 9 { name = "gleam_http", version = "3.5.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleam_http", source = "hex", outer_checksum = "FAE9AE3EB1CA90C2194615D20FFFD1E28B630E84DACA670B28D959B37BCBB02C" }, 10 10 { name = "gleam_json", version = "0.6.0", build_tools = ["gleam"], requirements = ["gleam_stdlib", "thoas"], otp_app = "gleam_json", source = "hex", outer_checksum = "C6CC5BEECA525117E97D0905013AB3F8836537455645DDDD10FE31A511B195EF" }, 11 11 { name = "gleam_otp", version = "0.6.0", build_tools = ["gleam"], requirements = ["gleam_erlang", "gleam_stdlib"], otp_app = "gleam_otp", source = "hex", outer_checksum = "E31B158857E3D2AF946FE6E90E0CB21699AF226F4630E93FBEAC5DB4515F8920" }, 12 12 { name = "gleam_stdlib", version = "0.30.1", build_tools = ["gleam"], requirements = [], otp_app = "gleam_stdlib", source = "hex", outer_checksum = "704258528887F95075FFED7AAE1CCF836A9B88E3AADA2F69F9DA15815F94A4F9" }, 13 13 { name = "gleeunit", version = "0.11.0", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "gleeunit", source = "hex", outer_checksum = "1397E5C4AC4108769EE979939AC39BF7870659C5AFB714630DEEEE16B8272AD5" }, 14 - { name = "glisten", version = "0.8.1", build_tools = ["gleam"], requirements = ["gleam_stdlib", "gleam_erlang", "gleam_otp"], otp_app = "glisten", source = "hex", outer_checksum = "B156D92DD8508D05C7E2D54FA6EB4EC57510E70F1B156F40B0FF1589CE72095D" }, 15 - { name = "mist", version = "0.13.1", build_tools = ["gleam"], requirements = ["gleam_stdlib", "gleam_erlang", "glisten", "gleam_http"], otp_app = "mist", source = "hex", outer_checksum = "178EDF5F396570DD53BE2A94C8F9759072093DACB81B62CD47A620B961DB2F2D" }, 14 + { name = "glisten", version = "0.8.1", build_tools = ["gleam"], requirements = ["gleam_erlang", "gleam_otp", "gleam_stdlib"], otp_app = "glisten", source = "hex", outer_checksum = "B156D92DD8508D05C7E2D54FA6EB4EC57510E70F1B156F40B0FF1589CE72095D" }, 15 + { name = "mist", version = "0.13.1", build_tools = ["gleam"], requirements = ["gleam_erlang", "gleam_http", "gleam_stdlib", "glisten"], otp_app = "mist", source = "hex", outer_checksum = "178EDF5F396570DD53BE2A94C8F9759072093DACB81B62CD47A620B961DB2F2D" }, 16 16 { name = "simplifile", version = "0.1.10", build_tools = ["gleam"], requirements = ["gleam_stdlib"], otp_app = "simplifile", source = "hex", outer_checksum = "263B7C7F4B29263555DEA2D30BA918425A27120CDD1E1352744EAB4D56CE01CE" }, 17 17 { name = "thoas", version = "0.4.1", build_tools = ["rebar3"], requirements = [], otp_app = "thoas", source = "hex", outer_checksum = "4918D50026C073C4AB1388437132C77A6F6F7C8AC43C60C13758CC0ADCE2134E" }, 18 18 ]