# Base

**Kind:** Class

Model class representing a base.

If you want the base model to automatically recalculate whenever the base schema changes, try the
`useBase` hook. Alternatively, you can manually subscribe to changes with
`useWatchable` (recommended) or [Base#watch](https://airtable.com/developers/extensions/api/models/base.md#watch).

```js
import {base} from '@airtable/blocks';

console.log('The name of your base is', base.name);
```

## Properties

### `activeCollaborators`

Type: `Array<CollaboratorData>`

The users who have access to this base.

```js
import {base} from '@airtable/blocks';
console.log(base.activeCollaborators[0].email);
```

### `color`

Type: `string`

The color of the base.

```js
import {base} from '@airtable/blocks';
import {Box} from '@airtable/blocks/ui';
const exampleBox = <Box backgroundColor={base.color}> This box's background is the same color as the base background</Box>
```

### `id`

Type: `string`

The ID for this model.

### `isDeleted`

Type: `boolean`

`true` if the model has been deleted, and `false` otherwise.

In general, it's best to avoid keeping a reference to an object past the
current event loop, since it may be deleted and trying to access any data
of a deleted object (other than its ID) will throw. But if you keep a
reference, you can use `isDeleted` to check that it's safe to access the
model's data.

### `name`

Type: `string`

The name of the base.

```js
import {base} from '@airtable/blocks';
console.log('The name of your base is', base.name);
```

### `tables`

Type: `Array<Table>`

The tables in this base. Can be watched to know when tables are created, deleted, or reordered in the base.

```js
import {base} from '@airtable/blocks';
console.log(`You have ${base.tables.length} tables`);
```

### `workspaceId`

Type: `string`

The workspace id of the base.

```js
import {base} from '@airtable/blocks';
console.log('The workspace id of your base is', base.workspaceId);
```

## Methods

### `checkPermissionsForCreateTable(name?, fields?)`

Checks whether the current user has permission to create a table.

Accepts partial input, in the same format as `createTableAsync`.

Returns `{hasPermission: true}` if the current user can update the specified record,
`{hasPermission: false, reasonDisplayString: string}` otherwise. `reasonDisplayString` may be
used to display an error message to the user.

**Parameters:**
- `name?` (`undefined | string`) — name for the table. must be case-insensitive unique
- `fields?` (`Array<object>`) — array of fields to create in the table

**Returns:** `PermissionCheckResult`

```js
const createTableCheckResult = base.checkPermissionsForCreateTable();

if (!createTableCheckResult.hasPermission) {
    alert(createTableCheckResult.reasonDisplayString);
}
```

### `createTableAsync(name, fields)`

Creates a new table.

Throws an error if the user does not have permission to create a table, if an invalid
table name is provided, or if invalid fields are provided (invalid name, type, options or
description).

Refer to `FieldType` for supported field types, the write format for field options, and
other specifics for certain field types.

At least one field must be specified. The first field in the `fields` array will be used as
the table's [primary field](https://support.airtable.com/hc/en-us/articles/202624179-The-Name-Field)
and must be a supported primary field type. Fields must have case-insensitive unique names
within the table.

A default grid view will be created with all fields visible.

This action is asynchronous. Unlike new records, new tables are **not** created
optimistically locally. You must `await` the returned promise before using the new
table in your extension.

**Parameters:**
- `name` (`string`) — name for the table. must be case-insensitive unique
- `fields` (`Array<object>`) — array of fields to create in the table: see below for an example. `name` and

**Returns:** `Promise<Table>`

```js
async function createNewTable() {
    const name = 'My new table';
    const fields = [
        // Name will be the primary field of the table.
        {name: 'Name', type: FieldType.SINGLE_LINE_TEXT, description: 'This is the primary field'},
        {name: 'Notes', type: FieldType.RICH_TEXT},
        {name: 'Attachments', type: FieldType.MULTIPLE_ATTACHMENTS},
        {name: 'Number', type: FieldType.NUMBER, options: {
            precision: 8,
        }},
        {name: 'Select', type: FieldType.SINGLE_SELECT, options: {
            choices: [
                {name: 'A'},
                {name: 'B'},
            ],
        }},
    ];

    if (base.hasPermissionToCreateTable(name, fields)) {
        await base.createTableAsync(name, fields);
    }
}
```

### `getCollaborator(idOrNameOrEmail)`

The user matching the given ID, name, or email address. Throws if that user does not exist
or does not have access to this base. Use `getCollaboratorIfExists` instead if you are
unsure whether a collaborator with the given ID exists and has access to this base.

This method is convenient when building an extension for a specific base, but for more generic
extensions the best practice is to use the `getCollaboratorById` method instead.

**Parameters:**
- `idOrNameOrEmail` (`UserId | string`)

**Returns:** `CollaboratorData | null`

### `getCollaboratorById(collaboratorId)`

The user matching the given ID. Throws if that user does not exist
or does not have access to this base. Use `getCollaboratorByIdIfExists`
instead if you are unsure whether a collaborator with the given ID exists
and has access to this base.

**Parameters:**
- `collaboratorId` (`UserId`) — The ID of the user.

**Returns:** `CollaboratorData`

### `getCollaboratorByIdIfExists(collaboratorId)`

The user matching the given ID, or `null` if that user does not exist or does not have access
to this base.

**Parameters:**
- `collaboratorId` (`UserId`) — The ID of the user.

**Returns:** `CollaboratorData | null`

### `getCollaboratorIfExists(idOrNameOrEmail)`

The user matching the given ID, name, or email address. Returns null if that user does not
exist or does not have access to this base.

This method is convenient when building an extension for a specific base, but for more generic
extensions the best practice is to use the `getCollaboratorByIdIfExists` method instead.

**Parameters:**
- `idOrNameOrEmail` (`UserId | string`)

**Returns:** `CollaboratorData | null`

### `getMaxRecordsPerTable()`

Returns the maximum number of records allowed in each table of this base.

**Returns:** `number`

### `getTable(tableIdOrName)`

The table matching the given ID or name. Throws if no matching table exists within this base.
Use `getTableIfExists` instead if you are unsure whether a table exists with the given
name/ID.

This method is convenient when building an extension for a specific base, but for more generic
extensions the best practice is to use the `getTableById` or `getTableByName` methods
instead.

**Parameters:**
- `tableIdOrName` (`TableId | string`) — The ID or name of the table you're looking for.

**Returns:** `Table`

### `getTableById(tableId)`

The table matching the given ID. Throws if that table does not exist in this base. Use
`getTableByIdIfExists` instead if you are unsure whether a table exists with the given
ID.

**Parameters:**
- `tableId` (`string`) — The ID of the table.

**Returns:** `Table`

### `getTableByIdIfExists(tableId)`

The table matching the given ID, or `null` if that table does not exist in this base.

**Parameters:**
- `tableId` (`string`) — The ID of the table.

**Returns:** `Table | null`

### `getTableByName(tableName)`

The table matching the given name. Throws if no table exists with that name in this base. Use
`getTableByNameIfExists` instead if you are unsure whether a table exists with the
given name.

**Parameters:**
- `tableName` (`string`) — The name of the table you're looking for.

**Returns:** `Table`

### `getTableByNameIfExists(tableName)`

The table matching the given name, or `null` if no table exists with that name in this base.

**Parameters:**
- `tableName` (`string`) — The name of the table you're looking for.

**Returns:** `Table | null`

### `getTableIfExists(tableIdOrName)`

The table matching the given ID or name. Returns `null` if no matching table exists within
this base.

This method is convenient when building an extension for a specific base, but for more generic
extensions the best practice is to use the `getTableByIdIfExists` or
`getTableByNameIfExists` methods instead.

**Parameters:**
- `tableIdOrName` (`TableId | string`) — The ID or name of the table you're looking for.

**Returns:** `Table | null`

### `hasPermissionToCreateTable(name?, fields?)`

An alias for `checkPermissionsForCreateTable(name, fields).hasPermission`.

Checks whether the current user has permission to create a table.

Accepts partial input, in the same format as `createTableAsync`.

**Parameters:**
- `name?` (`undefined | string`) — name for the table. must be case-insensitive unique
- `fields?` (`Array<object>`) — array of fields to create in the table

**Returns:** `boolean`

```js
const canCreateTable = table.hasPermissionToCreateTable();

if (!canCreateTable) {
    alert('not allowed!');
}
```

### `toString()`

A string representation of the model for use in debugging.

**Returns:** `string`

### `unwatch(keys, callback, context?)`

Unwatch keys watched with `.watch`.

Should be called with the same arguments given to `.watch`.

Returns the array of keys that were unwatched.

**Parameters:**
- `keys` (`WatchableBaseKey | ReadonlyArray<WatchableBaseKey>`) — the keys to unwatch
- `callback` (`object`) — the function passed to `.watch` for these keys
- `context?` (`FlowAnyObject | null`) — the context that was passed to `.watch` for this `callback`

**Returns:** `Array<WatchableBaseKey>`

### `watch(keys, callback, context?)`

Get notified of changes to the model.

Every call to `.watch` should have a matching call to `.unwatch`.

Returns the array of keys that were watched.

**Parameters:**
- `keys` (`WatchableBaseKey | ReadonlyArray<WatchableBaseKey>`) — the keys to watch
- `callback` (`object`) — a function to call when those keys change
- `context?` (`FlowAnyObject | null`) — an optional context for `this` in `callback`.

**Returns:** `Array<WatchableBaseKey>`
