Last year I felt I wanted to contribute in some way to the S̸c̸a̸l̸a̸ Kotlin community. Since I didn’t think I was smart enough to work on the main libraries or compiler, I started to look for something that I could build and would benefit me and the community in general. I started looking around and noticed that there weren’t async drivers for the good old relational databases. If you’re fancy and you’re using one of the new NoSQL ones, you are most likely covered, but if you still rely on databases like P̸o̸s̸t̸g̸r̸e̸S̸Q̸L̸ MySQL (like I do at my daily job), you have to rely on the official (and blocking) P̸o̸s̸t̸g̸r̸e̸S̸Q̸L̸ MySQL JDBC driver and so the p̸o̸s̸t̸g̸r̸e̸s̸q̸l̸-̸a̸s̸y̸n̸c̸ ̸p̸r̸o̸j̸e̸c̸t̸ jasync-sql was born.

I started looking at what people did in the NodeJS community like node-postgres and I noticed you don’t really need the full JDBC implementation for a usable database driver, as long as you can execute statements and get something back, you probably have all you need so this was the goal. Build something that would allow you to execute queries and get results back.

Connecting to the database

Let’s see some sample usage:

The basic usage pattern is quite simple, you ask for something, you get a CompletableFuture<*> back. In this case, I’m simplifying the code by blocking to get the results, but if you’re using an async framework (like A̸k̸k̸a̸ ̸o̸r̸ ̸P̸l̸a̸y̸ RxJava or kotlinx-coroutines) you can just compose on these futures to do your work.

The MySQLConnection is a real connection to the database, it implements the Connection interface and you should try to use the interface as much as possible. When you create a connection handler, it’s not connected to the database yet, you have to connect it yourself calling connect() and waiting for the future to return or composing on the future to do something else.