|
| 1 | +--- |
| 2 | +icon: ph:check-circle-duotone |
| 3 | +--- |
| 4 | + |
| 5 | +# Database Capabilities |
| 6 | + |
| 7 | +> Query database feature support at runtime with `db.capabilities`. |
| 8 | +
|
| 9 | +Different databases support different features. The `capabilities` property lets you check what the current database supports, enabling you to write portable code that adapts to the underlying database. |
| 10 | + |
| 11 | +## Usage |
| 12 | + |
| 13 | +Access capabilities directly on the database instance: |
| 14 | + |
| 15 | +```ts |
| 16 | +import { createDatabase } from "db0"; |
| 17 | +import sqlite from "db0/connectors/better-sqlite3"; |
| 18 | + |
| 19 | +const db = createDatabase(sqlite({})); |
| 20 | + |
| 21 | +console.log(db.capabilities); |
| 22 | +// { supportsJSON: true, supportsBooleans: false, supportsArrays: false, ... } |
| 23 | + |
| 24 | +if (db.capabilities.supportsArrays) { |
| 25 | + // Use PostgreSQL array syntax |
| 26 | +} else { |
| 27 | + // Use JSON or comma-separated values |
| 28 | +} |
| 29 | +``` |
| 30 | + |
| 31 | +## Available Flags |
| 32 | + |
| 33 | +| Flag | Description | |
| 34 | +| ----------------------- | ------------------------------------------------ | |
| 35 | +| `supportsJSON` | Native JSON column type and functions | |
| 36 | +| `supportsBooleans` | Native boolean type (not 0/1 integers) | |
| 37 | +| `supportsArrays` | Native array column type | |
| 38 | +| `supportsDates` | Native date/timestamp types with timezone | |
| 39 | +| `supportsUUIDs` | Native UUID column type | |
| 40 | +| `supportsTransactions` | Transaction support with BEGIN/COMMIT/ROLLBACK | |
| 41 | +| `supportsBatch` | Batch execution of multiple statements | |
| 42 | + |
| 43 | +## Capabilities by Connector |
| 44 | + |
| 45 | +| Connector | JSON | Bool | Array | Date | UUID | Tx | Batch | |
| 46 | +| ----------------------- |:----:|:----:|:-----:|:----:|:----:|:--:|:-----:| |
| 47 | +| better-sqlite3 | ✓ | — | — | — | — | ✓ | ✓ | |
| 48 | +| sqlite3 | ✓ | — | — | — | — | ✓ | ✓ | |
| 49 | +| bun-sqlite | ✓ | — | — | — | — | ✓ | ✓ | |
| 50 | +| node-sqlite | ✓ | — | — | — | — | ✓ | ✓ | |
| 51 | +| libsql | ✓ | — | — | — | — | ✓ | ✓ | |
| 52 | +| cloudflare-d1 | ✓ | — | — | — | — | ✓ | ✓ | |
| 53 | +| postgresql | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | |
| 54 | +| pglite | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | |
| 55 | +| mysql2 | ✓ | ✓ | — | ✓ | — | ✓ | ✓ | |
| 56 | +| planetscale | ✓ | ✓ | — | ✓ | — | ✓ | ✓ | |
| 57 | + |
| 58 | +> [!NOTE] |
| 59 | +> Cloudflare Hyperdrive connectors inherit the capabilities of their underlying database (PostgreSQL or MySQL). |
| 60 | +
|
| 61 | +## Use Cases |
| 62 | + |
| 63 | +### Conditional Feature Usage |
| 64 | + |
| 65 | +Adapt your queries based on database support: |
| 66 | + |
| 67 | +```ts |
| 68 | +function storeList(db: Database, items: string[]) { |
| 69 | + if (db.capabilities.supportsArrays) { |
| 70 | + return db.sql`INSERT INTO data (items) VALUES (${items})`; |
| 71 | + } else { |
| 72 | + return db.sql`INSERT INTO data (items) VALUES (${JSON.stringify(items)})`; |
| 73 | + } |
| 74 | +} |
| 75 | +``` |
| 76 | + |
| 77 | +### Runtime Validation |
| 78 | + |
| 79 | +Ensure the database meets your application requirements: |
| 80 | + |
| 81 | +```ts |
| 82 | +function initDatabase(db: Database) { |
| 83 | + if (!db.capabilities.supportsTransactions) { |
| 84 | + throw new Error("This application requires transaction support"); |
| 85 | + } |
| 86 | +} |
| 87 | +``` |
| 88 | + |
| 89 | +### Feature Detection in Libraries |
| 90 | + |
| 91 | +Build database-agnostic libraries that adapt automatically: |
| 92 | + |
| 93 | +```ts |
| 94 | +export function createRepository(db: Database) { |
| 95 | + return { |
| 96 | + saveTags(id: string, tags: string[]) { |
| 97 | + const value = db.capabilities.supportsArrays |
| 98 | + ? tags |
| 99 | + : tags.join(","); |
| 100 | + return db.sql`UPDATE items SET tags = ${value} WHERE id = ${id}`; |
| 101 | + }, |
| 102 | + }; |
| 103 | +} |
| 104 | +``` |
0 commit comments