PrivMX DOCS
JavaScript

Overview

KVDBs provide encrypted key-value databases.

Before working with KVDBs, follow our Getting Started Guide. It will show you how to set up your project to work with PrivMX Bridge. Sample code on this page is based on the initial assumptions.

Working with KVDBs

When working with KVDBs, you will use the following:

  • KvdbApi - provides methods used to manage KVDBs in given Context
const kvdbApi  = await Endpoint.createKvdbApi(connection);

Creating KVDBs

Creating a basic, unnamed KVDB, which can act as an encrypted data container:

const managers = [
    {userId: USER1_ID, pubKey: USER1_PUBLIC_KEY}
];

const users = [
    {userId: USER1_ID, pubKey: USER1_PUBLIC_KEY},
    {userId: USER2_ID, pubKey: USER2_PUBLIC_KEY}
];

// create a new KVDB with access for USER_1 as manager and USER_2 as regular user
const kvdbId = await kvdbApi.createKvdb(CONTEXT_ID, users, managers,
    serializeObject("some kvdb's public meta-data"),
    serializeObject("some kvdb's private meta-data")
);

Fetching KVDBs

Fetching the most recent KVDBs in given Context:

const defaultListQuery = {skip: 0, limit: 100, sortOrder: "desc"};
const kvdbList = await kvdbApi.listKvdbs(CONTEXT_ID, defaultListQuery);

As a result you will receive an object:

// kvdbList:
{
    readItems: [<kvdbObject1>, <kvdbObject2>,..., <kvdbObjectN>],
    totalAvailable: <number_of_all_kvdbs>
}

A detailed description of the Kvdb object fields can be found in API Reference.

Modifying KVDBs

How Updates Work

To update a KVDB you must always provide a full list of parameters.

The updateKvdb(...) method needs all the parameters as in the createKvdb(...) method and a few more. If we want to update one of the parameters – provide it in a new modified form. If, on the other hand, you want to leave the parameter unchanged – provide it as it was before. All current values ​​of the parameters of a given KVDB can be obtained using getKvdb(kvdbId).

const kvdb = await kvdbApi.getKvdb(kvdbId);

const newUsers = kvdb.users.map(user => ({
    //Your application must provide a way,
    //to get user's public key from their userId.
    userId: user,
    pubKey: 'USER_PUBLIC_KEY'
    })
);

const newManagers = newUsers.filter(user =>
    kvdb.managers.find(manager => manager == user.userId));

const newPrivateMeta = {
    title: 'New kvdb name'
};

await kvdbApi.updateKvdb(
    kvdbId,
    newUsers,
    newManagers,
    kvdb.publicMeta,
    serializeObject(newPrivateMeta),
    kvdb.version,
    false,
    false
);

We use cookies on our website. We use them to ensure the proper functioning of the site and, if you agree, for purposes we set, such as analytics or marketing.

On this page