1.0.0 • Published 1 year ago

@pfc/query-provider v1.0.0

Weekly downloads
-
License
ISC
Repository
-
Last release
1 year ago

Query Provider

This library provides Query and Strategy functions for Query Side servers.

Requirements

  • Typescript
  • pg-promise

Setup

To install, use the file pro package.json option in the project:

  "dependencies": {
    "query-provider": "file:../../libs/query-provider",
    ...
  }

To use database access, you need to provide an environment variable called DATABASE_URL with the following format:

DATABASE_URL='postgres://username:password@host:port/database'

How to Use

To use it, you must supply the desired Repository with a function that does what you need to the QueryProvider.

  • computeStrategy: Provides access to perform a common function.

  • fetchStrategy: provides access to a url to execute HTTP queries via the GET method.

  • repositoryStrategy: provides access to the database. For this you must have the DATABASE_URL configuration described in Setup. For more information on usage see pg-promise

Example:

import { QueryProvider, RepositoryStrategy, FetchStrategy, ComputeStrategy } from "query-provider";

const MyQueryMapper = {

  "/random/": QueryProvider(
    ComputeStrategy((body: any) => (body.offset || 0) + Math.random())
  ),

  "/time/": QueryProvider(
    FetchStrategy("http://worldtimeapi.org/api/timezone/America/Sao_Paulo")
  ),

  "/products/": QueryProvider(
    RepositoryStrategy((db: any) => await db.any('SELECT * FROM product WHERE price BETWEEN $1 AND $2', [1, 10]))
  ),

};