Compare commits

...

3 Commits

Author SHA1 Message Date
Margaret Culotta
69ea14c513 cleanup and move 2025-05-21 11:33:06 -05:00
Margaret Culotta
05d6f36f30 merge main and move doc section into schema 2025-05-21 11:06:42 -05:00
Margaret Culotta
02541d3f48 add findUnique docs 2025-05-21 10:02:14 -05:00
2 changed files with 38 additions and 1 deletions

View File

@@ -418,3 +418,40 @@ const fullName = Person.fullName(person);
const age = Person.ageAsOf(person, new Date());
```
</CodeGroup>
### Finding Unique CoValues with findUnique
Jazz provides a powerful mechanism to find unique CoMap instances based on their `CoValueUniqueness` identifier.
The `findUnique` method allows for the location of specific CoMap instances without needing to load them first.
#### When to use findUnique
Use findUnique when there's an assigned unique identifier on a CoMap (via CoValueUniqueness) and you want to retrieve that specific instance.
This is useful for:
- **Singleton CoValues** - When a `CoMap` should only ever have one instance
- **Referencing specific instances** - When there's a need to access a CoMap that is known by its unique ID and owner
<CodeGroup>
```ts twoslash
import {co, z, Group} from "jazz-tools"
const group = Group.create();
// ---cut---
const Person = co.map({
firstName: z.string(),
lastName: z.string(),
dateOfBirth: z.date()
});
const person = Person.create(
{
firstName: "John",
lastName: "Doe",
dateOfBirth: new Date("1990-01-01"),
},
{ owner: group, unique: { lastName: "Doe" } },
);
const foundPerson = Person.findUnique({ lastName: "Doe" }, group.id);
```
</CodeGroup>

View File

@@ -851,4 +851,4 @@ async function completeAllTasks(projectId: string) {
2. **Use framework integrations**: They handle subscription lifecycle automatically
3. **Clean up subscriptions**: Always store and call the unsubscribe function when you're done
4. **Handle all loading states**: Check for undefined (loading), null (not found), and success states
5. **Use the Loaded type**: Add compile-time type safety for components that require specific resolution patterns
5. **Use the Loaded type**: Add compile-time type safety for components that require specific resolution patterns