PGlite is a WASM Postgres build packaged into a TypeScript client library that enables you to run Postgres in the browser, Node.js and Bun, with no need to install any other dependencies.
—
Quality
Pending
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
Pending
The risk profile of this skill
PGlite supports multiple filesystem implementations for different environments and persistence requirements, from ephemeral in-memory storage to persistent browser and Node.js storage.
In-memory filesystem for temporary databases that don't persist after process exit.
/**
* In-memory filesystem implementation
* Data is lost when the process exits
*/
class MemoryFS implements Filesystem {
constructor();
readonly fsType: 'memoryfs';
syncToFs(mod: any): Promise<void>;
dumpTar(mod: any, compression?: DumpTarCompressionOptions): Promise<File | Blob>;
}Browser-based persistent storage using IndexedDB for client-side data persistence.
/**
* IndexedDB filesystem for browser persistence
* Data persists across browser sessions
*/
class IdbFs implements Filesystem {
constructor(dataDir: string);
readonly fsType: 'idbfs';
syncToFs(mod: any): Promise<void>;
dumpTar(mod: any, compression?: DumpTarCompressionOptions): Promise<File | Blob>;
}Server-side filesystem using the Node.js file system for persistent storage.
/**
* Node.js filesystem implementation
* Uses the native file system for persistence
*/
class NodeFS implements Filesystem {
constructor(dataDir: string);
readonly fsType: 'nodefs';
syncToFs(mod: any): Promise<void>;
dumpTar(mod: any, compression?: DumpTarCompressionOptions): Promise<File | Blob>;
}Advanced browser filesystem using Origin Private File System with access handle pooling for better performance.
/**
* Origin Private File System with Access Handle Pool
* High-performance browser storage with concurrent access
*/
class OpfsAhpFS implements Filesystem {
constructor(dataDir: string, options?: OpfsAhpOptions);
readonly fsType: 'opfs-ahp';
syncToFs(mod: any): Promise<void>;
dumpTar(mod: any, compression?: DumpTarCompressionOptions): Promise<File | Blob>;
}
interface OpfsAhpOptions {
/** Maximum number of concurrent file handles */
maxHandles?: number;
/** Handle timeout in milliseconds */
handleTimeout?: number;
}Abstract base class and utilities for custom filesystem implementations.
/**
* Abstract base class for filesystem implementations
*/
abstract class BaseFilesystem implements Filesystem {
abstract readonly fsType: FsType;
abstract syncToFs(mod: any): Promise<void>;
abstract dumpTar(mod: any, compression?: DumpTarCompressionOptions): Promise<File | Blob>;
}
/**
* Base class for Emscripten built-in filesystems
*/
abstract class EmscriptenBuiltinFilesystem extends BaseFilesystem {
constructor(dataDir: string);
protected readonly dataDir: string;
}Common interface implemented by all filesystem types.
interface Filesystem {
/** Filesystem type identifier */
readonly fsType: FsType;
/**
* Synchronize in-memory state to persistent storage
* @param mod - Emscripten module instance
*/
syncToFs(mod: any): Promise<void>;
/**
* Export filesystem contents as tarball
* @param mod - Emscripten module instance
* @param compression - Compression options
* @returns Tarball file or blob
*/
dumpTar(
mod: any,
compression?: DumpTarCompressionOptions
): Promise<File | Blob>;
}
type FsType = 'memoryfs' | 'idbfs' | 'nodefs' | 'opfs-ahp';
interface FsStats {
/** File size in bytes */
size: number;
/** Last modification time */
mtime: Date;
/** Whether this is a directory */
isDirectory: boolean;
/** Whether this is a regular file */
isFile: boolean;
}Constants used by filesystem implementations.
/** WebAssembly filesystem mount prefix */
const WASM_PREFIX = "/tmp/pglite";
/** PostgreSQL data directory path within WASM */
const PGDATA = `${WASM_PREFIX}/base`;
/** Common errno codes for filesystem operations */
const ERRNO_CODES: {
readonly ENOENT: number; // No such file or directory
readonly EIO: number; // I/O error
readonly EACCES: number; // Permission denied
readonly EBUSY: number; // Device or resource busy
readonly EEXIST: number; // File exists
readonly ENOTDIR: number; // Not a directory
readonly EISDIR: number; // Is a directory
readonly EINVAL: number; // Invalid argument
readonly EFBIG: number; // File too large
readonly ENOSPC: number; // No space left on device
readonly EROFS: number; // Read-only file system
readonly EMLINK: number; // Too many links
readonly EPIPE: number; // Broken pipe
};Compression and extraction utilities for filesystem export/import.
interface DumpTarCompressionOptions {
/** Compression algorithm to use */
compression?: 'gzip' | 'none';
/** Compression level (1-9 for gzip) */
level?: number;
}type FilesystemType = 'nodefs' | 'idbfs' | 'memoryfs' | 'opfs-ahp';Usage Examples:
import { PGlite, MemoryFS, IdbFs } from "@electric-sql/pglite";
import { NodeFS } from "@electric-sql/pglite/nodefs";
import { OpfsAhpFS } from "@electric-sql/pglite/opfs-ahp";
// In-memory database (data lost on exit)
const memoryDb = new PGlite({
fs: new MemoryFS()
});
// Browser persistent storage
const browserDb = new PGlite({
dataDir: "my-app-db",
fs: new IdbFs("my-app-db")
});
// Node.js persistent storage
const nodeDb = new PGlite({
dataDir: "./data/mydb",
fs: new NodeFS("./data/mydb")
});
// High-performance browser storage
const opfsDb = new PGlite({
dataDir: "fast-db",
fs: new OpfsAhpFS("fast-db", {
maxHandles: 20,
handleTimeout: 5000
})
});
// Export database to tarball
const tarball = await browserDb.dumpDataDir({
compression: 'gzip',
level: 6
});
// Load database from tarball
const restoredDb = new PGlite({
loadDataDir: tarball,
fs: new IdbFs("restored-db")
});Install with Tessl CLI
npx tessl i tessl/npm-electric-sql--pglite