feat(d1): add Cloudflare D1 database support
This commit is contained in:
1 parent
977e7f78ca
commit
2a92746dc0
61 files changed
+1732
-69
No files matched your search
@@ -16,6 +16,7 @@ describe("supportsTransaction", () => {
|
||||
expect(supportsTransaction("duckdb")).toBe(false);
|
||||
expect(supportsTransaction("qdrant")).toBe(false);
|
||||
expect(supportsTransaction("turso")).toBe(false);
|
||||
expect(supportsTransaction("cloudflare-d1")).toBe(false);
|
||||
expect(supportsTransaction("sqlite")).toBe(false);
|
||||
expect(supportsTransaction("clickhouse")).toBe(false);
|
||||
expect(supportsTransaction("sqlserver")).toBe(false);
|
||||
|
||||
+1
-1
@@ -9,7 +9,7 @@ describe("sqlFormatter", () => {
|
||||
});
|
||||
|
||||
it("maps SQLite-compatible database types to the sqlite formatter dialect", () => {
|
||||
for (const dbType of ["sqlite", "rqlite", "turso"]) {
|
||||
for (const dbType of ["sqlite", "rqlite", "turso", "cloudflare-d1"]) {
|
||||
expect(sqlFormatDialectForDbType(dbType)).toBe("sqlite");
|
||||
}
|
||||
});
|
||||
|
||||
@@ -0,0 +1,23 @@
|
||||
import type { ConnectionConfig } from "@/types/database";
|
||||
|
||||
type CloudflareD1Config = Pick<ConnectionConfig, "db_type" | "host" | "database" | "password">;
|
||||
type MutableCloudflareD1Config = CloudflareD1Config & Pick<ConnectionConfig, "port" | "username" | "ssl" | "url_params" | "transport_layers">;
|
||||
|
||||
export function isCloudflareD1Connection(config: Pick<ConnectionConfig, "db_type">): boolean {
|
||||
return config.db_type === "cloudflare-d1";
|
||||
}
|
||||
|
||||
export function hasCloudflareD1Credentials(config: CloudflareD1Config): boolean {
|
||||
return !!config.host.trim() && !!config.database?.trim() && !!config.password.trim();
|
||||
}
|
||||
|
||||
export function normalizeCloudflareD1Connection(config: MutableCloudflareD1Config): void {
|
||||
config.host = config.host.trim();
|
||||
config.database = config.database?.trim() || undefined;
|
||||
config.password = config.password.trim();
|
||||
config.port = 443;
|
||||
config.username = "";
|
||||
config.ssl = true;
|
||||
config.url_params = "";
|
||||
config.transport_layers = [];
|
||||
}
|
||||
@@ -17,6 +17,7 @@ export function connectionDriverLabel(connection?: Pick<ConnectionConfig, "db_ty
|
||||
|
||||
export function connectionEndpointLabel(connection?: ConnectionPresentationConfig): string {
|
||||
if (!connection) return "";
|
||||
if (connection.db_type === "cloudflare-d1") return [connection.host, connection.database].filter(Boolean).join("/");
|
||||
if (LOCAL_DATABASE_TYPES.has(connection.db_type) || (connection.db_type === "h2" && connection.port === 0)) {
|
||||
return connection.host || connection.database || "local";
|
||||
}
|
||||
@@ -45,6 +46,7 @@ function redactConnectionHost(host: string): string {
|
||||
|
||||
export function connectionRedactedEndpointLabel(connection?: ConnectionPresentationConfig): string {
|
||||
if (!connection) return "";
|
||||
if (connection.db_type === "cloudflare-d1") return `${REDACTED_HOST_SEGMENT}/${REDACTED_HOST_SEGMENT}`;
|
||||
if (LOCAL_DATABASE_TYPES.has(connection.db_type) || (connection.db_type === "h2" && connection.port === 0)) {
|
||||
return connectionEndpointLabel(connection);
|
||||
}
|
||||
@@ -111,6 +113,9 @@ export function connectionUrlPlaceholder(dbType: DatabaseType): string {
|
||||
case "turso":
|
||||
return "https://[your-db]-[org].turso.io";
|
||||
|
||||
case "cloudflare-d1":
|
||||
return "https://api.cloudflare.com/client/v4/accounts/{account_id}/d1/database/{database_id}";
|
||||
|
||||
case "duckdb":
|
||||
return "duckdb:///absolute/path/to/database.duckdb";
|
||||
|
||||
|
||||
@@ -3,7 +3,7 @@ import { filterDatabaseNamesForVisiblePicker, normalizeVisibleDatabaseSelection
|
||||
|
||||
const DRAFT_VISIBLE_DATABASES_PREFIX = "__visible_draft_";
|
||||
|
||||
const UNSUPPORTED_VISIBLE_DATABASE_TYPES = new Set<DatabaseType>(["elasticsearch", "qdrant", "milvus", "weaviate", "chromadb", "etcd", "zookeeper"]);
|
||||
const UNSUPPORTED_VISIBLE_DATABASE_TYPES = new Set<DatabaseType>(["cloudflare-d1", "elasticsearch", "qdrant", "milvus", "weaviate", "chromadb", "etcd", "zookeeper"]);
|
||||
|
||||
type VisibleDatabaseConnectionFields = Pick<
|
||||
ConnectionConfig,
|
||||
|
||||
@@ -75,4 +75,4 @@ export const DATABASE_OBJECT_TREE_TYPES = new Set<DatabaseType>(["jdbc"]);
|
||||
|
||||
export const PG_LIKE_STRUCTURE_TYPES = new Set<DatabaseType>(["postgres", "redshift", "gaussdb", "kwdb", "opengauss", "questdb"]);
|
||||
|
||||
export const DIAGRAM_SQL_TYPES = new Set<DatabaseType>(["mysql", "postgres", "sqlite", "rqlite", "turso", "sqlserver", "oracle", "redshift", "dameng", "gaussdb", "kwdb", "opengauss", "questdb", "oceanbase-oracle"]);
|
||||
export const DIAGRAM_SQL_TYPES = new Set<DatabaseType>(["mysql", "postgres", "sqlite", "rqlite", "turso", "cloudflare-d1", "sqlserver", "oracle", "redshift", "dameng", "gaussdb", "kwdb", "opengauss", "questdb", "oceanbase-oracle"]);
|
||||
@@ -118,7 +118,7 @@ export function supportsObjectBrowserTreeNode(dbType: DatabaseType | undefined,
|
||||
}
|
||||
|
||||
export function supportsTableTruncate(dbType?: DatabaseType): boolean {
|
||||
return !!dbType && dbType !== "sqlite" && dbType !== "rqlite" && dbType !== "turso" && dbType !== "duckdb" && dbType !== "influxdb" && dbType !== "manticoresearch";
|
||||
return !!dbType && dbType !== "sqlite" && dbType !== "rqlite" && dbType !== "turso" && dbType !== "cloudflare-d1" && dbType !== "duckdb" && dbType !== "influxdb" && dbType !== "manticoresearch";
|
||||
}
|
||||
|
||||
export function usesPostgresLikeStructureCopy(dbType?: DatabaseType): boolean {
|
||||
|
||||
@@ -20,6 +20,7 @@ export const DATABASE_NAMESPACE_CREATION_MATRIX = {
|
||||
sqlite: { deferred: "file-backed; create a new connection/file instead" },
|
||||
rqlite: { deferred: "single SQLite-compatible database per node" },
|
||||
turso: { deferred: "remote libSQL database lifecycle is provider-managed" },
|
||||
"cloudflare-d1": { deferred: "Cloudflare D1 database lifecycle is provider-managed" },
|
||||
redis: { deferred: "numbered logical databases are server-configured" },
|
||||
duckdb: { connection: "attach" },
|
||||
clickhouse: { connection: "database" },
|
||||
|
||||
@@ -35,6 +35,7 @@ const DATABASE_TYPE_OBJECTS = new Map<DatabaseType, SidebarObjectKind[]>([
|
||||
["sqlite", TABLE_VIEW_OBJECTS],
|
||||
["rqlite", TABLE_VIEW_OBJECTS],
|
||||
["turso", TABLE_VIEW_OBJECTS],
|
||||
["cloudflare-d1", TABLE_VIEW_OBJECTS],
|
||||
["duckdb", TABLE_VIEW_OBJECTS],
|
||||
["clickhouse", TABLE_VIEW_OBJECTS],
|
||||
["doris", TABLE_VIEW_OBJECTS],
|
||||
|
||||
@@ -21,6 +21,7 @@ export const DATABASE_PROPERTY_EDITING_MATRIX = {
|
||||
sqlite: { deferred: "file-backed database properties are not edited in-place" },
|
||||
rqlite: { deferred: "single SQLite-compatible database per node" },
|
||||
turso: { deferred: "remote libSQL database lifecycle is provider-managed" },
|
||||
"cloudflare-d1": { deferred: "Cloudflare D1 database lifecycle is provider-managed" },
|
||||
redis: { deferred: "numbered logical databases are server-configured" },
|
||||
duckdb: { deferred: "attached database file properties need a dedicated DuckDB workflow" },
|
||||
clickhouse: { deferred: "database property editing not verified for first pass" },
|
||||
|
||||
@@ -52,6 +52,7 @@ const NAVICAT_STYLE_TABLE_DATA_TYPES = new Set<DatabaseType>([
|
||||
"sqlite",
|
||||
"rqlite",
|
||||
"turso",
|
||||
"cloudflare-d1",
|
||||
"duckdb",
|
||||
"sqlserver",
|
||||
"oracle",
|
||||
|
||||
@@ -4,7 +4,8 @@ import { usesTreeSchemaMode } from "@/lib/database/databaseCapabilities";
|
||||
export const TREE_SCHEMA_DEFAULT_DATABASE_SELECT_VALUE = "__dbx_tree_schema_default_database__";
|
||||
export const EMPTY_DATABASE_SELECT_VALUE = "__dbx_empty_database__";
|
||||
|
||||
export function resolveDefaultDatabase(connection: Pick<ConnectionConfig, "database">, options: string[]): string {
|
||||
export function resolveDefaultDatabase(connection: Pick<ConnectionConfig, "database"> & Partial<Pick<ConnectionConfig, "db_type">>, options: string[]): string {
|
||||
if (connection.db_type === "cloudflare-d1") return "main";
|
||||
return connection.database || options[0] || "";
|
||||
}
|
||||
|
||||
@@ -29,6 +30,7 @@ export function formatDatabaseLabel(connection: Pick<ConnectionConfig, "db_type"
|
||||
return database || labels.noDatabase;
|
||||
}
|
||||
|
||||
export function isDefaultDatabase(connection: Pick<ConnectionConfig, "database"> | undefined, database: string): boolean {
|
||||
export function isDefaultDatabase(connection: (Pick<ConnectionConfig, "database"> & Partial<Pick<ConnectionConfig, "db_type">>) | undefined, database: string): boolean {
|
||||
if (connection?.db_type === "cloudflare-d1") return database === "main";
|
||||
return !!connection?.database && !!database && connection.database === database;
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
export const CLOUDFLARE_D1_COMMON_FUNCTION_NAMES = new Set(["ABS", "AVG", "CAST", "CEIL", "COALESCE", "CONCAT", "COUNT", "FLOOR", "LENGTH", "LOWER", "LTRIM", "MAX", "MIN", "MOD", "NULLIF", "REPLACE", "ROUND", "RTRIM", "SIGN", "SQRT", "SUBSTR", "SUBSTRING", "SUM", "TRIM", "UPPER"]);
|
||||
@@ -155,6 +155,7 @@ export function sqlSemanticDialectFor(options: { databaseType?: DatabaseType; di
|
||||
case "sqlite":
|
||||
case "rqlite":
|
||||
case "turso":
|
||||
case "cloudflare-d1":
|
||||
return SQL_SEMANTIC_DIALECTS.sqlite;
|
||||
case "duckdb":
|
||||
return SQL_SEMANTIC_DIALECTS.duckdb;
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import { Cassandra, MariaSQL, MSSQL, MySQL, PLSQL, PostgreSQL, SQLite, StandardSQL } from "@codemirror/lang-sql";
|
||||
import type { DatabaseType, SqlSnippet } from "@/types/database";
|
||||
import { buildMongoCompletionItemsFromContext, type MongoCompletionItem } from "@/lib/mongo/mongoCompletion";
|
||||
import { CLOUDFLARE_D1_COMMON_FUNCTION_NAMES } from "@/lib/sql/cloudflareD1";
|
||||
|
||||
const SQL_KEYWORDS = [
|
||||
"SELECT",
|
||||
@@ -470,6 +471,7 @@ const DATABASE_SQL_KEYWORDS: Partial<Record<DatabaseType, string[]>> = {
|
||||
sqlite: SQLITE_SQL_KEYWORDS,
|
||||
rqlite: SQLITE_SQL_KEYWORDS,
|
||||
turso: SQLITE_SQL_KEYWORDS,
|
||||
"cloudflare-d1": SQLITE_SQL_KEYWORDS,
|
||||
sqlserver: SQLSERVER_SQL_KEYWORDS,
|
||||
manticoresearch: MANTICORESEARCH_SQL_KEYWORDS,
|
||||
};
|
||||
@@ -896,6 +898,8 @@ const SQLITE_FUNCTION_SIGNATURES = new Map<string, string[]>([
|
||||
["NOW", []],
|
||||
]);
|
||||
|
||||
const CLOUDFLARE_D1_FUNCTION_SIGNATURES = new Map(Array.from(SQLITE_FUNCTION_SIGNATURES.entries()).filter(([name]) => name !== "NOW"));
|
||||
|
||||
const SQLSERVER_FUNCTION_SIGNATURES = new Map<string, string[]>([
|
||||
["TRY_CAST", ["expression AS type"]],
|
||||
["TRY_CONVERT", ["type", "expression"]],
|
||||
@@ -958,6 +962,7 @@ const DATABASE_FUNCTION_SIGNATURES: Partial<Record<DatabaseType, Map<string, str
|
||||
sqlite: SQLITE_FUNCTION_SIGNATURES,
|
||||
rqlite: SQLITE_FUNCTION_SIGNATURES,
|
||||
turso: SQLITE_FUNCTION_SIGNATURES,
|
||||
"cloudflare-d1": CLOUDFLARE_D1_FUNCTION_SIGNATURES,
|
||||
sqlserver: SQLSERVER_FUNCTION_SIGNATURES,
|
||||
manticoresearch: MANTICORESEARCH_FUNCTION_SIGNATURES,
|
||||
};
|
||||
@@ -3727,7 +3732,8 @@ function buildSnippetItems(prefix: string, snippets: SqlSnippet[], keywordCase?:
|
||||
}
|
||||
|
||||
function activeFunctionSignatures(databaseType?: DatabaseType): Map<string, string[]> {
|
||||
const signatures = databaseType ? new Map(Array.from(SQL_FUNCTION_SIGNATURES.entries()).filter(([name]) => COMMON_SQL_FUNCTION_NAMES.has(name))) : new Map(SQL_FUNCTION_SIGNATURES);
|
||||
const commonFunctionNames = databaseType === "cloudflare-d1" ? CLOUDFLARE_D1_COMMON_FUNCTION_NAMES : COMMON_SQL_FUNCTION_NAMES;
|
||||
const signatures = databaseType ? new Map(Array.from(SQL_FUNCTION_SIGNATURES.entries()).filter(([name]) => commonFunctionNames.has(name))) : new Map(SQL_FUNCTION_SIGNATURES);
|
||||
const databaseSignatures = databaseType ? DATABASE_FUNCTION_SIGNATURES[databaseType] : undefined;
|
||||
if (databaseSignatures) {
|
||||
for (const [name, parameters] of databaseSignatures) signatures.set(name, parameters);
|
||||
|
||||
@@ -30,6 +30,7 @@ export function sqlFormatDialectForDbType(dbType: string | null | undefined): Sq
|
||||
case "sqlite":
|
||||
case "rqlite":
|
||||
case "turso":
|
||||
case "cloudflare-d1":
|
||||
return "sqlite";
|
||||
case "sqlserver":
|
||||
return "sqlserver";
|
||||
|
||||
@@ -21,7 +21,7 @@ export function supportsObjectRename(databaseType: DatabaseType | undefined, obj
|
||||
if (objectType === "PROCEDURE" || objectType === "FUNCTION") {
|
||||
return false;
|
||||
}
|
||||
if (databaseType === "sqlite" || databaseType === "rqlite" || databaseType === "turso") return objectType === "TABLE";
|
||||
if (databaseType === "sqlite" || databaseType === "rqlite" || databaseType === "turso" || databaseType === "cloudflare-d1") return objectType === "TABLE";
|
||||
if (databaseType === "mysql" || databaseType === "goldendb") return objectType === "TABLE" || objectType === "VIEW";
|
||||
if (postgresLikeRenameTypes.has(databaseType)) return objectType === "TABLE" || objectType === "VIEW" || objectType === "MATERIALIZED_VIEW";
|
||||
if (oracleLikeRenameTypes.has(databaseType)) return objectType === "TABLE" || objectType === "VIEW" || objectType === "MATERIALIZED_VIEW";
|
||||
|
||||
@@ -151,29 +151,29 @@ export function importDataTypeForDatabase(inferredType: ImportInferredType, data
|
||||
case "boolean":
|
||||
if (["mysql", "doris", "starrocks", "goldendb", "sundb", "databend"].includes(databaseType || "")) return "TINYINT(1)";
|
||||
if (databaseType === "sqlserver") return "BIT";
|
||||
if (databaseType === "sqlite" || databaseType === "rqlite" || databaseType === "turso") return "INTEGER";
|
||||
if (databaseType === "sqlite" || databaseType === "rqlite" || databaseType === "turso" || databaseType === "cloudflare-d1") return "INTEGER";
|
||||
if (databaseType === "oracle" || databaseType === "oceanbase-oracle" || databaseType === "dameng") return "NUMBER(1)";
|
||||
if (databaseType === "clickhouse") return "UInt8";
|
||||
return "BOOLEAN";
|
||||
case "integer":
|
||||
if (databaseType === "sqlite" || databaseType === "rqlite" || databaseType === "turso") return "INTEGER";
|
||||
if (databaseType === "sqlite" || databaseType === "rqlite" || databaseType === "turso" || databaseType === "cloudflare-d1") return "INTEGER";
|
||||
if (databaseType === "oracle" || databaseType === "oceanbase-oracle" || databaseType === "dameng") return "NUMBER(19)";
|
||||
if (databaseType === "clickhouse") return "Int64";
|
||||
return "BIGINT";
|
||||
case "decimal":
|
||||
if (["postgres", "gaussdb", "opengauss", "redshift", "kingbase", "highgo", "kwdb", "vastbase"].includes(databaseType || "")) return "DOUBLE PRECISION";
|
||||
if (databaseType === "sqlite" || databaseType === "rqlite" || databaseType === "turso") return "REAL";
|
||||
if (databaseType === "sqlite" || databaseType === "rqlite" || databaseType === "turso" || databaseType === "cloudflare-d1") return "REAL";
|
||||
if (databaseType === "oracle" || databaseType === "oceanbase-oracle" || databaseType === "dameng") return "BINARY_DOUBLE";
|
||||
if (databaseType === "clickhouse") return "Float64";
|
||||
return "DOUBLE";
|
||||
case "date":
|
||||
if (databaseType === "sqlite" || databaseType === "rqlite" || databaseType === "turso") return "TEXT";
|
||||
if (databaseType === "sqlite" || databaseType === "rqlite" || databaseType === "turso" || databaseType === "cloudflare-d1") return "TEXT";
|
||||
if (databaseType === "clickhouse") return "Date";
|
||||
return "DATE";
|
||||
case "timestamp":
|
||||
if (["mysql", "doris", "starrocks", "goldendb", "sundb", "databend"].includes(databaseType || "")) return "DATETIME";
|
||||
if (databaseType === "sqlserver") return "DATETIME2";
|
||||
if (databaseType === "sqlite" || databaseType === "rqlite" || databaseType === "turso") return "TEXT";
|
||||
if (databaseType === "sqlite" || databaseType === "rqlite" || databaseType === "turso" || databaseType === "cloudflare-d1") return "TEXT";
|
||||
if (databaseType === "clickhouse") return "DateTime64";
|
||||
return "TIMESTAMP";
|
||||
case "json":
|
||||
|
||||
@@ -129,7 +129,7 @@ function columnPlaceholderValue(column: ColumnInfo, databaseType?: DatabaseType)
|
||||
const colName = column.name;
|
||||
|
||||
if (typeLooksNumeric(dataType)) return "0";
|
||||
if (typeLooksBoolean(dataType)) return databaseType === "mysql" || databaseType === "sqlite" || databaseType === "rqlite" ? "1" : "TRUE";
|
||||
if (typeLooksBoolean(dataType)) return databaseType === "mysql" || databaseType === "sqlite" || databaseType === "rqlite" || databaseType === "cloudflare-d1" ? "1" : "TRUE";
|
||||
if (typeLooksDateOnly(dataType)) return "'2024-01-01'";
|
||||
if (typeLooksTimeOnly(dataType)) return "'12:00:00'";
|
||||
if (typeLooksTimestamp(dataType)) return databaseType === "tdengine" ? "NOW" : "CURRENT_TIMESTAMP";
|
||||
|
||||
Reference in new issue
Block a user