CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-prisma--adapter-planetscale

Prisma's driver adapter for PlanetScale serverless database connectivity over HTTP

Pending

Quality

Pending

Does it follow best practices?

Impact

Pending

No eval scenarios have been run

Overview
Eval results
Files

index.mddocs/

Prisma Adapter PlanetScale

Prisma's driver adapter for PlanetScale's serverless database driver. This adapter enables HTTP-based database communication with PlanetScale databases through Prisma ORM, providing improved connection reliability and performance for serverless and edge environments.

Package Information

  • Package Name: @prisma/adapter-planetscale
  • Package Type: npm
  • Language: TypeScript
  • Installation: npm install @prisma/adapter-planetscale

Core Imports

import { PrismaPlanetScale } from "@prisma/adapter-planetscale";

For CommonJS:

const { PrismaPlanetScale } = require("@prisma/adapter-planetscale");

Basic Usage

import { PrismaPlanetScale } from "@prisma/adapter-planetscale";
import { PrismaClient } from "@prisma/client";

// Create adapter with PlanetScale configuration
const adapter = new PrismaPlanetScale({
  url: process.env.DATABASE_URL,
  fetch: fetch // Use built-in fetch (Node.js 18+) or provide your own
});

// Initialize Prisma Client with the adapter
const prisma = new PrismaClient({ adapter });

// Use Prisma Client as normal with full type-safety
const users = await prisma.user.findMany();

Architecture

The PlanetScale adapter is built around several key components:

  • Factory Pattern: PrismaPlanetScale (alias for PrismaPlanetScaleAdapterFactory) creates adapter instances
  • Adapter Implementation: PrismaPlanetScaleAdapter implements Prisma's SqlDriverAdapter interface
  • Connection Management: HTTP-based connections through PlanetScale's serverless driver
  • Transaction Support: Full transaction support with mutex-based synchronization
  • Type Conversion: Automatic conversion between PlanetScale and Prisma data types
  • Error Handling: Comprehensive MySQL error code mapping with structured error types

Capabilities

Adapter Factory

Creates PlanetScale adapter instances with flexible configuration options.

/**
 * Factory class for creating PlanetScale adapters
 */
class PrismaPlanetScale {
  readonly provider: "mysql";
  readonly adapterName: string;
  
  /**
   * Create adapter factory with PlanetScale configuration or client instance
   * @param arg - PlanetScale configuration object or existing client instance
   */
  constructor(arg: Config | Client);
  
  /**
   * Connect and create a new adapter instance
   * @returns Promise resolving to configured adapter
   */
  connect(): Promise<PrismaPlanetScaleAdapter>;
}

Database Operations

Core database operation capabilities through the adapter interface.

class PrismaPlanetScaleAdapter {
  readonly provider: "mysql";
  readonly adapterName: string;
  
  /**
   * Execute a raw SQL query and return result set
   * @param query - SQL query with parameters and type information
   * @returns Promise resolving to query results with column metadata
   */
  queryRaw(query: SqlQuery): Promise<SqlResultSet>;
  
  /**
   * Execute a raw SQL statement and return affected row count
   * @param query - SQL query with parameters and type information
   * @returns Promise resolving to number of affected rows
   */
  executeRaw(query: SqlQuery): Promise<number>;
  
  /**
   * Get connection information for the current database connection
   * @returns Connection metadata including schema name and capabilities
   */
  getConnectionInfo(): ConnectionInfo;
  
  /**
   * Start a new database transaction with optional isolation level
   * @param isolationLevel - Transaction isolation level (optional)
   * @returns Promise resolving to transaction instance
   */
  startTransaction(isolationLevel?: IsolationLevel): Promise<Transaction>;
  
  /**
   * Execute a SQL script (currently not implemented)
   * @param script - SQL script to execute
   * @returns Promise that throws "Not implemented yet" error
   */
  executeScript(script: string): Promise<void>;
  
  /**
   * Clean up adapter resources
   * @returns Promise resolving when cleanup is complete
   */
  dispose(): Promise<void>;
  
  /**
   * Access the underlying PlanetScale client instance
   * @returns The PlanetScale client used by this adapter
   */
  underlyingDriver(): Client;
}

Transaction Management

Full transaction support with commit/rollback capabilities.

interface Transaction {
  readonly options: TransactionOptions;
  
  /**
   * Execute a raw SQL query within the transaction
   * @param query - SQL query with parameters and type information
   * @returns Promise resolving to query results
   */
  queryRaw(query: SqlQuery): Promise<SqlResultSet>;
  
  /**
   * Execute a raw SQL statement within the transaction
   * @param query - SQL query with parameters and type information
   * @returns Promise resolving to number of affected rows
   */
  executeRaw(query: SqlQuery): Promise<number>;
  
  /**
   * Commit the transaction, making all changes permanent
   * @returns Promise resolving when commit is complete
   */
  commit(): Promise<void>;
  
  /**
   * Rollback the transaction, undoing all changes
   * @returns Promise resolving when rollback is complete
   */
  rollback(): Promise<void>;
}

Types

/**
 * SQL query structure with parameters and type information
 */
interface SqlQuery {
  /** SQL statement text */
  sql: string;
  /** Query parameter values */
  args: any[];
  /** Parameter type information */
  argTypes: ArgType[];
}

/**
 * Query result set with column metadata
 */
interface SqlResultSet {
  /** Column names in result order */
  columnNames: string[];
  /** Column type information */
  columnTypes: ColumnType[];
  /** Result data rows */
  rows: any[][];
  /** Last inserted ID (for INSERT operations) */
  lastInsertId?: string | number;
}

/**
 * Database connection information
 */
interface ConnectionInfo {
  /** Database schema name */
  schemaName?: string;
  /** Whether relation joins are supported */
  supportsRelationJoins: boolean;
}

/**
 * Transaction configuration options
 */
interface TransactionOptions {
  /** Whether to use phantom queries for transaction handling */
  usePhantomQuery: boolean;
}

/**
 * Transaction isolation levels
 */
type IsolationLevel = 
  | "READ UNCOMMITTED"
  | "READ COMMITTED" 
  | "REPEATABLE READ"
  | "SERIALIZABLE";

/**
 * PlanetScale column type definitions
 */
type PlanetScaleColumnType = 
  | "NULL" | "INT8" | "UINT8" | "INT16" | "UINT16" | "INT24" | "UINT24"
  | "INT32" | "UINT32" | "INT64" | "UINT64" | "FLOAT32" | "FLOAT64"
  | "TIMESTAMP" | "DATE" | "TIME" | "DATETIME" | "YEAR" | "DECIMAL"
  | "TEXT" | "BLOB" | "VARCHAR" | "VARBINARY" | "CHAR" | "BINARY"
  | "BIT" | "ENUM" | "SET" | "TUPLE" | "GEOMETRY" | "JSON"
  | "EXPRESSION" | "HEXNUM" | "HEXVAL" | "BITNUM";

/**
 * Deferred promise for external resolution/rejection control
 */
interface Deferred<T> {
  /** Resolve the deferred promise with a value */
  resolve(value: T | PromiseLike<T>): void;
  /** Reject the deferred promise with an error */
  reject(reason: unknown): void;
}

/**
 * Argument scalar type definitions from driver-adapter-utils
 */
type ArgScalarType = 'int' | 'bigint' | 'float' | 'double' | 'decimal' | 'boolean' | 'char' | 'text' | 'bytes' | 'json' | 'enum' | 'datetime' | 'date' | 'time';

/**
 * Argument type metadata from driver-adapter-utils
 */
interface ArgType {
  /** Scalar type classification */
  scalarType: ArgScalarType;
  /** Database-specific type identifier */
  dbType: string;
}

/**
 * Column type enumeration from driver-adapter-utils
 */
type ColumnType = 'int32' | 'int64' | 'float' | 'double' | 'numeric' | 'boolean' | 'char' | 'text' | 'date' | 'time' | 'datetime' | 'json' | 'enum' | 'bytes' | 'uuid' | 'xml';

/**
 * PlanetScale configuration object (from @planetscale/database)
 */
interface Config {
  /** Database connection URL */
  url: string;
  /** Custom fetch implementation (optional, defaults to global fetch) */
  fetch?: typeof fetch;
  /** Additional PlanetScale client options */
  [key: string]: any;
}

/**
 * PlanetScale client instance (from @planetscale/database)  
 */
interface Client {
  /** Client configuration */
  config: Config;
  /** Execute SQL query */
  execute(sql: string, args: any[], options?: any): Promise<any>;
  /** Get connection instance */
  connection(): Connection;
}

/**
 * PlanetScale connection instance (from @planetscale/database)
 */  
interface Connection {
  /** Execute SQL query on connection */
  execute(sql: string, args?: any[], options?: any): Promise<any>;
  /** Start transaction on connection */
  transaction<T>(fn: (tx: Transaction) => Promise<T>): Promise<T>;
}

Install with Tessl CLI

npx tessl i tessl/npm-prisma--adapter-planetscale

docs

index.md

tile.json