# postgres-partial

> postgres wrapper to apply partial queries and dynamic queries

Latest version **0.2.0** (published 2020-07-20) · Unlicense license · 0 weekly downloads

## Install

```sh
npm install postgres-partial
pnpm add postgres-partial
yarn add postgres-partial
bun add postgres-partial
```

## Health

**Score 35/100 (D)** — status: abandoned.

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

Warnings: low downloads; no esm support; pre 1.0.

Negative: abandoned.

## Facts

| | |
|---|---|
| Version | 0.2.0 |
| Published | 2020-07-20 |
| First published | 2020-07-19 |
| Weekly downloads | 0 |
| License | Unlicense |
| TypeScript types | bundled |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 11.2 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 6 |
| Author | yckao <yckaokarl@gmail.com> |
| Maintainers | yckao |
| Keywords | postgres.js, postgres, sql |

## Links

- npm: https://www.npmjs.com/package/postgres-partial
- Repository: https://github.com/yckao/postgres-partial
- Issues: https://github.com/yckao/postgres-partial/issues
- npm.io page: https://npm.io/package/postgres-partial

## Dependencies (1)

- [postgres](https://npm.io/package/postgres.md) beta

## Recent versions

- 0.2.0 (latest) — 2020-07-20
- 0.1.2 — 2020-07-19
- 0.1.1 — 2020-07-19
- 0.1.0 — 2020-07-19

## README

## postgres-partial
[![GitHub Workflow Status](https://img.shields.io/github/workflow/status/yckao/postgres-partial/Test)](https://github.com/yckao/postgres-partial/actions)
[![GitHub](https://img.shields.io/github/license/yckao/postgres-partial)](https://github.com/yckao/postgres-partial/blob/master/LICENSE)
[![codecov](https://codecov.io/gh/yckao/postgres-partial/branch/master/graph/badge.svg)](https://codecov.io/gh/yckao/postgres-partial)
[![npm](https://img.shields.io/npm/dw/postgres-partial)](https://www.npmjs.com/package/postgres-partial)
[![npm](https://img.shields.io/npm/v/postgres-partial)](https://www.npmjs.com/package/postgres-partial)

**This is in experimental state and may be deprecated when partial query has official support.**

**Only tested on postgres@beta**

This package is a simple wrapper providing two feature.

One is partial, that can dynamic build an query and also benefit from postgres's Tagged template system.

Other is skip, that can skip an block when it isn't needed.

More detail you can look below.

## Getting started

**Install**

```bash
$ npm install postgres-partial
```

**Use**

You need to use wrap function to wrap an existed sql instance

```js
const postgres = require("postgres");
const partial = require("postgres-partial");
const sql = partial.wrap(postgres());

module.exports = sql;
```

## Skip
You can skip an param block using `sql.skip`
```js
const row = sql`SELECT ${sql.skip} 1`
// will equal to
const row = sql`SELECT 1`
```

## Partial
You can use Partial to provide query out of sql``
```js
const foo = sql.partial`${'bar'} as foo`
const [row] = await sql`SELECT ${foo}`
// will translate to
SELECT $1 as foo
// and row will equal to 
{foo: 'bar'}

```

## Partial and Skip

You can mix Partial and Skip to provide dynamic queries.
```js
function (limit) {
  await sql`SELECT id FROM example ${limit ? sql.partial`LIMIT ${limit}` : sql.skip}`
  // or
  const limitQuery = limit ? sql.partial`LIMIT ${limit}` : sql.skip}`
  await sql`SELECT id FROM example ${limitQuery}`
}
// This will translate into below when limit is an truthy value.
SELECT id FROM example LIMIT $1
// And translate into below when limit is an falsy value.
SELECT id FROM example
```

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