kysely icon indicating copy to clipboard operation
kysely copied to clipboard

Kysely and Deno & MySQL Driver

Open mrtornado opened this issue 2 years ago • 9 comments

Anybody already written a MySQL driver to work with Kysely in Deno ? Or is that even needed anymore ? Sry my programming skills are bad and I can't write it for myself :)

mrtornado avatar Mar 15 '23 12:03 mrtornado

I managed to create one with the help of chatgpt :) for anybody who needs something like this here it goes. I don't know how good it is, but it's working for me :

https://pastecode.io/s/mgadtzi2

mrtornado avatar Mar 15 '23 15:03 mrtornado

That seems to be created from the SQLite driver, which has a single connection. Having the mutex there forces your MySQL driver to only have one connection, which will lead to bad performance once you have more than one concurrent request running.

koskimas avatar Mar 15 '23 15:03 koskimas

thanks for the info, I will try to remove it.

mrtornado avatar Mar 15 '23 15:03 mrtornado

I managed to modify it to use a connection pool, that currently has 10 max connections, but you can modify the maxConnections: number = 10 value according to your needs and server capacity.

https://pastecode.io/s/jwfpktky

Is this one better @koskimas ?

mrtornado avatar Mar 15 '23 15:03 mrtornado

After conducting a small test, I have found that the latest version of Deno already supports npm packages like kysely and mysql2.

#!/bin/bash
TEMP_YAML="$(mktemp)"
cat <<'EOF' > $TEMP_YAML
version: "3.8"
services:
  mysql404:
    image: 'mysql:5.7'
    ports:
      - '3306:3306'
    environment:
      MYSQL_ROOT_PASSWORD: root_pass
      MYSQL_DATABASE: test_db
    command: ['--character-set-server=utf8']
  deno404:
    image: denoland/deno:alpine-1.30.3
    command:
      - /bin/sh
      - -c
      - |
        env
        sleep 10
        deno run -A - <<EOOF
        import { createPool } from "npm:mysql2";
        import { Generated, Kysely, MysqlDialect } from "npm:kysely";
        import { assertEquals } from "https://deno.land/[email protected]/testing/asserts.ts";
        const mysqlDialect = new MysqlDialect({
          pool: createPool({
            database: "test_db",
            host: "mysql404",
            password: "root_pass",
            user: "root",
          }),
        });

        type Person = {
          id: Generated<number>;
          first_name: string;
          last_name: string;
        };

        type Database = {
          person: Person;
        };

        const db = new Kysely<Database>({
          dialect: mysqlDialect,
        });

        await db.schema.createTable("person")
        .addColumn("id", "integer", (col) => col.autoIncrement().primaryKey())
        .addColumn("first_name", "varchar(50)", (col) => col.notNull())
        .addColumn("last_name", "varchar(50)").execute();

        await db.insertInto("person").values({first_name: 'Foo404', last_name: 'Bar'}).execute()
        const result = await db.selectFrom("person").select('first_name').execute()
        console.log("RESULT===>",result)
        assertEquals(result[0], { first_name: "Foo404"})
        EOOF
EOF

function handler() 
{
  echo "remove containers and exit"
  docker compose -f $TEMP_YAML rm -sf
  rm $TEMP_YAML
  exit 1
}

trap 'handler' SIGINT
docker compose -f $TEMP_YAML up

y12studio avatar Mar 15 '23 15:03 y12studio

thanks for looking into it @y12studio but I prefer my method more, seems more cleaner.

mrtornado avatar Mar 15 '23 16:03 mrtornado

I'd also like to add that npm specifiers are not yet supported on deno deploy so the npm packages will not work there unless using cdns or esm.sh. So custom adapters/dialects for deno are the way to go for now for both mysql and postgres.

waptik avatar Mar 17 '23 05:03 waptik

I would love if I could just use kysely's query builder, target a particular dialect and then get a string. I want to pass the string to the database interface myself.

alexgleason avatar Aug 02 '23 00:08 alexgleason

@alexgleason https://kysely.dev/docs/recipes/splitting-build-compile-and-execute-code

igalklebanov avatar Aug 02 '23 07:08 igalklebanov