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 co
co,import z
z,Loaded,
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<...
class Group
Group,class Account
Account } from "jazz-tools"; export constLocation =
const Location: CoMapSchema<{ city: z.z.ZodString; country: z.z.ZodString; }>
import co
co.map({
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
city: z.z.ZodString
city:import z
z.string(),
function string(params?: string | z.z.core.$ZodStringParams): z.z.ZodString export string
country: z.z.ZodString
country:import z
z.string(), }); // co.ref can be used within CoMap fields to point to other CoValues const
function string(params?: string | z.z.core.$ZodStringParams): z.z.ZodString export string
Actor =
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<...>; }>
import co
co.map({
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
name: (params?: string | z.z.core.$ZodStringParams) => z.z.ZodString
name:import z
z.string,
function string(params?: string | z.z.core.$ZodStringParams): z.z.ZodString export string
imageURL: (params?: string | z.z.core.$ZodStringParams) => z.z.ZodString
imageURL:import z
z.string,
function string(params?: string | z.z.core.$ZodStringParams): z.z.ZodString export string
birthplace:
birthplace: 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 Location: CoMapSchema<{ city: z.z.ZodString; country: z.z.ZodString; }>
Movie =
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<...>; }>
import co
co.map({
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
title: (params?: string | z.z.core.$ZodStringParams) => z.z.ZodString
title:import z
z.string,
function string(params?: string | z.z.core.$ZodStringParams): z.z.ZodString export string
director: (params?: string | z.z.core.$ZodStringParams) => z.z.ZodString
director:import z
z.string,
function string(params?: string | z.z.core.$ZodStringParams): z.z.ZodString export string
cast:
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<...>; }>>
import co
co.list(
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
Actor), // ordered, mutable }) // A User CoMap can maintain a CoFeed of co.ref(Movie) to track their favorite movies 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<...>; }>
User =
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<...>; }>>; }>
import co
co.map({
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
username: (params?: string | z.z.core.$ZodStringParams) => z.z.ZodString
username:import z
z.string,
function string(params?: string | z.z.core.$ZodStringParams): z.z.ZodString export string
favoriteMovies:
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<...>; }>>
import co
co.feed(
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
Movie), // append-only })
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<...>; }>
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.