---
title: Quickstart
description: Set up a module, run one query, and test its result.
---

Run and test a query without a database.

## 1. Create a project

In a terminal, run these commands.

```bash
mkdir booking-demo
cd booking-demo
bun init -y
bun add logic-sdk zod
```

Your `package.json` lists `logic-sdk` and `zod`.

## 2. Define a module

Create `logic.ts`. It declares the scope type once for the whole app.

```ts twoslash
import { init } from "logic-sdk";

export const { query, createRegistry } = init<{ scope: { currency: string } }>();
```

Create `pricing.ts`.

```ts twoslash
// @filename: logic.ts
import { init } from "logic-sdk";
export const { query, createRegistry } = init<{ scope: { currency: string } }>();
// @filename: pricing.ts
// ---cut---
import { z } from "zod";
import { query } from "./logic";

/** Price a stay at a fixed nightly rate. */
export const priceStay = query({
  input: z.object({ nights: z.number().int().positive() }),
  async handler(ctx, { nights }) {
    return { total: nights * 100, currency: ctx.scope.currency };
  },
});
```

Your module exports `priceStay`. Its input requires a positive whole number of nights.

## 3. Run the query

Create `registry.ts` to register the module.

```ts twoslash
// @filename: logic.ts
import { init } from "logic-sdk";
export const { query, createRegistry } = init<{ scope: { currency: string } }>();
// @filename: pricing.ts
import { z } from "zod";
import { query } from "./logic";
export const priceStay = query({
  input: z.object({ nights: z.number().int().positive() }),
  async handler(ctx, { nights }) {
    return { total: nights * 100, currency: ctx.scope.currency };
  },
});
// @filename: registry.ts
// ---cut---
import { createRegistry } from "./logic";
import * as pricing from "./pricing";

export const registry = createRegistry({ modules: { pricing } });
```

Replace `index.ts`.

```ts twoslash
// @filename: logic.ts
import { init } from "logic-sdk";
export const { query, createRegistry } = init<{ scope: { currency: string } }>();
// @filename: pricing.ts
import { z } from "zod";
import { query } from "./logic";
export const priceStay = query({
  input: z.object({ nights: z.number().int().positive() }),
  async handler(ctx, { nights }) {
    return { total: nights * 100, currency: ctx.scope.currency };
  },
});
// @filename: registry.ts
import { createRegistry } from "./logic";
import * as pricing from "./pricing";
export const registry = createRegistry({ modules: { pricing } });
// @filename: index.ts
// ---cut---
import { priceStay } from "./pricing";
import { registry } from "./registry";

const caller = registry.as({ currency: "EUR" });
const price = await caller.run(priceStay, { nights: 3 });
console.log(JSON.stringify(price));
```

Run `bun index.ts`. The terminal prints `{"total":300,"currency":"EUR"}`.

- **Module key.** The key `pricing` gives the query its ID, `pricing.priceStay`.
- **Scope.** `registry.as` sets the currency that reaches the query as `ctx.scope.currency`.

## 4. Test the query

Install Vitest.

```bash
bun add -d vitest
```

Create `pricing.test.ts`.

```ts twoslash
// @filename: logic.ts
import { init } from "logic-sdk";
export const { query, createRegistry } = init<{ scope: { currency: string } }>();
// @filename: pricing.ts
import { z } from "zod";
import { query } from "./logic";
export const priceStay = query({
  input: z.object({ nights: z.number().int().positive() }),
  async handler(ctx, { nights }) {
    return { total: nights * 100, currency: ctx.scope.currency };
  },
});
// @filename: registry.ts
import { createRegistry } from "./logic";
import * as pricing from "./pricing";
export const registry = createRegistry({ modules: { pricing } });
// @filename: pricing.test.ts
// ---cut---
import { afterEach, beforeEach, expect, test } from "vitest";
import { createTestRegistry } from "logic-sdk/testing";
import { registry } from "./registry";
import { priceStay } from "./pricing";

const t = createTestRegistry(registry);
beforeEach(() => t.begin());
afterEach(() => t.rollback());

test("prices three nights in euros", async () => {
  const caller = t.as({ currency: "EUR" });
  const price = await caller.run(priceStay, { nights: 3 });
  expect(price).toEqual({ total: 300, currency: "EUR" });
});
```

Run `bunx vitest run`. The terminal reports one passing test.

## See also

- [Functions](/docs/concepts/functions)
- [Testing](/docs/guides/testing)
- [Examples](/docs/examples)
