# pool-query

> A package that provides function to pool multiple queries that are run in certain intervals into one call, and distributes the result back into those functions.

Latest version **1.1.9** (published 2022-11-15) · MIT license · 0 weekly downloads

## Install

```sh
npm install pool-query
pnpm add pool-query
yarn add pool-query
bun add pool-query
```

## Health

**Score 30/100 (F)** — status: abandoned.

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.1.9 |
| Published | 2022-11-15 |
| First published | 2022-11-11 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 0 |
| Unpacked size | 20.8 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Teodorus Nathaniel |
| Maintainers | teodorus-nathaniel |
| Keywords | typescript, npm, package, react query, pool query, query pooling |

## Links

- npm: https://www.npmjs.com/package/pool-query
- Repository: https://github.com/teodorus-nathaniel/pool-query
- Issues: https://github.com/teodorus-nathaniel/pool-query/issues
- npm.io page: https://npm.io/package/pool-query

## Alternatives

- [@openai/codex-sdk](https://npm.io/package/@openai/codex-sdk.md) — 731.4K weekly downloads
- [babel-plugin-transform-react-jsx](https://npm.io/package/babel-plugin-transform-react-jsx.md) — 565.0K weekly downloads
- [babel-helper-remove-or-void](https://npm.io/package/babel-helper-remove-or-void.md) — 508.5K weekly downloads
- [@pnpm/store-controller-types](https://npm.io/package/@pnpm/store-controller-types.md) — 186.9K weekly downloads
- [react-native-signature-canvas](https://npm.io/package/react-native-signature-canvas.md) — 155.6K weekly downloads

## Recent versions

- 1.1.9 (latest) — 2022-11-15
- 1.1.8 — 2022-11-15
- 1.1.7 — 2022-11-15
- 1.1.6 — 2022-11-14
- 1.1.5 — 2022-11-14
- 1.1.4 — 2022-11-14
- 1.1.3 — 2022-11-14
- 1.1.2 — 2022-11-11
- 1.1.1 — 2022-11-11
- 1.1.0 — 2022-11-11
- 1.0.0 — 2022-11-11

## README

# pool-query

A package that provides function to pool multiple queries that are run in certain intervals into one call, and distributes the result back into those functions.

# Installation

```
npm i pool-query
# or
yarn add pool-query
```

# Usage

```
import poolQuery from 'pool-query'

type Param = { queryKey: [string, string] } // param types
type Result = { id: string, name: string } // awaited response types
const pooledFunction = poolQuery<Param, Result>({
  singleCall: (param) => fetch('URL TO FETCH', { id: param.id }).then(data => data.json())
  multiCall: (params) => fetch('URL TO FETCH (for multiple ids)', { ids: params.map(({ id }) => id) }).then(data => data.json())
})

function main() {
  const data5 = await pooledFunction({ id: '5' })
  const data10 = await pooledFunction({ id: '10' })
  const data15 = await pooledFunction({ id: '15' })

  console.log({ data5, data10, data15 })
  // all data are distributed, but it only results in 1 fetch call (the `multiCall` function)
}
main()
```

## Integration with react-query

```
// App.tsx
export default function App() {
  return (
    <>
      <Pokemon name='arceus' />
      <Pokemon name='dialga' />
      <Pokemon name='palkia' />
    </>
  )
}

// Pokemon.tsx
import { useQuery } from '@tanstack/react-query'
import poolQuery from 'pool-query'

const getPokemon = poolQuery<{ queryKey: [string, string] }, { name: string }>({
  singleCall: (param) =>
    fetch(`https://pokeapi.co/api/v2/pokemon/${param}`).then((data) =>
      data.json()
    ),
  multiCall: (params) =>
    fetch(`https://pokeapi.co/api/v2/pokemon?limit=${params.length}`).then(
      (data) => data.json().then((data) => data.results)
    ),
})

export default function Pokemon({ name }: { name: string }) {
  const { data } = useQuery(['pokemon', name], getPokemon)
  console.log(data)

  return <div>{JSON.stringify(data)}</div>
}
```

---
_Source: https://npm.io/package/pool-query · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
