0.11.0 • Published 10 months ago

@flowblade/source-kysely v0.11.0

Weekly downloads
-
License
MIT
Repository
github
Last release
10 months ago

@flowblade/source-kysely

Install

yarn add @flowblade/source-kysely

# Install optional drivers
# 01. for Ms SqlServer or Azure Sql Edge
yarn add tedious tarn

Quick start

// Your db configuration, see Utils section for more details
import { db } from '@/config/db.config.ts'; 
import { KyselyDatasource, isQueryResultError } from '@flowblade/source-kysely';
import { sql } from 'kysely'; 

const ds = new KyselyDatasource({ db });
const query = ds.queryBuilder  // Kysely expression builder
        .selectFrom('brand as b')
        .select(['b.id', 'b.name'])
        .leftJoin('product as p', 'p.brand_id', 'b.id')
        .select(['p.id as product_id', 'p.name as product_name'])
        .where('b.created_at', '<', new Date())
        .orderBy('b.name', 'desc');

const result = await ds.query(query);

// Or with query information (will be sent in the metadata)
// const result = await ds.query(query, {
//  name: 'getBrands'
// });

// Discriminated usin with success: true | false
if (isQueryResultError(result)) {
    console.error(result.error);
    console.error(result.meta);
}  else {
    console.log(result.data);
    console.log(result.meta);
}

/** Raw queries support */
const data = await ds.queryRaw(sql<{ count: number }>`SELECT 1 as "count' FROM brand`);

Type helpers

InferQueryResultData

Infer the success part (data) of a QueryResult.

import type { InferQueryResultData, QueryResult } from "@flowblade/source-kysely";

type Row = { id: number };
const queryResult: QueryResult<Row[]> = {
  success: true,
  data: [ { id: 1 } ],
};
type TData = InferQueryResultData<typeof queryResult>;
// TData is Row[]

InferAsyncQueryResultSuccess

Infer the success part (data) of an AsyncQueryResult.

import type { InferAsyncQueryResultData, AsyncQueryResult } from "@flowblade/source-kysely";

type Row = { id: number };
const getQueryResult = async (): AsyncQueryResult<Row[]> => {
    return {
        success: true,
        data: [{ id: 1 }],
    };
};
type TData = InferAsyncQueryResultData<ReturnType<typeof getQueryResult>>;
// TData is Row[]

Utils

createKyselySqlServerDialect

import * as Tedious from 'tedious';
import { TediousConnUtils, createKyselySqlServerDialect } from '@flowblade/source-kysely';

const jdbcDsn = "sqlserver://localhost:1433;database=db;user=sa;password=pwd;trustServerCertificate=true;encrypt=false";
const tediousConfig = TediousConnUtils.fromJdbcDsn(jdbcDsn);
const tediousConnection = new Tedious.Connection(tediousConfig);

const dialect = createKyselySqlServerDialect(tediousConfig, {
  // Optional tarn pool options
  tarnPool: {
    min: 0,
    max: 10
  },
  // Optional: customize tedious types
  // Example based on https://github.com/kysely-org/kysely/issues/1161#issuecomment-2384539764
  tediousTypes: { ...Tedious.TYPES, NVarChar: Tedious.TYPES.VarChar}
});

const db = new Kysely<DB>({
  dialect
})

TediousConnUtils

fromJdbcDsn

Parse and validate a JDBC connection string and return a Tedious connection configuration.

import { TediousConnUtils } from '@flowblade/source-kysely';

const tediousConfig = TediousConnUtils.fromJdbcDsn(process.env.DB_JDBC_DSN);

const jdbcDsn = "sqlserver://localhost:1433;database=db;user=sa;password=pwd;trustServerCertificate=true;encrypt=false";
const tediousConfig = TediousConnUtils.fromJdbcDsn(jdbcDsn);
const tediousConnection = new Tedious.Connection(tediousConfig);

Contributors

Contributions are welcome. Have a look to the CONTRIBUTING document.

Sponsors

Sponsor, coffee, or star – All is spent for quality time with loved ones. Thanks ! 🙏❤️

Special thanks to

License

MIT © Sébastien Vanvelthem and contributors.

0.11.0

10 months ago

0.10.0

10 months ago

0.9.0

10 months ago

0.8.0

10 months ago

0.7.0

10 months ago

0.1.0

10 months ago

0.3.0

10 months ago

0.2.0

10 months ago

0.0.2

10 months ago

0.5.0

10 months ago

0.4.0

10 months ago

0.6.1

10 months ago

0.6.0

10 months ago

0.0.1

11 months ago