Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/andrewchambers/janet-pq
Bindings to libpq.
https://github.com/andrewchambers/janet-pq
janet postgres
Last synced: 2 months ago
JSON representation
Bindings to libpq.
- Host: GitHub
- URL: https://github.com/andrewchambers/janet-pq
- Owner: andrewchambers
- License: mit
- Created: 2020-01-01T06:33:52.000Z (about 5 years ago)
- Default Branch: master
- Last Pushed: 2022-06-09T23:49:51.000Z (over 2 years ago)
- Last Synced: 2024-08-04T04:01:04.144Z (5 months ago)
- Topics: janet, postgres
- Language: C
- Size: 50.8 KB
- Stars: 25
- Watchers: 5
- Forks: 4
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-janet - janet-pq
README
# janet-pq
Bindings to libpq.# quick-examples
Basic usage:
```
(import pq)
(def conn (pq/connect "postgresql://localhost?dbname=postgres"))
(pq/exec conn "create table users(name text, data jsonb);")
(pq/exec conn "insert into users(name, data) values($1, $2);" "ac" (pq/jsonb @{"some" "data"}))
(pq/row conn "select * from users where name = $1;" "ac")
{:name "ac" :data @{"some" "data"}}
(pq/all conn "select * from users")
[{:name "ac" :data @{"some" "data"}} ...]
(pq/val conn "select data from users where name = $1;" "ac")
@{"some" "data"}
```Transactions:
```
(import pq)
...
(pq/tx conn {:mode "isolation serializable read only" :retry true}
(unless (pq/val conn "select ....")
(pq/rollback conn))
(pq/val conn "select ...."))
```Custom type encoding/decoding:
```
(import pq)
...# directly insert custom encoding.
(pq/exec conn "insert into tab(x) values($1);" [TYPEOID ISBINARY BYTES])# use a method for custom type encoding.
(pq/exec conn "insert into tab(x) values($1);" {:pq/marshal (fn [self] [TYPEOID ISBINARY BYTES])})# Add a custom type decoder.
(put pg/*decoders* TYPEOID custom-decoder)
(pq/all conn "select * from tab;")```
Error handling:
```
(import pq)
...
(try
...
([err] (when (pq/error? err)
(def msg (pq/result-error-field err ...)))))
```# Special thanks
[Jon Staab](https://github.com/staab) - The author of the first janet postgres library from which this was inspired, and a core contributor to this library.