PrivMX DOCS
Swift

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 with the addition of the following values:

var kvdbApi : KvdbApi

let KVDB_ID = "ID of your KVDB"
let KVDB_ENTRY_KEY = "KEY"
let KVDB_ENTRY_KEY_1 = "KEY_1"
let KVDB_ENTRY_KEY_2 = "KEY_2"

Working with KVDBs

To access KVDBs methods, get the field kvdbApi from the active endpointSession. Session should be initialized with [.kvdb] and passed to PrivMXEndpoint. You can use a guard let statement to ensure the kvdbApi field is not null and avoid having to deal with optionals.

guard let sessionKvdbApi = endpointSession?.kvdbApi
else {return}
kvdbApi = sessionKvdbApi

In the below code snippets we assume that kvdbApi has been assigned like this.

Creating KVDBs

Creating a basic KVDB, which acts as an encrypted key-value database:

// ...

let publicMeta = Data()
let privateMeta = Data()

let kvdbId = try kvdbApi.createKvdb(
	in: CONTEXT_ID,
	for: [privmx.endpoint.core.UserWithPubKey(
		userId: USER1_ID,
		pubKey: USER1_PUBLIC_KEY)
		],
	managedBy: [privmx.endpoint.core.UserWithPubKey(
		userId: USER1_ID,
		pubKey: USER1_PUBLIC_KEY)
		],
	withPublicMeta: Data(),
	withPrivateMeta: Data())
}

// ...

Fetching KVDBs

Getting a list of KVDBs available for the user in the given Context:

// ...

let kvdbList = try kvdbApi.listKvdbs(
	from: CONTEXT_ID,
	basedOn: privmx.endpoint.core.PagingQuery(
		skip: 0,
		limit: 25,
		sortOrder: .desc))

// ...

As a result you will receive a privmx.KvdbsList object.

Getting a single KVDB:

// ...

let KVDB_ID = "ID of your KVDB"
		
let kvdb = kvdbApi.getKvdb(KVDB_ID)

// ...

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(_:atVersion:replacingUsers:replacingManagers:replacingPublicMeta:replacingPrivateMeta:force:forceGenerateNewKey:) method needs all the parameters as in the createKvdb(in:for:managedBy:withPublicMeta:withPrivateMeta:) method and a few more.

If you 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.

// ...

let KVDB_ID = "ID of your KVDB"

let currentKvdb = try kvdbApi.getKvdb(KVDB_ID)

try kvdbApi.updateKvdb(
	kvdbId,
	atVersion: currentKvdb.version,
	replacingUsers: [privmx.endpoint.core.UserWithPubKey(
		userId: USER1_ID,
		pubKey: USER1_PUBLIC_KEY)
		],
	replacingManagers: [privmx.endpoint.core.UserWithPubKey(
		userId: USER1_ID,
		pubKey: USER1_PUBLIC_KEY)
		],
	replacingPublicMeta: Data(from:currentKvdb.publicMeta),
	replacingPrivateMeta: Data(from: currentKvdb.privateMeta),
	force: false,
	forceGenerateNewKey: false)

// ...

Deleting KVDBs

To delete a KVDB, use deleteKvdb(_:) method.

// ...
let KVDB_ID = "ID of your KVDB"

try kvdbApi.deleteKvdb(KVDB_ID)
//...

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