Class DuckDbDialect

Kysely dialect for duckdb.

Quick Start and Usage Example

Please see also Kysely Docs and Duckdb Docs

Install

$ npm install --save kysely duckdb kysely-duckdb

Basic Usage Example

reding data from json file.

import * as duckdb from "duckdb";
import { Kysely } from "kysely";
import { DuckDbDialect } from "kysely-duckdb"

interface PersonTable {
first_name: string,
gender: string,
last_name: string,
};
interface DatabaseSchema {
person: PersonTable,
};

const db = new duckdb.Database(":memory:");
const duckdbDialect = new DuckDbDialect({
database: db,
tableMappings: {
person:
`read_json('./person.json', columns={"first_name": "STRING", "gender": "STRING", "last_name": "STRING"})`,
},
});
const kysely = new Kysely<DatabaseSchema>({ dialect: duckdbDialect });

const res = await kysely.selectFrom("person").selectAll().execute();

Hierarchy

  • DuckDbDialect

Implements

  • Dialect

Constructors

  • Parameters

    • config: {
          database: Database | (() => Promise<Database>);
          onCreateConnection?: ((conection) => Promise<void>);
          tableMappings: {
              [tableName: string]: string;
          };
      }

      configulations for DuckDbDialect

      • database: Database | (() => Promise<Database>)

        duckdb.Database instance or a function returns a Promise of duckdb.Database instance.

      • Optional onCreateConnection?: ((conection) => Promise<void>)
          • (conection): Promise<void>
          • called when a connection is created.

            Parameters

            • conection: Connection

              duckdb.Connection instance that is created.

            Returns Promise<void>

            Promise

      • tableMappings: {
            [tableName: string]: string;
        }

        Mappings of table name in kysely to duckdb table expressions.

        Duckdb can read external source(file, url or database) as table like: SELECT * FROM read_json_objects('path/to/file/*.json'). You can use raw duckdb table expression as table name, but it may be too long, preserving too many implementation details.

        This mappings is used to replace table name string to duckdb table expression.

        Example

        const dialect = new DuckDbDialect({
        database: db,
        tableMappings: {
        person: 'read_json_object("s3://my-bucket/person.json?s3_access_key_id=key&s3_secret_access_key=secret")'
        }
        });

        const db = new Kysely<{
        person: { first_name: string, last_name: string }, // 'person' is defined in tableMappings
        pet: { name: string, species: 'cat' | 'dog' }, // 'pet' is *not* defined in tableMappings
        >({ dialect });

        await db.selectFrom("person").selectAll().execute();
        // => Executed query is: `SELECT * FROM read_json_object("s3://my-bucket/person.json?s3_access_key_id=key&s3_secret_access_key=secret");`

        await db.selectFrom("pet").selectAll().execute(); // => Executed query is: SELECT * FROM pet;

        • [tableName: string]: string

    Returns DuckDbDialect

Properties

config: {
    database: Database | (() => Promise<Database>);
    onCreateConnection?: ((conection) => Promise<void>);
    tableMappings: {
        [tableName: string]: string;
    };
}

configulations for DuckDbDialect

Type declaration

  • database: Database | (() => Promise<Database>)

    duckdb.Database instance or a function returns a Promise of duckdb.Database instance.

  • Optional onCreateConnection?: ((conection) => Promise<void>)
      • (conection): Promise<void>
      • called when a connection is created.

        Parameters

        • conection: Connection

          duckdb.Connection instance that is created.

        Returns Promise<void>

        Promise

  • tableMappings: {
        [tableName: string]: string;
    }

    Mappings of table name in kysely to duckdb table expressions.

    Duckdb can read external source(file, url or database) as table like: SELECT * FROM read_json_objects('path/to/file/*.json'). You can use raw duckdb table expression as table name, but it may be too long, preserving too many implementation details.

    This mappings is used to replace table name string to duckdb table expression.

    Example

    const dialect = new DuckDbDialect({
    database: db,
    tableMappings: {
    person: 'read_json_object("s3://my-bucket/person.json?s3_access_key_id=key&s3_secret_access_key=secret")'
    }
    });

    const db = new Kysely<{
    person: { first_name: string, last_name: string }, // 'person' is defined in tableMappings
    pet: { name: string, species: 'cat' | 'dog' }, // 'pet' is *not* defined in tableMappings
    >({ dialect });

    await db.selectFrom("person").selectAll().execute();
    // => Executed query is: `SELECT * FROM read_json_object("s3://my-bucket/person.json?s3_access_key_id=key&s3_secret_access_key=secret");`

    await db.selectFrom("pet").selectAll().execute(); // => Executed query is: SELECT * FROM pet;

    • [tableName: string]: string

Methods

  • Returns DialectAdapter

  • Returns Driver

  • Parameters

    • db: Kysely<any>

    Returns DatabaseIntrospector

  • Returns QueryCompiler

Generated using TypeDoc