Skip to content

Instantly share code, notes, and snippets.

@twiddler
Created April 29, 2024 01:44
Show Gist options
  • Save twiddler/8f98b48ec61c75eaa5422249c1edebc9 to your computer and use it in GitHub Desktop.
Save twiddler/8f98b48ec61c75eaa5422249c1edebc9 to your computer and use it in GitHub Desktop.
Unexpected result with kysely-planetscale
PLANETSCALE_URL='mysql://foo:pscale_pw_bar@aws.connect.psdb.cloud/baz?ssl={"rejectUnauthorized":true}'
import { Kysely, MysqlDialect } from "kysely";
import { PlanetScaleDialect } from "kysely-planetscale";
import { createPool } from "mysql2";
import { strict as assert } from "node:assert";
import { describe, test } from "node:test";
import { fetch } from "undici";
describe("mysql", function () {
const db = new Kysely({
dialect: new MysqlDialect({
pool: createPool({
database: "mysql",
host: "localhost",
user: "root",
password: "",
}),
}),
});
test("one row", async function () {
const q = await db
.selectFrom([
db.selectNoFrom((eb) => eb.val(0).as("num_column")).as("foo"),
])
.selectAll()
.execute();
assert.deepStrictEqual(q, [{ num_column: 0 }]); // ✅
});
test("two rows", async function () {
const q = await db
.selectFrom([
db
.selectNoFrom((eb) => eb.val(0).as("num_column"))
.unionAll(db.selectNoFrom((eb) => eb.val(0).as("num_column")))
.as("foo"),
])
.selectAll()
.execute();
assert.deepStrictEqual(q, [{ num_column: 0 }, { num_column: 0 }]); // ✅
});
});
describe("planetscale", function () {
const db = new Kysely({
dialect: new PlanetScaleDialect({
url: process.env.PLANETSCALE_URL,
fetch,
}),
});
test("one row", async function () {
const q = await db
.selectFrom([
db.selectNoFrom((eb) => eb.val(0).as("num_column")).as("foo"),
])
.selectAll()
.execute();
assert.deepStrictEqual(q, [{ num_column: 0 }]); // ✅
});
test("two rows", async function () {
const q = await db
.selectFrom([
db
.selectNoFrom((eb) => eb.val(0).as("num_column"))
.unionAll(db.selectNoFrom((eb) => eb.val(0).as("num_column")))
.as("foo"),
])
.selectAll()
.execute();
assert.deepStrictEqual(q, [{ num_column: 0 }, { num_column: 0 }]); // ❌ actual: [ { num_column: '0' }, { num_column: '0' } ]
});
});
{
"private": true,
"type": "module",
"scripts": {
"test": "tsx --env-file=.env --test \"./*.test.mts\""
},
"devDependencies": {
"@types/node": "^20.12.7",
"tsx": "^4.7.3",
"typescript": "^5.4.5"
},
"dependencies": {
"@planetscale/database": "^1.17.0",
"kysely": "^0.27.3",
"kysely-planetscale": "^1.4.0",
"mysql2": "^3.9.7",
"undici": "^6.14.1"
}
}
{
"$schema": "https://json.schemastore.org/tsconfig",
"compilerOptions": {
"module": "NodeNext",
"moduleResolution": "NodeNext",
"strict": true
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment