new·Earn with mozg — 20% of every monthSend somebody here and take a fifth of every plan payment they make, for as long as they keep paying — not a bounty on the first invoice. Your handle is the link, the window is thirty days, and the commission lands on your balance the second they pay. Free to join: if you have signed in, you already have the link. mozg.sh/earnall news →
mozg.beta
Sign in

Supabase · Edge Functions · all subjects

edge functions/database

8 notes, read out of this brain and free to use. Each one was extracted from a source and is re-checked against its exam.

Kysely type-safe SQL query builder with Deno Postgres

Kysely is a type-safe and autocompletion-friendly TypeScript SQL query builder. Combining Kysely with Deno Postgres gives a convenient developer experience for interacting directly with a Postgres database from Supabase Edge Functions.

Pool connection with one connection example

Creating a database pool: new Pool({ tls: { caCertificates: [Deno.env.get('DB_SSL_CERT')!] }, database: 'postgres', hostname: Deno.env.get('DB_HOSTNAME'), user: 'postgres', port: 5432, password: Deno.env.get('DB_PASSWORD') }, 1) where the second argument is the number of connections.

Kysely database instance with PostgreSQL dialect

Create a Kysely database instance by instantiating new Kysely<Database> with a dialect object that implements createAdapter(), createDriver(), createIntrospector(), and createQueryCompiler() methods using PostgresAdapter, PostgresDriver, PostgresIntrospector, and PostgresQueryCompiler.

PostgresDriver implementation for Kysely

The PostgresDriver class implements the Driver interface and manages connection pooling with Deno Postgres. It requires init(), acquireConnection(), beginTransaction(), commitTransaction(), rollbackTransaction(), releaseConnection(), and destroy() methods. It uses a PostgresConnection class that implements DatabaseConnection interface with executeQuery() and streamQuery() methods.

Type-safe table schema definition with Kysely

Define a Database interface with table names as keys and table schema interfaces as values. Use Generated<T> type for auto-generated columns like id. Example: interface AnimalTable { id: Generated<bigint>; animal: string; created_at: Date; }

Kysely query execution with type safety

Execute queries with full type safety: const animals = await db.selectFrom('animals').select(['id', 'animal', 'created_at']).execute(). Results are properly typed, allowing access to typed properties like animals[0].created_at.getFullYear().

Handle BigInt serialization in JSON responses

BigInt values cannot be directly serialized to JSON. Convert them to strings before returning in a Response.json(). Example: map animal entries and convert bigint values to strings using typeof value === 'bigint' ? value.toString() : value.

PostgreSQL command types in query results

The deno-postgres queryObject result includes a command property that indicates the operation type: INSERT, UPDATE, or DELETE. For these commands, rowCount contains the number of affected rows.

Give your agent this brain