Skip to content

Commit

Permalink
docs: add usage section about prettier-plugin-embed (#327)
Browse files Browse the repository at this point in the history
  • Loading branch information
karlhorky authored Dec 9, 2023
1 parent bd624c2 commit 1272f18
Showing 1 changed file with 57 additions and 0 deletions.
57 changes: 57 additions & 0 deletions packages/sql/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,63 @@ npx prettier --write db.sql
yarn prettier --write db.sql
```

### SQL-in-JS with `prettier-plugin-embed`

Format SQL-in-JS tagged template literals by installing [`prettier-plugin-embed`](https://github.com/Sec-ant/prettier-plugin-embed) and configuring as follows:

`prettier.config.mjs`

```js
/** @type {import('prettier').Config} */
const prettierConfig = {
plugins: ['prettier-plugin-embed', 'prettier-plugin-sql'],
}

/** @type {import('prettier-plugin-embed').PrettierPluginEmbedOptions} */
const prettierPluginEmbedConfig = {
embeddedSqlIdentifiers: ['sql'],
}

/** @type {import('prettier-plugin-sql').SqlBaseOptions} */
const prettierPluginSqlConfig = {
language: 'postgresql',
keywordCase: 'upper',
}

const config = {
...prettierConfig,
...prettierPluginEmbedConfig,
...prettierPluginSqlConfig,
}

export default config
```

Before formatting:

```ts
const animals = await sql`
sELect first_name, species froM
animals
WhERE
id = ${id}
`
```

After formatting:

```ts
const animals = await sql`
SELECT
first_name,
species
FROM
animals
WHERE
id = ${id}
`
```

## Parser Options

```ts
Expand Down

0 comments on commit 1272f18

Please sign in to comment.