JSPM

postgres-partial

0.2.0
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 1
  • Score
    100M100P100Q14946F
  • License Unlicense

postgres wrapper to apply partial queries and dynamic queries

Package Exports

  • postgres-partial

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (postgres-partial) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

postgres-partial

GitHub Workflow Status GitHub codecov npm npm

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

$ npm install postgres-partial

Use

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

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

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``

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.

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