npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@ty-ras-extras/typed-sql-runtypes

v2.1.0

Published

Execute SQL queries exposing compile-time and runtime validation for both input and output of the queries, and utilizing `runtypes` as the data validator for both input and output.

Downloads

3

Readme

Typesafe REST API Specification Extras - Typed SQL Query Execution With Runtypes

Coverage

This folder contains @ty-ras-extras/typed-sql-runtypes library which exposes API to create callbacks which will execute SQL queries against a parametrizable client. These callbacks will expose the input signature at compile-time utilizing custom template functions, as well as compile-time types for query result. In addition to that, the callbacks will perform runtime validation using runtypes library on inputs to the query, as well as output of the query execution rows returned by client.

Usage

Input and Output Validation

The goal of this library is to capture executing SQL query as a callback with typed input and output, in a most feasible way from the point of typesafety and ease of usage. The runtime validation library runtypes is used as a tool to ensure that both the input and the output will be adhered to the types they claim to represent, at runtime.

Start by defining a SQL query using tagged template function provided by this library:

import * as t from "runtypes";
import * as sql from "@ty-ras-extras/typed-sql-runtypes";

// The parameter signifying the placeholder in the final SQL query string
const columnParameter = sql.parameter(
  // The name of the parameter -> will be directly mapped to input object property name
  "idParameter",
  // The Runtypes validation object for the parameter.
  // The type of the input object property value will be extracted from this using "t.Static<>".
  t.String,
);

// Construct object has typed callback response.
// The "prepareSQL" is the tagged template provided by this library.
const preparedQuery = sql.prepareSQL`
SELECT column
FROM table
WHERE id = ${columnParameter}
`;

The preparedQuery now acts as a factory to actually create a callback with typed input and output. The last missing piece in the puzzle is how the columnParameter should be transformed when building the final SQL string.

In PostgreSQL, the parameters are expressed as $1, $2, and so on, placeholders within SQL string. Let's build the callback from preparedQuery using this PostgreSQL provider:

import type * as pg from "pg";

// Declare reusable client information encapsulating the logic needed to create the callback we want
const postgreSQLClientInfo: sql.SQLClientInformation<pg.Client> = {
  // The SQL parameter at given index is transformed to SQL string placeholder using this callback.
  constructParameterReference: (index) => `$${index + 1}`,
  // When the SQL is executed, this callback will be called.
  executeQuery: async (client, sqlString, parameters) =>
    (
      await client.query(sqlString, parameters)
    ).rows,
};

// Create the callack bound to executing queries against PostgreSQL client
const preparedQueryPostgreSQL = preparedQuery(postgreSQLClientInfo);

type ExecutePostgreSQLQuery = typeof preparedQueryPostgreSQL;
//  The function signature "ExecutePostgreSQLQuery" is
//                         ⬇️
//  (client: pg.Client, parameters: { idParameter: string }) => Promise<Array<unknown>>

Notice how the "idParameter" string literal defined in first code sample is now transformed into a propery name of an input object. Furthermore, its type is string as that is the output type of runtypes library t.String validator.

Notice that preparedQueryPostgreSQL, in addition of being a callback, also exposes sqlString property, so that it could be used in e.g. unit tests or other scenarios:

const preparedSQLString = preparedQueryPostgreSQL.sqlString;
// The value of "preparedSQLString" will be the following multiline string:
// SELECT column
// FROM table
// WHERE id = $1

Now we can use the runtime-validated callback with static auto-complete:

const executeQueryWithClient = async (client: pg.Client) =>
  await preparedQueryPostgreSQL(client, { idParameter: "my-id" });
//                                        ⬆️            ⬆️
//                   auto-complete property name     validate property value both at compile- and runtime

The input looks good now! However, the output of the executeQueryWithClient is still Promise<Array<unknown>>. This can be fixed with functions validateRows and many exposed by this library:

const executeQuery = sql.validateRows(
  preparedQueryPostgreSQL,
  sql.many(t.object({
    column: t.string()
  }))
);

type ExecuteQuery = typeof executeQuery:
//  The function signature "ExecutePostgreSQLQuery" is
//                          ⬇️
//  (client: pg.Client, parameters: { idParameter: string }) => Promise<Array<{ column: string }>>

The return value of the callback has now changed from Promise<Array<unknown>> to Promise<Array<{ column: string }>>. Notice also that the sqlString property exposing the same SQL string is still available for executeQuery as well. Now we can modify our executeQueryWithClient to be properly typed:

// The type of executeQueryWithClient is now
//                  ⬇️
// (client: pg.Client) => Promise<Array<{ column: string }>> ⬅️ Return value is validated both at compile- and runtime
const executeQueryWithClient = async (client: pg.Client) =>
  await preparedQueryPostgreSQL(client, { idParameter: "my-id" });
//                                        ⬆️            ⬆️
//                   auto-complete property name     validate property value both at compile- and runtime

So now the executeQueryWithClient has both compile- and runtime validation for both input and output of SQL execution.

Helper for One Row Query

Unfortunately, the runtypes library does not have capability to transform values. There exists an issue and aso a PR, but they have been inactive for a while. This is why, unlike corresponding io-ts -variation, or zod -variation, this library does not expose one helper function.

Full code sample

For completeness sake, here is full code sample (with untyped return value part cut off):

import * as t from "runtypes";
import * as sql from "@ty-ras-extras/typed-sql-runtypes";
import type * as pg from "pg";

// The parameter signifying the placeholder in the final SQL query string
const columnParameter = sql.parameter(
  // The name of the parameter -> will be directly mapped to input object property name
  "idParameter",
  // The Runtypes validation object for the parameter.
  // The type of the input object property value will be extracted from this using "t.Static<>".
  t.String,
);

// Construct object has typed callback response.
// The "prepareSQL" is the tagged template provided by this library.
const preparedQuery = sql.prepareSQL`
SELECT column
FROM table
WHERE id = ${columnParameter}
`;

// Declare reusable client information encapsulating the logic needed to create the callback we want
const postgreSQLClientInfo: sql.SQLClientInformation<pg.Client> = {
  // The SQL parameter at given index is transformed to SQL string placeholder using this callback.
  constructParameterReference: (index) => `$${index + 1}`,
  // When the SQL is executed, this callback will be called.
  executeQuery: async (client, sqlString, parameters) =>
    (
      await client.query(sqlString, parameters)
    ).rows,
};

// Create the callack bound to executing queries against PostgreSQL client
const preparedQueryPostgreSQL = sql.validateRows(
  preparedQuery(postgreSQLClientInfo),
  sql.many(t.object({
    column: t.string()
  }))
);

type ExecutePostgreSQLQuery = typeof preparedQueryPostgreSQL;
//  The function signature "ExecutePostgreSQLQuery" is
//                         ⬇️
//  (client: pg.Client, parameters: { idParameter: string }) => Promise<Array<{ column: string }>>

// The type of executeQueryWithClient is now
//                  ⬇️
// (client: pg.Client) => Promise<Array<{ column: string }>> ⬅️ Return value is validated both at compile- and runtime
const executeQueryWithClient = async (client: pg.Client) =>
  await preparedQueryPostgreSQL(client, { idParameter: "my-id" });
//                                        ⬆️            ⬆️
//                   auto-complete property name     validate property value both at compile- and runtime