kysely-org / kysely

A type-safe typescript SQL query builder
https://kysely.dev
MIT License
10.8k stars 275 forks source link

Kysely and Deno & MySQL Driver #360

Closed mrtornado closed 6 months ago

mrtornado commented 1 year ago

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 commented 1 year ago

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

koskimas commented 1 year ago

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.

mrtornado commented 1 year ago

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

mrtornado commented 1 year ago

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 ?

y12studio commented 1 year ago

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/std@0.179.0/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
mrtornado commented 1 year ago

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

waptik commented 1 year ago

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.

alexgleason commented 1 year ago

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.

igalklebanov commented 1 year ago

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