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

plv8ts

v1.0.1

Published

Create PostgreSQL functions in Typescript.

Downloads

5

Readme

PLV8 Typescript

This project converts Typescript functions into PostgreSQL database PLV8 functions.

Description

Write PLV8 functions (including trigger functions) in Typescript and enjoy editor support, type checking, linting, and prettier. Following the steps below will set up the compiler and a post processing script to emit SQL that can be executed with \i from the psql command line.

Limitations

  • No support for including external packages.

Quick start

To try the example, copy the example files into a clean directory:

curl  https://raw.githubusercontent.com/Davepar/plv8ts/main/example/package.json -O
curl  https://raw.githubusercontent.com/Davepar/plv8ts/main/example/tsconfig.json -O
mkdir src; cd src
curl https://raw.githubusercontent.com/Davepar/plv8ts/main/example/src/sample_function.ts -O

Then install and build:

npm i
npm run build

The result will be in a subdirectory called sql. To add drop statements before each create function statement, use npm run build -- -d instead.

To use the lint and fix commands, copy the other files from the example directory on Github.

Note: You may be tempted to put the example code into an existing project, but the settings inside the example tsconfig.json are necessary to build the code correctly. See below for more details.

Writing functions

Add Typescript functions to the src directory, one function per file. Use the type names from the 'plv8ts' package to define parameters and return types. Be sure to export the function with export function. This trivial example:

export function add2(val: int8): smallint {
  return val + 2;
}

will produce this result:

create or replace function add2(val int8)
returns smallint AS $$
  return val + 2;
$$ language plv8;

To define functions in a schema (other than the default "public") create a subdirectory in src. This example will be defined in the private schema.

Calling PLV8 functions

To call functions defined by PLV8 add:

import {plv8} from 'plv8ts';

and use the functions that it defines. That will enable the Typescript compiler to do type checking. Some of those functions are generics that allow adding a type that will improve the type checking. For example, this code calls execute with an object type:

const value_rows = plv8.execute<{value: string}>(
  "select value from private.keys where key = 'EMAIL_TEMPLATE_PROMOTE_ATTENDEE'");
// Use value_rows[0].value

More examples in the function sample.

To see the full list of types and functions provided, take a look at the source code.

Trigger functions

To write a trigger function, return trigger<MyTableRow> where MyTableRow defines the type of the row for the trigger. You can also add a NEW parameter for insert and update triggers, and OLD for update and delete triggers.

export function sample_trigger(
  NEW: MyTableRow,
  OLD: MyTableRow
): trigger<MyTableRow> { ... }

See sample trigger for more details.

Function options

To add security definer, immutable, or stable to the function definition, add a comment in the function that starts with plv8:. For example:

// plv8: security definer, immutable

Lint

The example inclues Google Typescript Style, which provides a linter, a prettier auto-formatter, and strict code checking by default. This is useful for catching errors before using a function in your Postgres database.

Testing

The example Jasmine test shows how to write a local test for a PLV8 function. Any data returned from plv8.execute() and other PLV8 functions will need to be faked. Run tests with:

npm test

Tech details

The example Typescript config is currently using a target of es2015 and a moduleResolution of node. These were found through experimentation to produce the best output, i.e. the Javascript output is still readable. es2015 also seems to be the Javascript version supported by the current version of PLV8 (as of December 2022). The PLV8 documentation doesn't directly mention which ECMAScript version it supports.