How to write an API for PouchDB
In our forum someone has asked how he can use PouchDB in DukeScript. In case you don’t know PouchDB this is the description from their website:
“PouchDB is an in-browser database that allows applications to save data locally, so that users can enjoy all the features of an app even when they’re offline. Plus, the data is synchronized between clients, so users can stay up-to-date wherever they go.”
Maybe this is a nice chance for a blog post on writing a Java API for a JavaScript library. I’ve started by creating a little application “pouchdbdemo” based on the Knockout4j Archetype. Don’t bother doing this now, I’ve published the sources on Github for your convenience. The App does nothing fancy except creating a DB, storing one item and logging a message on success, but it might help you to see the ideas behind creating a simple API.
To create a Java API you only need to know two annotations: @JavaScriptBody and @JavaScriptRessource. The API Doc has some further info how to use them.
@JavaScriptRessource
The purpose of @JavaScriptRessource is simple. It allows you to load a JavaSript Library. Why is there an Annotation for this? You could also simply put a reference to it in your HTML-Page…
The reason is that it gives you a way to version the library. Others can then reference it via a Maven Dependency. And you can bundle it with your Java API. I’ve downloaded the latest version of PouchDB and put it into the “pouchdbdemo JavaScript Libraries” module. Now I create a class with this annotation:
NetBeans will complain that there needs to be at least one method with a @JavaScriptBody Annotation. We’ll fix that now:
@JavaScriptBody
Methods annotated with @JavaScriptBody can be used to execute JavaScript code from Java. In addition it makes it really easy to pass arguments to the JavaScriptCode. This is the code to create a pouchdb instance in JavaScript:
In order to wrap this call I can do this:
So instead of needing to construct the JavaScript String via some String operations, I can simply pass the name as an argument. In addition we’ve also made the method call type save. You can only pass in Strings.
Still the method isn’t very nice, because it would return some odd JavaScript Object to the user of your API. Java Developers don’t want to deal with that. The best strategy to make a nice API is to wrap this object and keep it out of sight. It’s enough that you as an API developer has to deal with ugly JavaScript:
Much nicer. The user of your API can now create a new PouchDB and will receive a beautiful type save Java Object:
Not only are we able to use the JavaScript Library, we’ve also made it better. The user of the API immediately knows what he will get back from the call, a PouchDB. And also the IDE knows what it will get, and can offer methods you can call on this Object (we’ll create some soon). And also the compiler knows what this is.
All that JavaScript users get is a “var” with no further clues on how to use it. It could be anything from an int to a function.
So let’s see what else the PouchDB API has… The tutorial stores a message like this:
Let’s convert it to a Java API:
I mainly left the JS code unchanged, but added some logging. Notice how the public API method only requires you to pass the string. We then get the wrapped JavaScript “pouch” and pass it to the implementation. I usually follow this pattern in all my APIs.
You can now call the API like this:
So instead of a bunch of letters in a bag (aka JavaScript) you have a nice and type save API. Let’s stop here for now, as this should be the most important concepts you need for writing an API.
If you’re interested in having such an API or helping, let us know. This is a great chance to get some in depth knowledge about DukeScript. And in case you need this right now, but have no time, be a sponsor and bribe us to put it on top of our todo list ;-).
If you just want to play with it, do so here.