ObjectBox is a fast object-oriented database for IoT and Mobile edge computing. With its Go support, ObjectBox is a perfect match for Raspberry Pi and will add instant speed to your next Raspberry project.

To get started right away, jump straight to the Installation section.

Local data persistence

In general, databases are great to add data persistence to your project. If you need to turn off the device or if there is a power outage, your sensor statistics or other interesting information are safe. The next time you boot up the application it’s there again as if nothing happened. Another rather obvious advantage is speed and independence from an Internet connection. Storing data on a local drive is just faster than sending it to the cloud and back. And when you store your data locally, your application works with or without an Internet connection. This also means, you have full control over all your data, especially regarding who shall be able to access it… Finally, this reduces cloud costs.

For many databases (e.g. SQLite, PostgreSQL or MySQL), you need to know SQL. And for managing complex datasets, you need to be really good at it. Some like it, but for many developers this is annoying. Also, you might worry about SQL injection attacks.

Advantages of ObjectBox

ObjectBox is a database uniquely designed to sit on IoT and Mobile devices and fix these issues.

Firstly, you do not formulate your queries in SQL. ObjectBox comes with easy APIs allowing you to use the constructs and syntax native to the language you are using anyway. In this way ObjectBox is a NoSQL database. Secondly, benchmarks show that ObjectBox is faster than alternatives. So, even if you’d like to insert the data of a lot of sensors simultaneously, you don’t need to worry about speed anymore. This has the nice side effect that more speed means less CPU and thus is resourceful in every respect. Last not least, because ObjectBox is designed for small devices, it just needs about 1 MB of disk space. If you need something smaller, you can check out the ObjectBox client for Azure Sphere.

If you want to use ObjectBox in your next Raspberry Pi project, here is how you get started:

Installation

We’ll talk about the Go version of ObjectBox here. There are bindings for C, Java and Swift as well – and we will publish articles in the future about them too.

First, you need to have access to the shell (i.e. command line) of your Raspberry Pi. It does not matter if you’re connected via SSH or directly, just make sure that your Raspberry Pi has a working internet connection. Then, just execute the following command to get started right away:

This creates a folder named go in your home directory with the following subdirectories:

  • go with Go 1.12.7 installation, unless you already had Go 1.12+ installed
  • objectbox with the shell script update-objectbox.sh you can execute to easily update ObjectBox upon a new release
  • projects/objectbox-go-test mainly with the file main.go which contains a demo application, with an entity model in its own directory.

Note: The script will install libobjectbox.so globally (confirm by typing “y”), so it might prompt you to enter your root password.

Working with ObjectBox Go

The first tool you will come across is objectbox-gogen. It generates the correct Go files out of a Go structure to make it usable with ObjectBox. Assuming your main.go looks something like this:

As long as you only change the program’s logic, you don’t need to do anything. If you change the model, simply execute go genereate ./... to update the auto-generated files for the ObjectBox model interface. Afterwards, you can run your program using go run . .

As a result, you’ll get a directory called objectbox with the files data.mdb and lock.mdb; these are the underlying database files and they will be reused when you execute the program again.

Summary

So, that’s basically it! Setting up ObjectBox and using ObjectBox is easy. Check it out yourself and please share your thoughts with us; we appreciate any feedback. 🙂

Until we release the next part of this tutorial discussing the features of ObjectBox in Go more thoroughly, feel free to check out the fully-fledged demo application in its official repository. Also, if you’d like to learn more about all other features of ObjectBox Go, have a look at the official documentation!

If you encounter any problems during this process, please reach out to us. The best way is to ask a question and tag it “ObjectBox” on Stack Overflow. Finally, we would love hearing your thoughts on ObjectBox via TwitterFacebook, or Mail (contact [at] objectbox . io).