A reactive notebook for Python — run reproducible experiments, query with SQL, execute as a script, deploy as an app, and version with git. Stored as pure Python. All in a modern, AI-native editor.
Python artificial-intelligencedagdata-sciencedata-visualizationdataflowdeveloper-toolsmachine-learningnotebookspipelinepythonreactivesqlweb-app
add sqlalchemy supabase connector (#7068)
Shahmir Varqha0238fb935dd1
diff --git a/frontend/src/components/databases/icon.tsx b/frontend/src/components/databases/icon.tsxindex e78883584b..adca92dae1 100644--- a/frontend/src/components/databases/icon.tsx+++ b/frontend/src/components/databases/icon.tsx@@ -16,6 +16,7 @@ import RedshiftIcon from "./icons/redshift.svg";import SnowflakeIcon from "./icons/snowflake.svg";import PySparkIcon from "./icons/spark.svg";import SQLiteIcon from "./icons/sqlite.svg";+import SupabaseIcon from "./icons/supabase.svg";import TimeplusIcon from "./icons/timeplus.svg";import TrinoIcon from "./icons/trino.svg";@@ -35,7 +36,8 @@ export type DBLogoName =| "iceberg"| "datafusion"| "pyspark"- | "redshift";+ | "redshift"+ | "supabase";/*** Icons are from https://simpleicons.org/@@ -63,6 +65,7 @@ const URLS: Record<DBLogoName, string | undefined> = {datafusion: DataFusionIcon,pyspark: PySparkIcon,redshift: RedshiftIcon,+ supabase: SupabaseIcon,};export const DatabaseLogo: FC<DatabaseLogoProps> = ({ name, className }) => {diff --git a/frontend/src/components/databases/icons/supabase.svg b/frontend/src/components/databases/icons/supabase.svgnew file mode 100644index 0000000000..b773557067--- /dev/null+++ b/frontend/src/components/databases/icons/supabase.svg@@ -0,0 +1 @@+<svg role="img" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><title>Supabase</title><path d="M11.9 1.036c-.015-.986-1.26-1.41-1.874-.637L.764 12.05C-.33 13.427.65 15.455 2.409 15.455h9.579l.113 7.51c.014.985 1.259 1.408 1.873.636l9.262-11.653c1.093-1.375.113-3.403-1.645-3.403h-9.642z"/></svg>\ No newline at end of filediff --git a/frontend/src/components/editor/database/__tests__/__snapshots__/as-code.test.ts.snap b/frontend/src/components/editor/database/__tests__/__snapshots__/as-code.test.ts.snapindex f2491ca76e..bf2083cb38 100644--- a/frontend/src/components/editor/database/__tests__/__snapshots__/as-code.test.ts.snap+++ b/frontend/src/components/editor/database/__tests__/__snapshots__/as-code.test.ts.snap@@ -265,6 +265,34 @@ DATABASE_URL = "sqlite:////path/to/db.sqlite"engine = sqlmodel.create_engine(DATABASE_URL)"`;+exports[`generateDatabaseCode > basic connections > supabase with SQLAlchemy 1`] = `+"import os+import sqlalchemy++_password = os.environ.get("SUPABASE_PASSWORD", "my_password")+DATABASE_URL = f"postgresql+psycopg2://postgres:{_password}@db.example.supabase.co:5432/postgres?sslmode=require"+engine = sqlalchemy.create_engine(DATABASE_URL)"+`;++exports[`generateDatabaseCode > basic connections > supabase with SQLModel 1`] = `+"import os+import sqlmodel++_password = os.environ.get("SUPABASE_PASSWORD", "my_password")+DATABASE_URL = f"postgresql+psycopg2://postgres:{_password}@db.example.supabase.co:5432/postgres?sslmode=require"+engine = sqlmodel.create_engine(DATABASE_URL)"+`;++exports[`generateDatabaseCode > basic connections > supabase with client-side pooling disabled 1`] = `+"from sqlalchemy.pool import NullPool+import os+import sqlalchemy++_password = os.environ.get("SUPABASE_PASSWORD", "my_password")+DATABASE_URL = f"postgresql+psycopg2://postgres.session:{_password}@db.example.supabase.co:6543/postgres?sslmode=require"+engine = sqlalchemy.create_engine(DATABASE_URL, poolclass=NullPool)"+`;+exports[`generateDatabaseCode > basic connections > timeplus 1`] = `"import osimport sqlalchemy@@ -359,6 +387,18 @@ engine = sqlmodel.create_engine()"`;+exports[`generateDatabaseCode > connections with secrets > supabase with all connection details as secrets 1`] = `+"import os+import sqlalchemy++_password = os.environ.get("SUPABASE_PASSWORD")+_username = os.environ.get("SUPABASE_USER")+_host = os.environ.get("SUPABASE_HOST")+_database = os.environ.get("SUPABASE_DB")+DATABASE_URL = f"postgresql+psycopg2://{_username}:{_password}@{_host}:5432/{_database}?sslmode=require"+engine = sqlalchemy.create_engine(DATABASE_URL)"+`;+exports[`generateDatabaseCode > connections with secrets > timeplus with all connection details as secrets 1`] = `"import osimport sqlalchemydiff --git a/frontend/src/components/editor/database/__tests__/as-code.test.ts b/frontend/src/components/editor/database/__tests__/as-code.test.tsindex 90739b2fd1..e975364fa3 100644--- a/frontend/src/components/editor/database/__tests__/as-code.test.ts+++ b/frontend/src/components/editor/database/__tests__/as-code.test.ts@@ -210,6 +210,26 @@ describe("generateDatabaseCode", () => {schema: "my_schema",};+ const supabaseConnection: DatabaseConnection = {+ type: "supabase",+ host: "db.example.supabase.co",+ port: 5432,+ database: "postgres",+ username: "postgres",+ password: "my_password",+ disable_client_pooling: false,+ };++ const supabaseWithPooling: DatabaseConnection = {+ type: "supabase",+ host: "db.example.supabase.co",+ port: 6543,+ database: "postgres",+ username: "postgres.session",+ password: "my_password",+ disable_client_pooling: true,+ };+describe("basic connections", () => {const testCases: [string, DatabaseConnection, ConnectionLibrary][] = [["postgres with SQLModel", basePostgres, "sqlmodel"],@@ -243,6 +263,12 @@ describe("generateDatabaseCode", () => {"sqlalchemy",],["databricks with ibis", databricksConnection, "ibis"],+ ["supabase with SQLModel", supabaseConnection, "sqlmodel"],+ [+ "supabase with client-side pooling disabled",+ supabaseWithPooling,+ "sqlalchemy",+ ],];it.each(testCases)("%s", (_name, connection, orm) => {@@ -317,6 +343,17 @@ describe("generateDatabaseCode", () => {},"duckdb",],+ [+ "supabase with all connection details as secrets",+ {+ ...supabaseConnection,+ host: prefixSecret("SUPABASE_HOST"),+ username: prefixSecret("SUPABASE_USER"),+ password: prefixSecret("SUPABASE_PASSWORD"),+ database: prefixSecret("SUPABASE_DB"),+ },+ "sqlalchemy",+ ],])("%s", (_name, connection, orm) => {expect(generateDatabaseCode(connection, orm as ConnectionLibrary),diff --git a/frontend/src/components/editor/database/add-database-form.tsx b/frontend/src/components/editor/database/add-database-form.tsxindex bc0b5b62de..89ff768330 100644--- a/frontend/src/components/editor/database/add-database-form.tsx+++ b/frontend/src/components/editor/database/add-database-form.tsx@@ -50,6 +50,7 @@ import {RedshiftConnectionSchema,SnowflakeConnectionSchema,SQLiteConnectionSchema,+ SupabaseConnectionSchema,TimeplusConnectionSchema,TrinoConnectionSchema,} from "./schemas";@@ -221,6 +222,16 @@ const DATABASES = [preferred: "sqlalchemy",},},+ {+ name: "Supabase",+ schema: SupabaseConnectionSchema,+ color: "#238F5F",+ logo: "supabase",+ connectionLibraries: {+ libraries: ["sqlalchemy", "sqlmodel"],+ preferred: "sqlalchemy",+ },+ },] satisfies ConnectionSchema[];const DATA_CATALOGS = [diff --git a/frontend/src/components/editor/database/as-code.ts b/frontend/src/components/editor/database/as-code.tsindex f2c68d81d8..d8defbcc97 100644--- a/frontend/src/components/editor/database/as-code.ts+++ b/frontend/src/components/editor/database/as-code.ts@@ -765,6 +765,42 @@ class DatabricksGenerator extends CodeGenerator<"databricks"> {}}+class SupabaseGenerator extends CodeGenerator<"supabase"> {+ generateImports(): string[] {+ if (this.connection.disable_client_pooling) {+ return ["from sqlalchemy.pool import NullPool"];+ }+ return [];+ }++ generateConnectionCode(): string {+ const password = this.secrets.printPassword(+ this.connection.password,+ "SUPABASE_PASSWORD",+ true,+ );+ const username = this.secrets.printInFString(+ "username",+ this.connection.username,+ );+ const host = this.secrets.printInFString("host", this.connection.host);+ const port = this.secrets.printInFString("port", this.connection.port);+ const database = this.secrets.printInFString(+ "database",+ this.connection.database,+ );++ const poolClass = this.connection.disable_client_pooling+ ? ", poolclass=NullPool"+ : "";++ return dedent(`+ DATABASE_URL = f"postgresql+psycopg2://${username}:${password}@${host}:${port}/${database}?sslmode=require"+ engine = ${this.orm}.create_engine(DATABASE_URL${poolClass})+ `);+ }+}+class CodeGeneratorFactory {public secrets = new SecretContainer();@@ -805,6 +841,8 @@ class CodeGeneratorFactory {return new RedshiftGenerator(connection, orm, this.secrets);case "databricks":return new DatabricksGenerator(connection, orm, this.secrets);+ case "supabase":+ return new SupabaseGenerator(connection, orm, this.secrets);default:assertNever(connection);}diff --git a/frontend/src/components/editor/database/schemas.ts b/frontend/src/components/editor/database/schemas.tsindex 78fca69583..61e72b7408 100644--- a/frontend/src/components/editor/database/schemas.ts+++ b/frontend/src/components/editor/database/schemas.ts@@ -485,6 +485,25 @@ export const DatabricksConnectionSchema = z}).describe(FieldOptions.of({ direction: "two-columns" }));+export const SupabaseConnectionSchema = z+ .object({+ type: z.literal("supabase"),+ host: hostField(),+ port: portField(5432).optional(),+ database: databaseField(),+ username: usernameField(),+ password: passwordField(),+ disable_client_pooling: z+ .boolean()+ .default(false)+ .describe(+ FieldOptions.of({+ label: "Disable Client-Side Pooling",+ }),+ ),+ })+ .describe(FieldOptions.of({ direction: "two-columns" }));+export const DatabaseConnectionSchema = z.discriminatedUnion("type", [PostgresConnectionSchema,MySQLConnectionSchema,@@ -502,6 +521,7 @@ export const DatabaseConnectionSchema = z.discriminatedUnion("type", [PySparkConnectionSchema,RedshiftConnectionSchema,DatabricksConnectionSchema,+ SupabaseConnectionSchema,]);export type DatabaseConnection = z.infer<typeof DatabaseConnectionSchema>;diff --git a/marimo/_smoke_tests/supabase_example.py b/marimo/_smoke_tests/supabase_example.pynew file mode 100644index 0000000000..e83136d0fd--- /dev/null+++ b/marimo/_smoke_tests/supabase_example.py@@ -0,0 +1,85 @@+# /// script+# requires-python = ">=3.13"+# dependencies = [+# "polars==1.35.1",+# "psycopg2-binary==2.9.11",+# "sqlalchemy==2.0.44",+# "supabase==2.23.2",+# ]+# ///++import marimo++__generated_with = "0.17.6"+app = marimo.App(width="medium")++with app.setup:+ import marimo as mo+ import polars as pl++ import os++[email protected](hide_code=True)+def _():+ mo.md(r"""+ ## Supabase SDK+ """)+ return+++def _():+ url: str = mo.ui.text(+ value=os.environ.get("SUPABASE_URL", ""), label="Supabase URL"+ )+ key: str = mo.ui.text(+ value=os.environ.get("SUPABASE_KEY", ""),+ label="Supabase Key",+ kind="password",+ )++ mo.hstack([url, key], justify="start", gap=1.5)+ return key, url+++def _(Client, create_client, key: str, url: str):+ mo.stop(url.value == "" or key.value == "", mo.md("Please enter URL and Key"))++ supabase_client: Client = create_client(url.value, key.value)+ return++[email protected](hide_code=True)+def _():+ mo.md(r"""+ ## SQLAlchemy Supabase Client+ """)+ return+++def _():+ import sqlalchemy++ _password = os.environ.get("SUPABASE_PASSWORD")+ _host = os.environ.get("SUPABASE_HOST")+ DATABASE_URL = f"postgresql+psycopg2://postgres:{_password}@{_host}:5432/postgres?sslmode=require"+ supabase_engine = sqlalchemy.create_engine(DATABASE_URL)+ return (supabase_engine,)+++def _(supabase_engine):+ _df = mo.sql(+ f"""+ SELECT * FROM "storage"."buckets" LIMIT 100+ """,+ engine=supabase_engine+ )+ return+++if __name__ == "__main__":+ app.run()