0.2.9 • Published 7 months ago

ydb-codegen v0.2.9

Weekly downloads
-
License
-
Repository
-
Last release
7 months ago

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)
  )
);
0.2.5-beta1

10 months ago

0.2.7

8 months ago

0.2.6

10 months ago

0.2.9

7 months ago

0.2.8

7 months ago

0.2.5

10 months ago

0.2.5-beta2

10 months ago

0.2.4

10 months ago

0.1.0

1 year ago

0.2.1

1 year ago

0.1.2

1 year ago

0.0.3

1 year ago

0.2.0

1 year ago

0.1.1

1 year ago

0.0.8

1 year ago

0.2.3

1 year ago

0.1.4

1 year ago

0.0.5

1 year ago

0.2.2

1 year ago

0.1.3

1 year ago

0.0.4

1 year ago

0.0.7

1 year ago

0.1.5

1 year ago

0.0.6

1 year ago

0.0.2

1 year ago

0.0.1

1 year ago