# Resources

> Reference for defineResource, which declares a read of data with an input schema and a refresh policy that DSUI runs for your pages.

A resource describes data that a page wants. You write the query, and DSUI decides when to run it, shares its result with every component that watches it, and refreshes it on the policy you choose. A resource only reads. Put changes in an [action](/docs/adapter-sdk/actions).

## Define a resource without input

An inputless resource takes no parameters. Its query receives `undefined` and the adapter context:

```ts
import { defineResource } from "@northgraindata/dsui-adapter-sdk";
import type { MyContext } from "./context.js";

export const status = defineResource({
  id: "status",
  query: async (_input: undefined, ctx: MyContext) => ({
    url: ctx.config.baseUrl,
    ok: await ctx.client.ping(),
  }),
});
```

The `id` must be unique within the adapter. Call the resource to create a binding that a component can read: `status()`. Creating a binding doesn't run the query.

## Define a resource with input

Add a Zod `input` schema to make a resource that takes parameters. DSUI validates every binding's input against it, and the query receives the parsed value:

```ts
import { defineResource, z } from "@northgraindata/dsui-adapter-sdk";

export const echo = defineResource({
  id: "echo",
  input: z.object({ text: z.string().min(1) }),
  query: ({ text }, ctx: MyContext) => ({ text, from: ctx.config.baseUrl }),
});
```

Bind it with its input: `echo({ text: "hi" })`. DSUI rejects an input that doesn't match the schema, and the caller gets the Zod issues as the error. The same schema gives your bindings their TypeScript types.

## Describe a resource

An optional `description` explains what the resource returns. DSUI publishes it with the input schema, so tools that list the adapter's resources, such as the AI assistant, can tell them apart:

```ts
defineResource({
  id: "sample-orders",
  description: "Read up to 10 sample orders from the selected region.",
  input: z.object({ region: z.string() }),
  query: ({ region }, ctx: MyContext) => ctx.client.sampleOrders(region, 10),
});
```

A description is documentation. It doesn't limit what the query can do, so your code must enforce permissions, limits, and timeouts itself.

## Choose a refresh policy

`refresh` says how a watched resource stays fresh. The default is `manual()`.

| Policy | Behavior |
| --- | --- |
| `manual()` | DSUI runs the query when a component starts watching it, and again when an action invalidates it |
| `poll(interval)` | The same, and DSUI also runs it again on a fixed interval while someone watches it |

```ts
import { defineResource, poll } from "@northgraindata/dsui-adapter-sdk";

export const warehouses = defineResource({
  id: "warehouses",
  query: (_input: undefined, ctx: MyContext) => ctx.client.listWarehouses(),
  refresh: poll("5s"),
});
```

`poll` takes `"500ms"`, `"5s"`, `"2m"`, or a number of milliseconds. A value that isn't positive, or text in another format, fails when the adapter loads with `Invalid poll interval`. Polling starts when the first component watches the resource and stops when the last one leaves.

## Show a resource on a page

Pass a binding to a component as its `source`:

```ts
definePage({
  path: "/",
  render: () => [
    Table({ source: warehouses() }),
    Value({ source: status(), field: "url" }),
  ],
});
```

The query never runs in the page. It runs in the adapter's process, and the result goes to every component that watches the same binding. See [pages](/docs/adapter-sdk/pages).

## The context argument

The second argument to `query` is the adapter context from `defineAdapter`. DSUI also adds `stores` and `blobs` to it, which give access to the adapter's stores and to binary storage private to the service. Keep UI state out of resources. That belongs in [stores](/docs/adapter-sdk/stores).

If a query throws, the components that watch it show the error.
