Vault Functions
To manage vaults, use the functions exported by the Vault adapter.
Importing the adapter
import { vaultAdapter } from 'epicenter-libs';
The vaultAdapter namespace exports functions that make calls to the Vault API.
For descriptions of the objects used by the Vault adapter functions, read Vault entities.
Create
Define vault
Creates or updates a vault with the specified name and scope.
Vault permit
If you don't provide a permit, the following value is used as the default for both readLock and writeLock:
PARTICIPANTif the vault is scoped to a world.USERotherwise.
Mutation strategy
The optionals.mutationStrategy parameter determines how an existing vault is affected by an update:
ALLOW- An upsert. If the vault exists, it is updated with the values you pass to the function.DISALLOW- An insert. If the vault exists, no changes are made. Thechangedflag is set tofalse.ERROR- If the vault exists, no changes are made, and a conflict exception is thrown.
If optionals.mutationStrategy is omitted, the default behavior is ALLOW.
Permissions
Requires a role of PARTICIPANT or higher.
Function description
The define() function:
- Constructs a
POSTrequest to the endpoint/vault/{name}. - Defines a new vault or updates an existing one using the provided scope and optional configuration.
- Vault names are unique within their scope.
- Returns a
Vaultobject representing the created or updated vault.
export async function define(
name: string,
scope: { userKey?: string } & GenericScope,
optionals: {
items?: Items,
readLock?: keyof typeof ROLE,
writeLock?: keyof typeof ROLE,
ttlSeconds?: number,
mutationStrategy?: string,
allowChannel?: boolean,
} & RoutingOptions = {}
): Promise<Vault<unknown>>
Parameters
The vault name must be unique within the vault's scope.
name(string): Name of the vault to be created or updated.scope({ userKey?: string } & GenericScope): Defines the vault's scope. See theGenericScopeinterface.scope.scopeBoundary(string): Defines the type of scope boundary. Pass a value of theSCOPE_BOUNDARYenum.scope.scopeKey(string): Unique identifier for the scope.scope.userKey?(string, optional): Optional user key for a user-specific scope.
optionals(Type:{ ... } & RoutingOptions = {}):items?(Items): Data items for updating vault contents. To learn how to use this parameter, read about theItemsinterface.set: Key-value pairs setting and deleting items.push: Key-value pairs to append to arrays within an item.pop: Indicate which elements to remove from arrays.
readLock?(keyof typeof ROLE): Role allowed to view the vault.writeLock?(keyof typeof ROLE): Role allowed to update to the vault.ttlSeconds?(number): Vault lifespan in seconds. Can benull. The minimum lifespan you can set is 1800 seconds (30 minutes).mutationStrategy?(string): Determines behavior if vault already exists. For details, see Mutation strategy.allowChannel?(boolean): Iftrue, send out push notifications for this vault. Applicable in projects with phylogeny >= SILENT.RoutingOptions: Additional routing options. Pass network call options overrides here.
Return value
A promise that resolves to a Vault object representing the newly created or updated vault.
Usage example
import { vaultAdapter, SCOPE_BOUNDARY } from 'epicenter-libs';
await vaultAdapter.define('my-vault-name', {
scopeBoundary: SCOPE_BOUNDARY.GROUP,
scopeKey: 'GROUP_KEY'
});
Retrieve
Get vault
Retrieves a specific vault by its key.
Permissions
Requires a role of PARTICIPANT or higher.
Function description
The get() function:
- Constructs a
GETrequest to the endpoint/vault/{vaultKey}. - Attempts to retrieve the vault identified by the
vaultKey. - If vault is not found, returns
undefined. - Returns a
Vaultobject if the vault exists.
export async function get(
vaultKey: string,
optionals: RoutingOptions = {}
): Promise<Vault<unknown>>
Parameters
vaultKey(string): The unique key identifying the vault to retrieve.optionals(Type:RoutingOptions = {}): Optional routing options to override default behavior.
Return value
A promise that resolves to a Vault object if found, or undefined if not found.
Usage example
import { vaultAdapter } from 'epicenter-libs';
const vault = await vaultAdapter.get('00000166d59adcb0f497ddc1aad0270s7u45');
if (!vault) {
console.log('Vault not found');
}
Get vault with scope
Retrieves a specific vault using its name and scope.
Permissions
Requires a role of PARTICIPANT or higher.
Function description
The withScope() function:
- Constructs and sends a
GETrequest to the endpoint/vault/with/{scopeBoundary}/{scopeKey}/{userKey}/{name}. - Returns a
Vaultobject, orundefinedif the vault is not found.
export async function withScope(
name: string,
scope: { userKey?: string } & GenericScope,
optionals: RoutingOptions = {}
): Promise<Vault<unknown>>
Parameters
name(string): The name of the vault to retrieve.scope(Type:{ userKey?: string } & GenericScope): The scope of the vault.scopeBoundary(string): Pass a value of theSCOPE_BOUNDARYenum.scopeKey(string): A unique identifier of the scope.userKey(string, optional): Optional user key for a user-specific scope.
optionals(Type:RoutingOptions = {}): Additional routing options. Pass network overrides here.
Return value
A promise that resolves to a Vault object containing the vault data or undefined if not found.
Usage example
import { vaultAdapter } from 'epicenter-libs';
const vault = await vaultAdapter.withScope('my-vault', {
scopeBoundary: 'GROUP',
scopeKey: '0000017a445032dc38cb2cecd5fc56361946',
userKey: '0000018d61f1217b22ce0ae605ff00329w5y'
});
Get vaults by name
Retrieves one or more vaults that match the specified name.
Permissions
Requires a role of PARTICIPANT or higher.
Function description
The byName() function:
- Constructs a
GETrequest to the endpoint/vault/in/{groupName}/{episodeName}/{name}. - Accepts a vault name and optional parameters such as group name, episode name, user key, and whether to include vaults from related episodes.
- Returns a list of
Vaultobjects matching the criteria.
export async function byName(
name: string,
optionals: {
groupName?: string,
episodeName?: string,
userKey?: string,
includeEpisodes?: boolean,
} & RoutingOptions = {}
): Promise<Vault<unknown>[]>
Parameters
name(string): The name of the vault to look up.optionals(Type:{ groupName?: string, episodeName?: string, userKey?: string, includeEpisodes?: boolean } & RoutingOptions = {}): Additional filtering and routing options.groupName(string, optional): The group name to search within. Defaults to the current session's group name.episodeName(string, optional): The episode name to further scope the search.userKey(string, optional): User key.includeEpisodes(boolean, optional): If true, includes vaults from related episodes in the result.RoutingOptions: Additional routing options. Pass network call options overrides here.
Return value
A promise that resolves to an array of Vault objects that match the specified name and scope.
Usage example
import { vaultAdapter } from 'epicenter-libs';
// Retrieve vaults by name within a specific group and episode
const vaults = await vaultAdapter.byName('progress-tracker', {
groupName: 'group-123',
episodeName: 'episode-abc',
userKey: '0000018d61f1217b22ce0ae605ff00329w5y',
includeEpisodes: true
});
Search for vaults
Retrieves a list of vaults matching the specified search filters.
For reference on search filters, read Filter and sort syntax.
Permissions
Requires a role of PARTICIPANT or higher.
Function description
The list() function:
- Constructs a
GETrequest to the/vault/searchendpoint. - Accepts filtering and pagination options defined by the
GenericSearchOptionsinterface. - Accepts an optional parameter
optionalswhich can contain: - Returns a list of
Vaultobjects that match the criteria.
export async function list(
searchOptions: GenericSearchOptions,
optionals: { groupName?: string } & RoutingOptions = {}
): Promise<Vault<unknown>[]>
Parameters
searchOptions(Type:GenericSearchOptions): Search parameters, including filter and sort.optionals(Type:{ groupName?: string } & RoutingOptions = {}): An optional parameter containing:- A group name to list vaults scoped to a group.
- Additional routing options. Pass network overrides here.
Return value
A promise that resolves to an array of Vault objects matching the search criteria.
Usage example
import { vaultAdapter } from 'epicenter-libs';
const results = await vaultAdapter.list({// Pass a GenericSearchOptions object containing
filter: ['name=my-vault', 'scopeBoundary=GROUP'], // two filter strings and
first: 0, // pagination parameters
max: 20
});
Get count
Counts the number of vaults that match the specified search criteria.
For reference on search criteria, read Filter and sort syntax.
Permissions
Requires a role of PARTICIPANT or higher.
Function description
The count() function:
- Constructs a
GETrequest to the/vault/countendpoint. - Accepts a
GenericSearchOptionsobject to filter the results. - Accepts an optional parameter
optionalswhich can contain: - Returns the number of
Vaultobjects` that match the provided criteria.
export async function count(
searchOptions: GenericSearchOptions,
optionals: { groupName?: string } & RoutingOptions = {}
): Promise<Vault<unknown>[]>
Parameters
searchOptions(GenericSearchOptions): Criteria used to filter the vaults.optionals(Type:{ groupName?: string } & RoutingOptions = {}): Group name and additional routing options.
Return value
A promise that resolves to the number of Vault objects` matching the search filters.
Usage example
import { vaultAdapter } from 'epicenter-libs';
// Count how many vaults match a specific filter
const vaultCount = await vaultAdapter.count({ // Pass a GenericSearchOptions object containing
filter: ['type:progress'], // a filter string and
first: 0, // pagination parameters
max: 100
});
Update
Update contents
Updates the contents of a vault with new values.
The update() function modifies the data stored in a vault. To modify the vault (i.e., the container), there are two options:
- The
define()function updates both the properties and the data. - The
updateProperties()function updates only the properties of the vault.
Mutation key
To avoid conflicts from simultaneous data updates, use the mutationKey parameter. For details, read Updating the data.
Permissions
Requires a role of PARTICIPANT or higher.
Function description
The update() function:
- Constructs a
PUTrequest to the endpoint/vault/{vaultKey}. - Accepts an
itemsobject with optionalset,push, andpopoperations as defined by theItemsinterface. - Optionally includes a
mutationKeyand other routing options. - Returns an updated
Vaultobject.
export async function update(
vaultKey: string,
items: Items,
optionals: { mutationKey?: string } & RoutingOptions = {}
): Promise<Vault<unknown>>
Parameters
vaultKey(string): The unique key identifying the vault to be updated.items(Items): An object specifying the updates to be made. To learn how to use this parameter, read about theItemsinterface.set: Key-value pairs setting and deleting items.push: Key-value pairs to append to arrays within an item.pop: Indicate which elements to remove from arrays.
optionals(Type:{ mutationKey?: string } & RoutingOptions = {}): Additional options.mutationKey(string, optional): Use this parameter to ensure you are updating the latest copy of the vault's data. For details, read Updating the data.RoutingOptions: Additional routing options. Pass network call options overrides here.
Return value
A promise that resolves to an updated Vault object.
Usage example
import { vaultAdapter } from 'epicenter-libs';
// Change the name of the first student in the vault to "Bob"
await vaultAdapter.update(
'00000166d59adcb0f497ddc1aad0270f6e89',
{ set: { 'students.0.name': 'Bob' } }
);
Update properties
Updates the properties of a vault.
To update the vault's contents, use the update() function.
Permissions
Requires a role of PARTICIPANT or higher.
Function description
The updateProperties() function:
- Constructs a
PATCHrequest to the endpoint/vault/{vaultKey}. - Accepts an
updateobject with optional fields likeallowChannel,permit,ttlSeconds, andmutationKey. - Optionally includes routing options.
- Returns an updated
Vaultobject.
export async function updateProperties(
vaultKey: string,
update: {
mutationKey?: string,
allowChannel?: boolean,
permit?: Permit,
ttlSeconds?: number,
},
optionals: RoutingOptions = {}
): Promise<Vault<unknown>>
Parameters
vaultKey(string): The unique key identifying the vault to be updated.update(object): An object specifying the vault properties to update.mutationKey?(string, optional): Ensures you're updating the latest version of the vault. See Updating the data.allowChannel?(boolean, optional): Iftrue, allow push notifications for the vault. Applicable in projects with phylogeny >= SILENT.permit?(Permit, optional): Defines read/write access roles.readLock: Role required to read the vault.writeLock: Role required to modify the vault.
ttlSeconds?(number, optional): Vault lifespan in seconds. The minimum lifespan you can set is 1800 seconds (30 minutes).
optionals(RoutingOptions = {}, optional): Additional network call options.
Return value
A promise that resolves to an updated Vault object`.
Usage example
import { vaultAdapter, ROLE } from 'epicenter-libs';
// Update the vault's lifespan to 1 hour and restrict access to facilitators
await vaultAdapter.updateProperties(
'00000166d59adcb0f497ddc1aad0270c0a62',
{
allowChannel: true,
permit: {
readLock: ROLE.FACILITATOR,
writeLock: ROLE.FACILITATOR,
},
ttlSeconds: 3600,
}
);
Delete
Remove vault
Deletes a specific vault by its unique key.
Permissions
Requires a role of PARTICIPANT or higher.
Function description
The remove() function:
- Constructs a
DELETErequest to the endpoint/vault/{vaultKey}. - Optionally includes a
mutationKeyand other routing options. - Returns a
Promise<void>indicating the operation has completed.
export async function remove(
vaultKey: string,
optionals: { mutationKey?: string } & RoutingOptions = {}
): Promise<void>
Parameters
vaultKey(string): The unique key identifying the vault to be deleted.optionals(Type:{ mutationKey?: string } & RoutingOptions = {}): Additional options.mutationKey(string, optional): Mutation identifier to track the deletion.RoutingOptions: Additional routing options. Pass network call options overrides here.
Return value
A Promise<void> that resolves once the vault has been deleted.
Usage example
import { vaultAdapter } from 'epicenter-libs';
// Delete a vault by its key
await vaultAdapter.remove(
'00000166d59adcb0f497ddc1aad0270f6e89'
);