How to share data between users through Organizations
This guide shows you how to share a set of CoValues between users. Different apps have different names for this concept, such as "teams" or "workspaces".
We'll use the term Organization.
Defining the schema for an Organization
Create a CoMap shared by the users of the same organization to act as a root (or "main database") for the shared data within an organization.
For this example, users within an Organization will be sharing Projects.
export const Project = co.map({ name: z.string(), }); export const Organization = co.map({ name: z.string(), // shared data between users of each organization projects: co.list(Project), }); export const ListOfOrganizations = co.list(Organization);
Learn more about defining schemas.
Adding a list of Organizations to the user's Account
Let's add the list of Organizations to the user's Account root so they can access them.
export const JazzAccountRoot = co.map({ organizations: co.list(Organization), }); export const JazzAccount = co .account({ root: JazzAccountRoot, profile: co.profile(), }) .withMigration((account) => { if (!account.$jazz.has("root")) { // Using a Group as an owner allows you to give access to other users const organizationGroup = Group.create(); const organizations = co.list(Organization).create([ // Create the first Organization so users can start right away Organization.create( { name: "My organization", projects: co.list(Project).create([], organizationGroup), }, organizationGroup, ), ]); account.$jazz.set("root", { organizations }); } });
This schema now allows users to create Organizations and add Projects to them.
See the schema for the example app here.
Adding members to an Organization
Here are different ways to add members to an Organization.
- Send users an invite link.
- The user requests to join.
This guide and the example app show you the first method.
Adding members through invite links
Here's how you can generate an invite link.
When the user accepts the invite, add the Organization to the user's organizations list.