glitr/service
This module helps you create standard CRUD services routes
Example :
import glitr/service
import glitr/convert
pub type Todo {
Todo(id: String, title: String)
}
pub type UpsertTodo {
UpsertTodo(title: String)
}
pub fn todo_converter() -> convert.Converter(Todo) {
convert.object({
use id <- convert.parameter
use title <- convert.parameter
use <- convert.constructor
Todo(id:, title:)
})
|> convert.field("id", fn(v) { v.id }, convert.string())
|> convert.field("title", fn(v) { v.title }, convert.string())
|> convert.to_converter()
}
pub fn upsert_todo_converter() -> convert.Converter(UpsertTodo) {
convert.object({
use title <- convert.parameter
use <- convert.constructor
UpsertTodo(title:)
})
|> convert.field("title", fn(v) { v.title }, convert.string())
|> convert.to_converter()
}
pub fn todo_service() -> service.RouteService(Todo, UpsertTodo) {
service.new()
|> service.with_root_path(["todos"])
|> service.with_base_converter(todo_converter())
|> service.with_upsert_converter(upsert_todo_converter())
}
pub fn create_todo_route() {
todo_service() |> service.create_route()
}
Types
The RouteService type
Contains the data necessary to build the CRUD routes.
Note that all data transmission will be done via JSON objects.
pub type RouteService(base_type, upsert_type) {
RouteService(
root_path: List(String),
base: #(
fn(base_type) -> json.Json,
fn(dynamic.Dynamic) ->
Result(base_type, List(dynamic.DecodeError)),
),
upsert: #(
fn(upsert_type) -> json.Json,
fn(dynamic.Dynamic) ->
Result(upsert_type, List(dynamic.DecodeError)),
),
)
}
Constructors
-
RouteService( root_path: List(String), base: #( fn(base_type) -> json.Json, fn(dynamic.Dynamic) -> Result(base_type, List(dynamic.DecodeError)), ), upsert: #( fn(upsert_type) -> json.Json, fn(dynamic.Dynamic) -> Result(upsert_type, List(dynamic.DecodeError)), ), )
Functions
pub fn create_route(
service: RouteService(a, b),
) -> Route(Nil, Nil, b, a)
Generate a create route associated with a service
pub fn delete_route(
service: RouteService(a, b),
) -> Route(String, Nil, Nil, String)
Generate a delete route associated with a service
Note that the return is the id of the deleted instance
pub fn get_all_route(
service: RouteService(a, b),
) -> Route(Nil, Nil, Nil, List(a))
Generate a get-all route associated with a service
pub fn get_route(
service: RouteService(a, b),
) -> Route(String, Nil, Nil, a)
Generate a get route associated with a service
pub fn new() -> RouteService(Nil, Nil)
Create a new empty service
The base and upsert types will have to be specified !
pub fn update_route(
service: RouteService(a, b),
) -> Route(String, Nil, b, a)
Generate a update route associated with a service
pub fn with_base_converter(
service: RouteService(a, b),
converter: Converter(c),
) -> RouteService(c, b)
Specify the base type of a service by providing a glitr_convert type
The base type of a service represent the type of object your service is associated with
pub fn with_base_type(
service: RouteService(a, b),
base_encoder: fn(c) -> Json,
base_decoder: fn(Dynamic) -> Result(c, List(DecodeError)),
) -> RouteService(c, b)
Specify the base type of a service by providing a JSON encoder & decoder
The base type of a service represent the type of object your service is associated with
pub fn with_root_path(
service: RouteService(a, b),
root_path: List(String),
) -> RouteService(a, b)
Change the root path of a service
pub fn with_upsert_converter(
service: RouteService(a, b),
converter: Converter(c),
) -> RouteService(a, c)
Specify the upsert type of a service by providing a glitr_convert type
The upsert type of a service represent the type used to create or update objects of your service
pub fn with_upsert_type(
service: RouteService(a, b),
upsert_encoder: fn(c) -> Json,
upsert_decoder: fn(Dynamic) -> Result(c, List(DecodeError)),
) -> RouteService(a, c)
Specify the upsert type of a service by providing a JSON encoder & decoder
The upsert type of a service represent the type used to create or update objects of your service