Skip to main content

Running on Deno

Kysely doesn't include drivers for Deno, but you can still use Kysely as a query builder or implement your own driver:

// We use jsdeliver to get Kysely from npm.
import {
DummyDriver,
Generated,
Kysely,
PostgresAdapter,
PostgresIntrospector,
PostgresQueryCompiler,
} from 'https://cdn.jsdelivr.net/npm/kysely/dist/esm/index.js'

interface Person {
id: Generated<number>
first_name: string
last_name: string | null
}

interface Database {
person: Person
}

const db = new Kysely<Database>({
dialect: {
createAdapter() {
return new PostgresAdapter()
},
createDriver() {
// You need a driver to be able to execute queries. In this example
// we use the dummy driver that never does anything.
return new DummyDriver()
},
createIntrospector(db: Kysely<unknown>) {
return new PostgresIntrospector(db)
},
createQueryCompiler() {
return new PostgresQueryCompiler()
},
},
})

const query = db.selectFrom('person').select('id')
const sql = query.compile()

console.log(sql.sql)

Deno works well with PostgresJS, to that end you can use the Kysely dialect and the driver:

import { PostgresJSDialect } from "npm:kysely-postgres-js";
import postgres from 'https://deno.land/x/postgresjs/mod.js'
import { Kysely } from "kysely";

interface Database {}

// adjust accordingly
const databaseConfig = {
postgres: postgres({
database: "my_database_name",
host: "localhost",
max: 10,
port: 5432,
user: "postgres",
}),
};

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

// example assuming there is a "users" table:
const users = await db.selectFrom("users")
.selectAll()
.execute();

console.log(users)