-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* update/add packages * fix deprecation * add migrations to remove old tables * add caqti encoders, add populate function for database * add role assignments to guardian
- Loading branch information
Showing
18 changed files
with
500 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
(** Helper functions for creating Caqti types with custom encoder/decoders. *) | ||
|
||
module Data = struct | ||
type _ t = | ||
| [] : unit t | ||
| ( :: ) : ('a * 'b t) -> ('a * 'b) t | ||
|
||
let rec make_value : type a. a t -> a = | ||
fun xs -> | ||
match xs with | ||
| [] -> () | ||
| x :: xs -> x, make_value xs | ||
;; | ||
end | ||
|
||
module Schema = struct | ||
type _ t = | ||
| [] : unit t | ||
| ( :: ) : ('a Caqti_type.t * 'b t) -> ('a * 'b) t | ||
|
||
let rec make_type : type a. a t -> a Caqti_type.t = | ||
fun xs -> | ||
match xs with | ||
| [] -> failwith "Schema shouldn't be empty" | ||
| x :: [] -> Caqti_type.(t2 x unit) | ||
| x :: xs -> Caqti_type.(t2 x (make_type xs)) | ||
;; | ||
end | ||
|
||
let custom | ||
: type a b. | ||
encode:(b -> (a Data.t, string) result) | ||
-> decode:(a -> (b, string) result) | ||
-> a Schema.t | ||
-> b Caqti_type.t | ||
= | ||
fun ~encode ~decode schema -> | ||
let typ = Schema.make_type schema in | ||
let encode data = encode data |> Result.map Data.make_value in | ||
Caqti_type.custom ~encode ~decode typ | ||
;; | ||
|
||
let custom_ok | ||
: type a b. | ||
encode:(b -> a Data.t) -> decode:(a -> b) -> a Schema.t -> b Caqti_type.t | ||
= | ||
let open CCFun in | ||
fun ~encode ~decode schema -> | ||
let typ = Schema.make_type schema in | ||
let encode = CCResult.(encode %> return %> map Data.make_value) in | ||
Caqti_type.custom ~encode ~decode:(decode %> CCResult.return) typ | ||
;; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,4 +19,5 @@ | |
ppx_deriving.eq | ||
ppx_deriving.make | ||
ppx_deriving.ord | ||
ppx_deriving.show))) | ||
ppx_deriving.show | ||
ppx_string))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
let find_request_sql | ||
sql_select_columns | ||
default_where | ||
table_name | ||
?(default_where = default_where) | ||
?(count = false) | ||
?(joins = "") | ||
where_fragment | ||
= | ||
let where_fragment = | ||
CCOption.map_or ~default:where_fragment (fun default_where -> | ||
[%string | ||
{sql| | ||
WHERE %{default_where} | ||
AND %{CCString.replace ~which:`Left ~by:"" ~sub:"WHERE" where_fragment} | ||
|sql}]) | ||
in | ||
let columns = | ||
if count then "COUNT(*)" else sql_select_columns |> CCString.concat ", " | ||
in | ||
Format.asprintf | ||
{sql|SELECT %s FROM %s %s %s|sql} | ||
columns | ||
table_name | ||
joins | ||
(where_fragment default_where) | ||
;; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.