import CodeBlock from "@theme/CodeBlock";
import SqlDBExample from "@examples/chains/sql_db.ts";
import SqlDBSqlOutputExample from "@examples/chains/sql_db_sql_output.ts";
import SqlDBSqlCustomPromptExample from "@examples/chains/sql_db_custom_prompt.ts";

This example uses Chinook database, which is a sample database available for SQL Server, Oracle, MySQL, etc.

## Set up

First install `typeorm`:

```bash npm2yarn
npm install typeorm
```

Then install the dependencies needed for your database. For example, for SQLite:

```bash npm2yarn
npm install sqlite3
```

For other databases see https://typeorm.io/#installation

Finally follow the instructions on https://database.guide/2-sample-databases-sqlite/ to get the sample database for this example.

<CodeBlock language="typescript">{SqlDBExample}</CodeBlock>

You can include or exclude tables when creating the `SqlDatabase` object to help the chain focus on the tables you want.
It can also reduce the number of tokens used in the chain.

```typescript
const db = await SqlDatabase.fromDataSourceParams({
  appDataSource: datasource,
  includesTables: ["Track"],
});
```

If desired, you can return the used SQL command when calling the chain.

<CodeBlock language="typescript">{SqlDBSqlOutputExample}</CodeBlock>

## Custom prompt

You can also customize the prompt that is used. Here is an example prompting the model to understand that "foobar" is the same as the Employee table:

<CodeBlock language="typescript">{SqlDBSqlCustomPromptExample}</CodeBlock>
