diff options
author | Kai WU <kaiwu2004@gmail.com> | 2024-07-29 18:14:36 +0800 |
---|---|---|
committer | Kai WU <kaiwu2004@gmail.com> | 2024-07-29 18:14:36 +0800 |
commit | 4970563dde7f1ef93b5aecaa6abc8d15e464c946 (patch) | |
tree | 5ef1b5baa51b26963da9c97cd826612e17eb19f1 | |
parent | 0fe4cc7247fb34afdc6a0277903a22777b583f28 (diff) | |
download | glv8-1.0.0.tar.gz glv8-1.0.0.zip |
fix readmev1.0.0
-rw-r--r-- | README.md | 57 | ||||
-rw-r--r-- | gleam.toml | 4 |
2 files changed, 52 insertions, 9 deletions
@@ -1,20 +1,63 @@ # glv8 -glv8 facilitates one to write [gleam](https://gleam.run) for postgresql as trusted language extension. -It provides [plv8](https://plv8.github.io) bindings and tools like [plv8ify](https://github.com/divyenduz/plv8ify) +Ever wanted to write functional type-safe postgresql procedures ? Let's use [gleam](https://gleam.run) ! [](https://hex.pm/packages/glv8) [](https://hexdocs.pm/glv8/) -glv8 is *not* a SQL query builder, it is not an application framework either. Instead it promotes -gleam as an alternative to JavaScript to do functional computation in a restricted JavaScript runtime, -directly inside postgresql database. +glv8 coworks with [plv8](https://plv8.github.io). It provides plv8's API bindings so one can +write procedures with gleam and let `plv8` do the needed proxying. Specifically, gleam codes are +compiled as JavaScript and interoperate with plv8 ecosystem. For example +```gleam +pub fn catch_sql_error() -> Nil { + let r = database.execute("throw SQL error", Nil) + use <- bool.guard(result.is_ok(r), Nil) + + case r { + Error(glv8.DBErrorJson(j)) -> elog_notice(j |> json.to_string) + _ -> elog_notice("should not come here") + } +} +``` + +The snippet can be provisioned with `plv8` declaration + +```sql +CREATE FUNCTION catch_sql_error() RETURNS void AS +$$ + glv8.catch_sql_error(); +$$ LANGUAGE plv8; +``` + +More examples can be found at `src/app/plv8.gleam` Further documentation can be found at <https://hexdocs.pm/glv8>. ## Development +- write any gleam functions, with `pub fn` specifier in any packages. +- provide needed proxying sqls +- declare them in the `exports` and `sqls` of `bundle.gleam` +- build (or watch build) them with npm scripts +- the artifacts are `dist/glv8_init.sql` which wraps up all the exported gleam functions + and the needed proxying sqls +- by default, the gleam functions use prefix `glv8.` as namespace, one can overwrite + it with envionment variable `GLV8_NAMESPACE` + ```sh -gleam build # Run the project -gleam test # Run the tests +$ npm run build +$ npm run watch +$ npm run clean +$ npm run purge ``` + +## Deployment + +- postgresql database with `plv8` trusted language extension. +- glv8 needs `SET plv8.start_proc = 'glv8_init';` either in `postgresql.conf` or session pre-request +- deploy artifacts `glv8_init.sql` and all the other proxying sqls +- enjoy + +## RoadMap + +- auto-generate proxying sqls as [plv8ify](https://github.com/divyenduz/plv8ify) with `glance` (it might be rigid and over-kill after all) @@ -1,6 +1,6 @@ name = "glv8" -version = "0.1.0" -description = "Facilities to write postgresql procedures with gleam, through plv8's JavaScript binding" +version = "1.0.0" +description = "Write functional type-safe postgresql procedures with gleam, through plv8's JavaScript binding" target = "javascript" # Fill out these fields if you intend to generate HTML documentation or publish |