glv8

Write functional type-safe postgresql procedures with gleam, through plv8's JavaScript binding


License
Artistic-1.0-cl8

Documentation

glv8

Ever wanted to write functional type-safe postgresql procedures ? Let's use gleam !

Package Version Hex Docs

glv8 coworks with plv8. 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

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

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
$ 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 with glance (it might be rigid and over-kill after all)