Skip to content

Instantly share code, notes, and snippets.

@coolaj86
Last active November 13, 2024 18:05
Show Gist options
  • Save coolaj86/94cd9d1cc19ff1dca71d3dffb726e607 to your computer and use it in GitHub Desktop.
Save coolaj86/94cd9d1cc19ff1dca71d3dffb726e607 to your computer and use it in GitHub Desktop.

https://jswithtypes.com

npm init -y
npx jswt init

sqlc.yaml:

version: "2"
plugins:
  - name: ts
    wasm:
      url: https://downloads.sqlc.dev/plugin/sqlc-gen-typescript_0.1.3.wasm
      sha256: 287df8f6cc06377d67ad5ba02c9e0f00c585509881434d15ea8bd9fc751a9368
sql:
  - engine: "postgresql"
    schema: "./sql/migrations/"
    queries: "./sql/queries/"
    gen:
      json:
        out: "./db/json/"
        filename: 'db.json'
        indent: '  '
    codegen:
      - out: "./db/ts/"
        plugin: ts
        options:
          runtime: node
          driver: pg
#!/bin/sh
#shellcheck disable=SC1090
set -e
set -u

if test -f ~/.config/envman/PATH.env; then
    . ~/.config/envman/PATH.env
fi

fn_install_deps() { (
    cd /tmp/ || return 1

    if ! command -v sqlc > /dev/null; then
        curl https://webi.sh/sqlc | sh
        . ~/.config/envman/PATH.env
    fi
    if ! command -v node > /dev/null; then
        curl https://webi.sh/node | sh
        . ~/.config/envman/PATH.env
    fi
    if ! command -v ts-to-jsdoc > /dev/null; then
        npm install --location=global ts-to-jsdoc
    fi
    if ! command -v prettier > /dev/null; then
        npm install --location=global prettier
    fi
); }

fn_sql_gen() { (
    mkdir -p ./sql/migrations
    mkdir -p ./sql/queries
    mkdir -p ./db/

    echo "compiling and generating from ./sql/ to ./db/ts/"
    sqlc generate -f sqlc.yaml
    echo "decompiling JavaScript from ./db/ts/ to ./db/esm/"
    ts-to-jsdoc -f -o ./db/esm/ ./db/ts/
    echo "decompiling CommonJS in ./db/esm/ to ./db/"
    node ./scripts/db-esm-to-commonjs.js ./db/esm/ ./db/
    echo "formatting ./db/*.js"
    prettier -w ./db/*.js
    echo "done"
); }

fn_install_deps
fn_sql_gen

scripts/db-esm-to-commonjs.js:

#!/usr/bin/env node
"use strict";

let Fs = require("fs/promises");
let Path = require("path");

async function main() {
  // ex: ./db/esm/
  let src = process.argv[2] || '.';
  // ex: ./db/
  let dst = process.argv[3] || src;

  let entries = await Fs.readdir(src);
  for (let entry of entries) {
    let srcpath = Path.join(src, entry);

    let isJs = entry.endsWith(".js");
    if (!isJs) {
      console.info(`    ${srcpath}: skip`);
      continue;
    }
    console.info(`    ${srcpath}: processing...`);

    let js = await Fs.readFile(srcpath, "utf8");

    js = js.replace(/(.*@import.*)/, "$1\n\nlet Queries = module.exports;");
    js = js.replace(/export const (\w+) =/g, "\nQueries.$1 =");
    js = js.replace(
      /export async function (\w+)/g,
      "Queries.$1 = async function ",
    );
    js = js.replace(/ (\w+Query)\b/g, " Queries.$1");
    js = js.replace(/([^\n])\n\/\*\*/gm, "$1\n\n/**");

    let dstpath = Path.join(dst, entry);
    await Fs.writeFile(dstpath, js, "utf8");
    console.info(`    ${srcpath}: processed`);
  }
}

main();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment