electric-sql / electric-sql/pglite

How to pass an array parameter into a live query?

Open
#547 5 comments 0 reactions 1 assignee Claimed by @tdrz View on GitHub
question
Dominant language
TypeScript
Stars
16k
Forks
442
Avg merge
20h 19m
Merged PRs (30d)
7

Description

I have a useLiveQuery where I'm trying to use a `WHERE IN` clause. I can make it work by hacking in a list of dollar signs:

```ts
const placeholders = parentIds.map((_, i) => `$${i + 1}`).join(', ')
const results = useLiveQuery(`
SELECT value FROM items
WHERE parent_id IN (`${placeholders}`)
ORDER BY id
`, parentIds)
```

Although beware that you need more boilerplate if the `parentIds` can ever be empty, cough #544.

I can also do it by padding:

```ts
const paddedIds = [...parentIds, ...Array(10 - parentIds.length).fill(-1)]
const results = useLiveQuery(`
SELECT value FROM items
WHERE parent_id IN ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10)
ORDER BY id
`, paddedIds)
```

But is there / could there be a more ergonomic way that passes in the array as a single parameter?

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.