Skip to content

Commit c3f6524

Browse files
authored
chore: generate template dynamically for specific enabled plugins (#127)
* feat(template): generate template dynamically for specific enabled plugins * chore: fix bugs for template * chore: fix dependencies * chore: simplify the SQL to execute * chore: update Readme * chore: revert switching to CommonJS * chore: unhide Lakebase plugin * chore: remove unused imports
1 parent 1c7a70e commit c3f6524

19 files changed

Lines changed: 1845 additions & 902 deletions

File tree

docs/docs/plugins/lakebase.md

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -59,33 +59,34 @@ Click the **Connect** button on your Lakebase branch and copy the `PGHOST` and `
5959

6060
### 5. Grant access to the service principal
6161

62-
Navigate to the **SQL Editor** tab on your Lakebase branch. Run the following SQL against the `databricks_postgres` database, replacing `<DATABRICKS_CLIENT_ID>` with the value from step 1 everywhere it appears:
62+
Navigate to the **SQL Editor** tab on your Lakebase branch. Run the following SQL against the `databricks_postgres` database, replacing the service principal ID in the `DECLARE` block with the `DATABRICKS_CLIENT_ID` value from step 1:
6363

6464
```sql
65-
-- 1. Create the extension and role
6665
CREATE EXTENSION IF NOT EXISTS databricks_auth;
67-
SELECT databricks_create_role('<DATABRICKS_CLIENT_ID>', 'SERVICE_PRINCIPAL');
68-
69-
-- 2. Basic connection & usage
70-
GRANT CONNECT ON DATABASE "databricks_postgres" TO "<DATABRICKS_CLIENT_ID>";
71-
GRANT ALL ON SCHEMA public TO "<DATABRICKS_CLIENT_ID>";
72-
73-
-- 3. Grant on existing objects
74-
GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA public TO "<DATABRICKS_CLIENT_ID>";
75-
GRANT ALL PRIVILEGES ON ALL SEQUENCES IN SCHEMA public TO "<DATABRICKS_CLIENT_ID>";
76-
GRANT ALL PRIVILEGES ON ALL FUNCTIONS IN SCHEMA public TO "<DATABRICKS_CLIENT_ID>";
77-
GRANT ALL PRIVILEGES ON ALL PROCEDURES IN SCHEMA public TO "<DATABRICKS_CLIENT_ID>";
78-
79-
-- 4. Grant on future objects
80-
-- NOTE: This applies to objects created by the user running this script.
81-
ALTER DEFAULT PRIVILEGES IN SCHEMA public
82-
GRANT ALL ON TABLES TO "<DATABRICKS_CLIENT_ID>";
83-
ALTER DEFAULT PRIVILEGES IN SCHEMA public
84-
GRANT ALL ON SEQUENCES TO "<DATABRICKS_CLIENT_ID>";
85-
ALTER DEFAULT PRIVILEGES IN SCHEMA public
86-
GRANT ALL ON FUNCTIONS TO "<DATABRICKS_CLIENT_ID>";
87-
ALTER DEFAULT PRIVILEGES IN SCHEMA public
88-
GRANT ALL ON ROUTINES TO "<DATABRICKS_CLIENT_ID>";
66+
67+
DO $$
68+
DECLARE
69+
sp TEXT := 'your-service-principal-id'; -- Replace with DATABRICKS_CLIENT_ID from Step 1
70+
BEGIN
71+
-- Create service principal role
72+
PERFORM databricks_create_role(sp, 'SERVICE_PRINCIPAL');
73+
74+
-- Connection and schema access
75+
EXECUTE format('GRANT CONNECT ON DATABASE "databricks_postgres" TO %I', sp);
76+
EXECUTE format('GRANT ALL ON SCHEMA public TO %I', sp);
77+
78+
-- Privileges on existing objects
79+
EXECUTE format('GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA public TO %I', sp);
80+
EXECUTE format('GRANT ALL PRIVILEGES ON ALL SEQUENCES IN SCHEMA public TO %I', sp);
81+
EXECUTE format('GRANT ALL PRIVILEGES ON ALL FUNCTIONS IN SCHEMA public TO %I', sp);
82+
EXECUTE format('GRANT ALL PRIVILEGES ON ALL PROCEDURES IN SCHEMA public TO %I', sp);
83+
84+
-- Default privileges on future objects you create
85+
EXECUTE format('ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT ALL ON TABLES TO %I', sp);
86+
EXECUTE format('ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT ALL ON SEQUENCES TO %I', sp);
87+
EXECUTE format('ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT ALL ON FUNCTIONS TO %I', sp);
88+
EXECUTE format('ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT ALL ON ROUTINES TO %I', sp);
89+
END $$;
8990
```
9091

9192
![SQL Editor](./assets/lakebase-setup/step-5.png)

packages/appkit/src/plugins/lakebase/manifest.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"displayName": "Lakebase",
55
"description": "SQL query execution against Databricks Lakebase Autoscaling",
66
"onSetupMessage": "Configure environment variables before running or deploying the app.\nSee: https://databricks.github.io/appkit/docs/plugins/lakebase",
7-
"hidden": true,
7+
"hidden": false,
88
"resources": {
99
"required": [],
1010
"optional": []

template/.env.example.tmpl

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,15 @@
11
DATABRICKS_HOST=https://...
2-
{{- if .dotenv_example}}
3-
{{.dotenv_example}}
2+
{{- if .dotEnv.example}}
3+
{{.dotEnv.example}}
4+
{{- end}}
5+
{{- if .plugins.lakebase}}
6+
PGHOST=your-lakebase-host.databricks.com
7+
PGDATABASE=databricks_postgres
8+
# Run: databricks postgres list-endpoints projects/{project-id}/branches/{branch-id}
9+
LAKEBASE_ENDPOINT=projects/<project-id>/branches/<branch-id>/endpoints/<endpoint-id>
10+
# PGUSER=your_user # optional, defaults to DATABRICKS_CLIENT_ID
11+
PGSSLMODE=require
412
{{- end}}
513
DATABRICKS_APP_PORT=8000
6-
DATABRICKS_APP_NAME={{.project_name}}
14+
DATABRICKS_APP_NAME={{.projectName}}
715
FLASK_RUN_HOST=0.0.0.0

template/.env.tmpl

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,18 @@
1-
{{if ne .profile ""}}DATABRICKS_CONFIG_PROFILE={{.profile}}{{else}}DATABRICKS_HOST={{.workspace_host}}{{end}}
2-
{{- if .dotenv}}
3-
{{.dotenv}}
1+
{{- if ne .profile ""}}
2+
DATABRICKS_CONFIG_PROFILE={{.profile}}
3+
{{- else}}
4+
DATABRICKS_HOST={{.workspaceHost}}
5+
{{- end}}
6+
{{- if .dotEnv.content}}
7+
{{.dotEnv.content}}
8+
{{- end}}
9+
{{- if .plugins.lakebase}}
10+
PGHOST='' # Copy from the Lakebase Postgres UI
11+
PGDATABASE='databricks_postgres' # Copy from the Lakebase Postgres UI
12+
LAKEBASE_ENDPOINT='' # Run: databricks postgres list-endpoints projects/{project-id}/branches/{branch-id}
13+
# PGUSER='' # optional, defaults to DATABRICKS_CLIENT_ID
14+
PGSSLMODE=require
415
{{- end}}
516
DATABRICKS_APP_PORT=8000
6-
DATABRICKS_APP_NAME={{.project_name}}
17+
DATABRICKS_APP_NAME={{.projectName}}
718
FLASK_RUN_HOST=0.0.0.0

template/README.md

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
# Minimal Databricks App
22

3-
A minimal Databricks App powered by Databricks AppKit, featuring React, TypeScript, tRPC, and Tailwind CSS.
3+
A minimal Databricks App powered by Databricks AppKit, featuring React, TypeScript, and Tailwind CSS.
44

55
## Prerequisites
66

7-
- Node.js 18+ and npm
7+
- Node.js v22+ and npm
88
- Databricks CLI (for deployment)
99
- Access to a Databricks workspace
1010

@@ -18,12 +18,12 @@ For local development, configure your environment variables by creating a `.env`
1818
cp env.example .env
1919
```
2020

21-
Edit `.env` and set the following:
21+
Edit `.env` and set the environment variables you need:
2222

2323
```env
2424
DATABRICKS_HOST=https://your-workspace.cloud.databricks.com
25-
DATABRICKS_WAREHOUSE_ID=your-warehouse-id
2625
DATABRICKS_APP_PORT=8000
26+
# ... other environment variables, depending on the plugins you use
2727
```
2828

2929
### CLI Authentication
@@ -103,6 +103,8 @@ npm start
103103

104104
## Code Quality
105105

106+
There are a few commands to help you with code quality:
107+
106108
```bash
107109
# Type checking
108110
npm run typecheck
@@ -166,18 +168,20 @@ databricks bundle deploy -t prod
166168

167169
```
168170
* client/ # React frontend
169-
* src/ # Source code
170-
* public/ # Static assets
171+
* src/ # Source code
172+
* public/ # Static assets
171173
* server/ # Express backend
172-
* server.ts # Server entry point
173-
* trpc.ts # tRPC router
174+
* server.ts # Server entry point
175+
* routes/ # Routes
174176
* shared/ # Shared types
175177
* databricks.yml # Bundle configuration
178+
* app.yaml # App configuration
179+
* .env.example # Environment variables example
176180
```
177181

178182
## Tech Stack
179183

180-
- **Frontend**: React 19, TypeScript, Vite, Tailwind CSS
181-
- **Backend**: Node.js, Express, tRPC
184+
- **Backend**: Node.js, Express
185+
- **Frontend**: React.js, TypeScript, Vite, Tailwind CSS, React Router
182186
- **UI Components**: Radix UI, shadcn/ui
183-
- **Databricks**: App Kit SDK, Analytics SDK
187+
- **Databricks**: AppKit SDK

template/app.yaml.tmpl

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,17 @@
11
command: ['npm', 'run', 'start']
2-
{{- if .app_env}}
32
env:
4-
{{.app_env}}
3+
{{- if .appEnv}}
4+
{{.appEnv}}
55
{{- end}}
6+
{{- if .plugins.lakebase}}
7+
- name: PGHOST
8+
value: "" # Copy from the Lakebase Postgres UI
9+
- name: PGDATABASE
10+
value: "databricks_postgres" # Copy from the Lakebase Postgres UI
11+
- name: LAKEBASE_ENDPOINT
12+
value: "" # Run: databricks postgres list-endpoints projects/{project-id}/branches/{branch-id}
13+
- name: PGSSLMODE
14+
value: "require"
15+
# - name: PGUSER
16+
# value: "" # optional, defaults to DATABRICKS_CLIENT_ID
17+
{{- end}}

template/appkit.plugins.json

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,17 @@
2626
"optional": []
2727
}
2828
},
29+
"lakebase": {
30+
"name": "lakebase",
31+
"displayName": "Lakebase",
32+
"description": "SQL query execution against Databricks Lakebase Autoscaling",
33+
"package": "@databricks/appkit",
34+
"resources": {
35+
"required": [],
36+
"optional": []
37+
},
38+
"onSetupMessage": "Configure environment variables before running or deploying the app.\nSee: https://databricks.github.io/appkit/docs/plugins/lakebase"
39+
},
2940
"server": {
3041
"name": "server",
3142
"displayName": "Server Plugin",

template/client/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
<link rel="apple-touch-icon" sizes="180x180" href="/apple-touch-icon.png" />
1010
<link rel="manifest" href="/site.webmanifest" />
1111
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
12-
<title>{{.project_name}}</title>
12+
<title>{{.projectName}}</title>
1313
</head>
1414
<body>
1515
<div id="root"></div>

template/client/public/site.webmanifest

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
2-
"name": "{{.project_name}}",
3-
"short_name": "{{.project_name}}",
2+
"name": "{{.projectName}}",
3+
"short_name": "{{.projectName}}",
44
"icons": [
55
{
66
"src": "/favicon-192x192.png",

0 commit comments

Comments
 (0)