# Database

> Databases: SQL for an app or an agent, SQLite by default and Postgres when you outgrow it.

```python
db = noclick.Database.from_name("orders", create_if_missing=True)
db.migrate("migrations/")
db.execute("insert into orders (id, item, qty) values (?, ?, ?)", ["o1", "bun", 3])
for row in db.query("select item, qty from orders where qty > ?", [2]):
    print(row["item"], row["qty"])
```

The same object serves an app (bound to a project) and an agent keeping its own
state. Placeholders are `?` with a list, or `:name` with a dict, on either engine.

## Database

```typescript
class Database
```

A SQL database: SQLite (the default) or Postgres. `query` reads, `execute` and `batch` write,
`migrate` applies your schema. `Database.fromName("orders", { createIfMissing: true })` is lazy.
In an agent's or a toolkit's `tools` it is three tools, `{name}__query`, `{name}__execute` and
`{name}__schema`; `only(...)` picks some of them.

| Attribute | Type | Description |
| --- | --- | --- |
| `id` | `string` |  |
| `name` | `string` |  |
| `engine` | `DatabaseEngine` | `"sqlite"` or `"postgres"`. |
| `status` | `DatabaseStatus` | `ready`, `restoring` or `branching`. |
| `sizeBytes` | `number | null` |  |
| `tableCount` | `number | null` |  |
| `parentId` | `string | null` | The database a branch was made from (null if it isn't one). |
| `usedBy` | `UsedBy[]` | Who uses it: apps that bind it (`kind` `project`, with the binding name) and agents with it in their tools (`kind` `agent`). |
| `region` | `RegionName | null` | The region it keeps its data in, fixed when it was created. |
| `userId` | `string | null` |  |
| `workflow` | `{ id: string; name: string | null } | null` | The automation whose tables it holds (NoClick makes one for each automation that keeps tables), or null. |
| `createdAt` | `Date | null` |  |
| `consoleUrl` | `string | null` | This database in the developer console (null off the hosted API). |

Types:

```typescript
type DatabaseEngine = "sqlite" | "postgres";

type DatabaseStatus = "ready" | "restoring" | "branching";

/** An app that binds a database or a bucket, under its `binding` name. */
interface UsedBy {
  kind: string;
  id: string;
  name: string;
  binding: string | null;
}

type RegionName = "us-east" | "us-west" | "eu" | "ap" | "in";
```

### Database.only

```typescript
only(...operations: DatabaseTool[]): DatabaseOperations
```

A tool ref to some of its operations, for `tools`: `"query"` (a read-only statement),
`"execute"` (a statement that writes) and `"schema"` (its tables and columns). The database
itself in `tools` gives all three. `"upsert"` writes one row of one table by its primary key,
its parameters that table's columns; bind its table with `.bind({ table })`.

Parameters:

- `operations` (`DatabaseTool[]`, required)

Returns: A ref to just those operations, to list in `tools=`; `bind(...)` fixes arguments.

Types:

```typescript
/** `upsert` writes one row of the table bound to it; only `only(...)` gives it. */
type DatabaseTool = (typeof DATABASE_TOOLS)[number] | "upsert";
```

### Database.create

```typescript
static create(
  name: string,
  options?: DatabaseCreateOptions & AllowExisting,
): Promise<Database>
```

Create a database, unique by name per user (a taken name throws `Conflict` with `existingId`, or with
`allowExisting: true` returns the database already there, data and all).

Parameters:

- `name` (`string`, required)
- `options.engine` (`DatabaseEngine`, default `"sqlite"`): `"sqlite"` (the default, cheap while idle) or `"postgres"`.
- `options.region` (`RegionName`): Where it runs or keeps its data (`regions()`); omitted, your account's default region.
- `options.allowExisting` (`boolean`): A taken name returns the object already there, as it is: nothing about it changes.
- `options.user` (`UserOption`, default: the current user): `undefined` = the ambient user (`asUser`), `null` = the account itself.
- `options.client` (`NoClick`): The client to send with; defaults to the module client built from `NOCLICK_API_KEY`.

Returns: The new database, `ready` and empty.

Types:

```typescript
type DatabaseEngine = "sqlite" | "postgres";

/** `undefined` = the ambient user (from `asUser`), `null` = the account itself. */
type UserOption = UserRef | null | undefined;

/** A `User` or your own id for the user. */
type UserRef = string | UserLike;

/** Anything that can name a user lazily (a `User`). */
interface UserLike {
  _userId(): Promise<string>;
}

type RegionName = "us-east" | "us-west" | "eu" | "ap" | "in";
```

### Database.fromName

```typescript
static fromName(
  name: string,
  options?: DatabaseCreateOptions & {
    createIfMissing?: boolean;
    branchData?: BranchData;
  },
): Database
```

A lazy reference to the database called `name` for the current (or given) user. With
`createIfMissing` it's created on first use with `engine`; one that exists is used as it is.

In a preview branch of an app whose `preview_resources` is `branch`, the branch gets its own copy
at its first write and reads production's as it is until then. A Postgres copy is a copy-on-write
branch of production's data; a SQLite copy holds the schema and migrations only (`migrate`'s `seed`
fills it), unless `branchData: "copy"` asks for production's rows too, read at one moment while
production keeps serving (up to 200,000 rows and 16 MB). It changes nothing outside a preview.

Parameters:

- `name` (`string`, required)
- `options.createIfMissing` (`boolean`)
- `options.engine` (`DatabaseEngine`, default `"sqlite"`): `"sqlite"` (the default, cheap while idle) or `"postgres"`.
- `options.region` (`RegionName`): Where it runs or keeps its data (`regions()`); omitted, your account's default region.
- `options.branchData` (`BranchData`)
- `options.user` (`UserOption`, default: the current user): `undefined` = the ambient user (`asUser`), `null` = the account itself.
- `options.client` (`NoClick`): The client to send with; defaults to the module client built from `NOCLICK_API_KEY`.

Returns: A database that loads (or is created) the first time it's used. Nothing is sent yet.

Types:

```typescript
type DatabaseEngine = "sqlite" | "postgres";

/** `undefined` = the ambient user (from `asUser`), `null` = the account itself. */
type UserOption = UserRef | null | undefined;

/** A `User` or your own id for the user. */
type UserRef = string | UserLike;

/** Anything that can name a user lazily (a `User`). */
interface UserLike {
  _userId(): Promise<string>;
}

type RegionName = "us-east" | "us-west" | "eu" | "ap" | "in";

/** What a preview branch's copy of a SQLite database holds besides its schema: production's rows too. */
type BranchData = "copy";
```

### Database.fromId

```typescript
static fromId(databaseId: string, options?: CallOptions): Database
```

A lazy reference to a database by id.

Parameters:

- `databaseId` (`string`, required)
- `options.client` (`NoClick`): The client to send with; defaults to the module client built from `NOCLICK_API_KEY`.
- `options.user` (`UserOption`, default: the current user): `undefined` = the ambient user (`asUser`), `null` = the account itself.

Returns: A database that loads the first time you read a field. Nothing is sent yet.

Types:

```typescript
/** `undefined` = the ambient user (from `asUser`), `null` = the account itself. */
type UserOption = UserRef | null | undefined;

/** A `User` or your own id for the user. */
type UserRef = string | UserLike;

/** Anything that can name a user lazily (a `User`). */
interface UserLike {
  _userId(): Promise<string>;
}
```

### Database.get

```typescript
static get(databaseId: string, options?: CallOptions): Promise<Database>
```

Load a database by id now; an unknown id raises `noclick.NotFound`.

Parameters:

- `databaseId` (`string`, required)
- `options.user` (`UserOption`, default: the current user): `undefined` = the ambient user (`asUser`), `null` = the account itself.
- `options.client` (`NoClick`): The client to send with; defaults to the module client built from `NOCLICK_API_KEY`.

Returns: The database, loaded: its engine, size, table count and status.

Types:

```typescript
/** `undefined` = the ambient user (from `asUser`), `null` = the account itself. */
type UserOption = UserRef | null | undefined;

/** A `User` or your own id for the user. */
type UserRef = string | UserLike;

/** Anything that can name a user lazily (a `User`). */
interface UserLike {
  _userId(): Promise<string>;
}
```

### Database.list

```typescript
static list(
  options?: ReadOptions & { engine?: DatabaseEngine; limit?: number },
): Promise<Database[]>
```

The current user's databases, branches included (or those `userFilter` covers).

Parameters:

- `options.engine` (`DatabaseEngine`)
- `options.limit` (`number`): Return at most this many.
- `options.userFilter` (`UserFilter`): Which users the read covers (`user_id` query), instead of acting as one (`user`).
- `options.user` (`UserOption`, default: the current user): `undefined` = the ambient user (`asUser`), `null` = the account itself.
- `options.client` (`NoClick`): The client to send with; defaults to the module client built from `NOCLICK_API_KEY`.

Returns: Every matching database, newest first, fetched page by page.

Types:

```typescript
/**
 * Which users a read covers: `ALL_USERS`, `NO_USER`, a `User` or your id for one. Sent as the
 * `user_id` query parameter, never as the `NoClick-User` header, so it filters without acting as the user.
 */
type UserFilter = typeof ALL_USERS | typeof NO_USER | UserRef;

/** A `User` or your own id for the user. */
type UserRef = string | UserLike;

/** Anything that can name a user lazily (a `User`). */
interface UserLike {
  _userId(): Promise<string>;
}

/** `undefined` = the ambient user (from `asUser`), `null` = the account itself. */
type UserOption = UserRef | null | undefined;

type DatabaseEngine = "sqlite" | "postgres";
```

### Database.listPage

```typescript
static listPage(
  options?: ReadOptions & PageOptions & { engine?: DatabaseEngine },
): Promise<Page<Database>>
```

One page of databases; pass `nextCursor` back as `cursor`.

Parameters:

- `options.cursor` (`string | null`): The `next_cursor` of the page before; omit it for the first page.
- `options.limit` (`number`): Return at most this many.
- `options.engine` (`DatabaseEngine`)
- `options.userFilter` (`UserFilter`): Which users the read covers (`user_id` query), instead of acting as one (`user`).
- `options.user` (`UserOption`, default: the current user): `undefined` = the ambient user (`asUser`), `null` = the account itself.
- `options.client` (`NoClick`): The client to send with; defaults to the module client built from `NOCLICK_API_KEY`.

Returns: A page whose `data` holds the databases and whose `next_cursor` is `None` on the last page.

Types:

```typescript
/**
 * Which users a read covers: `ALL_USERS`, `NO_USER`, a `User` or your id for one. Sent as the
 * `user_id` query parameter, never as the `NoClick-User` header, so it filters without acting as the user.
 */
type UserFilter = typeof ALL_USERS | typeof NO_USER | UserRef;

/** A `User` or your own id for the user. */
type UserRef = string | UserLike;

/** Anything that can name a user lazily (a `User`). */
interface UserLike {
  _userId(): Promise<string>;
}

/** `undefined` = the ambient user (from `asUser`), `null` = the account itself. */
type UserOption = UserRef | null | undefined;

type DatabaseEngine = "sqlite" | "postgres";
```

### Database.query

```typescript
query(sql: string, params?: SqlParams): Promise<QueryResult>
```

Run a read: one `select` (or `with ... select`). It runs read-only: a statement that writes
throws `InvalidRequest`; use `execute` for those. At most 10 MB of rows; 30 seconds. On
Postgres, `noclick.embed(:q, model)` turns a parameter or a quoted text into a `halfvec`
before the statement runs (up to 100 distinct texts a request; not over `connectionUrl`), so a
read (an agent's `query` tool too) can search by meaning (billed as AI to the database's user).

    await kb.query("select id from articles order by embedding <=> noclick.embed(:q, :model) limit 5", { q, model });

Parameters:

- `sql` (`string`, required)
- `params` (`SqlParams`)

Returns: The rows as dicts, in `rows` or by iterating the result, with `columns`, `rows_read`
and `duration_ms`.

Types:

```typescript
/** `?` placeholders take an array, `:name` placeholders an object. */
type SqlParams = readonly unknown[] | Readonly<Record<string, unknown>>;
```

### Database.execute

```typescript
execute(sql: string, params?: SqlParams): Promise<QueryResult>
```

Run one statement that writes (several go in a `batch`). A SQLite database that would pass
10 GB throws `QuotaExceeded`, whose next step is branching into Postgres.

Parameters:

- `sql` (`string`, required)
- `params` (`SqlParams`)

Returns: What it did: `rows_written`, `last_row_id` for an insert on SQLite, and the `rows`
a `returning` clause gave back.

Types:

```typescript
/** `?` placeholders take an array, `:name` placeholders an object. */
type SqlParams = readonly unknown[] | Readonly<Record<string, unknown>>;
```

### Database.batch

```typescript
batch(statements: readonly SqlStatement[]): Promise<QueryResult[]>
```

Run up to 100 statements in one transaction: all apply or none do.

Parameters:

- `statements` (`readonly SqlStatement[]`, required)

Returns: One `QueryResult` per statement, in order.

Types:

```typescript
/** One statement of a batch: the SQL alone, or the SQL and its parameters. */
type SqlStatement = string | readonly [sql: string, params?: SqlParams];

/** `?` placeholders take an array, `:name` placeholders an object. */
type SqlParams = readonly unknown[] | Readonly<Record<string, unknown>>;
```

### Database.migrate

```typescript
migrate(
  migrations: Migrations,
  options?: { dryRun?: boolean; seed?: string },
): Promise<MigrationResult>
```

Apply the migrations the database hasn't run, in name order, in one transaction (a failing
one applies none). In Node, read your `migrations/*.sql` files into an object first.
`dryRun` reports what would run and changes nothing. `seed` is SQL run once, after them and in
their transaction, on a preview branch's database that holds none of production's data (a SQLite
copy made without its rows, or one only the branch has); production never runs it, and it shows
in `applied` as `(seed)`.

Parameters:

- `migrations` (`Migrations`, required)
- `options.dryRun` (`boolean`)
- `options.seed` (`string`)

Returns: The migrations applied now, and those still pending (all the new ones after a dry run).

Types:

```typescript
/** Migrations to apply: `{ "0001_init.sql": "create table ..." }`, or `{ name, sql }` items. */
type Migrations = Readonly<Record<string, string>> | readonly { name: string; sql: string }[];
```

### Database.migrations

```typescript
migrations(): Promise<Migration[]>
```

The migrations this database has applied, oldest first.

Returns: Each applied migration with when it ran, oldest first.

### Database.schema

```typescript
schema(): Promise<Table[]>
```

The database's tables, their columns and row counts.

Returns: Every table by name, each with its `columns` in order and its `row_count`.

### Database.restore

```typescript
restore(options: { at: Date | string }): Promise<Operation<Database>>
```

Put the database back as it was at `at` (within its history: 30 days on SQLite, 7 on
Postgres), in place. What it held just before is kept in the trash for 30 days as a database
copy (the operation's `keptCopyId`): `undo()` on the operation, or restoring that `TrashItem`,
puts it back. `wait()` resolves to this database, restored. To look at the past without
changing the present, `branch` at that time instead.

Parameters:

- `options.at` (`Date | string`, required)

Returns: An operation whose `wait()` gives this database, restored.

### Database.branch

```typescript
branch(
  name: string,
  options?: { at?: Date | string; engine?: DatabaseEngine },
): Promise<Operation<Database>>
```

Copy the database into a new one called `name`, as it is now or as it was `at` (within its
history). `engine: "postgres"` branches SQLite into Postgres. While a SQLite database is
copied its `status` is `branching`. `wait()` resolves to the new `Database`.

Parameters:

- `name` (`string`, required)
- `options.at` (`Date | string`)
- `options.engine` (`DatabaseEngine`)

Returns: An operation whose `wait()` gives the new `Database`.

Types:

```typescript
type DatabaseEngine = "sqlite" | "postgres";
```

### Database.branches

```typescript
branches(): Promise<Database[]>
```

The databases branched from this one.

Returns: Each branch as a `Database`, newest first.

### Database.connectionUrl

```typescript
connectionUrl(options?: { ttlS?: number; readOnly?: boolean }): Promise<string>
```

A `postgresql://` URL with a password that stops working after `ttlS` seconds (default
3600), when any session still open on it ends. SQLite throws `NotAvailable`. Treat it as a
secret.

Parameters:

- `options.ttlS` (`number`, default `3600`)
- `options.readOnly` (`boolean`)

Returns: A `postgresql://` URL with its short-lived credentials inside; treat it as a secret.

### Database.export

```typescript
export(): Promise<Operation<ExportBundle>>
```

Export as SQL that recreates its schema and rows in any database of its engine:
`sqlite3 booking.db < databases/booking-db.sql`, or a `pg_dump` script for `psql` (NoClick's own
`noclick` schema, owners and grants left out). Exporting a Postgres database wakes it, and its
compute is billed as any use is. `wait()` resolves to an `ExportBundle`: a zip at `url` holding the dump.

Returns: An operation whose `wait()` gives an `ExportBundle`: a zip at `url` holding the dump.

### Database.delete

```typescript
delete(): Promise<TrashItem>
```

Delete the database and its data: it goes to the trash, restorable for 30 days, and its
connection URLs stop working. One with branches or in an agent's tools throws `Conflict`. One an
app binds goes to the trash too: the app keeps its binding, reaching nothing until you restore
the database. Returns its `TrashItem`.

Returns: Its `TrashItem`: `restore()` puts it back, tables and rows with it.

## DatabaseOperations

```typescript
class DatabaseOperations
```

Some of a database's operations as tools (`db.only(...)`), with the arguments `bind(...)` fixed.

| Attribute | Type | Description |
| --- | --- | --- |
| `database` | `Database` |  |
| `operations` | `DatabaseTool[]` |  |
| `bindings` | `JsonObject` | The arguments NoClick fills on every call, out of the model's reach. |

Types:

```typescript
/** `upsert` writes one row of the table bound to it; only `only(...)` gives it. */
type DatabaseTool = (typeof DATABASE_TOOLS)[number] | "upsert";

type JsonObject = { [key: string]: unknown };
```

### DatabaseOperations.bind

```typescript
bind(args: JsonObject): DatabaseOperations
```

Fix arguments of these operations, as `ConnectionOperations.bind` does. `upsert` needs `table`
bound; bind a column too and every row it writes carries that value.

    leads.only("upsert").bind({ table: "enriched" })

Parameters:

- `args` (`JsonObject`, required)

Returns: A new ref with these arguments bound on top of any bound before.

Types:

```typescript
type JsonObject = { [key: string]: unknown };
```

## QueryResult

```typescript
class QueryResult
```

What one statement returned: `rows` (an object per row, keyed by `columns`), and what it
touched (on SQLite the rows read and written, what it's billed on; on Postgres the rows
returned and changed, not billed). Iterating it yields the rows. `lastRowId` is the rowid an
insert made on SQLite (null on Postgres, where `returning` gives it).

| Attribute | Type | Description |
| --- | --- | --- |
| `columns` | `string[]` |  |
| `rows` | `JsonObject[]` |  |
| `rowsRead` | `number` |  |
| `rowsWritten` | `number` |  |
| `lastRowId` | `number | null` |  |
| `durationMs` | `number | null` |  |
| `raw` | `JsonObject` | The response JSON exactly as the API sent it. |

Types:

```typescript
type JsonObject = { [key: string]: unknown };
```

### QueryResult.length

```typescript
length: number
```

## Migration

```typescript
interface Migration
```

A migration the database has applied, and the checksum of the SQL that ran.

| Attribute | Type | Description |
| --- | --- | --- |
| `name` | `string` |  |
| `appliedAt` | `Date | null` |  |
| `checksum` | `string | null` |  |
| `raw` | `JsonObject` | The response JSON exactly as the API sent it. |

Types:

```typescript
type JsonObject = { [key: string]: unknown };
```

## MigrationResult

```typescript
interface MigrationResult
```

What `migrate` did: what it ran now, and what's still to run (everything new after a dry run).

| Attribute | Type | Description |
| --- | --- | --- |
| `applied` | `string[]` |  |
| `pending` | `string[]` |  |
| `raw` | `JsonObject` | The response JSON exactly as the API sent it. |

Types:

```typescript
type JsonObject = { [key: string]: unknown };
```

## Table

```typescript
interface Table
```

A table: its columns in order and its row count (the planner's estimate on Postgres).

| Attribute | Type | Description |
| --- | --- | --- |
| `name` | `string` |  |
| `columns` | `Column[]` |  |
| `rowCount` | `number | null` |  |
| `raw` | `JsonObject` | The response JSON exactly as the API sent it. |

Types:

```typescript
type JsonObject = { [key: string]: unknown };
```

## Column

```typescript
interface Column
```

One column of a table. `default` is its default expression, or null.

| Attribute | Type | Description |
| --- | --- | --- |
| `name` | `string` |  |
| `type` | `string` |  |
| `nullable` | `boolean` |  |
| `primaryKey` | `boolean` |  |
| `default` | `string | null` |  |
