> ## Documentation Index
> Fetch the complete documentation index at: https://corebase-docs-new.trivyaa.in/llms.txt
> Use this file to discover all available pages before exploring further.

# Database

> Read, write, and manage your data.

Working with your database is simple. Use our chainable commands to find, add, update, and remove data.

## Read Data (Select)

### Get Everything

Fetch all rows from a table.

```typescript theme={null}
const { data, error, count } = await corebase
  .from('posts')
  .select('*');
```

### Get Specific Fields

Fetch only the columns you need.

```typescript theme={null}
const { data } = await corebase
  .from('users')
  .select('id, name, email');
```

### Filter Results

Find specific data (like `WHERE` in SQL).

```typescript theme={null}
const { data } = await corebase
  .from('posts')
  .select('*')
  .eq('status', 'published')
  .match({ author_id: 'user_123', category: 'tech' });
```

### Sort and Limit

Order your results and pick how many to show.

```typescript theme={null}
const { data, count } = await corebase
  .from('posts')
  .select('*')
  .order('created_at', { ascending: false }) // Newest first
  .limit(10)                                 // Only 10
  .page(1);                                  // Page 1
```

### Get One Record

Fetch a single specific row.

```typescript theme={null}
const { data: user, error } = await corebase
  .from('users')
  .select('*')
  .eq('id', 1)
  .single();
```

## Add Data (Insert)

Add new rows to your table.

<CodeGroup>
  ```typescript Add one row theme={null}
  const { data, error } = await corebase
    .from('posts')
    .insert({ 
      title: 'My New Post', 
      content: 'Hello World', 
      user_id: 'user_123' 
    });
  ```

  ```typescript Add multiple rows theme={null}
  const { data, error } = await corebase
    .from('posts')
    .insert([
      { title: 'Post 1', user_id: 'user_123' },
      { title: 'Post 2', user_id: 'user_123' }
    ]);
  ```
</CodeGroup>

## Update Data

Change existing data.

```typescript theme={null}
const { data, error } = await corebase
  .from('posts')
  .update({ status: 'archived' }) // Change this
  .eq('status', 'draft')          // For these rows
  .match({ user_id: 'user_123' });
```

## Remove Data (Delete)

Remove rows from your table.

```typescript theme={null}
const { data, error } = await corebase
  .from('posts')
  .delete()
  .eq('id', 123);
```

<Tip title="TypeScript Support">
  If you use TypeScript, you can define your data types to get autocomplete and error checking.

  ```typescript theme={null}
  // 1. Define your data shape
  interface Post {
    id: number;
    title: string;
    content: string;
  }

  // 2. Pass it to the .from() method
  const { data } = await corebase.from<Post>('posts').select('*');

  // data is now typed as Post[] | null
  ```
</Tip>

## Managing Tables (Schema)

Everything above works on row data. To define the shape of your tables, use `corebase.db` instead.

### Create a Table

```typescript theme={null}
const { data, error } = await corebase.db.createTable({
  table: 'posts',
  columns: [
    { name: 'id', type: 'text', primary: true },
    { name: 'title', type: 'text', notNull: true },
    {
      name: 'author_id',
      type: 'text',
      references: { table: 'users', column: 'id', onDelete: 'cascade' },
    },
  ],
  indexes: [{ columns: ['author_id'] }],
});
```

<Note>
  `rls` (row-level security expressions) can be attached to a table today, but the
  gateway does not enforce them yet — treat it as reserved for a future release, not a
  working access control mechanism.
</Note>

### List & Inspect Tables

```typescript theme={null}
const { data: tables } = await corebase.db.listTables();
const { data: schema } = await corebase.db.getTable('posts');
```

### Add, Rename, or Remove a Column

```typescript theme={null}
await corebase.db.addColumn('posts', { name: 'published', type: 'boolean' });
await corebase.db.renameColumn('posts', 'published', { name: 'is_published' });
await corebase.db.deleteColumn('posts', 'is_published');
```

### Add a Foreign Key

```typescript theme={null}
await corebase.db.addForeignKey('posts', {
  column: 'author_id',
  references: { table: 'users', column: 'id', onDelete: 'cascade' },
});
```

<Tip>
  Adding a foreign key rebuilds the table under the hood — batch schema changes
  together when you can, rather than making them one at a time.
</Tip>
