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 co
co,import z
z } from "jazz-tools"; constMyAppRoot =
const MyAppRoot: CoMapSchema<{ myChats: CoListSchema<CoMapSchema<{}>>; }>
import co
co.map({
map<{ myChats: CoListSchema<CoMapSchema<{}>>; }>(shape: { myChats: CoListSchema<CoMapSchema<{}>>; }): CoMapSchema<{ myChats: CoListSchema<CoMapSchema<{}>>; }> export map
myChats: CoListSchema<CoMapSchema<{}>>
myChats:import co
co.list(
list<CoMapSchema<{}>>(element: CoMapSchema<{}>): CoListSchema<CoMapSchema<{}>> export list
const Chat: CoMapSchema<{}>
Chat), }); export constMyAppAccount =
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>; }>; }>
import co
co.account({
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
root:
root: CoMapSchema<{ myChats: CoListSchema<CoMapSchema<{}>>; }>
MyAppRoot,
const MyAppRoot: CoMapSchema<{ myChats: CoListSchema<CoMapSchema<{}>>; }>
profile:
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>; }>
import co
co.profile(), });
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
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
Account =
const Account: AccountSchema<{ root: CoMapSchema<{}>; profile: CoProfileSchema<{ name: z.z.ZodString; }>; }>
import co
co.account({
account<{ root: CoMapSchema<{}>; profile: CoProfileSchema<{ name: z.z.ZodString; }>; }>(shape?: { root: CoMapSchema<{}>; profile: CoProfileSchema<{ name: z.z.ZodString; }>; } | undefined): AccountSchema<...> export account
root: CoMapSchema<{}>
root:import co
co.map({}),
map<{}>(shape: {}): CoMapSchema<{}> export map
profile:
profile: CoProfileSchema<{ name: z.z.ZodString; }>
import co
co.profile({
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
name: z.z.ZodString & z.z.core.$ZodString<string>
name:import z
z.string(), }), });
function string(params?: string | z.z.core.$ZodStringParams): z.z.ZodString export 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
MyAppRoot =
const MyAppRoot: CoMapSchema<{ myChats: CoListSchema<CoMapSchema<{}>>; }>
import co
co.map({
map<{ myChats: CoListSchema<CoMapSchema<{}>>; }>(shape: { myChats: CoListSchema<CoMapSchema<{}>>; }): CoMapSchema<{ myChats: CoListSchema<CoMapSchema<{}>>; }> export map
myChats: CoListSchema<CoMapSchema<{}>>
myChats:import co
co.list(
list<CoMapSchema<{}>>(element: CoMapSchema<{}>): CoListSchema<CoMapSchema<{}>> export list
const Chat: CoMapSchema<{}>
Chat), }); export constMyAppAccount =
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>; }>; }>
import co
co.account({
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
root:
root: CoMapSchema<{ myChats: CoListSchema<CoMapSchema<{}>>; }>
MyAppRoot,
const MyAppRoot: CoMapSchema<{ myChats: CoListSchema<CoMapSchema<{}>>; }>
profile:
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>; }>
import co
co.profile(), });
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
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
MyAppRoot =
const MyAppRoot: CoMapSchema<{ myChats: CoListSchema<CoMapSchema<{}>>; }>
import co
co.map({
map<{ myChats: CoListSchema<CoMapSchema<{}>>; }>(shape: { myChats: CoListSchema<CoMapSchema<{}>>; }): CoMapSchema<{ myChats: CoListSchema<CoMapSchema<{}>>; }> export map
myChats: CoListSchema<CoMapSchema<{}>>
myChats:import co
co.list(
list<CoMapSchema<{}>>(element: CoMapSchema<{}>): CoListSchema<CoMapSchema<{}>> export list
const Chat: CoMapSchema<{}>
Chat), }); export constMyAppProfile =
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<...>>, { ...; }>>; }>
import co
co.profile({
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
name: z.z.ZodString & z.z.core.$ZodString<string>
name:import z
z.string(), // compatible with default Profile schema
function string(params?: string | z.z.core.$ZodStringParams): z.z.ZodString export string
avatar:
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<...>>, { ...; }>>
import z
z.optional(
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
import co
co.image()), }); export const
function image(): typeof ImageDefinition export image
MyAppAccount =
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<...>>, { ...; }>>; }>; }>
import co
co.account({
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
root:
root: CoMapSchema<{ myChats: CoListSchema<CoMapSchema<{}>>; }>
MyAppRoot,
const MyAppRoot: CoMapSchema<{ myChats: CoListSchema<CoMapSchema<{}>>; }>
profile:
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<...>>, { ...; }>>; }>
MyAppProfile, });
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<...>>, { ...; }>>; }>
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 {
useAccount } from "jazz-react"; function
function useAccount<A extends AccountClass<Account> | AnyAccountSchema>(AccountSchema?: A): { me: Loaded<A, true>; logOut: () => void; } (+1 overload)
function DashboardPageComponent(): React.JSX.Element
DashboardPageComponent() { const {me } =
const me: ({ profile: { name: string; inbox: string | undefined; inboxInvite: string | undefined; } & CoMap & Profile; root: { myChats: ({} & CoMap)[] & CoList<({} & CoMap) | null>; } & { ...; } & CoMap; } & { ...; } & Account) | null | undefined
useAccount(
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)
MyAppAccount, {
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>; }>; }>
resolve: {
resolve?: RefsToResolve<{ ...; } & Account, 10, []> | undefined
profile: true,
profile?: RefsToResolve<{ name: string; inbox: string | undefined; inboxInvite: string | undefined; } & CoMap & Profile, 10, [0]> | undefined
root: {
root?: RefsToResolve<{ myChats: CoList<({} & CoMap) | null> | null; } & CoMap, 10, [...]> | undefined
myChats?: RefsToResolve<CoList<({} & CoMap) | null>, 10, [0, 0]> | undefined
myChats: {$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> {me ? ( <
const me: ({ profile: { name: string; inbox: string | undefined; inboxInvite: string | undefined; } & CoMap & Profile; root: { myChats: ({} & CoMap)[] & CoList<({} & CoMap) | null>; } & { ...; } & CoMap; } & { ...; } & Account) | null | undefined
JSX.IntrinsicElements.div: React.DetailedHTMLProps<React.HTMLAttributes<HTMLDivElement>, HTMLDivElement>
div> <JSX.IntrinsicElements.p: React.DetailedHTMLProps<React.HTMLAttributes<HTMLParagraphElement>, HTMLParagraphElement>
p>Logged in as {me.
const me: { profile: { name: string; inbox: string | undefined; inboxInvite: string | undefined; } & CoMap & Profile; root: { myChats: ({} & CoMap)[] & CoList<({} & CoMap) | null>; } & { ...; } & CoMap; } & { ...; } & Account
profile.
Account.profile: { name: string; inbox: string | undefined; inboxInvite: string | undefined; } & CoMap & Profile
Profile.name: string
name}</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> {me.
const me: { profile: { name: string; inbox: string | undefined; inboxInvite: string | undefined; } & CoMap & Profile; root: { myChats: ({} & CoMap)[] & CoList<({} & CoMap) | null>; } & { ...; } & CoMap; } & { ...; } & Account
root.
Account.root: { myChats: ({} & CoMap)[] & CoList<({} & CoMap) | null>; } & { myChats: CoList<({} & CoMap) | null> | null; } & CoMap
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.map((chat: {} & CoMap
chat) => ( <class ChatPreview
ChatPreviewReact.Attributes.key?: React.Key | null | undefined
key={chat: {} & CoMap
chat.CoMap.id: string
The ID of this `CoMap`id}chat: {} & CoMap
chat={chat: {} & CoMap
chat} /> ))} </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
andprofile
- add more data to your
root
andprofile
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
MyAppAccount =
const MyAppAccount: AccountSchema<{ root: CoMapSchema<{ myChats: CoListSchema<CoMapSchema<{}>>; }>; profile: CoProfileSchema<{ name: z.z.ZodString; bookmarks: CoListSchema<CoMapSchema<{}>>; }>; }>
import co
co.account({
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
root:
root: CoMapSchema<{ myChats: CoListSchema<CoMapSchema<{}>>; }>
MyAppRoot,
const MyAppRoot: CoMapSchema<{ myChats: CoListSchema<CoMapSchema<{}>>; }>
profile:
profile: CoProfileSchema<{ name: z.z.ZodString; bookmarks: CoListSchema<CoMapSchema<{}>>; }>
MyAppProfile, }).
const MyAppProfile: CoProfileSchema<{ name: z.z.ZodString; bookmarks: CoListSchema<CoMapSchema<{}>>; }>
withMigration((
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<...>
account,
account: { root: { myChats: CoList<{} & CoMap>; } & CoMap; profile: { name: string; bookmarks: CoList<{} & CoMap>; inbox: string | undefined; inboxInvite: string | undefined; } & CoMap; } & { ...; } & Account
creationProps?: {
creationProps: { name: string; } | undefined
name: string
name: string }) => { // we specifically need to check for undefined, // because the root might simply be not loaded (`null`) yet if (account.
account: { root: { myChats: CoList<{} & CoMap>; } & CoMap; profile: { name: string; bookmarks: CoList<{} & CoMap>; inbox: string | undefined; inboxInvite: string | undefined; } & CoMap; } & { ...; } & Account
root ===
Account.root: { myChats: CoList<{} & CoMap>; } & CoMap
var undefined
undefined) {account.
account: { root: { myChats: CoList<{} & CoMap>; } & CoMap; profile: { name: string; bookmarks: CoList<{} & CoMap>; inbox: string | undefined; inboxInvite: string | undefined; } & CoMap; } & { ...; } & Account
root =
Account.root: { myChats: CoList<{} & CoMap>; } & CoMap
MyAppRoot.
const MyAppRoot: CoMapSchema<{ myChats: CoListSchema<CoMapSchema<{}>>; }>
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.
create: (init: { myChats: CoList<({} & CoMap) | null>; }, options?: { owner: Account | Group; unique?: CoValueUniqueness["uniqueness"]; } | Account | Group) => { ...; } & CoMap
myChats: CoList<({} & CoMap) | null>
myChats:import co
co.list(
list<CoMapSchema<{}>>(element: CoMapSchema<{}>): CoListSchema<CoMapSchema<{}>> export list
const Chat: CoMapSchema<{}>
Chat).create([],
create: (items: CoListInit<CoMapSchema<{}>>, options?: { owner: Account | Group; } | Account | Group) => CoList<...>
class Group
Group.create()), }); } if (
Group.create<Group>(this: CoValueClass<Group>, options?: { owner: Account; } | Account): Group
account.
account: { root: { myChats: CoList<{} & CoMap>; } & CoMap; profile: { name: string; bookmarks: CoList<{} & CoMap>; inbox: string | undefined; inboxInvite: string | undefined; } & CoMap; } & { ...; } & Account
profile ===
profile: { name: string; bookmarks: CoList<{} & CoMap>; inbox: string | undefined; inboxInvite: string | undefined; } & CoMap & Profile
var undefined
undefined) { constconst profileGroup: Group
profileGroup =class Group
Group.create(); // Unlike the root, we want the profile to be publicly readable.
Group.create<Group>(this: CoValueClass<Group>, options?: { owner: Account; } | Account): Group
const profileGroup: Group
profileGroup.Group.addMember(member: Everyone, role: "writer" | "reader" | "writeOnly"): void (+1 overload)
addMember("everyone", "reader");account.
account: { root: { myChats: CoList<{} & CoMap>; } & CoMap; profile: { name: string; bookmarks: CoList<{} & CoMap>; inbox: string | undefined; inboxInvite: string | undefined; } & CoMap; } & { ...; } & Account
profile =
profile: { name: string; bookmarks: CoList<{} & CoMap>; inbox: string | undefined; inboxInvite: string | undefined; } & CoMap & Profile
MyAppProfile.
const MyAppProfile: CoProfileSchema<{ name: z.z.ZodString; bookmarks: CoListSchema<CoMapSchema<{}>>; }>
create({
create: (init: { inbox?: string | undefined; inboxInvite?: string | undefined; bookmarks: CoList<({} & CoMap) | null>; name: string; }, options: { owner: Exclude<Group, Account>; } | Exclude<Group, Account>) => CoMapInstance<...>
name: string
name:creationProps?.
creationProps: { name: string; } | undefined
name: string | undefined
name ?? "New user",bookmarks: CoList<({} & CoMap) | null>
bookmarks:import co
co.list(
list<CoMapSchema<{}>>(element: CoMapSchema<{}>): CoListSchema<CoMapSchema<{}>> export list
const Bookmark: CoMapSchema<{}>
Bookmark).create([],
create: (items: CoListInit<CoMapSchema<{}>>, options?: { owner: Account | Group; } | Account | Group) => CoList<...>
const profileGroup: Group
profileGroup), },const profileGroup: Group
profileGroup); } });
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
MyAppRoot =
const MyAppRoot: CoMapSchema<{ myChats: CoListSchema<CoMapSchema<{}>>; myBookmarks: z.ZodOptional<CoListSchema<CoMapSchema<{}>>>; }>
import co
co.map({
map<{ myChats: CoListSchema<CoMapSchema<{}>>; myBookmarks: z.ZodOptional<CoListSchema<CoMapSchema<{}>>>; }>(shape: { myChats: CoListSchema<CoMapSchema<{}>>; myBookmarks: z.ZodOptional<CoListSchema<CoMapSchema<{}>>>; }): CoMapSchema<...> export map
myChats: CoListSchema<CoMapSchema<{}>>
myChats:import co
co.list(
list<CoMapSchema<{}>>(element: CoMapSchema<{}>): CoListSchema<CoMapSchema<{}>> export list
const Chat: CoMapSchema<{}>
Chat),myBookmarks: z.ZodOptional<CoListSchema<CoMapSchema<{}>>>
myBookmarks:import z
z.optional(
optional<CoListSchema<CoMapSchema<{}>>>(innerType: CoListSchema<CoMapSchema<{}>>): z.ZodOptional<CoListSchema<CoMapSchema<{}>>> export optional
import co
co.list(
list<CoMapSchema<{}>>(element: CoMapSchema<{}>): CoListSchema<CoMapSchema<{}>> export list
const Bookmark: CoMapSchema<{}>
Bookmark)), }); export constMyAppAccount =
const MyAppAccount: AccountSchema<{ root: CoMapSchema<{ myChats: CoListSchema<CoMapSchema<{}>>; myBookmarks: z.ZodOptional<CoListSchema<CoMapSchema<{}>>>; }>; profile: CoProfileSchema<...>; }>
import co
co.account({
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
root:
root: CoMapSchema<{ myChats: CoListSchema<CoMapSchema<{}>>; myBookmarks: z.ZodOptional<CoListSchema<CoMapSchema<{}>>>; }>
MyAppRoot,
const MyAppRoot: CoMapSchema<{ myChats: CoListSchema<CoMapSchema<{}>>; myBookmarks: z.ZodOptional<CoListSchema<CoMapSchema<{}>>>; }>
profile:
profile: CoProfileSchema<{ name: z.z.ZodString; bookmarks: CoListSchema<CoMapSchema<{}>>; }>
MyAppProfile, }).
const MyAppProfile: CoProfileSchema<{ name: z.z.ZodString; bookmarks: CoListSchema<CoMapSchema<{}>>; }>
withMigration(async (
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<...>
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; profile: { name: string; bookmarks: CoList<{} & CoMap>; inbox: string | undefined; inboxInvite: string | undefined; } & CoMap; } & { ...; } & Account
root ===
Account.root: { myChats: CoList<{} & CoMap>; myBookmarks: CoList<{} & CoMap> | undefined; } & CoMap
var undefined
undefined) {account.
account: { root: { myChats: CoList<{} & CoMap>; myBookmarks: CoList<{} & CoMap> | undefined; } & CoMap; profile: { name: string; bookmarks: CoList<{} & CoMap>; inbox: string | undefined; inboxInvite: string | undefined; } & CoMap; } & { ...; } & Account
root =
Account.root: { myChats: CoList<{} & CoMap>; myBookmarks: CoList<{} & CoMap> | undefined; } & CoMap
MyAppRoot.
const MyAppRoot: CoMapSchema<{ myChats: CoListSchema<CoMapSchema<{}>>; myBookmarks: z.ZodOptional<CoListSchema<CoMapSchema<{}>>>; }>
create({
create: (init: { myBookmarks?: CoList<({} & CoMap) | null> | undefined; myChats: CoList<({} & CoMap) | null>; }, options?: { owner: Account | Group; unique?: CoValueUniqueness["uniqueness"]; } | Account | Group) => { ...; } & CoMap
myChats: CoList<({} & CoMap) | null>
myChats:import co
co.list(
list<CoMapSchema<{}>>(element: CoMapSchema<{}>): CoListSchema<CoMapSchema<{}>> export list
const Chat: CoMapSchema<{}>
Chat).create([],
create: (items: CoListInit<CoMapSchema<{}>>, options?: { owner: Account | Group; } | Account | Group) => CoList<...>
class Group
Group.create()), }); } // We need to load the root field to check for the myContacts field const {
Group.create<Group>(this: CoValueClass<Group>, options?: { owner: Account; } | Account): Group
root } = await
const root: { myChats: CoList<{} & CoMap>; myBookmarks: CoList<{} & CoMap> | undefined; } & CoMap
account.
account: { root: { myChats: CoList<{} & CoMap>; myBookmarks: CoList<{} & CoMap> | undefined; } & CoMap; profile: { name: string; bookmarks: CoList<{} & CoMap>; inbox: string | undefined; inboxInvite: string | undefined; } & CoMap; } & { ...; } & Account
ensureLoaded({
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<...>
resolve: {
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, []>
root: true } }); // we specifically need to check for undefined, // because myBookmarks might simply be not loaded (`null`) yet if (
root?: RefsToResolve<NonNullable<{ myChats: CoList<{} & CoMap>; myBookmarks: CoList<{} & CoMap> | undefined; } & CoMap>, 10, [0]> | undefined
root.
const root: { myChats: CoList<{} & CoMap>; myBookmarks: CoList<{} & CoMap> | undefined; } & CoMap
myBookmarks: CoList<{} & CoMap> | undefined
myBookmarks ===var undefined
undefined) {root.
const root: { myChats: CoList<{} & CoMap>; myBookmarks: CoList<{} & CoMap> | undefined; } & CoMap
myBookmarks: CoList<{} & CoMap> | undefined
myBookmarks =import co
co.list(
list<CoMapSchema<{}>>(element: CoMapSchema<{}>): CoListSchema<CoMapSchema<{}>> export list
const Bookmark: CoMapSchema<{}>
Bookmark).create([],
create: (items: CoListInit<CoMapSchema<{}>>, options?: { owner: Account | Group; } | Account | Group) => CoList<...>
class Group
Group.create()); } });
Group.create<Group>(this: CoValueClass<Group>, options?: { owner: Account; } | Account): Group