The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.
TypeScript aialternativeauthdatabasedenoembeddingsexamplefirebasenextjsoauth2pgvectorpostgispostgrespostgresqlpostgrestrealtimesupabasevectorswebsockets
feat: add kysely postgres example.
thorwebdev200ca3c87779
diff --git a/examples/edge-functions/supabase/.env.local.example b/examples/edge-functions/supabase/.env.local.exampleindex ed44709092f..de3b9e65137 100644--- a/examples/edge-functions/supabase/.env.local.example+++ b/examples/edge-functions/supabase/.env.local.example@@ -10,9 +10,13 @@ IPINFO_TOKEN="your https://ipinfo.io token"# openaiOPENAI_API_KEY="<YOUR API KEY HERE>"-# postgres-on-the-edge+# postgres-on-the-edge & kysely-postgresDB_HOSTNAME=DB_PASSWORD=+DB_SSL_CERT="-----BEGIN CERTIFICATE-----+GET YOUR CERT FROM YOUR PROJECT DASHBOARD+https://app.supabase.com/project/_/settings/database+-----END CERTIFICATE-----"# puppeteerPUPPETEER_BROWSERLESS_IO_TOKEN=diff --git a/examples/edge-functions/supabase/functions/import_map.json b/examples/edge-functions/supabase/functions/import_map.jsonindex de16ef14e67..f703de7b56c 100644--- a/examples/edge-functions/supabase/functions/import_map.json+++ b/examples/edge-functions/supabase/functions/import_map.json@@ -15,6 +15,7 @@"puppeteer": "https://deno.land/x/[email protected]/mod.ts","React": "https://esm.sh/[email protected]?deno-std=0.177.0","upstash_redis": "https://deno.land/x/[email protected]/mod.ts",- "xhr_polyfill": "https://deno.land/x/[email protected]/mod.ts"+ "xhr_polyfill": "https://deno.land/x/[email protected]/mod.ts",+ "kysely": "https://esm.sh/[email protected]"}}diff --git a/examples/edge-functions/supabase/functions/kysely-postgres/DenoPostgresDriver.ts b/examples/edge-functions/supabase/functions/kysely-postgres/DenoPostgresDriver.tsnew file mode 100644index 00000000000..970e2f2f625--- /dev/null+++ b/examples/edge-functions/supabase/functions/kysely-postgres/DenoPostgresDriver.ts@@ -0,0 +1,152 @@+import {+ CompiledQuery,+ DatabaseConnection,+ Driver,+ PostgresCursorConstructor,+ QueryResult,+ TransactionSettings,+} from 'https://esm.sh/[email protected]'+import { freeze, isFunction } from 'https://esm.sh/[email protected]/dist/esm/util/object-utils.js'+import { extendStackTrace } from 'https://esm.sh/[email protected]/dist/esm/util/stack-trace-utils.js'+import { Pool, PoolClient } from 'https://deno.land/x/[email protected]/mod.ts'++export interface PostgresDialectConfig {+ pool: Pool | (() => Promise<Pool>)+ cursor?: PostgresCursorConstructor+ onCreateConnection?: (connection: DatabaseConnection) => Promise<void>+}++const PRIVATE_RELEASE_METHOD = Symbol()++export class PostgresDriver implements Driver {+ readonly #config: PostgresDialectConfig+ readonly #connections = new WeakMap<PoolClient, DatabaseConnection>()+ #pool?: Pool++ constructor(config: PostgresDialectConfig) {+ this.#config = freeze({ ...config })+ }++ async init(): Promise<void> {+ this.#pool = isFunction(this.#config.pool) ? await this.#config.pool() : this.#config.pool+ }++ async acquireConnection(): Promise<DatabaseConnection> {+ const client = await this.#pool!.connect()+ let connection = this.#connections.get(client)++ if (!connection) {+ connection = new PostgresConnection(client, {+ cursor: this.#config.cursor ?? null,+ })+ this.#connections.set(client, connection)++ // The driver must take care of calling `onCreateConnection` when a new+ // connection is created. The `pg` module doesn't provide an async hook+ // for the connection creation. We need to call the method explicitly.+ if (this.#config?.onCreateConnection) {+ await this.#config.onCreateConnection(connection)+ }+ }++ return connection+ }++ async beginTransaction(+ connection: DatabaseConnection,+ settings: TransactionSettings+ ): Promise<void> {+ if (settings.isolationLevel) {+ await connection.executeQuery(+ CompiledQuery.raw(`start transaction isolation level ${settings.isolationLevel}`)+ )+ } else {+ await connection.executeQuery(CompiledQuery.raw('begin'))+ }+ }++ async commitTransaction(connection: DatabaseConnection): Promise<void> {+ await connection.executeQuery(CompiledQuery.raw('commit'))+ }++ async rollbackTransaction(connection: DatabaseConnection): Promise<void> {+ await connection.executeQuery(CompiledQuery.raw('rollback'))+ }++ async releaseConnection(connection: PostgresConnection): Promise<void> {+ connection[PRIVATE_RELEASE_METHOD]()+ }++ async destroy(): Promise<void> {+ if (this.#pool) {+ const pool = this.#pool+ this.#pool = undefined+ await pool.end()+ }+ }+}++interface PostgresConnectionOptions {+ cursor: PostgresCursorConstructor | null+}++class PostgresConnection implements DatabaseConnection {+ #client: PoolClient+ #options: PostgresConnectionOptions++ constructor(client: PoolClient, options: PostgresConnectionOptions) {+ this.#client = client+ this.#options = options+ }++ async executeQuery<O>(compiledQuery: CompiledQuery): Promise<QueryResult<O>> {+ try {+ const result = await this.#client.queryObject<O>(compiledQuery.sql, [+ ...compiledQuery.parameters,+ ])++ if (+ result.command === 'INSERT' ||+ result.command === 'UPDATE' ||+ result.command === 'DELETE'+ ) {+ const numAffectedRows = BigInt(result.rowCount || 0)++ return {+ // TODO: remove.+ numUpdatedOrDeletedRows: numAffectedRows,+ numAffectedRows,+ rows: result.rows ?? [],+ } as any+ }++ return {+ rows: result.rows ?? [],+ }+ } catch (err) {+ throw extendStackTrace(err, new Error())+ }+ }++ async *streamQuery<O>(+ _compiledQuery: CompiledQuery,+ chunkSize: number+ ): AsyncIterableIterator<QueryResult<O>> {+ if (!this.#options.cursor) {+ throw new Error(+ "'cursor' is not present in your postgres dialect config. It's required to make streaming work in postgres."+ )+ }++ if (!Number.isInteger(chunkSize) || chunkSize <= 0) {+ throw new Error('chunkSize must be a positive integer')+ }++ // stream not available+ return null+ }++ [PRIVATE_RELEASE_METHOD](): void {+ this.#client.release()+ }+}diff --git a/examples/edge-functions/supabase/functions/kysely-postgres/index.ts b/examples/edge-functions/supabase/functions/kysely-postgres/index.tsnew file mode 100644index 00000000000..2fd3a97cf7c--- /dev/null+++ b/examples/edge-functions/supabase/functions/kysely-postgres/index.ts@@ -0,0 +1,90 @@+// Follow this setup guide to integrate the Deno language server with your editor:+// https://deno.land/manual/getting_started/setup_your_environment+// This enables autocomplete, go to definition, etc.++import { serve } from 'std/server'+import { Pool } from 'postgres'+import {+ Kysely,+ Generated,+ PostgresAdapter,+ PostgresIntrospector,+ PostgresQueryCompiler,+} from 'kysely'+import { PostgresDriver } from './DenoPostgresDriver.ts'++console.log(`Function "kysely-postgres" up and running!`)++interface AnimalTable {+ id: Generated<bigint>+ animal: string+ created_at: Date+}++// Keys of this interface are table names.+interface Database {+ animals: AnimalTable+}++// Create a database pool with one connection.+const pool = new Pool(+ {+ tls: { caCertificates: [Deno.env.get('DB_SSL_CERT')!] },+ database: 'postgres',+ hostname: 'db.bljghubhkofddfrezkhn.supabase.co',+ user: 'postgres',+ port: 5432,+ password: Deno.env.get('DB_PASSWORD'),+ },+ 1+)++// You'd create one of these when you start your app.+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 PostgresDriver({ pool })+ },+ createIntrospector(db: Kysely<unknown>) {+ return new PostgresIntrospector(db)+ },+ createQueryCompiler() {+ return new PostgresQueryCompiler()+ },+ },+})++serve(async (_req) => {+ try {+ // Run a query+ const animals = await db.selectFrom('animals').select(['id', 'animal', 'created_at']).execute()++ // Neat, it's properly typed \o/+ console.log(animals[0].created_at.getFullYear())++ // Encode the result as pretty printed JSON+ const body = JSON.stringify(+ animals,+ (key, value) => (typeof value === 'bigint' ? value.toString() : value),+ 2+ )++ // Return the response with the correct content type header+ return new Response(body, {+ status: 200,+ headers: {+ 'Content-Type': 'application/json; charset=utf-8',+ },+ })+ } catch (err) {+ console.error(err)+ return new Response(String(err?.message ?? err), { status: 500 })+ }+})++// To invoke: navigate to http://localhost:54321/functions/v1/kysely-postgres