Create Group
The group creation functionality enables the organization and management of documents within a database by grouping them under specified categories. This can help facilitate access control, permissions management, and operations organization within a Zero-Knowledge Database (zkDatabase).
Syntax
The create method is used to create a new group within a specified database, adding structure and categorization to the database.
const dbTest = zkdb.db(databaseName);
const group = dbTest.group(groupName);
// Check if group exists before creating
if (!(await group.exist()).unwrapOr(false)) {
(await group.create({ groupDescription: 'My group description' })).unwrap();
}
Parameters
groupName:string- The name of the new group to be created.groupDescription:string- A textual description of the group.
Returns
- A
Resultthat can be checked with.isOk()/.isErr()or unwrapped with.unwrap().
Example
const dbTest = zkdb.db('my_db');
const editorsGroup = dbTest.group('editors');
// Create group if not exists
if (!(await editorsGroup.exist()).unwrapOr(false)) {
(await editorsGroup.create({ groupDescription: 'Content editors' })).unwrap();
console.log('Group created');
}
// Get group info
const info = (await editorsGroup.info()).unwrap();
console.log('Group:', info);
Important Notes
-
Owner-Only Operation: Only the database owner has the permissions required to create a group. Ensure that your application logic checks user roles and ownership before attempting to call
createGroup. -
Unique Group Names: Group names must be unique within the database. Attempting to create a group with a duplicate name will result in an error.
Use Case: Managing Permissions with Groups
Creating groups is essential for managing permissions effectively in a database. By grouping users based on their roles or responsibilities, you can simplify permission management by assigning permissions at the group level rather than individually for each user.
For example, you might create groups such as "Admins", "Editors", or "Viewers" and assign them varying levels of access to different collections or documents. This approach enhances both security and ease of management.
Summary
The createGroup method in zkDatabase provides a powerful way to manage user permissions by grouping users within a database. This method is restricted to database owners, ensuring that only authorized users can modify group structures and permissions, thus maintaining the integrity and security of the database.