Accounts & Migrations

CoValues as a graph of data rooted in accounts

Compared to traditional relational databases with tables and foreign keys, Jazz is more like a graph database, or GraphQL APIs — where CoValues can arbitrarily refer to each other and you can resolve references without having to do a join. (See Subscribing & deep loading).

To find all data related to a user, the account acts as a root node from where you can resolve all the data they have access to. These root references are modeled explicitly in your schema, distinguishing between data that is typically public (like a user's profile) and data that is private (like their messages).

Account.root - private data a user cares about

Every Jazz app that wants to refer to per-user data needs to define a custom root CoMap schema and declare it in a custom Account schema as the root field:

import { import coco, import zz } from "jazz-tools";

const 
const MyAppRoot: CoMapSchema<{
    myChats: CoListSchema<CoMapSchema<{}>>;
}>
MyAppRoot
= import coco.
map<{
    myChats: CoListSchema<CoMapSchema<{}>>;
}>(shape: {
    myChats: CoListSchema<CoMapSchema<{}>>;
}): CoMapSchema<{
    myChats: CoListSchema<CoMapSchema<{}>>;
}>
export map
map
({
myChats: CoListSchema<CoMapSchema<{}>>myChats: import coco.
list<CoMapSchema<{}>>(element: CoMapSchema<{}>): CoListSchema<CoMapSchema<{}>>
export list
list
(const Chat: CoMapSchema<{}>Chat),
}); export const
const MyAppAccount: AccountSchema<{
    root: CoMapSchema<{
        myChats: CoListSchema<CoMapSchema<{}>>;
    }>;
    profile: CoProfileSchema<{
        name: z.z.core.$ZodString<string>;
        inbox?: z.z.core.$ZodOptional<z.z.core.$ZodString>;
        inboxInvite?: z.z.core.$ZodOptional<z.z.core.$ZodString>;
    }>;
}>
MyAppAccount
= import coco.
account<{
    root: CoMapSchema<{
        myChats: CoListSchema<CoMapSchema<{}>>;
    }>;
    profile: CoProfileSchema<{
        name: z.z.core.$ZodString<string>;
        inbox?: z.z.core.$ZodOptional<z.z.core.$ZodString>;
        inboxInvite?: z.z.core.$ZodOptional<z.z.core.$ZodString>;
    }>;
}>(shape?: {
    root: CoMapSchema<{
        myChats: CoListSchema<CoMapSchema<{}>>;
    }>;
    profile: CoProfileSchema<{
        name: z.z.core.$ZodString<string>;
        inbox?: z.z.core.$ZodOptional<z.z.core.$ZodString>;
        inboxInvite?: z.z.core.$ZodOptional<z.z.core.$ZodString>;
    }>;
} | undefined): AccountSchema<...>
export account
account
({
root: CoMapSchema<{
    myChats: CoListSchema<CoMapSchema<{}>>;
}>
root
:
const MyAppRoot: CoMapSchema<{
    myChats: CoListSchema<CoMapSchema<{}>>;
}>
MyAppRoot
,
profile: CoProfileSchema<{
    name: z.z.core.$ZodString<string>;
    inbox?: z.z.core.$ZodOptional<z.z.core.$ZodString>;
    inboxInvite?: z.z.core.$ZodOptional<z.z.core.$ZodString>;
}>
profile
: import coco.
profile<{
    name: z.z.core.$ZodString<string>;
    inbox?: z.z.core.$ZodOptional<z.z.core.$ZodString>;
    inboxInvite?: z.z.core.$ZodOptional<z.z.core.$ZodString>;
}>(shape?: ({
    name: z.z.core.$ZodString<string>;
    inbox?: z.z.core.$ZodOptional<z.z.core.$ZodString>;
    inboxInvite?: z.z.core.$ZodOptional<z.z.core.$ZodString>;
} & {
    ...;
}) | undefined): CoProfileSchema<...>
export profile
profile
(),
});

Account.profile - public data associated with a user

The built-in Account schema class comes with a default profile field, which is a CoMap (in a Group with "everyone": "reader" - so publicly readable permissions) that is set up for you based on the username the AuthMethod provides on account creation.

Their pre-defined schemas roughly look like this:

// ...somewhere in jazz-tools itself...
const 
const Account: AccountSchema<{
    root: CoMapSchema<{}>;
    profile: CoProfileSchema<{
        name: z.z.ZodString;
    }>;
}>
Account
= import coco.
account<{
    root: CoMapSchema<{}>;
    profile: CoProfileSchema<{
        name: z.z.ZodString;
    }>;
}>(shape?: {
    root: CoMapSchema<{}>;
    profile: CoProfileSchema<{
        name: z.z.ZodString;
    }>;
} | undefined): AccountSchema<...>
export account
account
({
root: CoMapSchema<{}>root: import coco.
map<{}>(shape: {}): CoMapSchema<{}>
export map
map
({}),
profile: CoProfileSchema<{
    name: z.z.ZodString;
}>
profile
: import coco.
profile<{
    name: z.z.ZodString;
}>(shape?: ({
    name: z.z.ZodString;
} & {
    name?: z.z.core.$ZodString<string>;
    inbox?: z.z.core.$ZodOptional<z.z.core.$ZodString>;
    inboxInvite?: z.z.core.$ZodOptional<z.z.core.$ZodString>;
}) | undefined): CoProfileSchema<...>
export profile
profile
({
name: z.z.ZodString & z.z.core.$ZodString<string>name: import zz.
function string(params?: string | z.z.core.$ZodStringParams): z.z.ZodString
export string
string
(),
}), });

If you want to keep the default co.profile() schema, but customise your account's private root, all you have to do is define a new root field in your account schema and use co.profile() without options:

const 
const MyAppRoot: CoMapSchema<{
    myChats: CoListSchema<CoMapSchema<{}>>;
}>
MyAppRoot
= import coco.
map<{
    myChats: CoListSchema<CoMapSchema<{}>>;
}>(shape: {
    myChats: CoListSchema<CoMapSchema<{}>>;
}): CoMapSchema<{
    myChats: CoListSchema<CoMapSchema<{}>>;
}>
export map
map
({
myChats: CoListSchema<CoMapSchema<{}>>myChats: import coco.
list<CoMapSchema<{}>>(element: CoMapSchema<{}>): CoListSchema<CoMapSchema<{}>>
export list
list
(const Chat: CoMapSchema<{}>Chat),
}); export const
const MyAppAccount: AccountSchema<{
    root: CoMapSchema<{
        myChats: CoListSchema<CoMapSchema<{}>>;
    }>;
    profile: CoProfileSchema<{
        name: z.z.core.$ZodString<string>;
        inbox?: z.z.core.$ZodOptional<z.z.core.$ZodString>;
        inboxInvite?: z.z.core.$ZodOptional<z.z.core.$ZodString>;
    }>;
}>
MyAppAccount
= import coco.
account<{
    root: CoMapSchema<{
        myChats: CoListSchema<CoMapSchema<{}>>;
    }>;
    profile: CoProfileSchema<{
        name: z.z.core.$ZodString<string>;
        inbox?: z.z.core.$ZodOptional<z.z.core.$ZodString>;
        inboxInvite?: z.z.core.$ZodOptional<z.z.core.$ZodString>;
    }>;
}>(shape?: {
    root: CoMapSchema<{
        myChats: CoListSchema<CoMapSchema<{}>>;
    }>;
    profile: CoProfileSchema<{
        name: z.z.core.$ZodString<string>;
        inbox?: z.z.core.$ZodOptional<z.z.core.$ZodString>;
        inboxInvite?: z.z.core.$ZodOptional<z.z.core.$ZodString>;
    }>;
} | undefined): AccountSchema<...>
export account
account
({
root: CoMapSchema<{
    myChats: CoListSchema<CoMapSchema<{}>>;
}>
root
:
const MyAppRoot: CoMapSchema<{
    myChats: CoListSchema<CoMapSchema<{}>>;
}>
MyAppRoot
,
profile: CoProfileSchema<{
    name: z.z.core.$ZodString<string>;
    inbox?: z.z.core.$ZodOptional<z.z.core.$ZodString>;
    inboxInvite?: z.z.core.$ZodOptional<z.z.core.$ZodString>;
}>
profile
: import coco.
profile<{
    name: z.z.core.$ZodString<string>;
    inbox?: z.z.core.$ZodOptional<z.z.core.$ZodString>;
    inboxInvite?: z.z.core.$ZodOptional<z.z.core.$ZodString>;
}>(shape?: ({
    name: z.z.core.$ZodString<string>;
    inbox?: z.z.core.$ZodOptional<z.z.core.$ZodString>;
    inboxInvite?: z.z.core.$ZodOptional<z.z.core.$ZodString>;
} & {
    ...;
}) | undefined): CoProfileSchema<...>
export profile
profile
(),
});

If you want to extend the profile to contain additional fields (such as an avatar co.image()), you can declare your own profile schema class using co.profile({...}):

export const 
const MyAppRoot: CoMapSchema<{
    myChats: CoListSchema<CoMapSchema<{}>>;
}>
MyAppRoot
= import coco.
map<{
    myChats: CoListSchema<CoMapSchema<{}>>;
}>(shape: {
    myChats: CoListSchema<CoMapSchema<{}>>;
}): CoMapSchema<{
    myChats: CoListSchema<CoMapSchema<{}>>;
}>
export map
map
({
myChats: CoListSchema<CoMapSchema<{}>>myChats: import coco.
list<CoMapSchema<{}>>(element: CoMapSchema<{}>): CoListSchema<CoMapSchema<{}>>
export list
list
(const Chat: CoMapSchema<{}>Chat),
}); export const
const MyAppProfile: CoProfileSchema<{
    name: z.z.ZodString;
    avatar: z.ZodOptional<WithHelpers<CoMapSchema<{
        originalSize: z.z.ZodTuple<[z.z.ZodNumber, z.z.ZodNumber], null>;
        placeholderDataURL: z.ZodOptional<z.z.ZodString>;
    }, z.z.core.$catchall<...>>, {
        ...;
    }>>;
}>
MyAppProfile
= import coco.
profile<{
    name: z.z.ZodString;
    avatar: z.ZodOptional<WithHelpers<CoMapSchema<{
        originalSize: z.z.ZodTuple<[z.z.ZodNumber, z.z.ZodNumber], null>;
        placeholderDataURL: z.ZodOptional<z.z.ZodString>;
    }, z.z.core.$catchall<...>>, {
        ...;
    }>>;
}>(shape?: ({
    name: z.z.ZodString;
    avatar: z.ZodOptional<WithHelpers<CoMapSchema<{
        originalSize: z.z.ZodTuple<[z.z.ZodNumber, z.z.ZodNumber], null>;
        placeholderDataURL: z.ZodOptional<z.z.ZodString>;
    }, z.z.core.$catchall<...>>, {
        ...;
    }>>;
} & {
    ...;
}) | undefined): CoProfileSchema<...>
export profile
profile
({
name: z.z.ZodString & z.z.core.$ZodString<string>name: import zz.
function string(params?: string | z.z.core.$ZodStringParams): z.z.ZodString
export string
string
(), // compatible with default Profile schema
avatar: z.ZodOptional<WithHelpers<CoMapSchema<{
    originalSize: z.z.ZodTuple<[z.z.ZodNumber, z.z.ZodNumber], null>;
    placeholderDataURL: z.ZodOptional<z.z.ZodString>;
}, z.z.core.$catchall<...>>, {
    ...;
}>>
avatar
: import zz.
optional<WithHelpers<CoMapSchema<{
    originalSize: z.z.ZodTuple<[z.z.ZodNumber, z.z.ZodNumber], null>;
    placeholderDataURL: z.ZodOptional<z.z.ZodString>;
}, z.z.core.$catchall<...>>, {
    ...;
}>>(innerType: WithHelpers<...>): z.ZodOptional<...>
export optional
optional
(import coco.
function image(): typeof ImageDefinition
export image
image
()),
}); export const
const MyAppAccount: AccountSchema<{
    root: CoMapSchema<{
        myChats: CoListSchema<CoMapSchema<{}>>;
    }>;
    profile: CoProfileSchema<{
        name: z.z.ZodString;
        avatar: z.ZodOptional<WithHelpers<CoMapSchema<{
            originalSize: z.z.ZodTuple<[z.z.ZodNumber, z.z.ZodNumber], null>;
            placeholderDataURL: z.ZodOptional<z.z.ZodString>;
        }, z.z.core.$catchall<...>>, {
            ...;
        }>>;
    }>;
}>
MyAppAccount
= import coco.
account<{
    root: CoMapSchema<{
        myChats: CoListSchema<CoMapSchema<{}>>;
    }>;
    profile: CoProfileSchema<{
        name: z.z.ZodString;
        avatar: z.ZodOptional<WithHelpers<CoMapSchema<{
            originalSize: z.z.ZodTuple<[z.z.ZodNumber, z.z.ZodNumber], null>;
            placeholderDataURL: z.ZodOptional<z.z.ZodString>;
        }, z.z.core.$catchall<...>>, {
            ...;
        }>>;
    }>;
}>(shape?: {
    root: CoMapSchema<{
        myChats: CoListSchema<CoMapSchema<{}>>;
    }>;
    profile: CoProfileSchema<{
        name: z.z.ZodString;
        avatar: z.ZodOptional<WithHelpers<CoMapSchema<{
            originalSize: z.z.ZodTuple<[z.z.ZodNumber, z.z.ZodNumber], null>;
            placeholderDataURL: z.ZodOptional<z.z.ZodString>;
        }, z.z.core.$catchall<...>>, {
            ...;
        }>>;
    }>;
} | undefined): AccountSchema<...>
export account
account
({
root: CoMapSchema<{
    myChats: CoListSchema<CoMapSchema<{}>>;
}>
root
:
const MyAppRoot: CoMapSchema<{
    myChats: CoListSchema<CoMapSchema<{}>>;
}>
MyAppRoot
,
profile: CoProfileSchema<{
    name: z.z.ZodString;
    avatar: z.ZodOptional<WithHelpers<CoMapSchema<{
        originalSize: z.z.ZodTuple<[z.z.ZodNumber, z.z.ZodNumber], null>;
        placeholderDataURL: z.ZodOptional<z.z.ZodString>;
    }, z.z.core.$catchall<...>>, {
        ...;
    }>>;
}>
profile
:
const MyAppProfile: CoProfileSchema<{
    name: z.z.ZodString;
    avatar: z.ZodOptional<WithHelpers<CoMapSchema<{
        originalSize: z.z.ZodTuple<[z.z.ZodNumber, z.z.ZodNumber], null>;
        placeholderDataURL: z.ZodOptional<z.z.ZodString>;
    }, z.z.core.$catchall<...>>, {
        ...;
    }>>;
}>
MyAppProfile
,
});

Resolving CoValues starting at profile or root

To use per-user data in your app, you typically use useAccount somewhere in a high-level component, pass it your custom Account schema and specify which references to resolve using a resolve query (see Subscribing & deep loading).

import { 
function useAccount<A extends AccountClass<Account> | AnyAccountSchema>(AccountSchema?: A): {
    me: Loaded<A, true>;
    logOut: () => void;
} (+1 overload)
useAccount
} from "jazz-react";
function function DashboardPageComponent(): React.JSX.ElementDashboardPageComponent() { const {
const me: ({
    profile: {
        name: string;
        inbox: string | undefined;
        inboxInvite: string | undefined;
    } & CoMap & Profile;
    root: {
        myChats: ({} & CoMap)[] & CoList<({} & CoMap) | null>;
    } & {
        ...;
    } & CoMap;
} & {
    ...;
} & Account) | null | undefined
me
} =
useAccount<AccountSchema<{
    root: CoMapSchema<{
        myChats: CoListSchema<CoMapSchema<{}>>;
    }>;
    profile: CoProfileSchema<{
        name: z.z.core.$ZodString<string>;
        inbox: z.z.core.$ZodOptional<z.z.core.$ZodString>;
        inboxInvite: z.z.core.$ZodOptional<z.z.core.$ZodString>;
    }>;
}>, {
    ...;
}>(AccountSchema: AccountSchema<...>, options?: {
    ...;
} | undefined): {
    ...;
} (+1 overload)
useAccount
(
const MyAppAccount: AccountSchema<{
    root: CoMapSchema<{
        myChats: CoListSchema<CoMapSchema<{}>>;
    }>;
    profile: CoProfileSchema<{
        name: z.z.core.$ZodString<string>;
        inbox: z.z.core.$ZodOptional<z.z.core.$ZodString>;
        inboxInvite: z.z.core.$ZodOptional<z.z.core.$ZodString>;
    }>;
}>
MyAppAccount
, {
resolve?: RefsToResolve<{
    ...;
} & Account, 10, []> | undefined
resolve
: {
profile?: RefsToResolve<{
    name: string;
    inbox: string | undefined;
    inboxInvite: string | undefined;
} & CoMap & Profile, 10, [0]> | undefined
profile
: true,
root?: RefsToResolve<{
    myChats: CoList<({} & CoMap) | null> | null;
} & CoMap, 10, [...]> | undefined
root
: {
myChats?: RefsToResolve<CoList<({} & CoMap) | null>, 10, [0, 0]> | undefinedmyChats: { $each: RefsToResolve<{} & CoMap, 10, [0, 0, 0]>$each: true }, } }}); return ( <JSX.IntrinsicElements.div: React.DetailedHTMLProps<React.HTMLAttributes<HTMLDivElement>, HTMLDivElement>div> <JSX.IntrinsicElements.h1: React.DetailedHTMLProps<React.HTMLAttributes<HTMLHeadingElement>, HTMLHeadingElement>h1>Dashboard</JSX.IntrinsicElements.h1: React.DetailedHTMLProps<React.HTMLAttributes<HTMLHeadingElement>, HTMLHeadingElement>h1> {
const me: ({
    profile: {
        name: string;
        inbox: string | undefined;
        inboxInvite: string | undefined;
    } & CoMap & Profile;
    root: {
        myChats: ({} & CoMap)[] & CoList<({} & CoMap) | null>;
    } & {
        ...;
    } & CoMap;
} & {
    ...;
} & Account) | null | undefined
me
? (
<JSX.IntrinsicElements.div: React.DetailedHTMLProps<React.HTMLAttributes<HTMLDivElement>, HTMLDivElement>div> <JSX.IntrinsicElements.p: React.DetailedHTMLProps<React.HTMLAttributes<HTMLParagraphElement>, HTMLParagraphElement>p>Logged in as {
const me: {
    profile: {
        name: string;
        inbox: string | undefined;
        inboxInvite: string | undefined;
    } & CoMap & Profile;
    root: {
        myChats: ({} & CoMap)[] & CoList<({} & CoMap) | null>;
    } & {
        ...;
    } & CoMap;
} & {
    ...;
} & Account
me
.
Account.profile: {
    name: string;
    inbox: string | undefined;
    inboxInvite: string | undefined;
} & CoMap & Profile
profile
.Profile.name: stringname}</JSX.IntrinsicElements.p: React.DetailedHTMLProps<React.HTMLAttributes<HTMLParagraphElement>, HTMLParagraphElement>p>
<JSX.IntrinsicElements.h2: React.DetailedHTMLProps<React.HTMLAttributes<HTMLHeadingElement>, HTMLHeadingElement>h2>My chats</JSX.IntrinsicElements.h2: React.DetailedHTMLProps<React.HTMLAttributes<HTMLHeadingElement>, HTMLHeadingElement>h2> {
const me: {
    profile: {
        name: string;
        inbox: string | undefined;
        inboxInvite: string | undefined;
    } & CoMap & Profile;
    root: {
        myChats: ({} & CoMap)[] & CoList<({} & CoMap) | null>;
    } & {
        ...;
    } & CoMap;
} & {
    ...;
} & Account
me
.
Account.root: {
    myChats: ({} & CoMap)[] & CoList<({} & CoMap) | null>;
} & {
    myChats: CoList<({} & CoMap) | null> | null;
} & CoMap
root
.myChats: ({} & CoMap)[] & CoList<({} & CoMap) | null>myChats.Array<T>.map<React.JSX.Element>(callbackfn: (value: {} & CoMap, index: number, array: ({} & CoMap)[]) => React.JSX.Element, thisArg?: any): React.JSX.Element[] (+1 overload)
Calls a defined callback function on each element of an array, and returns an array that contains the results.
@paramcallbackfn A function that accepts up to three arguments. The map method calls the callbackfn function one time for each element in the array.@paramthisArg An object to which the this keyword can refer in the callbackfn function. If thisArg is omitted, undefined is used as the this value.
map
((chat: {} & CoMapchat) => (
<class ChatPreviewChatPreview React.Attributes.key?: React.Key | null | undefinedkey={chat: {} & CoMapchat.CoMap.id: string
The ID of this `CoMap`
@categoryContent
id
} chat: {} & CoMapchat={chat: {} & CoMapchat} />
))} </JSX.IntrinsicElements.div: React.DetailedHTMLProps<React.HTMLAttributes<HTMLDivElement>, HTMLDivElement>div> ) : ( "Loading..." )} </JSX.IntrinsicElements.div: React.DetailedHTMLProps<React.HTMLAttributes<HTMLDivElement>, HTMLDivElement>div> ); }

Populating and evolving root and profile schemas with migrations

As you develop your app, you'll likely want to

  • initialise data in a user's root and profile
  • add more data to your root and profile schemas

You can achieve both by overriding the migrate() method on your Account schema class.

When migrations run

Migrations are run after account creation and every time a user logs in. Jazz waits for the migration to finish before passing the account to your app's context.

Initialising user data after account creation

export const 
const MyAppAccount: AccountSchema<{
    root: CoMapSchema<{
        myChats: CoListSchema<CoMapSchema<{}>>;
    }>;
    profile: CoProfileSchema<{
        name: z.z.ZodString;
        bookmarks: CoListSchema<CoMapSchema<{}>>;
    }>;
}>
MyAppAccount
= import coco.
account<{
    root: CoMapSchema<{
        myChats: CoListSchema<CoMapSchema<{}>>;
    }>;
    profile: CoProfileSchema<{
        name: z.z.ZodString;
        bookmarks: CoListSchema<CoMapSchema<{}>>;
    }>;
}>(shape?: {
    root: CoMapSchema<{
        myChats: CoListSchema<CoMapSchema<{}>>;
    }>;
    profile: CoProfileSchema<{
        name: z.z.ZodString;
        bookmarks: CoListSchema<CoMapSchema<{}>>;
    }>;
} | undefined): AccountSchema<...>
export account
account
({
root: CoMapSchema<{
    myChats: CoListSchema<CoMapSchema<{}>>;
}>
root
:
const MyAppRoot: CoMapSchema<{
    myChats: CoListSchema<CoMapSchema<{}>>;
}>
MyAppRoot
,
profile: CoProfileSchema<{
    name: z.z.ZodString;
    bookmarks: CoListSchema<CoMapSchema<{}>>;
}>
profile
:
const MyAppProfile: CoProfileSchema<{
    name: z.z.ZodString;
    bookmarks: CoListSchema<CoMapSchema<{}>>;
}>
MyAppProfile
,
}).
function withMigration(migration: (account: {
    root: {
        myChats: CoList<{} & CoMap>;
    } & CoMap;
    profile: {
        name: string;
        bookmarks: CoList<{} & CoMap>;
        inbox: string | undefined;
        inboxInvite: string | undefined;
    } & CoMap;
} & {
    ...;
} & Account, creationProps?: {
    name: string;
}) => void): AccountSchema<...>
withMigration
((
account: {
    root: {
        myChats: CoList<{} & CoMap>;
    } & CoMap;
    profile: {
        name: string;
        bookmarks: CoList<{} & CoMap>;
        inbox: string | undefined;
        inboxInvite: string | undefined;
    } & CoMap;
} & {
    ...;
} & Account
account
,
creationProps: {
    name: string;
} | undefined
creationProps
?: { name: stringname: string }) => {
// we specifically need to check for undefined, // because the root might simply be not loaded (`null`) yet if (
account: {
    root: {
        myChats: CoList<{} & CoMap>;
    } & CoMap;
    profile: {
        name: string;
        bookmarks: CoList<{} & CoMap>;
        inbox: string | undefined;
        inboxInvite: string | undefined;
    } & CoMap;
} & {
    ...;
} & Account
account
.
Account.root: {
    myChats: CoList<{} & CoMap>;
} & CoMap
root
=== var undefinedundefined) {
account: {
    root: {
        myChats: CoList<{} & CoMap>;
    } & CoMap;
    profile: {
        name: string;
        bookmarks: CoList<{} & CoMap>;
        inbox: string | undefined;
        inboxInvite: string | undefined;
    } & CoMap;
} & {
    ...;
} & Account
account
.
Account.root: {
    myChats: CoList<{} & CoMap>;
} & CoMap
root
=
const MyAppRoot: CoMapSchema<{
    myChats: CoListSchema<CoMapSchema<{}>>;
}>
MyAppRoot
.
create: (init: {
    myChats: CoList<({} & CoMap) | null>;
}, options?: {
    owner: Account | Group;
    unique?: CoValueUniqueness["uniqueness"];
} | Account | Group) => {
    ...;
} & CoMap
create
({
// Using a group to set the owner is always a good idea. // This way if in the future we want to share // this coValue we can do so easily. myChats: CoList<({} & CoMap) | null>myChats: import coco.
list<CoMapSchema<{}>>(element: CoMapSchema<{}>): CoListSchema<CoMapSchema<{}>>
export list
list
(const Chat: CoMapSchema<{}>Chat).
create: (items: CoListInit<CoMapSchema<{}>>, options?: {
    owner: Account | Group;
} | Account | Group) => CoList<...>
create
([], class Group
@categoryIdentity & Permissions
Group
.
Group.create<Group>(this: CoValueClass<Group>, options?: {
    owner: Account;
} | Account): Group
create
()),
}); } if (
account: {
    root: {
        myChats: CoList<{} & CoMap>;
    } & CoMap;
    profile: {
        name: string;
        bookmarks: CoList<{} & CoMap>;
        inbox: string | undefined;
        inboxInvite: string | undefined;
    } & CoMap;
} & {
    ...;
} & Account
account
.
profile: {
    name: string;
    bookmarks: CoList<{} & CoMap>;
    inbox: string | undefined;
    inboxInvite: string | undefined;
} & CoMap & Profile
profile
=== var undefinedundefined) {
const const profileGroup: GroupprofileGroup = class Group
@categoryIdentity & Permissions
Group
.
Group.create<Group>(this: CoValueClass<Group>, options?: {
    owner: Account;
} | Account): Group
create
();
// Unlike the root, we want the profile to be publicly readable. const profileGroup: GroupprofileGroup.Group.addMember(member: Everyone, role: "writer" | "reader" | "writeOnly"): void (+1 overload)addMember("everyone", "reader");
account: {
    root: {
        myChats: CoList<{} & CoMap>;
    } & CoMap;
    profile: {
        name: string;
        bookmarks: CoList<{} & CoMap>;
        inbox: string | undefined;
        inboxInvite: string | undefined;
    } & CoMap;
} & {
    ...;
} & Account
account
.
profile: {
    name: string;
    bookmarks: CoList<{} & CoMap>;
    inbox: string | undefined;
    inboxInvite: string | undefined;
} & CoMap & Profile
profile
=
const MyAppProfile: CoProfileSchema<{
    name: z.z.ZodString;
    bookmarks: CoListSchema<CoMapSchema<{}>>;
}>
MyAppProfile
.
create: (init: {
    inbox?: string | undefined;
    inboxInvite?: string | undefined;
    bookmarks: CoList<({} & CoMap) | null>;
    name: string;
}, options: {
    owner: Exclude<Group, Account>;
} | Exclude<Group, Account>) => CoMapInstance<...>
create
({
name: stringname:
creationProps: {
    name: string;
} | undefined
creationProps
?.name: string | undefinedname ?? "New user",
bookmarks: CoList<({} & CoMap) | null>bookmarks: import coco.
list<CoMapSchema<{}>>(element: CoMapSchema<{}>): CoListSchema<CoMapSchema<{}>>
export list
list
(const Bookmark: CoMapSchema<{}>Bookmark).
create: (items: CoListInit<CoMapSchema<{}>>, options?: {
    owner: Account | Group;
} | Account | Group) => CoList<...>
create
([], const profileGroup: GroupprofileGroup),
}, const profileGroup: GroupprofileGroup); } });

Adding/changing fields to root and profile

To add new fields to your root or profile schemas, amend their corresponding schema classes with new fields, and then implement a migration that will populate the new fields for existing users (by using initial data, or by using existing data from old fields).

To do deeply nested migrations, you might need to use the asynchronous ensureLoaded() method before determining whether the field already exists, or is simply not loaded yet.

Now let's say we want to add a myBookmarks field to the root schema:

const 
const MyAppRoot: CoMapSchema<{
    myChats: CoListSchema<CoMapSchema<{}>>;
    myBookmarks: z.ZodOptional<CoListSchema<CoMapSchema<{}>>>;
}>
MyAppRoot
= import coco.
map<{
    myChats: CoListSchema<CoMapSchema<{}>>;
    myBookmarks: z.ZodOptional<CoListSchema<CoMapSchema<{}>>>;
}>(shape: {
    myChats: CoListSchema<CoMapSchema<{}>>;
    myBookmarks: z.ZodOptional<CoListSchema<CoMapSchema<{}>>>;
}): CoMapSchema<...>
export map
map
({
myChats: CoListSchema<CoMapSchema<{}>>myChats: import coco.
list<CoMapSchema<{}>>(element: CoMapSchema<{}>): CoListSchema<CoMapSchema<{}>>
export list
list
(const Chat: CoMapSchema<{}>Chat),
myBookmarks: z.ZodOptional<CoListSchema<CoMapSchema<{}>>>myBookmarks: import zz.
optional<CoListSchema<CoMapSchema<{}>>>(innerType: CoListSchema<CoMapSchema<{}>>): z.ZodOptional<CoListSchema<CoMapSchema<{}>>>
export optional
optional
(import coco.
list<CoMapSchema<{}>>(element: CoMapSchema<{}>): CoListSchema<CoMapSchema<{}>>
export list
list
(const Bookmark: CoMapSchema<{}>Bookmark)),
}); export const
const MyAppAccount: AccountSchema<{
    root: CoMapSchema<{
        myChats: CoListSchema<CoMapSchema<{}>>;
        myBookmarks: z.ZodOptional<CoListSchema<CoMapSchema<{}>>>;
    }>;
    profile: CoProfileSchema<...>;
}>
MyAppAccount
= import coco.
account<{
    root: CoMapSchema<{
        myChats: CoListSchema<CoMapSchema<{}>>;
        myBookmarks: z.ZodOptional<CoListSchema<CoMapSchema<{}>>>;
    }>;
    profile: CoProfileSchema<...>;
}>(shape?: {
    root: CoMapSchema<{
        myChats: CoListSchema<CoMapSchema<{}>>;
        myBookmarks: z.ZodOptional<CoListSchema<CoMapSchema<{}>>>;
    }>;
    profile: CoProfileSchema<...>;
} | undefined): AccountSchema<...>
export account
account
({
root: CoMapSchema<{
    myChats: CoListSchema<CoMapSchema<{}>>;
    myBookmarks: z.ZodOptional<CoListSchema<CoMapSchema<{}>>>;
}>
root
:
const MyAppRoot: CoMapSchema<{
    myChats: CoListSchema<CoMapSchema<{}>>;
    myBookmarks: z.ZodOptional<CoListSchema<CoMapSchema<{}>>>;
}>
MyAppRoot
,
profile: CoProfileSchema<{
    name: z.z.ZodString;
    bookmarks: CoListSchema<CoMapSchema<{}>>;
}>
profile
:
const MyAppProfile: CoProfileSchema<{
    name: z.z.ZodString;
    bookmarks: CoListSchema<CoMapSchema<{}>>;
}>
MyAppProfile
,
}).
function withMigration(migration: (account: {
    root: {
        myChats: CoList<{} & CoMap>;
        myBookmarks: CoList<{} & CoMap> | undefined;
    } & CoMap;
    profile: {
        name: string;
        bookmarks: CoList<{} & CoMap>;
        inbox: string | undefined;
        inboxInvite: string | undefined;
    } & CoMap;
} & {
    ...;
} & Account, creationProps?: {
    name: string;
}) => void): AccountSchema<...>
withMigration
(async (
account: {
    root: {
        myChats: CoList<{} & CoMap>;
        myBookmarks: CoList<{} & CoMap> | undefined;
    } & CoMap;
    profile: {
        name: string;
        bookmarks: CoList<{} & CoMap>;
        inbox: string | undefined;
        inboxInvite: string | undefined;
    } & CoMap;
} & {
    ...;
} & Account
account
) => {
if (
account: {
    root: {
        myChats: CoList<{} & CoMap>;
        myBookmarks: CoList<{} & CoMap> | undefined;
    } & CoMap;
    profile: {
        name: string;
        bookmarks: CoList<{} & CoMap>;
        inbox: string | undefined;
        inboxInvite: string | undefined;
    } & CoMap;
} & {
    ...;
} & Account
account
.
Account.root: {
    myChats: CoList<{} & CoMap>;
    myBookmarks: CoList<{} & CoMap> | undefined;
} & CoMap
root
=== var undefinedundefined) {
account: {
    root: {
        myChats: CoList<{} & CoMap>;
        myBookmarks: CoList<{} & CoMap> | undefined;
    } & CoMap;
    profile: {
        name: string;
        bookmarks: CoList<{} & CoMap>;
        inbox: string | undefined;
        inboxInvite: string | undefined;
    } & CoMap;
} & {
    ...;
} & Account
account
.
Account.root: {
    myChats: CoList<{} & CoMap>;
    myBookmarks: CoList<{} & CoMap> | undefined;
} & CoMap
root
=
const MyAppRoot: CoMapSchema<{
    myChats: CoListSchema<CoMapSchema<{}>>;
    myBookmarks: z.ZodOptional<CoListSchema<CoMapSchema<{}>>>;
}>
MyAppRoot
.
create: (init: {
    myBookmarks?: CoList<({} & CoMap) | null> | undefined;
    myChats: CoList<({} & CoMap) | null>;
}, options?: {
    owner: Account | Group;
    unique?: CoValueUniqueness["uniqueness"];
} | Account | Group) => {
    ...;
} & CoMap
create
({
myChats: CoList<({} & CoMap) | null>myChats: import coco.
list<CoMapSchema<{}>>(element: CoMapSchema<{}>): CoListSchema<CoMapSchema<{}>>
export list
list
(const Chat: CoMapSchema<{}>Chat).
create: (items: CoListInit<CoMapSchema<{}>>, options?: {
    owner: Account | Group;
} | Account | Group) => CoList<...>
create
([], class Group
@categoryIdentity & Permissions
Group
.
Group.create<Group>(this: CoValueClass<Group>, options?: {
    owner: Account;
} | Account): Group
create
()),
}); } // We need to load the root field to check for the myContacts field const {
const root: {
    myChats: CoList<{} & CoMap>;
    myBookmarks: CoList<{} & CoMap> | undefined;
} & CoMap
root
} = await
account: {
    root: {
        myChats: CoList<{} & CoMap>;
        myBookmarks: CoList<{} & CoMap> | undefined;
    } & CoMap;
    profile: {
        name: string;
        bookmarks: CoList<{} & CoMap>;
        inbox: string | undefined;
        inboxInvite: string | undefined;
    } & CoMap;
} & {
    ...;
} & Account
account
.
Account.ensureLoaded<{
    root: {
        myChats: CoList<{} & CoMap>;
        myBookmarks: CoList<{} & CoMap> | undefined;
    } & CoMap;
    profile: {
        name: string;
        bookmarks: CoList<{} & CoMap>;
        inbox: string | undefined;
        inboxInvite: string | undefined;
    } & CoMap;
} & {
    ...;
} & Account, {
    ...;
}>(this: {
    root: {
        myChats: CoList<{} & CoMap>;
        myBookmarks: CoList<{} & CoMap> | undefined;
    } & CoMap;
    profile: {
        name: string;
        bookmarks: CoList<{} & CoMap>;
        inbox: string | undefined;
        inboxInvite: string | undefined;
    } & CoMap;
} & ... 1 more ... & Account, options: {
    ...;
}): Promise<...>
@categorySubscription & Loading
ensureLoaded
({
resolve: RefsToResolve<{
    root: {
        myChats: CoList<{} & CoMap>;
        myBookmarks: CoList<{} & CoMap> | undefined;
    } & CoMap;
    profile: {
        name: string;
        bookmarks: CoList<{} & CoMap>;
        inbox: string | undefined;
        inboxInvite: string | undefined;
    } & CoMap;
} & {
    ...;
} & Account, 10, []>
resolve
: {
root?: RefsToResolve<NonNullable<{
    myChats: CoList<{} & CoMap>;
    myBookmarks: CoList<{} & CoMap> | undefined;
} & CoMap>, 10, [0]> | undefined
root
: true }
}); // we specifically need to check for undefined, // because myBookmarks might simply be not loaded (`null`) yet if (
const root: {
    myChats: CoList<{} & CoMap>;
    myBookmarks: CoList<{} & CoMap> | undefined;
} & CoMap
root
.myBookmarks: CoList<{} & CoMap> | undefinedmyBookmarks === var undefinedundefined) {
const root: {
    myChats: CoList<{} & CoMap>;
    myBookmarks: CoList<{} & CoMap> | undefined;
} & CoMap
root
.myBookmarks: CoList<{} & CoMap> | undefinedmyBookmarks = import coco.
list<CoMapSchema<{}>>(element: CoMapSchema<{}>): CoListSchema<CoMapSchema<{}>>
export list
list
(const Bookmark: CoMapSchema<{}>Bookmark).
create: (items: CoListInit<CoMapSchema<{}>>, options?: {
    owner: Account | Group;
} | Account | Group) => CoList<...>
create
([], class Group
@categoryIdentity & Permissions
Group
.
Group.create<Group>(this: CoValueClass<Group>, options?: {
    owner: Account;
} | Account): Group
create
());
} });