CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-supabase--postgrest-js

Isomorphic PostgREST client for PostgreSQL database interactions with fluent API

Pending
Overview
Eval results
Files

client-configuration.mddocs/

Client Configuration

Core client setup and configuration for connecting to PostgREST endpoints with custom headers, schema switching, and fetch customization.

Capabilities

PostgrestClient Constructor

Creates a new PostgREST client instance for connecting to a PostgREST endpoint.

/**
 * Creates a PostgREST client
 * @param url - URL of the PostgREST endpoint
 * @param options - Named parameters
 * @param options.headers - Custom headers
 * @param options.schema - Postgres schema to switch to
 * @param options.fetch - Custom fetch implementation
 */
class PostgrestClient<
  Database = any,
  ClientOptions extends ClientServerOptions = {},
  SchemaName extends string = 'public',
  Schema extends GenericSchema = any
> {
  constructor(
    url: string,
    options?: {
      headers?: HeadersInit;
      schema?: SchemaName;
      fetch?: Fetch;
    }
  );
  
  // Properties
  url: string;
  headers: Headers;
  schemaName?: SchemaName;
  fetch?: Fetch;
}

Usage Examples:

import { PostgrestClient } from "@supabase/postgrest-js";

// Basic client
const client = new PostgrestClient("https://api.example.com");

// Client with custom headers
const clientWithAuth = new PostgrestClient("https://api.example.com", {
  headers: {
    'Authorization': 'Bearer your-token',
    'X-Custom-Header': 'value'
  }
});

// Client with specific schema
const publicClient = new PostgrestClient("https://api.example.com", {
  schema: "public"
});

// Client with custom fetch (Node.js with specific options)
const nodeClient = new PostgrestClient("https://api.example.com", {
  fetch: (url, options) => fetch(url, {
    ...options,
    timeout: 30000,
    agent: myCustomAgent
  })
});

Schema Switching

Switch to a different PostgreSQL schema for subsequent queries.

/**
 * Select a schema to query or perform an function (rpc) call.
 * The schema needs to be on the list of exposed schemas inside Supabase.
 * @param schema - The schema to query
 */
schema<DynamicSchema extends string & keyof Database>(
  schema: DynamicSchema
): PostgrestClient<Database, ClientOptions, DynamicSchema, Database[DynamicSchema]>;

Usage Examples:

const client = new PostgrestClient("https://api.example.com");

// Switch to private schema
const privateClient = client.schema("private");

// Query from private schema
const { data } = await privateClient
  .from("private_table")
  .select("*");

// Switch back to public schema
const publicClient = client.schema("public");

Table and View Queries

Access tables and views to begin query operations.

/**
 * Perform a query on a table or a view
 * @param relation - The table or view name to query
 */
from<TableName extends string & keyof Schema['Tables']>(
  relation: TableName
): PostgrestQueryBuilder<ClientOptions, Schema, Schema['Tables'][TableName], TableName>;

from<ViewName extends string & keyof Schema['Views']>(
  relation: ViewName
): PostgrestQueryBuilder<ClientOptions, Schema, Schema['Views'][ViewName], ViewName>;

from(relation: string): PostgrestQueryBuilder<ClientOptions, Schema, any, any>;

Usage Examples:

const client = new PostgrestClient("https://api.example.com");

// Query a table
const usersQuery = client.from("users");

// Query a view
const activeUsersQuery = client.from("active_users_view");

// Chain immediately
const { data } = await client
  .from("products")
  .select("name, price")
  .eq("category", "electronics");

Custom Headers

Set or modify HTTP headers for requests.

/**
 * Set an HTTP header for the request
 * @param name - Header name
 * @param value - Header value
 */
setHeader(name: string, value: string): this;

Note: Headers can be set at the client level during construction or on individual query builders.

Usage Examples:

// Set headers during client construction
const client = new PostgrestClient("https://api.example.com", {
  headers: {
    'Authorization': 'Bearer token',
    'X-API-Key': 'your-api-key'
  }
});

// Set headers on individual queries
const { data } = await client
  .from("users")
  .select("*")
  .setHeader("X-Request-ID", "unique-request-id");

Custom Fetch Implementation

Provide a custom fetch implementation for specific environments or requirements.

type Fetch = typeof fetch;

Usage Examples:

import fetch from 'node-fetch';
import { PostgrestClient } from "@supabase/postgrest-js";

// Node.js with node-fetch
const client = new PostgrestClient("https://api.example.com", {
  fetch: fetch as any
});

// Custom fetch with timeout
const customFetch = (url: string, options: RequestInit) => {
  return fetch(url, {
    ...options,
    timeout: 10000 // 10 second timeout
  });
};

const clientWithTimeout = new PostgrestClient("https://api.example.com", {
  fetch: customFetch
});

// Fetch with custom user agent
const clientWithUA = new PostgrestClient("https://api.example.com", {
  fetch: (url, options) => fetch(url, {
    ...options,
    headers: {
      ...options?.headers,
      'User-Agent': 'MyApp/1.0.0'
    }
  })
});

Environment Detection

The client automatically detects the environment and uses appropriate fetch implementation:

  • Browser: Uses native fetch
  • Node.js: Uses @supabase/node-fetch when native fetch is not available
  • Custom: Uses provided fetch implementation

Schema Type Safety

When using TypeScript with database type definitions, the client provides full type safety:

// With typed database schema
interface Database {
  public: {
    Tables: {
      users: {
        Row: { id: number; name: string; email: string };
        Insert: { name: string; email: string };
        Update: { name?: string; email?: string };
      };
    };
    Views: {
      active_users: {
        Row: { id: number; name: string; last_active: string };
      };
    };
    Functions: {
      get_user_stats: {
        Args: { user_id: number };
        Returns: { total_posts: number; total_likes: number };
      };
    };
  };
}

const typedClient = new PostgrestClient<Database>("https://api.example.com");

// Full type safety for table queries
const users = await typedClient
  .from("users") // ✅ Type-safe table name
  .select("id, name, email") // ✅ Type-safe column names
  .eq("id", 123); // ✅ Type-safe filtering

// Type inference for results
// users.data will be of type: { id: number; name: string; email: string }[] | null

Install with Tessl CLI

npx tessl i tessl/npm-supabase--postgrest-js

docs

client-configuration.md

error-handling.md

filtering-conditions.md

index.md

query-operations.md

result-transformation.md

stored-procedures.md

tile.json