Sharing data through Organizations

Organizations are a way to share a set of data 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.

// schema.ts export class Project extends CoMap { name = co.string; } export class ListOfProjects extends CoList.Of(co.ref(Project)) {} export class Organization extends CoMap { name = co.string; // shared data between users of each organization projects = co.ref(ListOfProjects); } export class ListOfOrganizations extends CoList.Of(co.ref(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.

// schema.ts export class JazzAccountRoot extends CoMap { organizations = co.ref(ListOfOrganizations); } export class JazzAccount extends Account { root = co.ref(JazzAccountRoot); async migrate(creationProps?: { name: string }) { super.migrate(creationProps); if (!this._refs.root) { // Using a Group as an owner allows you to give access to other users const initialOrganizationOwnership = { owner: Group.create({ owner: this }), }; const organizations = ListOfOrganizations.create( [ // Create the first Organization so users can start right away Organization.create( { name: "My organization", projects: ListOfProjects.create([], initialOrganizationOwnership), }, initialOrganizationOwnership, ), ], { owner: this }, ); this.root = JazzAccountRoot.create( { organizations }, { owner: this }, ); } } }

This schema now allows users to create Organizations and add Projects to them.

Adding other users to an Organization

To give users access to an Organization, you can either send them an invite link, or add their Account manually.

Here's how you can generate an invite link.

When the user accepts the invite, add the Organization to the user's organizations list.

const onAccept = (organizationId: ID<Organization>) => { if (me?.root?.organizations) { Organization.load(organizationId, me, []).then((organization) => { if (organization) { // Avoid duplicates const ids = me.root.organizations.map( (organization) => organization?.id, ); if (ids.includes(organizationId)) return; me.root.organizations.push(organization); navigate("/organizations/" + organizationId); } }); } }; useAcceptInvite({ invitedObjectSchema: Organization, onAccept, });

Adding users through their Account ID

...more on this coming soon