Connecting CoValues with direct linking

CoValues can form relationships with each other by linking directly to other CoValues. This creates a powerful connection where one CoValue can point to the unique identity of another. Instead of embedding all the details of one CoValue directly within another, you use its Jazz-Tools schema as the field type. This allows multiple CoValues to point to the same piece of data effortlessly.

import { import coco, import zz, 
type Loaded<T extends CoValueClassOrSchema, R extends ResolveQuery<T> = true> = R extends boolean | undefined ? NonNullable<InstanceOfSchemaCoValuesNullable<T>> : [NonNullable<InstanceOfSchemaCoValuesNullable<T>>] extends [...] ? Exclude<...> extends CoValue ? R extends {
    ...;
} ? ((CoValue & ... 1 more ... & (ItemDepth extends boolean | undefined ? CoValue & Exclude<...> : [...] extends [...] ? Exclude<...> extends CoValue ? ItemDepth extends {
    ...;
} ? ((CoValue & ... 1 more ... & (ItemDepth extends boolean | undefined ? CoValue & Exclude<...> : [...] extends [...] ? Exclude<...> extends CoValue ? ItemDepth extends {
    ...;
} ? ((CoValue & ... 1 more ... & (ItemDepth extends boolean | undefined ? CoValue & Exclude<...> : [...] extends [...] ? Exclude<...> extends CoValue ? ItemDepth extends {
    ...;
} ? ((CoValue & ... 1 more ... & (ItemDepth extends boolean | undefined ? CoValue & Exclude<...> : [...] extends [...] ? Exclude<...> extends CoValue ? ItemDepth extends {
    ...;
} ? ((CoValue & ... 1 more ... & (ItemDepth extends boolean | undefined ? CoValue & Exclude<...> : [...] extends [...] ? Exclude<...> extends CoValue ? ItemDepth extends {
    ...;
} ? ((CoValue & ... 1 more ... & (ItemDepth extends boolean | undefined ? CoValue & Exclude<...> : [...] extends [...] ? Exclude<...> extends CoValue ? ItemDepth extends {
    ...;
} ? ((CoValue & ... 1 more ... & (ItemDepth extends boolean | undefined ? CoValue & Exclude<...> : [...] extends [...] ? Exclude<...> extends CoValue ? ItemDepth extends {
    ...;
} ? ((CoValue & ... 1 more ... & (ItemDepth extends boolean | undefined ? CoValue & Exclude<...> : [...] extends [...] ? Exclude<...
Loaded
, class Group
@categoryIdentity & Permissions
Group
, class Account
@categoryIdentity & Permissions
Account
} from "jazz-tools";
export const
const Location: co.Map<{
    city: z.z.ZodString;
    country: z.z.ZodString;
}, unknown, Account | Group>
Location
= import coco.
map<{
    city: z.z.ZodString;
    country: z.z.ZodString;
}>(shape: {
    city: z.z.ZodString;
    country: z.z.ZodString;
}): co.Map<{
    city: z.z.ZodString;
    country: z.z.ZodString;
}, unknown, Account | Group>
export map
map
({
city: z.z.ZodStringcity: import zz.
function string(params?: string | z.z.core.$ZodStringParams): z.z.ZodString (+1 overload)
export string
string
(),
country: z.z.ZodStringcountry: import zz.
function string(params?: string | z.z.core.$ZodStringParams): z.z.ZodString (+1 overload)
export string
string
(),
}); export type
type Location = {
    city: string;
    country: string;
} & CoMap
Location
= import coco.
type loaded<T extends CoValueClassOrSchema, R extends ResolveQuery<T> = true> = R extends boolean | undefined ? NonNullable<InstanceOfSchemaCoValuesNullable<T>> : [NonNullable<InstanceOfSchemaCoValuesNullable<T>>] extends [...] ? Exclude<...> extends CoValue ? R extends {
    ...;
} ? ((CoValue & ... 1 more ... & (ItemDepth extends boolean | undefined ? CoValue & Exclude<...> : [...] extends [...] ? Exclude<...> extends CoValue ? ItemDepth extends {
    ...;
} ? ((CoValue & ... 1 more ... & (ItemDepth extends boolean | undefined ? CoValue & Exclude<...> : [...] extends [...] ? Exclude<...> extends CoValue ? ItemDepth extends {
    ...;
} ? ((CoValue & ... 1 more ... & (ItemDepth extends boolean | undefined ? CoValue & Exclude<...> : [...] extends [...] ? Exclude<...> extends CoValue ? ItemDepth extends {
    ...;
} ? ((CoValue & ... 1 more ... & (ItemDepth extends boolean | undefined ? CoValue & Exclude<...> : [...] extends [...] ? Exclude<...> extends CoValue ? ItemDepth extends {
    ...;
} ? ((CoValue & ... 1 more ... & (ItemDepth extends boolean | undefined ? CoValue & Exclude<...> : [...] extends [...] ? Exclude<...> extends CoValue ? ItemDepth extends {
    ...;
} ? ((CoValue & ... 1 more ... & (ItemDepth extends boolean | undefined ? CoValue & Exclude<...> : [...] extends [...] ? Exclude<...> extends CoValue ? ItemDepth extends {
    ...;
} ? ((CoValue & ... 1 more ... & (ItemDepth extends boolean | undefined ? CoValue & Exclude<...> : [...] extends [...] ? Exclude<...> extends CoValue ? ItemDepth extends {
    ...;
} ? ((CoValue & ... 1 more ... & (ItemDepth extends boolean | undefined ? CoValue & Exclude<...> : [...] extends [...] ? Exclude<...
export loaded
loaded
<typeof
const Location: co.Map<{
    city: z.z.ZodString;
    country: z.z.ZodString;
}, unknown, Account | Group>
Location
>;
// co.ref can be used within CoMap fields to point to other CoValues const
const Actor: co.Map<{
    name: {
        (params?: string | z.z.core.$ZodStringParams): z.z.ZodString;
        <T extends string>(params?: string | z.z.core.$ZodStringParams): z.z.core.$ZodType<T, T>;
    };
    imageURL: {
        (params?: string | z.z.core.$ZodStringParams): z.z.ZodString;
        <T extends string>(params?: string | z.z.core.$ZodStringParams): z.z.core.$ZodType<T, T>;
    };
    birthplace: co.Map<...>;
}, unknown, Account | Group>
Actor
= import coco.
map<{
    name: {
        (params?: string | z.z.core.$ZodStringParams): z.z.ZodString;
        <T extends string>(params?: string | z.z.core.$ZodStringParams): z.z.core.$ZodType<T, T>;
    };
    imageURL: {
        (params?: string | z.z.core.$ZodStringParams): z.z.ZodString;
        <T extends string>(params?: string | z.z.core.$ZodStringParams): z.z.core.$ZodType<T, T>;
    };
    birthplace: co.Map<...>;
}>(shape: {
    name: {
        (params?: string | z.z.core.$ZodStringParams): z.z.ZodString;
        <T extends string>(params?: string | z.z.core.$ZodStringParams): z.z.core.$ZodType<T, T>;
    };
    imageURL: {
        (params?: string | z.z.core.$ZodStringParams): z.z.ZodString;
        <T extends string>(params?: string | z.z.core.$ZodStringParams): z.z.core.$ZodType<T, T>;
    };
    birthplace: co.Map<...>;
}): co.Map<...>
export map
map
({
name: {
    (params?: string | z.z.core.$ZodStringParams): z.z.ZodString;
    <T extends string>(params?: string | z.z.core.$ZodStringParams): z.z.core.$ZodType<T, T>;
}
name
: import zz.
function string(params?: string | z.z.core.$ZodStringParams): z.z.ZodString (+1 overload)
export string
string
,
imageURL: {
    (params?: string | z.z.core.$ZodStringParams): z.z.ZodString;
    <T extends string>(params?: string | z.z.core.$ZodStringParams): z.z.core.$ZodType<T, T>;
}
imageURL
: import zz.
function string(params?: string | z.z.core.$ZodStringParams): z.z.ZodString (+1 overload)
export string
string
,
birthplace: co.Map<{
    city: z.z.ZodString;
    country: z.z.ZodString;
}, unknown, Account | Group>
birthplace
:
const Location: co.Map<{
    city: z.z.ZodString;
    country: z.z.ZodString;
}, unknown, Account | Group>
Location
// Links directly to the Location CoMap above.
}) export type
type Actor = {
    name: never;
    imageURL: never;
    birthplace: ({
        city: string;
        country: string;
    } & CoMap) | null;
} & CoMap
Actor
= import coco.
type loaded<T extends CoValueClassOrSchema, R extends ResolveQuery<T> = true> = R extends boolean | undefined ? NonNullable<InstanceOfSchemaCoValuesNullable<T>> : [NonNullable<InstanceOfSchemaCoValuesNullable<T>>] extends [...] ? Exclude<...> extends CoValue ? R extends {
    ...;
} ? ((CoValue & ... 1 more ... & (ItemDepth extends boolean | undefined ? CoValue & Exclude<...> : [...] extends [...] ? Exclude<...> extends CoValue ? ItemDepth extends {
    ...;
} ? ((CoValue & ... 1 more ... & (ItemDepth extends boolean | undefined ? CoValue & Exclude<...> : [...] extends [...] ? Exclude<...> extends CoValue ? ItemDepth extends {
    ...;
} ? ((CoValue & ... 1 more ... & (ItemDepth extends boolean | undefined ? CoValue & Exclude<...> : [...] extends [...] ? Exclude<...> extends CoValue ? ItemDepth extends {
    ...;
} ? ((CoValue & ... 1 more ... & (ItemDepth extends boolean | undefined ? CoValue & Exclude<...> : [...] extends [...] ? Exclude<...> extends CoValue ? ItemDepth extends {
    ...;
} ? ((CoValue & ... 1 more ... & (ItemDepth extends boolean | undefined ? CoValue & Exclude<...> : [...] extends [...] ? Exclude<...> extends CoValue ? ItemDepth extends {
    ...;
} ? ((CoValue & ... 1 more ... & (ItemDepth extends boolean | undefined ? CoValue & Exclude<...> : [...] extends [...] ? Exclude<...> extends CoValue ? ItemDepth extends {
    ...;
} ? ((CoValue & ... 1 more ... & (ItemDepth extends boolean | undefined ? CoValue & Exclude<...> : [...] extends [...] ? Exclude<...> extends CoValue ? ItemDepth extends {
    ...;
} ? ((CoValue & ... 1 more ... & (ItemDepth extends boolean | undefined ? CoValue & Exclude<...> : [...] extends [...] ? Exclude<...
export loaded
loaded
<typeof
const Actor: co.Map<{
    name: {
        (params?: string | z.z.core.$ZodStringParams): z.z.ZodString;
        <T extends string>(params?: string | z.z.core.$ZodStringParams): z.z.core.$ZodType<T, T>;
    };
    imageURL: {
        (params?: string | z.z.core.$ZodStringParams): z.z.ZodString;
        <T extends string>(params?: string | z.z.core.$ZodStringParams): z.z.core.$ZodType<T, T>;
    };
    birthplace: co.Map<...>;
}, unknown, Account | Group>
Actor
>;
// actual actor data is stored in the separate Actor CoValue const
const Movie: co.Map<{
    title: {
        (params?: string | z.z.core.$ZodStringParams): z.z.ZodString;
        <T extends string>(params?: string | z.z.core.$ZodStringParams): z.z.core.$ZodType<T, T>;
    };
    director: {
        (params?: string | z.z.core.$ZodStringParams): z.z.ZodString;
        <T extends string>(params?: string | z.z.core.$ZodStringParams): z.z.core.$ZodType<T, T>;
    };
    cast: co.List<...>;
}, unknown, Account | Group>
Movie
= import coco.
map<{
    title: {
        (params?: string | z.z.core.$ZodStringParams): z.z.ZodString;
        <T extends string>(params?: string | z.z.core.$ZodStringParams): z.z.core.$ZodType<T, T>;
    };
    director: {
        (params?: string | z.z.core.$ZodStringParams): z.z.ZodString;
        <T extends string>(params?: string | z.z.core.$ZodStringParams): z.z.core.$ZodType<T, T>;
    };
    cast: co.List<...>;
}>(shape: {
    title: {
        (params?: string | z.z.core.$ZodStringParams): z.z.ZodString;
        <T extends string>(params?: string | z.z.core.$ZodStringParams): z.z.core.$ZodType<T, T>;
    };
    director: {
        (params?: string | z.z.core.$ZodStringParams): z.z.ZodString;
        <T extends string>(params?: string | z.z.core.$ZodStringParams): z.z.core.$ZodType<T, T>;
    };
    cast: co.List<...>;
}): co.Map<...>
export map
map
({
title: {
    (params?: string | z.z.core.$ZodStringParams): z.z.ZodString;
    <T extends string>(params?: string | z.z.core.$ZodStringParams): z.z.core.$ZodType<T, T>;
}
title
: import zz.
function string(params?: string | z.z.core.$ZodStringParams): z.z.ZodString (+1 overload)
export string
string
,
director: {
    (params?: string | z.z.core.$ZodStringParams): z.z.ZodString;
    <T extends string>(params?: string | z.z.core.$ZodStringParams): z.z.core.$ZodType<T, T>;
}
director
: import zz.
function string(params?: string | z.z.core.$ZodStringParams): z.z.ZodString (+1 overload)
export string
string
,
cast: co.List<co.Map<{
    name: {
        (params?: string | z.z.core.$ZodStringParams): z.z.ZodString;
        <T extends string>(params?: string | z.z.core.$ZodStringParams): z.z.core.$ZodType<T, T>;
    };
    imageURL: {
        (params?: string | z.z.core.$ZodStringParams): z.z.ZodString;
        <T extends string>(params?: string | z.z.core.$ZodStringParams): z.z.core.$ZodType<T, T>;
    };
    birthplace: co.Map<...>;
}, unknown, Account | Group>>
cast
: import coco.
list<co.Map<{
    name: {
        (params?: string | z.z.core.$ZodStringParams): z.z.ZodString;
        <T extends string>(params?: string | z.z.core.$ZodStringParams): z.z.core.$ZodType<T, T>;
    };
    imageURL: {
        (params?: string | z.z.core.$ZodStringParams): z.z.ZodString;
        <T extends string>(params?: string | z.z.core.$ZodStringParams): z.z.core.$ZodType<T, T>;
    };
    birthplace: co.Map<...>;
}, unknown, Account | Group>>(element: co.Map<...>): co.List<...>
export list
list
(
const Actor: co.Map<{
    name: {
        (params?: string | z.z.core.$ZodStringParams): z.z.ZodString;
        <T extends string>(params?: string | z.z.core.$ZodStringParams): z.z.core.$ZodType<T, T>;
    };
    imageURL: {
        (params?: string | z.z.core.$ZodStringParams): z.z.ZodString;
        <T extends string>(params?: string | z.z.core.$ZodStringParams): z.z.core.$ZodType<T, T>;
    };
    birthplace: co.Map<...>;
}, unknown, Account | Group>
Actor
), // ordered, mutable
}) export type
type Movie = {
    title: never;
    director: never;
    cast: CoList<({
        name: never;
        imageURL: never;
        birthplace: ({
            city: string;
            country: string;
        } & CoMap) | null;
    } & CoMap) | null> | null;
} & CoMap
Movie
= import coco.
type loaded<T extends CoValueClassOrSchema, R extends ResolveQuery<T> = true> = R extends boolean | undefined ? NonNullable<InstanceOfSchemaCoValuesNullable<T>> : [NonNullable<InstanceOfSchemaCoValuesNullable<T>>] extends [...] ? Exclude<...> extends CoValue ? R extends {
    ...;
} ? ((CoValue & ... 1 more ... & (ItemDepth extends boolean | undefined ? CoValue & Exclude<...> : [...] extends [...] ? Exclude<...> extends CoValue ? ItemDepth extends {
    ...;
} ? ((CoValue & ... 1 more ... & (ItemDepth extends boolean | undefined ? CoValue & Exclude<...> : [...] extends [...] ? Exclude<...> extends CoValue ? ItemDepth extends {
    ...;
} ? ((CoValue & ... 1 more ... & (ItemDepth extends boolean | undefined ? CoValue & Exclude<...> : [...] extends [...] ? Exclude<...> extends CoValue ? ItemDepth extends {
    ...;
} ? ((CoValue & ... 1 more ... & (ItemDepth extends boolean | undefined ? CoValue & Exclude<...> : [...] extends [...] ? Exclude<...> extends CoValue ? ItemDepth extends {
    ...;
} ? ((CoValue & ... 1 more ... & (ItemDepth extends boolean | undefined ? CoValue & Exclude<...> : [...] extends [...] ? Exclude<...> extends CoValue ? ItemDepth extends {
    ...;
} ? ((CoValue & ... 1 more ... & (ItemDepth extends boolean | undefined ? CoValue & Exclude<...> : [...] extends [...] ? Exclude<...> extends CoValue ? ItemDepth extends {
    ...;
} ? ((CoValue & ... 1 more ... & (ItemDepth extends boolean | undefined ? CoValue & Exclude<...> : [...] extends [...] ? Exclude<...> extends CoValue ? ItemDepth extends {
    ...;
} ? ((CoValue & ... 1 more ... & (ItemDepth extends boolean | undefined ? CoValue & Exclude<...> : [...] extends [...] ? Exclude<...
export loaded
loaded
<typeof
const Movie: co.Map<{
    title: {
        (params?: string | z.z.core.$ZodStringParams): z.z.ZodString;
        <T extends string>(params?: string | z.z.core.$ZodStringParams): z.z.core.$ZodType<T, T>;
    };
    director: {
        (params?: string | z.z.core.$ZodStringParams): z.z.ZodString;
        <T extends string>(params?: string | z.z.core.$ZodStringParams): z.z.core.$ZodType<T, T>;
    };
    cast: co.List<...>;
}, unknown, Account | Group>
Movie
>;
// A User CoMap can maintain a CoFeed of co.ref(Movie) to track their favorite movies const
const User: co.Map<{
    username: {
        (params?: string | z.z.core.$ZodStringParams): z.z.ZodString;
        <T extends string>(params?: string | z.z.core.$ZodStringParams): z.z.core.$ZodType<T, T>;
    };
    favoriteMovies: co.Feed<...>;
}, unknown, Account | Group>
User
= import coco.
map<{
    username: {
        (params?: string | z.z.core.$ZodStringParams): z.z.ZodString;
        <T extends string>(params?: string | z.z.core.$ZodStringParams): z.z.core.$ZodType<T, T>;
    };
    favoriteMovies: co.Feed<...>;
}>(shape: {
    username: {
        (params?: string | z.z.core.$ZodStringParams): z.z.ZodString;
        <T extends string>(params?: string | z.z.core.$ZodStringParams): z.z.core.$ZodType<T, T>;
    };
    favoriteMovies: co.Feed<...>;
}): co.Map<...>
export map
map
({
username: {
    (params?: string | z.z.core.$ZodStringParams): z.z.ZodString;
    <T extends string>(params?: string | z.z.core.$ZodStringParams): z.z.core.$ZodType<T, T>;
}
username
: import zz.
function string(params?: string | z.z.core.$ZodStringParams): z.z.ZodString (+1 overload)
export string
string
,
favoriteMovies: co.Feed<co.Map<{
    title: {
        (params?: string | z.z.core.$ZodStringParams): z.z.ZodString;
        <T extends string>(params?: string | z.z.core.$ZodStringParams): z.z.core.$ZodType<T, T>;
    };
    director: {
        (params?: string | z.z.core.$ZodStringParams): z.z.ZodString;
        <T extends string>(params?: string | z.z.core.$ZodStringParams): z.z.core.$ZodType<T, T>;
    };
    cast: co.List<...>;
}, unknown, Account | Group>>
favoriteMovies
: import coco.
feed<co.Map<{
    title: {
        (params?: string | z.z.core.$ZodStringParams): z.z.ZodString;
        <T extends string>(params?: string | z.z.core.$ZodStringParams): z.z.core.$ZodType<T, T>;
    };
    director: {
        (params?: string | z.z.core.$ZodStringParams): z.z.ZodString;
        <T extends string>(params?: string | z.z.core.$ZodStringParams): z.z.core.$ZodType<T, T>;
    };
    cast: co.List<...>;
}, unknown, Account | Group>>(element: co.Map<...>): co.Feed<...>
export feed
feed
(
const Movie: co.Map<{
    title: {
        (params?: string | z.z.core.$ZodStringParams): z.z.ZodString;
        <T extends string>(params?: string | z.z.core.$ZodStringParams): z.z.core.$ZodType<T, T>;
    };
    director: {
        (params?: string | z.z.core.$ZodStringParams): z.z.ZodString;
        <T extends string>(params?: string | z.z.core.$ZodStringParams): z.z.core.$ZodType<T, T>;
    };
    cast: co.List<...>;
}, unknown, Account | Group>
Movie
), // append-only
}) export type
type User = {
    username: never;
    favoriteMovies: CoFeed<({
        title: never;
        director: never;
        cast: CoList<({
            name: never;
            imageURL: never;
            birthplace: ({
                city: string;
                country: string;
            } & CoMap) | null;
        } & CoMap) | null> | null;
    } & CoMap) | null> | null;
} & CoMap
User
= import coco.
type loaded<T extends CoValueClassOrSchema, R extends ResolveQuery<T> = true> = R extends boolean | undefined ? NonNullable<InstanceOfSchemaCoValuesNullable<T>> : [NonNullable<InstanceOfSchemaCoValuesNullable<T>>] extends [...] ? Exclude<...> extends CoValue ? R extends {
    ...;
} ? ((CoValue & ... 1 more ... & (ItemDepth extends boolean | undefined ? CoValue & Exclude<...> : [...] extends [...] ? Exclude<...> extends CoValue ? ItemDepth extends {
    ...;
} ? ((CoValue & ... 1 more ... & (ItemDepth extends boolean | undefined ? CoValue & Exclude<...> : [...] extends [...] ? Exclude<...> extends CoValue ? ItemDepth extends {
    ...;
} ? ((CoValue & ... 1 more ... & (ItemDepth extends boolean | undefined ? CoValue & Exclude<...> : [...] extends [...] ? Exclude<...> extends CoValue ? ItemDepth extends {
    ...;
} ? ((CoValue & ... 1 more ... & (ItemDepth extends boolean | undefined ? CoValue & Exclude<...> : [...] extends [...] ? Exclude<...> extends CoValue ? ItemDepth extends {
    ...;
} ? ((CoValue & ... 1 more ... & (ItemDepth extends boolean | undefined ? CoValue & Exclude<...> : [...] extends [...] ? Exclude<...> extends CoValue ? ItemDepth extends {
    ...;
} ? ((CoValue & ... 1 more ... & (ItemDepth extends boolean | undefined ? CoValue & Exclude<...> : [...] extends [...] ? Exclude<...> extends CoValue ? ItemDepth extends {
    ...;
} ? ((CoValue & ... 1 more ... & (ItemDepth extends boolean | undefined ? CoValue & Exclude<...> : [...] extends [...] ? Exclude<...> extends CoValue ? ItemDepth extends {
    ...;
} ? ((CoValue & ... 1 more ... & (ItemDepth extends boolean | undefined ? CoValue & Exclude<...> : [...] extends [...] ? Exclude<...
export loaded
loaded
<typeof
const User: co.Map<{
    username: {
        (params?: string | z.z.core.$ZodStringParams): z.z.ZodString;
        <T extends string>(params?: string | z.z.core.$ZodStringParams): z.z.core.$ZodType<T, T>;
    };
    favoriteMovies: co.Feed<...>;
}, unknown, Account | Group>
User
>;

Understanding CoList and CoFeed

  • CoList is a collaborative list where each item is a reference to a CoValue
  • CoFeed contains an append-only list of references to CoValues.

This direct linking approach offers a single source of truth. When you update a referenced CoValue, all other CoValues that point to it are automatically updated, ensuring data consistency across your application.

By connecting CoValues through these direct references, you can build robust and collaborative applications where data is consistent, efficient to manage, and relationships are clearly defined. The ability to link different CoValue types to the same underlying data is fundamental to building complex applications with Jazz.