Skip to content

Commit 11dde05

Browse files
authored
Merge pull request supabase-community#26 from supabase-community/feat/download-db
feat: download databases
2 parents 17c8de6 + d46afc0 commit 11dde05

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

apps/postgres-new/components/sidebar.tsx

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
ArrowRightToLine,
77
CircleEllipsis,
88
Database as DbIcon,
9+
Download,
910
Loader,
1011
LogOut,
1112
PackagePlus,
@@ -26,6 +27,7 @@ import { useDatabasesQuery } from '~/data/databases/databases-query'
2627
import { useDeployWaitlistCreateMutation } from '~/data/deploy-waitlist/deploy-waitlist-create-mutation'
2728
import { useIsOnDeployWaitlistQuery } from '~/data/deploy-waitlist/deploy-waitlist-query'
2829
import { Database } from '~/lib/db'
30+
import { downloadFile, titleToKebabCase } from '~/lib/util'
2931
import { cn } from '~/lib/utils'
3032
import { useApp } from './app-provider'
3133
import { CodeBlock } from './code-block'
@@ -208,7 +210,7 @@ type DatabaseMenuItemProps = {
208210

209211
function DatabaseMenuItem({ database, isActive }: DatabaseMenuItemProps) {
210212
const router = useRouter()
211-
const { user } = useApp()
213+
const { user, dbManager } = useApp()
212214
const [isPopoverOpen, setIsPopoverOpen] = useState(false)
213215
const { mutateAsync: deleteDatabase } = useDatabaseDeleteMutation()
214216
const { mutateAsync: updateDatabase } = useDatabaseUpdateMutation()
@@ -364,6 +366,28 @@ function DatabaseMenuItem({ database, isActive }: DatabaseMenuItemProps) {
364366
onClick={async (e) => {
365367
e.preventDefault()
366368

369+
if (!dbManager) {
370+
throw new Error('dbManager is not available')
371+
}
372+
373+
const db = await dbManager.getDbInstance(database.id)
374+
const dumpBlob = await db.dumpDataDir()
375+
376+
const fileName = `${titleToKebabCase(database.name ?? 'My Database')}-${Date.now()}`
377+
const file = new File([dumpBlob], fileName, { type: dumpBlob.type })
378+
379+
downloadFile(file)
380+
}}
381+
>
382+
<Download size={16} strokeWidth={2} className="flex-shrink-0" />
383+
384+
<span>Download</span>
385+
</Button>
386+
<Button
387+
className="bg-inherit justify-start hover:bg-neutral-200 flex gap-3"
388+
onClick={async (e) => {
389+
e.preventDefault()
390+
367391
setIsDeployDialogOpen(true)
368392
setIsPopoverOpen(false)
369393
}}

apps/postgres-new/lib/util.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,12 @@ export function isAutomatedUserMessage(message: Message) {
3838
message.data.automated === true
3939
)
4040
}
41+
42+
export function titleToKebabCase(str: string): string {
43+
return str
44+
.toLowerCase()
45+
.replace(/[_\s]+/g, '-') // Replace spaces and underscores with dashes
46+
.replace(/[^a-z0-9-]/g, '') // Remove any non-alphanumeric characters except dashes
47+
.replace(/-+/g, '-') // Replace multiple dashes with a single dash
48+
.replace(/^-|-$/g, '') // Remove leading and trailing dashes
49+
}

0 commit comments

Comments
 (0)