PostgreSQL API Client

PostgreSQL API language search

PostgreSQL API functions response:

We don't use PostgreSQL in the usual way.

We do everything through API functions, which always return two values:

  1. "ok" boolean to say whether API call was successful
  2. "js" JSON response, whether as intended or with just one key: "error"

So we only need a low-level PostgreSQL library to access this.

PostgreSQL API test table and functions

One table, "things", with two sample rows inserted for testing.

Four functions with four different parameters:

  1. things()
  2. thing_get(integer)
  3. thing_add(text, text)
  4. thing_rename(integer, text)

See the file postgresql.sql here.

API client usage

The API client should:

  1. take the function name
  2. … and variable arguments of different types
  3. returning the "ok" boolean and "js" parsed JSON as a hash/map/array

API client example:

ok, js = qa("things")
ok, js = qa("thing_get", 1)
ok, js = qa("thing_add", "new name", "category")
ok, js = qa("thing_rename", 2, "new name")

API client, behind the scenes:

The API client needs to convert a call into SQL, like this:

First, count the incoming arguments to make parameter string

  • no arguments = "()"
  • one argument = "($1)"
  • two arguments = "($1, $2)"
  • three arguments = "($1, $2, $3)"
  • … and so on

Then, combine the function name to make the SQL string with numbered parameters. To match the above API client examples:

"select ok, js from things()"
"select ok, js from thing_get($1)"
"select ok, js from thing_add($1, $2)"
"select ok, js from thing_rename($1, $2)"

Then, pass that to PostgreSQL's "exec_params" with the actual arguments in an array.

Then, parse the PostgreSQL response so that:

  1. "t" or "f" is converted to native boolean true or false
  2. JSON is decoded into a hash/map or array, whose values can be string (UTF-8), integer, float, boolean, null, hash/map, or array

Dates do not need to be converted to native date type. They can remain strings.

YOUR MISSION: Do this in your language

See my Ruby example here: ruby-test.rb

I'd like to see how this can be done in your language.

First, install PostgreSQL and Ruby with the -init.sh scripts here.

Then, run "ruby test-ruby.rb" to make sure my example test works for you.

Then, make your own in your language.

GOALS:

1. simple

Don't be complex and abstract.

Make it as simple as possible. This is just a proof of concept for a tiny app. Not a massive enterprise.

My Ruby API client is only 5 lines, plus 1 line to connect to the database.

Yours will probably be longer, but not much longer.

2. minimal included modules

Use whatever is built-in to your language as much as possible, for easier deployment and management.

My Ruby API client has only one requirement: 'pg'

3. generic

Although the tiny example here only has one database table and four functions, the real world usage of this has hundreds of functions with various responses.

So your API must be able to pass back any JSON response, not try to model responses with a Struct.

Make one generic function, as described in "API client, behind the scenes", above. Don't make separate API functions for the four PostgreSQL functions given here.

Again: this one API function has to work for hundreds of different PostgreSQL functions and JSON responses.

4. init script and unit tests as proof

Besides the (Linux/BSD) OS-level installing of your language, make an init script (to be run as superuser) to install any libraries your example needs.

My example here, ruby-init.sh, is just one line: "gem install pg". Then the unit tests can be run as "ruby test-ruby.rb". Yours should be equally simple.

Re-create the seven tests I have in test-ruby.rb.

No need for a fancy testing suite. Note my one "assert_equal" function in Ruby was enough.

Owner
Similar Resources

Couchbase client in Go

A smart client for couchbase in go This is a unoffical version of a Couchbase Golang client. If you are looking for the Offical Couchbase Golang clien

Nov 27, 2022

Go client library for Pilosa

Go Client for Pilosa Go client for Pilosa high performance distributed index. What's New? See: CHANGELOG Requirements Go 1.12 and higher. Install Down

Dec 3, 2022

Golang client for redislabs' ReJSON module with support for multilple redis clients (redigo, go-redis)

Go-ReJSON - a golang client for ReJSON (a JSON data type for Redis) Go-ReJSON is a Go client for ReJSON Redis Module. ReJSON is a Redis module that im

Dec 25, 2022

redis client implement by golang, inspired by jedis.

godis redis client implement by golang, refers to jedis. this library implements most of redis command, include normal redis command, cluster command,

Dec 6, 2022

Go Memcached client library #golang

About This is a memcache client library for the Go programming language (http://golang.org/). Installing Using go get $ go get github.com/bradfitz/gom

Jan 8, 2023

Neo4j REST Client in golang

DEPRECATED! Consider these instead: https://github.com/johnnadratowski/golang-neo4j-bolt-driver https://github.com/go-cq/cq Install: If you don't ha

Nov 9, 2022

Neo4j client for Golang

neoism - Neo4j client for Go Package neoism is a Go client library providing access to the Neo4j graph database via its REST API. Status System Status

Dec 30, 2022

Go client for Redis

Redigo Redigo is a Go client for the Redis database. Features A Print-like API with support for all Redis commands. Pipelining, including pipelined tr

Jan 1, 2023

Type-safe Redis client for Golang

Redis client for Golang ❤️ Uptrace.dev - distributed traces, logs, and errors in one place Join Discord to ask questions. Documentation Reference Exam

Jan 1, 2023
Comments
  • Update cl1

    Update cl1

    Hello, Derek!

    I have updated the installation inctructions, init.sh script, and added all tests (according to your ruby example). Please, check it, and let me know if anything is not working properly. Also I am going to figure out how to make a binary and I will update the instructions accordingly.

  • Add files to github repo

    Add files to github repo

    1. Run sbcl-init.sh, it will create a compiled sbcl image sbcl-with-ql-postmodern-cljson.core in current directory.
    2. Then run the tests with run-sbcl.sh.
Related tags
PostgreSQL driver and toolkit for Go

pgx - PostgreSQL Driver and Toolkit pgx is a pure Go driver and toolkit for PostgreSQL. pgx aims to be low-level, fast, and performant, while also ena

Jan 4, 2023
Simple key-value store abstraction and implementations for Go (Redis, Consul, etcd, bbolt, BadgerDB, LevelDB, Memcached, DynamoDB, S3, PostgreSQL, MongoDB, CockroachDB and many more)

gokv Simple key-value store abstraction and implementations for Go Contents Features Simple interface Implementations Value types Marshal formats Road

Dec 24, 2022
Data access layer for PostgreSQL, CockroachDB, MySQL, SQLite and MongoDB with ORM-like features.
Data access layer for PostgreSQL, CockroachDB, MySQL, SQLite and MongoDB with ORM-like features.

upper/db is a productive data access layer (DAL) for Go that provides agnostic tools to work with different data sources

Jan 3, 2023
pogo is a lightweight Go PostgreSQL internal state query engine.

pogo is a lightweight Go PostgreSQL internal state query engine. It focuses on the data that are highly dynamic in nature, and provides some conv

Sep 19, 2021
logical is tool for synchronizing from PostgreSQL to custom handler through replication slot

logical logical is tool for synchronizing from PostgreSQL to custom handler through replication slot Required Postgresql 10.0+ Howto Download Choose t

Sep 2, 2022
Examples and code to assign a name to your MongoDB, MySQL, PostgreSQL, RabbitMQ, and redis connection.
Examples and code to assign a name to your MongoDB, MySQL, PostgreSQL, RabbitMQ, and redis connection.

your connection deserves a name ?? When your app interacts with an external system, assign a name to the connection. An external system in this contex

Dec 14, 2022
Go driver for PostgreSQL over SSH. This driver can connect to postgres on a server via SSH using the local ssh-agent, password, or private-key.

pqssh Go driver for PostgreSQL over SSH. This driver can connect to postgres on a server via SSH using the local ssh-agent, password, or private-key.

Nov 6, 2022
Package pbpgx provides a toolkit for easier Protocol Buffers interaction with PostgreSQL databases.

PBPGX Package pbpgx provides a toolkit for easier Protocol Buffers interaction with PostgreSQL databases. Pbpgx supports the Protocol Buffer types gen

Jun 27, 2022
Neo4j Rest API Client for Go lang

neo4j.go Implementation of client package for communication with Neo4j Rest API. For more information and documentation please read Godoc Neo4j Page s

Nov 9, 2022
Aerospike Client Go

Aerospike Go Client An Aerospike library for Go. This library is compatible with Go 1.9+ and supports the following operating systems: Linux, Mac OS X

Dec 14, 2022