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

ydb-codegen

v0.2.9

Published

[![Node version](https://img.shields.io/npm/v/ydb-codegen.svg?style=flat)](https://www.npmjs.com/package/ydb-codegen) \ Generates TypeScript queries from sql files.

Downloads

30

Readme

ydb-ts-codegen

Node version
Generates TypeScript queries from sql files.

Given a file complex.sql

declare $primitive as Uint64;
declare $optional_primitive as Optional<Datetime>;
declare $list as List<Struct<id: Uint64, created_at: Datetime, sublist: List<Struct<paid_for: Optional<Uint64>>>>>;

select * from As_Table($list)
 where id = $primitive
    or created_at = $optional_primitive;

select *
  from company;

will generate

import { TypedData, TypedValues, Types, Driver, Session, withRetries } from "ydb-sdk";

import Long from "long";

export interface ComplexVariables {
    list: {
        "created_at": Date;
        "id": number | Long;
        "sublist": {
            "paid_for"?: number | Long;
        }[];
    }[];
    optionalPrimitive?: Date;
    primitive: number | Long;
}

function prepareComplexVariables(variables: ComplexVariables) {
    return {
        $list: TypedValues.list(Types.struct({
            "created_at": Types.DATETIME,
            "id": Types.UINT64,
            "sublist": Types.list(Types.struct({
                "paid_for": Types.optional(Types.UINT64)
            }))
        }), variables.list!),
        $optional_primitive: TypedValues.optional(TypedValues.datetime(variables.optionalPrimitive!)),
        $primitive: TypedValues.uint64(variables.primitive!)
    };
}

export interface ComplexResult0 {
    "created_at": Date;
    "id": number | Long;
    "sublist": {
        "paid_for"?: number | Long;
    }[];
}

export interface ComplexResult1 {
    "id": number | Long;
    "name"?: string;
}

async function executeComplex(driver: Driver, variables: ComplexVariables, queryOptions?: Parameters<Session["executeQuery"]>[2]) {
    const payload = prepareComplexVariables(variables);
    const sql = "declare $primitive as Uint64;\r\ndeclare $optional_primitive as Optional<Datetime>;\r\ndeclare $list as List<Struct<id: Uint64, created_at: Datetime, sublist: List<Struct<paid_for: Optional<Uint64>>>>>;\r\n\r\nselect * from As_Table($list)\r\n where id = $primitive\r\n    or created_at = $optional_primitive;\r\n\r\nselect *\r\n  from company;";
    async function sessionHandler(session: Session) {
        return withRetries(() => session.executeQuery(sql, payload, queryOptions));
    }
    const response = await driver.tableClient.withSession(sessionHandler);
    const result = {
        ComplexResult0: TypedData.createNativeObjects(response.resultSets[0]) as unknown as ComplexResult0[],
        ComplexResult1: TypedData.createNativeObjects(response.resultSets[1]) as unknown as ComplexResult1[]
    };
    return result;
}

export default executeComplex;

so you can then use it like this with all the nice typings:

executeComplex(driver, {
    list: [{ id: 1, created_at: new Date(), sublist: [] }],
    primitive: 1,
}).then(console.log)

Usage

Work very much in progress, but we use it in production.
Here is an example on how to use the library. Notice that it's important to pass connection to the database, because this is how it finds out about input (and in the future output) types. It's recommended to use IamAuthService() but you can also try getCredentialsFromEnv()

import dotenv from "dotenv";
import { writeFile } from "fs/promises";
import { Driver, IamAuthService, getSACredentialsFromJson } from "ydb-sdk";
import { processFolder } from "ydb-codegen";

const config = {
  endpoint: process.env["DATABASE_ENDPOINT"],
  database: process.env["DATABASE_NAME"],
};

const saCredentials = getSACredentialsFromJson("./authorized_key.json");
const authService = new IamAuthService(saCredentials);

const driver = new Driver({
  authService,
  ...config,
});

processFolder(".", driver).then((files) =>
  files.forEach((file) =>
    writeFile(`./my/output/${file.name}.ts`, file.content)
  )
);