-
Notifications
You must be signed in to change notification settings - Fork 117
Expand file tree
/
Copy pathdump.ts
More file actions
80 lines (71 loc) · 3.06 KB
/
dump.ts
File metadata and controls
80 lines (71 loc) · 3.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import { executeOperation } from '.'
import { StarbaseDBConfiguration } from '../handler'
import { DataSource } from '../types'
import { createResponse } from '../utils'
export async function dumpDatabaseRoute(
dataSource: DataSource,
config: StarbaseDBConfiguration
): Promise<Response> {
try {
const { readable, writable } = new TransformStream();
const writer = writable.getWriter();
const encoder = new TextEncoder();
// Proses streaming berjalan di background
(async () => {
try {
// 1. Ambil semua nama tabel
const tablesResult = await executeOperation(
[{ sql: "SELECT name FROM sqlite_master WHERE type='table';" }],
dataSource,
config
)
const tables = tablesResult.map((row: any) => row.name)
// 2. Kirim Header SQLite
await writer.write(encoder.encode('SQLite format 3\0'))
for (const table of tables) {
// 3. Ambil dan kirim Schema Tabel
const schemaResult = await executeOperation(
[{ sql: `SELECT sql FROM sqlite_master WHERE type='table' AND name='${table}';` }],
dataSource,
config
)
if (schemaResult.length) {
const schema = schemaResult[0].sql
await writer.write(encoder.encode(`\n-- Table: ${table}\n${schema};\n\n`))
}
// 4. Ambil Data Tabel (Streaming per baris)
const dataResult = await executeOperation(
[{ sql: `SELECT * FROM ${table};` }],
dataSource,
config
)
for (const row of dataResult) {
const values = Object.values(row).map((value) =>
typeof value === 'string'
? `'${value.replace(/'/g, "''")}'`
: value
)
const line = `INSERT INTO ${table} VALUES (${values.join(', ')});\n`
await writer.write(encoder.encode(line))
}
await writer.write(encoder.encode('\n'))
}
} catch (err) {
console.error("Streaming Error:", err)
} finally {
await writer.close()
}
})()
// 5. Kembalikan Response berupa Stream (Gak pake Blob lagi!)
return new Response(readable, {
headers: {
'Content-Type': 'application/x-sqlite3',
'Content-Disposition': 'attachment; filename="database_dump.sql"',
'Transfer-Encoding': 'chunked'
}
})
} catch (error: any) {
console.error('Database Dump Error:', error)
return createResponse(undefined, 'Failed to create database dump', 500)
}
}