Skip to content

Commit

Permalink
don't contradict urself
Browse files Browse the repository at this point in the history
  • Loading branch information
mmkal committed Mar 13, 2024
1 parent 5af8ca4 commit e678fae
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 30 deletions.
20 changes: 4 additions & 16 deletions packages/client/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ Note that @pgkit/migra and @pgkit/schemainspect are pure ports of their Python e
- [sql.join](#sqljoin)
- [sql.fragment](#sqlfragment)
- [nested `sql` tag](#nested-sql-tag)
- [sql.fragment](#sqlfragment-1)
- [sql.interval](#sqlinterval)
- [sql.binary](#sqlbinary)
- [sql.json](#sqljson)
Expand Down Expand Up @@ -274,9 +273,9 @@ expect(result).toEqual({id: 100, name: 'one hundred'})
Use `sql.fragment` to build reusable pieces which can be plugged into full queries.

```typescript
const idGreaterThan = (id: number) => sql.fragment`id >= ${id}`
const idGreaterThan = (id: number) => sql.fragment`id > ${id}`
const result = await client.any(sql`
select * from usage_test where ${idGreaterThan(2)}
select * from usage_test where ${idGreaterThan(1)}
`)

expect(result).toEqual([
Expand All @@ -290,9 +289,9 @@ expect(result).toEqual([
You can also use `` sql`...` `` to create a fragment of SQL, but it's recommended to use `sql.fragment` instead for explicitness. Support for [type-generation](https://npmjs.com/package/@pgkit/typegen) is better using `sql.fragment` too.

```typescript
const idGreaterThan = (id: number) => sql`id >= ${id}`
const idGreaterThan = (id: number) => sql`id > ${id}`
const result = await client.any(sql`
select * from usage_test where ${idGreaterThan(2)}
select * from usage_test where ${idGreaterThan(1)}
`)

expect(result).toEqual([
Expand All @@ -301,17 +300,6 @@ expect(result).toEqual([
])
```

### sql.fragment

Lets you create reusable SQL fragments, for example a where clause. Note that right now, fragments do not allow parameters.

```typescript
const condition = sql.fragment`id = 1`

const result = await client.one(sql`select * from usage_test where ${condition}`)
expect(result).toEqual({id: 1, name: 'one'})
```

### sql.interval

A strongly typed helper for creating a PostgreSQL interval. Note that you could also do something like `'1 day'::interval`, but this way avoids a cast and offers typescript types.
Expand Down
18 changes: 4 additions & 14 deletions packages/client/test/api-usage.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,9 @@ test('sql.join', async () => {
* Use `sql.fragment` to build reusable pieces which can be plugged into full queries.
*/
test('sql.fragment', async () => {
const idGreaterThan = (id: number) => sql.fragment`id >= ${id}`
const idGreaterThan = (id: number) => sql.fragment`id > ${id}`
const result = await client.any(sql`
select * from usage_test where ${idGreaterThan(2)}
select * from usage_test where ${idGreaterThan(1)}
`)

expect(result).toEqual([
Expand All @@ -104,9 +104,9 @@ test('sql.fragment', async () => {
* Support for [type-generation](https://npmjs.com/package/@pgkit/typegen) is better using `sql.fragment` too.
*/
test('nested `sql` tag', async () => {
const idGreaterThan = (id: number) => sql`id >= ${id}`
const idGreaterThan = (id: number) => sql`id > ${id}`
const result = await client.any(sql`
select * from usage_test where ${idGreaterThan(2)}
select * from usage_test where ${idGreaterThan(1)}
`)

expect(result).toEqual([
Expand All @@ -115,16 +115,6 @@ test('nested `sql` tag', async () => {
])
})

/**
* Lets you create reusable SQL fragments, for example a where clause. Note that right now, fragments do not allow parameters.
*/
test('sql.fragment', async () => {
const condition = sql.fragment`id = 1`

const result = await client.one(sql`select * from usage_test where ${condition}`)
expect(result).toEqual({id: 1, name: 'one'})
})

/**
* A strongly typed helper for creating a PostgreSQL interval. Note that you could also do something like `'1 day'::interval`, but this way avoids a cast and offers typescript types.
*/
Expand Down

0 comments on commit e678fae

Please sign in to comment.