reactive/data-client / .agents/skills/data-client-schema
data-client-schema
VerifiedModel the app's data with @data-client schemas, which normalize responses into one shared cache so an update shows everywhere. Use it whenever an endpoint returning data is added or its response changes (resource, RestEndpoint, GraphQL, Endpoint), when the data's shape changes, when data from different endpoints must be joined or related, when deriving views from loaded data, or when one item looks different in two places. Covers Entity, GQLEntity, Collection, Union and Query.
references/Array.vue.md
View on GitHub<!-- Generated by `yarn build:skills` from docs/rest/api/Array.md (vue). Edit the source doc, not this file. -->
# schema.Array
Creates a schema to normalize an array of schemas. If the input value is an [Object](./Object.vue.md) instead of an `Array`,
the normalized result will be an `Array` of the [Object](./Object.vue.md)'s values.
_Note: The same behavior can be defined with shorthand syntax: `[ mySchema ]`_
- `definition`: **required** A singular schema that this array contains _or_ a mapping of attribute values to schema.
- `schemaAttribute`: _optional_ (required if `definition` is not a singular schema) The attribute on each entity found that defines what schema, per the definition mapping, to use when normalizing.
Can be a string or a function. If given a function, accepts the following arguments:
\_ `value`: The input value of the entity.
\_ `parent`: The parent object of the input array. \* `key`: The key at which the input array appears on the parent object.
> **Tip**
>
> For unbounded collections with `string` keys, use [schema.Values](./Values.vue.md)
> **Tip**
>
> Make it mutable (new items can be [pushed](./Collection.vue.md#push)/[unshifted](./Collection.vue.md#unshift)) with [Collections](./Collection.vue.md)
## Instance Methods
- `define(definition)`: When used, the `definition` passed in will be merged with the original definition passed to the `Array` constructor. This method tends to be useful for creating circular references in schema.
## Usage
To describe a simple array of a singular entity type:
```ts title="api/User"
import { Entity, RestEndpoint, schema } from '@data-client/rest';
export class User extends Entity {
id = '';
name = '';
}
export const getUsers = new RestEndpoint({
path: '/users',
schema: new schema.Array(User),
});
```
```html title="UsersPage.vue"
<script setup lang="ts">
import { useSuspense } from '@data-client/vue';
import { getUsers } from './api/User';
const users = await useSuspense(getUsers);
</script>
<template>
<div>
<div v-for="user in users" :key="user.pk()">{{ user.name }}</div>
</div>
</template>
```
### Updating many entities
Use an Array with [Controller.set()](https://dataclient.io/vue/api/Controller#set-array) to write many entities in one store update,
without an endpoint.
```ts
ctrl.set(
[User],
[
{ id: '123', name: 'Jim' },
{ id: '456', name: 'Jane' },
],
);
```
### Polymorphic types
If your input data is an array of more than one type of entity, it is necessary to define a schema mapping.
> **Note**
>
> If your data returns an object that you did not provide a mapping for, the original object will be returned in the result and an entity will not be created.
#### string schemaAttribute
```typescript title="api/Feed"
import { Entity, RestEndpoint, schema } from '@data-client/rest';
export abstract class FeedItem extends Entity {
readonly id: number = 0;
declare readonly type: 'link' | 'post';
}
export class Link extends FeedItem {
readonly type = 'link' as const;
readonly url: string = '';
readonly title: string = '';
}
export class Post extends FeedItem {
readonly type = 'post' as const;
readonly content: string = '';
}
export const getFeed = new RestEndpoint({
path: '/feed',
schema: new schema.Array(
{
link: Link,
post: Post,
},
'type',
),
});
```
```html title="LinkItem.vue"
<script setup lang="ts">
import { type Link } from './api/Feed';
defineProps<{ link: Link }>();
</script>
<template>
<a :href="link.url">{{ link.title }}</a>
</template>
```
```html title="PostItem.vue"
<script setup lang="ts">
import { type Post } from './api/Feed';
defineProps<{ post: Post }>();
</script>
<template>
<div>{{ post.content }}</div>
</template>
```
```html title="FeedList.vue"
<script setup lang="ts">
import { useSuspense } from '@data-client/vue';
import { getFeed } from './api/Feed';
import LinkItem from './LinkItem.vue';
import PostItem from './PostItem.vue';
const feedItems = await useSuspense(getFeed);
</script>
<template>
<div>
<template v-for="item in feedItems" :key="item.pk()">
<LinkItem v-if="item.type === 'link'" :link="item" />
<PostItem v-else :post="item" />
</template>
</div>
</template>
```
#### function schemaAttribute
The return values should match a key in the `definition`. Here we'll show the same behavior as the 'string'
case, except we'll append an 's'.
```typescript title="api/Feed"
import { Entity, RestEndpoint, schema } from '@data-client/rest';
export abstract class FeedItem extends Entity {
readonly id: number = 0;
declare readonly type: 'link' | 'post';
}
export class Link extends FeedItem {
readonly type = 'link' as const;
readonly url: string = '';
readonly title: string = '';
}
export class Post extends FeedItem {
readonly type = 'post' as const;
readonly content: string = '';
}
export const getFeed = new RestEndpoint({
path: '/feed',
schema: new schema.Array(
{
links: Link,
posts: Post,
},
(input: Link | Post, parent: unknown, key: string) => `${input.type}s`,
),
});
```
```html title="LinkItem.vue"
<script setup lang="ts">
import { type Link } from './api/Feed';
defineProps<{ link: Link }>();
</script>
<template>
<a :href="link.url">{{ link.title }}</a>
</template>
```
```html title="PostItem.vue"
<script setup lang="ts">
import { type Post } from './api/Feed';
defineProps<{ post: Post }>();
</script>
<template>
<div>{{ post.content }}</div>
</template>
```
```html title="FeedList.vue"
<script setup lang="ts">
import { useSuspense } from '@data-client/vue';
import { getFeed } from './api/Feed';
import LinkItem from './LinkItem.vue';
import PostItem from './PostItem.vue';
const feedItems = await useSuspense(getFeed);
</script>
<template>
<div>
<template v-for="item in feedItems" :key="item.pk()">
<LinkItem v-if="item.type === 'link'" :link="item" />
<PostItem v-else :post="item" />
</template>
</div>
</template>
```
Related skills
| Skill | Stars | Updated | Works with | Status | Install |
|---|---|---|---|---|---|
| slack-gif-creatoranthropics/skillsKnowledge and utilities for creating animated GIFs optimized for Slack. Provides constraints, validation tools, and animation concepts. Use when users request animated GIFs for Slack like "make me a GIF of X doing Y for Slack." | Stars: 196k | Updated | Claude CodeCodex | Verified | Install |
| ponytailDietrichGebert/ponytailLazy senior dev mode for any coding task: the smallest change that fully solves it, and a reply a busy human understands in one read. | Stars: 193k | Updated | Claude CodeCodex | Verified | Install |
| ponytail-debtDietrichGebert/ponytailHarvest every shortcut: comment into one debt ledger, so deferrals get tracked instead of forgotten. One-shot report. | Stars: 193k | Updated | Claude CodeCodex | Verified | Install |
| ponytail-debtDietrichGebert/ponytailList every `shortcut:` comment (and older `ponytail:` ones) as a debt ledger. One-shot report, changes nothing. Use for "ponytail debt", "what did ponytail defer", "list the shortcuts", /ponytail-debt. | Stars: 193k | Updated | Claude CodeCodex | Verified | Install |