Overview
Stores provide encrypted block storage, enabling simple file uploading and downloading.
Info
Before working with Stores, follow our Getting Started Guide. It will show you how to set up your project to work with PrivMX Bridge.
The sample code on this page is based on the same assumptions mentioned in Fist App.
Working with Stores
When working with Stores, you will use the following:
When working with Threads, you will use the following:
storeApi
- provides methods used to manage Stores in given Context
const storeApi = await EndpointFactory.createStoreApi(CONNECTION_ID);
Creating Stores
Creating a basic unnamed Store, which can act as an encrypted block 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 Store with access for user_1 as manager and user_2 as regular user
// for example, let's put the name of the store as an object in the store's private meta
const storeId = await storesApi.createStore(CONTEXT_ID, users, managers,
serializeObject("some store's public meta-data"),
serializeObject("some store's private meta-data")
);
Fetching Stores
Fetching the most recent Stores in given Context:
const defaultListQuery = {skip: 0, limit: 100, sortOrder: "desc"};
const storesList = await storeApi.listStores(
CONTEXT_ID,
defaultListQuery
);
As a result you will receive an object:
// storesList:
{
readItems: [<storeObject1>, <storeObject2>,..., <storeObjectN>],
totalAvailable: <number_of_all_stores>
}
A detailed description of the Store
object fields can be found here.
Modifying Stores
The updateStore(...)
method needs all the parameters as in the createStore(...)
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. All current values of the parameters of a given Store can be
obtained using getStore(threadId)
.
const store = await storeApi.getStore(
storeId
);
const newUsers = store.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 =>
store.managers.find(manager => manager == user.userId));
const newPrivateMeta = {
title: 'New store name'
};
await storeApi.updateStore(
storeId
newUsers,
newManagers,
store.publicMeta,
serializeObject(newPrivateMeta)
store.version,
false,
false
);