# Server client - TypeScript

> agdb example: managing users and databases and running queries on agdb_server from TypeScript

## What it shows

The TypeScript version of the [Rust server client](/docs/examples/server-client-rust) example using the `@agnesoft/agdb_api` package: it logs in as the admin, creates a user and a database and runs two batches of queries. Plain JavaScript objects are inserted as node values and the result of the first batch is passed directly to the second one.

## Run it

From the repository root:

```bash
pnpm run examples --filter @agnesoft/agdb_api
```

The script compiles the example, builds the `agdb_server` and runs the example against a freshly started server, so you do not need to start one yourself. The port 3000 must be free (stop any `agdb_server` you started yourself).

## Users and databases

The TypeScript client takes named arguments as objects:

```ts
const client = AgdbApi.new(new FetchClient(), "localhost:3000");

// Log in as admin and create a new user.
await client.user_login({ username: "admin", password: "admin" });
await client.admin_user_add({
  username: "client",
  password: "password111",
});

// Switch to the new user and create a database.
await client.user_login({ username: "client", password: "password111" });
await client.db_add({ owner: "client", db: "db", db_type: "memory" });
```

## Running queries

```ts
const users = [
  { username: "user1", password: "password123" },
  { username: "user2", password: "password456" },
];

// Insert a root "users" alias and the user nodes.
let queries = [
  QueryBuilder.insert().nodes().aliases(["users"]).query(),
  QueryBuilder.insert().nodes().values(users).query(),
];
let results = (
  await client.db_exec_mut({ owner: "client", db: "db", queries })
)[1];

// Link users to root and search for one by username.
queries = [
  QueryBuilder.insert().edges().from("users").to(results[1]).query(),
  QueryBuilder.select()
    .search()
    .depth_first()
    .from("users")
    .where()
    .key("username")
    .value("user1")
    .query(),
];
results = (await client.db_exec_mut({ owner: "client", db: "db", queries }))[1];

console.log(`User: ${results[1].elements[0].id}`);
for (const { key, value } of results[1].elements[0].values) {
  console.log(`  ${JSON.stringify(key)}: ${JSON.stringify(value)}`);
}
```

Note that `where_()` from Rust is `where()` in TypeScript.

## Learn more

- [TypeScript client](/api-docs/typescript) - installing and using `@agnesoft/agdb_api` in your project.
- [Server reference](/docs/references/server)

<style>

html pre.shiki code .sbgvK, html code.shiki .sbgvK{--shiki-light:#E2931D;--shiki-default:#6F42C1;--shiki-dark:#B392F0}html pre.shiki code .s_sjI, html code.shiki .s_sjI{--shiki-light:#91B859;--shiki-default:#032F62;--shiki-dark:#9ECBFF}html pre.shiki code .stzsN, html code.shiki .stzsN{--shiki-light:#91B859;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html .light .shiki span {color: var(--shiki-light);background: var(--shiki-light-bg);font-style: var(--shiki-light-font-style);font-weight: var(--shiki-light-font-weight);text-decoration: var(--shiki-light-text-decoration);}html.light .shiki span {color: var(--shiki-light);background: var(--shiki-light-bg);font-style: var(--shiki-light-font-style);font-weight: var(--shiki-light-font-weight);text-decoration: var(--shiki-light-text-decoration);}html .default .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html.dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html pre.shiki code .sbsja, html code.shiki .sbsja{--shiki-light:#9C3EDA;--shiki-default:#D73A49;--shiki-dark:#F97583}html pre.shiki code .s_hVV, html code.shiki .s_hVV{--shiki-light:#90A4AE;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .smGrS, html code.shiki .smGrS{--shiki-light:#39ADB5;--shiki-default:#D73A49;--shiki-dark:#F97583}html pre.shiki code .su5hD, html code.shiki .su5hD{--shiki-light:#90A4AE;--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .sP7_E, html code.shiki .sP7_E{--shiki-light:#39ADB5;--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .sGLFI, html code.shiki .sGLFI{--shiki-light:#6182B8;--shiki-default:#6F42C1;--shiki-dark:#B392F0}html pre.shiki code .sjJ54, html code.shiki .sjJ54{--shiki-light:#39ADB5;--shiki-default:#032F62;--shiki-dark:#9ECBFF}html pre.shiki code .sutJx, html code.shiki .sutJx{--shiki-light:#90A4AE;--shiki-light-font-style:italic;--shiki-default:#6A737D;--shiki-default-font-style:inherit;--shiki-dark:#6A737D;--shiki-dark-font-style:inherit}html pre.shiki code .sVHd0, html code.shiki .sVHd0{--shiki-light:#39ADB5;--shiki-light-font-style:italic;--shiki-default:#D73A49;--shiki-default-font-style:inherit;--shiki-dark:#F97583;--shiki-dark-font-style:inherit}html pre.shiki code .skxfh, html code.shiki .skxfh{--shiki-light:#E53935;--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .sfCm-, html code.shiki .sfCm-{--shiki-light:#90A4AE;--shiki-default:#6F42C1;--shiki-dark:#B392F0}html pre.shiki code .srdBf, html code.shiki .srdBf{--shiki-light:#F76D47;--shiki-default:#005CC5;--shiki-dark:#79B8FF}html pre.shiki code .sfo-9, html code.shiki .sfo-9{--shiki-light:#90A4AE;--shiki-default:#032F62;--shiki-dark:#9ECBFF}

</style>

---

- [View source](https://github.com/agnesoft/agdb/tree/main/agdb_api/typescript/examples/server_client)
