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 of 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 CoValueClass | AnyCoSchema, 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: CoMapSchema<{
    city: z.z.ZodString;
    country: z.z.ZodString;
}>
Location
= import coco.
map<{
    city: z.z.ZodString;
    country: z.z.ZodString;
}>(shape: {
    city: z.z.ZodString;
    country: z.z.ZodString;
}): CoMapSchema<{
    city: z.z.ZodString;
    country: z.z.ZodString;
}>
export map
map
({
city: z.z.ZodStringcity: import zz.
function string(params?: string | z.z.core.$ZodStringParams): z.z.ZodString
export string
string
(),
country: z.z.ZodStringcountry: import zz.
function string(params?: string | z.z.core.$ZodStringParams): z.z.ZodString
export string
string
(),
}); // co.ref can be used within CoMap fields to point to other CoValues const
const Actor: CoMapSchema<{
    name: (params?: string | z.z.core.$ZodStringParams) => z.z.ZodString;
    imageURL: (params?: string | z.z.core.$ZodStringParams) => z.z.ZodString;
    birthplace: CoMapSchema<...>;
}>
Actor
= import coco.
map<{
    name: (params?: string | z.z.core.$ZodStringParams) => z.z.ZodString;
    imageURL: (params?: string | z.z.core.$ZodStringParams) => z.z.ZodString;
    birthplace: CoMapSchema<...>;
}>(shape: {
    name: (params?: string | z.z.core.$ZodStringParams) => z.z.ZodString;
    imageURL: (params?: string | z.z.core.$ZodStringParams) => z.z.ZodString;
    birthplace: CoMapSchema<...>;
}): CoMapSchema<...>
export map
map
({
name: (params?: string | z.z.core.$ZodStringParams) => z.z.ZodStringname: import zz.
function string(params?: string | z.z.core.$ZodStringParams): z.z.ZodString
export string
string
,
imageURL: (params?: string | z.z.core.$ZodStringParams) => z.z.ZodStringimageURL: import zz.
function string(params?: string | z.z.core.$ZodStringParams): z.z.ZodString
export string
string
,
birthplace: CoMapSchema<{
    city: z.z.ZodString;
    country: z.z.ZodString;
}>
birthplace
:
const Location: CoMapSchema<{
    city: z.z.ZodString;
    country: z.z.ZodString;
}>
Location
// Links directly to the Location CoMap above.
}) // actual actor data is stored in the separate Actor CoValue const
const Movie: CoMapSchema<{
    title: (params?: string | z.z.core.$ZodStringParams) => z.z.ZodString;
    director: (params?: string | z.z.core.$ZodStringParams) => z.z.ZodString;
    cast: CoListSchema<...>;
}>
Movie
= import coco.
map<{
    title: (params?: string | z.z.core.$ZodStringParams) => z.z.ZodString;
    director: (params?: string | z.z.core.$ZodStringParams) => z.z.ZodString;
    cast: CoListSchema<...>;
}>(shape: {
    title: (params?: string | z.z.core.$ZodStringParams) => z.z.ZodString;
    director: (params?: string | z.z.core.$ZodStringParams) => z.z.ZodString;
    cast: CoListSchema<...>;
}): CoMapSchema<...>
export map
map
({
title: (params?: string | z.z.core.$ZodStringParams) => z.z.ZodStringtitle: import zz.
function string(params?: string | z.z.core.$ZodStringParams): z.z.ZodString
export string
string
,
director: (params?: string | z.z.core.$ZodStringParams) => z.z.ZodStringdirector: import zz.
function string(params?: string | z.z.core.$ZodStringParams): z.z.ZodString
export string
string
,
cast: CoListSchema<CoMapSchema<{
    name: (params?: string | z.z.core.$ZodStringParams) => z.z.ZodString;
    imageURL: (params?: string | z.z.core.$ZodStringParams) => z.z.ZodString;
    birthplace: CoMapSchema<...>;
}>>
cast
: import coco.
list<CoMapSchema<{
    name: (params?: string | z.z.core.$ZodStringParams) => z.z.ZodString;
    imageURL: (params?: string | z.z.core.$ZodStringParams) => z.z.ZodString;
    birthplace: CoMapSchema<...>;
}>>(element: CoMapSchema<...>): CoListSchema<...>
export list
list
(
const Actor: CoMapSchema<{
    name: (params?: string | z.z.core.$ZodStringParams) => z.z.ZodString;
    imageURL: (params?: string | z.z.core.$ZodStringParams) => z.z.ZodString;
    birthplace: CoMapSchema<...>;
}>
Actor
), // ordered, mutable
}) // A User CoMap can maintain a CoFeed of co.ref(Movie) to track their favorite movies const
const User: CoMapSchema<{
    username: (params?: string | z.z.core.$ZodStringParams) => z.z.ZodString;
    favoriteMovies: CoFeedSchema<CoMapSchema<{
        title: (params?: string | z.z.core.$ZodStringParams) => z.z.ZodString;
        director: (params?: string | z.z.core.$ZodStringParams) => z.z.ZodString;
        cast: CoListSchema<...>;
    }>>;
}>
User
= import coco.
map<{
    username: (params?: string | z.z.core.$ZodStringParams) => z.z.ZodString;
    favoriteMovies: CoFeedSchema<CoMapSchema<{
        title: (params?: string | z.z.core.$ZodStringParams) => z.z.ZodString;
        director: (params?: string | z.z.core.$ZodStringParams) => z.z.ZodString;
        cast: CoListSchema<...>;
    }>>;
}>(shape: {
    username: (params?: string | z.z.core.$ZodStringParams) => z.z.ZodString;
    favoriteMovies: CoFeedSchema<CoMapSchema<{
        title: (params?: string | z.z.core.$ZodStringParams) => z.z.ZodString;
        director: (params?: string | z.z.core.$ZodStringParams) => z.z.ZodString;
        cast: CoListSchema<...>;
    }>>;
}): CoMapSchema<...>
export map
map
({
username: (params?: string | z.z.core.$ZodStringParams) => z.z.ZodStringusername: import zz.
function string(params?: string | z.z.core.$ZodStringParams): z.z.ZodString
export string
string
,
favoriteMovies: CoFeedSchema<CoMapSchema<{
    title: (params?: string | z.z.core.$ZodStringParams) => z.z.ZodString;
    director: (params?: string | z.z.core.$ZodStringParams) => z.z.ZodString;
    cast: CoListSchema<...>;
}>>
favoriteMovies
: import coco.
feed<CoMapSchema<{
    title: (params?: string | z.z.core.$ZodStringParams) => z.z.ZodString;
    director: (params?: string | z.z.core.$ZodStringParams) => z.z.ZodString;
    cast: CoListSchema<...>;
}>>(element: CoMapSchema<...>): CoFeedSchema<...>
export feed
feed
(
const Movie: CoMapSchema<{
    title: (params?: string | z.z.core.$ZodStringParams) => z.z.ZodString;
    director: (params?: string | z.z.core.$ZodStringParams) => z.z.ZodString;
    cast: CoListSchema<...>;
}>
Movie
), // append-only
})

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.