0.15.2 β€’ Published 5 months ago

@flowblade/source-kysely v0.15.2

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

@flowblade/source-kysely

A source adapter for Kysely.

npm changelog codecov bundles node browserslist size downloads license

Install

yarn add @flowblade/core @flowblade/source-kysely kysely

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

Kysely supports

Postgres MySQL MicrosoftSQLServer SQLite

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';

import { KyselyDatasource, isQueryResultError } from '@flowblade/source-kysely';

const ds = new KyselyDatasource({ db });
const query = ds.queryBuilder // This gives access to 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'
// });


// Option 1: the QResult object contains the data, metadata and error
//  - data:  the result rows (TData or undefined if error)
//  - error: the error (QError or undefined if success)
//  - meta:  the metadata (always present)

const { data, meta, error } = result;

// Option 2: You operate over the result, ie: mapping the data

const { data: data2 } = result.map((row) => {
    return {
        ...data,
        key: `key-${row.productId}`
    }})

Utils

createKyselyMssqlDialect

Create a Kysely dialect for Ms SqlServer or Azure Sql Edge.

import * as Tedious from 'tedious';
import { TediousConnUtils, createKyselyMssqlDialect } 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 dialect = createKyselyMssqlDialect({
    tediousConfig,
    // πŸ‘‰ Optional tarn pool options
    poolOptions: {
        min: 0,  // πŸ‘‰ Minimum number of connections, default 0
        max: 10, // πŸ‘‰ Maximum number of connections, default 10
        propagateCreateError: true, // πŸ‘‰ Propagate connection creation errors, default false
    },
    dialectConfig: {
        /**
         * When true, connections are validated before being acquired from the pool,
         * resulting in additional requests to the database.
         *
         * In safe scenarios, this can be set to false to improve performance.
         *
         * Defaults to `true`.
         */
        validateConnections: true,
        /**
         * When true, connections are reset to their initial states when released back to the pool,
         * resulting in additional requests to the database.
         *
         * Defaults to `false`.
         */
        resetConnectionsOnRelease: false,
    },
});

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

Note: For performance you can avoid connection roundtrips by setting validateConnections to false and resetConnectionOnRelease to false.

TediousConnUtils

fromJdbcDsn

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

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

// In your .env file
// DB_JDBC_DSN="sqlserver://localhost:1433;database=db;user=sa;password=pwd;trustServerCertificate=true;encrypt=false";

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

const tediousConnection = new Tedious.Connection(tediousConfig);

Compatibility

LevelCIDescription
Nodeβœ…CI for 18.x, 20.x & 22.x.
Cloudflareβœ…Ensured with @cloudflare/vitest-pool-workers (see wrangler.toml
Browserslistβœ…> 95% on 01/2025. Chrome 96+, Firefox 90+, Edge 19+, ios 15+, Safari 15+ and Opera 77+
Typescriptβœ…TS 5.0 + / are-the-type-wrong checks on CI.
ES2022βœ…Dist files checked with es-check
Performanceβœ…Monitored with codspeed.io

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.14.5

5 months ago

0.13.6

9 months ago

0.13.7

9 months ago

0.13.8

9 months ago

0.13.9

9 months ago

0.12.0

10 months ago

0.13.0

10 months ago

0.14.0

6 months ago

0.13.1

10 months ago

0.15.0

5 months ago

0.14.1

5 months ago

0.13.2

10 months ago

0.15.1

5 months ago

0.14.2

5 months ago

0.13.3

9 months ago

0.15.2

5 months ago

0.14.3

5 months ago

0.13.4

9 months ago

0.14.4

5 months ago

0.13.5

9 months ago

0.13.12

8 months ago

0.13.11

9 months ago

0.13.10

9 months ago

0.11.0

12 months ago

0.10.0

12 months ago

0.9.0

12 months ago

0.8.0

12 months ago

0.7.0

12 months ago

0.1.0

1 year ago

0.3.0

1 year ago

0.2.0

1 year ago

0.0.2

1 year ago

0.5.0

1 year ago

0.4.0

1 year ago

0.6.1

12 months ago

0.6.0

12 months ago

0.0.1

1 year ago