NoSQL Key-Value database with open web services

Jonas Birmé - Feb 10 - - Dev Community

In this blog post we will show how quickly you can get started with storing data in a NoSQL Key-Value database in the cloud based on open web services in Eyevinn Open Source Cloud. Based on Apache CouchDB as a service in Eyevinn Open Source Cloud you don't have to manage and maintain the database server yourself and can immediately start to store and read your data.

Apache CouchDB in Eyevinn OSC

In this guide

  1. Get an API Access Token and setup project
  2. Setup and create a database
  3. Insert and read documents

Prerequisites

Get an API Access Token and setup project

Navigate to Settings / API in the Eyevinn Open Source Cloud web console.

Access token

Copy this token and store in your shell's environment in the environment variable OSC_ACCESS_TOKEN.

% export OSC_ACCESS_TOKEN=<access-token-copied-above>
Enter fullscreen mode Exit fullscreen mode

Setup a NodeJS project.

% mkdir nosql
% cd nosql
% npm init
Enter fullscreen mode Exit fullscreen mode

Install the Javascript client SDK.

% npm install --save @osaas/client-core @osaas/client-services
Enter fullscreen mode Exit fullscreen mode

A NoSQL database for a blogging platform

As an example we will build a simple blog platform where we will store the blog posts in a NoSQL database. Instead of using tables, a NoSQL database stores data in JSON-like documents.

Create a file called blog.js and then add the following code that will create an CouchDB instance in Eyevinn Open Source Cloud with the name "example" if it does not already exists. Then it will create a database called "blog".

const { Context } = require('@osaas/client-core');
const {
  createApacheCouchdbInstance,
  getApacheCouchdbInstance
} = require('@osaas/client-services');
const nano = require('nano');

async function setup() {
  const ctx = new Context();
  let instance = await getApacheCouchdbInstance(ctx, 'example');
  if (!instance) {
    instance = await createApacheCouchdbInstance(ctx, {
      name: 'example',
      AdminPassword: 'secret'
    });
  }
  const url = new URL(instance.url);
  url.password = instance.AdminPassword;
  url.username = 'admin';
  const client = nano(url.toString());
  const dbList = await client.db.list();
  if (!dbList.includes('blog')) {
    await client.db.create('blog');
  }
  return client.db.use('blog');
}

Enter fullscreen mode Exit fullscreen mode

We will use the Apache CouchDB client SDK for javascript called nano so we need to install that dependency.

% npm install --save nano
Enter fullscreen mode Exit fullscreen mode

Now add a main() function to our file.

async function main() {
  const db = await setup();
}
Enter fullscreen mode Exit fullscreen mode

Then we can try to run this file.

% node blog.js
Enter fullscreen mode Exit fullscreen mode

To verify that a database has been created we can go to the Eyevinn Open Source Cloud web console and click on the instance card for the database instance called "example".

Eyevinn OSC web console

After logging in with the credentials that we specified when creating the instance we find the list of databases on this instance.

Apache CouchDB in Eyevinn OSC

We now see that we have a database called "blog" that we created from our application.

Insert and read documents

Now let us add a document in the database by adding the following to our main function.

async function main() {
  const db = await setup();
  await db.insert({
    title: 'Getting started with NoSQL in Eyevinn OSC',
    content: 'Learn the basics of NoSQL in Eyevinn OSC',
    author: 'Jonas Birmé',
    createdAt: new Date()
  });
}
Enter fullscreen mode Exit fullscreen mode

Running the application again we can check in the web console that a new document has been inserted.

A document has been added

To query data from this database we can replace the insert in our main function with the following:

async function main() {
  const db = await setup();
  const posts = await db.list({ include_docs: true });
  posts.rows.forEach((post) => console.log(post.doc));
}
Enter fullscreen mode Exit fullscreen mode

Now running this application we will get.

% node blog.js
{
  _id: '448ac3db332c49841771e2068900197b',
  _rev: '1-dabd2ae20ac9fa41dfbee4d87dcc7861',
  title: 'Getting started with NoSQL in Eyevinn OSC',
  content: 'Learn the basics of NoSQL in Eyevinn OSC',
  author: 'Jonas Birmé',
  createdAt: '2025-02-10T20:14:53.582Z'
}
Enter fullscreen mode Exit fullscreen mode

We can also query all blog posts written by the author "Jonas Birmé".

async function main() {
  const db = await setup();
  const result = await db.find({
    selector: {
      author: { "$eq": "Jonas Birmé" }
    },
    fields: [ "title" ]
  });
  result.docs.forEach((post) => console.log(post));
}
Enter fullscreen mode Exit fullscreen mode

And the output will then be:

% node blog.js
{ title: 'Getting started with NoSQL in Eyevinn OSC' }
Enter fullscreen mode Exit fullscreen mode

Conclusion

This was a guiding example of how you quickly can get a NoSQL database available for your application without having to worry about database servers and hosting infrastructure. Something made possible with Apache CouchDB available as an open web service in Eyevinn Open Source Cloud. You are also not locked in with a specific cloud vendor as it is based on open source.

. . . . . .