-
Notifications
You must be signed in to change notification settings - Fork 50
feat: add database capabilities #207
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
onmax
wants to merge
2
commits into
unjs:main
Choose a base branch
from
onmax:feat/capabilities
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,112 @@ | ||
| --- | ||
| icon: ph:check-circle-duotone | ||
| --- | ||
|
|
||
| # Database Capabilities | ||
|
|
||
| > The `db.capabilities` property exposes database feature support at runtime. | ||
|
|
||
| Use capabilities to write portable code that adapts to the underlying database. | ||
|
|
||
| ## Usage | ||
|
|
||
| You can access capabilities directly on the database instance: | ||
|
|
||
| ```ts | ||
| import { createDatabase } from "db0"; | ||
| import sqlite from "db0/connectors/better-sqlite3"; | ||
|
|
||
| const db = createDatabase(sqlite({})); | ||
|
|
||
| console.log(db.capabilities); | ||
| // { supportsJSON: true, supportsBooleans: false, supportsArrays: false, ... } | ||
|
|
||
| if (db.capabilities.supportsArrays) { | ||
| // Use PostgreSQL array syntax | ||
| } else { | ||
| // Use JSON or comma-separated values | ||
| } | ||
| ``` | ||
|
|
||
| ## Available Flags | ||
|
|
||
| | Flag | Description | | ||
| | ----------------------- | ------------------------------------------------------------- | | ||
| | `supportsJSON` | The database supports native JSON column types and functions. | | ||
| | `supportsBooleans` | The database supports native boolean types (not 0/1). | | ||
| | `supportsArrays` | The database supports native array column types. | | ||
| | `supportsDates` | The database supports native date/timestamp types. | | ||
| | `supportsUUIDs` | The database supports native UUID column types. | | ||
| | `supportsTransactions` | The database supports transactions. | | ||
| | `supportsBatch` | The database supports batch execution of statements. | | ||
|
|
||
| ## Capabilities by Connector | ||
|
|
||
| > [!TIP] | ||
| > See [db-compat.onmax.me](https://db-compat.onmax.me) for a comprehensive database feature comparison. | ||
|
|
||
| <!-- automd:file src="./_capabilities-table.md" --> | ||
|
|
||
| <!-- Auto-generated by scripts/gen-capabilities-docs.ts. Do not edit manually. --> | ||
| | Connector | JSON | Bool | Array | Date | UUID | Tx | Batch | | ||
| | :--------:|:---:|:---:|:----:|:---:|:---:|:-:|:----: | | ||
| | better-sqlite3 | ✓ | — | — | — | — | ✓ | ✓ | | ||
| | sqlite3 | ✓ | — | — | — | — | ✓ | ✓ | | ||
| | bun-sqlite | ✓ | — | — | — | — | ✓ | ✓ | | ||
| | node-sqlite | ✓ | — | — | — | — | ✓ | ✓ | | ||
| | libsql | ✓ | — | — | — | — | ✓ | ✓ | | ||
| | cloudflare-d1 | ✓ | — | — | — | — | ✓ | ✓ | | ||
| | postgresql | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | | ||
| | pglite | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | | ||
| | mysql2 | ✓ | ✓ | — | ✓ | — | ✓ | ✓ | | ||
| | planetscale | ✓ | ✓ | — | ✓ | — | ✓ | ✓ | | ||
|
|
||
| <!-- /automd --> | ||
|
|
||
| > [!NOTE] | ||
| > Cloudflare Hyperdrive connectors inherit the capabilities of their underlying database (PostgreSQL or MySQL). | ||
|
|
||
| ## Use Cases | ||
|
|
||
| ### Conditional Feature Usage | ||
|
|
||
| You can adapt your queries based on database support: | ||
|
|
||
| ```ts | ||
| function storeList(db: Database, items: string[]) { | ||
| if (db.capabilities.supportsArrays) { | ||
| return db.sql`INSERT INTO data (items) VALUES (${items})`; | ||
| } else { | ||
| return db.sql`INSERT INTO data (items) VALUES (${JSON.stringify(items)})`; | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| ### Runtime Validation | ||
|
|
||
| You can validate that the database meets your application's requirements: | ||
|
|
||
| ```ts | ||
| function initDatabase(db: Database) { | ||
| if (!db.capabilities.supportsTransactions) { | ||
| throw new Error("This application requires transaction support"); | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| ### Feature Detection in Libraries | ||
|
|
||
| You can build database-agnostic libraries that adapt automatically: | ||
|
|
||
| ```ts | ||
| export function createRepository(db: Database) { | ||
| return { | ||
| saveTags(id: string, tags: string[]) { | ||
| const value = db.capabilities.supportsArrays | ||
| ? tags | ||
| : tags.join(","); | ||
| return db.sql`UPDATE items SET tags = ${value} WHERE id = ${id}`; | ||
| }, | ||
| }; | ||
| } | ||
| ``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| <!-- Auto-generated by scripts/gen-capabilities-docs.ts from src/capabilities.ts. Do not edit manually. --> | ||
| | Connector | JSON | Bool | Array | Date | UUID | Tx | Batch | | ||
| | :--------:|:---:|:---:|:----:|:---:|:---:|:-:|:----: | | ||
| | better-sqlite3 | ✓ | — | — | — | — | ✓ | ✓ | | ||
| | sqlite3 | ✓ | — | — | — | — | ✓ | ✓ | | ||
| | bun-sqlite | ✓ | — | — | — | — | ✓ | ✓ | | ||
| | node-sqlite | ✓ | — | — | — | — | ✓ | ✓ | | ||
| | libsql | ✓ | — | — | — | — | ✓ | ✓ | | ||
| | cloudflare-d1 | ✓ | — | — | — | — | ✓ | ✓ | | ||
| | postgresql | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | | ||
| | pglite | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | | ||
| | cloudflare-hyperdrive-postgresql | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | ✓ | | ||
| | mysql2 | ✓ | ✓ | — | ✓ | — | ✓ | ✓ | | ||
| | planetscale | ✓ | ✓ | — | ✓ | — | ✓ | ✓ | | ||
| | cloudflare-hyperdrive-mysql | ✓ | ✓ | — | ✓ | — | ✓ | ✓ | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| import { writeFile } from "node:fs/promises"; | ||
| import { fileURLToPath } from "node:url"; | ||
| import { connectorCapabilities } from "../src/connectors/_internal/capabilities.ts"; | ||
| import type { DatabaseCapabilities } from "../src/types.ts"; | ||
|
|
||
| const outputFile = fileURLToPath(new URL("../docs/1.guide/_capabilities-table.md", import.meta.url)); | ||
|
|
||
| const db0Connectors = [ | ||
| "better-sqlite3", "sqlite3", "bun-sqlite", "node-sqlite", "libsql", "cloudflare-d1", | ||
| "postgresql", "pglite", "cloudflare-hyperdrive-postgresql", | ||
| "mysql2", "planetscale", "cloudflare-hyperdrive-mysql", | ||
| ]; | ||
|
|
||
| const capabilityLabels: Record<keyof DatabaseCapabilities, string> = { | ||
| supportsJSON: "JSON", | ||
| supportsBooleans: "Bool", | ||
| supportsArrays: "Array", | ||
| supportsDates: "Date", | ||
| supportsUUIDs: "UUID", | ||
| supportsTransactions: "Tx", | ||
| supportsBatch: "Batch", | ||
| }; | ||
|
|
||
| const check = "✓"; | ||
| const cross = "—"; | ||
|
|
||
| function generateTable(connectorCapabilities: Record<string, DatabaseCapabilities>): string { | ||
| const headers = ["Connector", ...Object.values(capabilityLabels)]; | ||
| const separator = headers.map((h) => ":".padEnd(h.length, "-") + ":"); | ||
|
|
||
| const rows = Object.entries(connectorCapabilities).map(([name, caps]) => { | ||
| const cells = Object.keys(capabilityLabels).map((key) => | ||
| caps[key as keyof DatabaseCapabilities] ? check : cross, | ||
| ); | ||
| return [name, ...cells]; | ||
| }); | ||
|
|
||
| const lines = [headers.join(" | "), separator.join("|"), ...rows.map((row) => row.join(" | "))]; | ||
| return lines.map((line) => `| ${line} |`).join("\n"); | ||
| } | ||
|
|
||
| const orderedCapabilities: Record<string, DatabaseCapabilities> = Object.fromEntries( | ||
| db0Connectors.map((id) => { | ||
| const caps = connectorCapabilities[id]; | ||
| if (!caps) { | ||
| throw new Error(`No capabilities mapping found for connector: ${id}`); | ||
| } | ||
| return [id, caps] as const; | ||
| }), | ||
| ); | ||
|
|
||
| const content = `<!-- Auto-generated by scripts/gen-capabilities-docs.ts from src/capabilities.ts. Do not edit manually. --> | ||
| ${generateTable(orderedCapabilities)} | ||
| `; | ||
|
|
||
| await writeFile(outputFile, content, "utf8"); | ||
| console.log("Generated capabilities table to", outputFile); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| import type { DatabaseCapabilities, SQLDialect } from "./types.ts"; | ||
|
|
||
| const sqlite: DatabaseCapabilities = { | ||
| supportsJSON: true, | ||
| supportsBooleans: false, | ||
| supportsArrays: false, | ||
| supportsDates: false, | ||
| supportsUUIDs: false, | ||
| supportsTransactions: true, | ||
| supportsBatch: true, | ||
| }; | ||
|
|
||
| const postgresql: DatabaseCapabilities = { | ||
| supportsJSON: true, | ||
| supportsBooleans: true, | ||
| supportsArrays: true, | ||
| supportsDates: true, | ||
| supportsUUIDs: true, | ||
| supportsTransactions: true, | ||
| supportsBatch: true, | ||
| }; | ||
|
|
||
| const mysql: DatabaseCapabilities = { | ||
| supportsJSON: true, | ||
| supportsBooleans: true, | ||
| supportsArrays: false, | ||
| supportsDates: true, | ||
| supportsUUIDs: false, | ||
| supportsTransactions: true, | ||
| supportsBatch: true, | ||
| }; | ||
|
|
||
| export const dialectCapabilities: Record<SQLDialect, DatabaseCapabilities> = { | ||
| sqlite, | ||
| libsql: sqlite, | ||
| postgresql, | ||
| mysql, | ||
| }; | ||
|
|
||
| export function getCapabilities( | ||
| dialect: SQLDialect, | ||
| overrides?: Partial<DatabaseCapabilities>, | ||
| ): DatabaseCapabilities { | ||
| return overrides | ||
| ? { ...dialectCapabilities[dialect], ...overrides } | ||
| : dialectCapabilities[dialect]; | ||
| } | ||
|
|
||
| export type { DatabaseCapabilities } from "./types.ts"; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| import { dialectCapabilities } from "../../capabilities.ts"; | ||
| import type { DatabaseCapabilities } from "../../types.ts"; | ||
|
|
||
| // Connector-to-capabilities mapping for documentation generation | ||
| export const connectorCapabilities: Record<string, DatabaseCapabilities> = { | ||
| "better-sqlite3": dialectCapabilities.sqlite, | ||
| sqlite3: dialectCapabilities.sqlite, | ||
| "bun-sqlite": dialectCapabilities.sqlite, | ||
| "node-sqlite": dialectCapabilities.sqlite, | ||
| libsql: dialectCapabilities.libsql, | ||
| "cloudflare-d1": dialectCapabilities.sqlite, | ||
| postgresql: dialectCapabilities.postgresql, | ||
| pglite: dialectCapabilities.postgresql, | ||
| "cloudflare-hyperdrive-postgresql": dialectCapabilities.postgresql, | ||
| mysql2: dialectCapabilities.mysql, | ||
| planetscale: dialectCapabilities.mysql, | ||
| "cloudflare-hyperdrive-mysql": dialectCapabilities.mysql, | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Currently, it looks like a dialect-mapped table. I suggest exposing it as a subpath like
db0/capabilitieswith a map from dialects to capabilities users can use.Later we might only expose
capabilityOverridesfrom connectors that have exception (for example have one less or extra feature)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i amended pr with this comments