Skip to content

Commit 5c17687

Browse files
committed
Export & Import
Fix up export and import functionality for internal sources
1 parent f61d015 commit 5c17687

File tree

8 files changed

+45
-26
lines changed

8 files changed

+45
-26
lines changed

src/export/csv.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
import { getTableData, createExportResponse } from './index';
22
import { createResponse } from '../utils';
33
import { DataSource } from '../types';
4+
import { StarbaseDBConfiguration } from '../handler';
45

56
export async function exportTableToCsvRoute(
67
tableName: string,
7-
dataSource: DataSource
8+
dataSource: DataSource,
9+
config: StarbaseDBConfiguration
810
): Promise<Response> {
911
try {
10-
const data = await getTableData(tableName, dataSource);
12+
const data = await getTableData(tableName, dataSource, config);
1113

1214
if (data === null) {
1315
return createResponse(undefined, `Table '${tableName}' does not exist.`, 404);

src/export/dump.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,31 @@
11
import { executeOperation } from '.';
2+
import { StarbaseDBConfiguration } from '../handler';
23
import { DataSource } from '../types';
34
import { createResponse } from '../utils';
45

56
export async function dumpDatabaseRoute(
6-
dataSource: DataSource
7+
dataSource: DataSource,
8+
config: StarbaseDBConfiguration
79
): Promise<Response> {
810
try {
911
// Get all table names
10-
const tablesResult = await executeOperation([{ sql: "SELECT name FROM sqlite_master WHERE type='table';" }], dataSource)
12+
const tablesResult = await executeOperation([{ sql: "SELECT name FROM sqlite_master WHERE type='table';" }], dataSource, config)
1113

1214
const tables = tablesResult.map((row: any) => row.name);
1315
let dumpContent = "SQLite format 3\0"; // SQLite file header
1416

1517
// Iterate through all tables
1618
for (const table of tables) {
1719
// Get table schema
18-
const schemaResult = await executeOperation([{ sql: `SELECT sql FROM sqlite_master WHERE type='table' AND name='${table}';` }], dataSource)
20+
const schemaResult = await executeOperation([{ sql: `SELECT sql FROM sqlite_master WHERE type='table' AND name='${table}';` }], dataSource, config)
1921

2022
if (schemaResult.length) {
2123
const schema = schemaResult[0].sql;
2224
dumpContent += `\n-- Table: ${table}\n${schema};\n\n`;
2325
}
2426

2527
// Get table data
26-
const dataResult = await executeOperation([{ sql: `SELECT * FROM ${table};` }], dataSource)
28+
const dataResult = await executeOperation([{ sql: `SELECT * FROM ${table};` }], dataSource, config)
2729

2830
for (const row of dataResult) {
2931
const values = Object.values(row).map(value =>

src/export/index.ts

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,32 @@
11
import { DataSource } from "../types";
22
import { executeTransaction } from "../operation";
3-
4-
export async function executeOperation(queries: { sql: string, params?: any[] }[], dataSource: DataSource): Promise<any> {
5-
const results: any[] = (await executeTransaction(queries, false, dataSource)) as any[];
3+
import { StarbaseDBConfiguration } from "../handler";
4+
5+
export async function executeOperation(queries: { sql: string, params?: any[] }[], dataSource: DataSource, config: StarbaseDBConfiguration): Promise<any> {
6+
const results: any[] = (await executeTransaction({
7+
queries,
8+
isRaw: false,
9+
dataSource,
10+
config
11+
})) as any[];
612
return results?.length > 0 ? results[0] : undefined;
713
}
814

915
export async function getTableData(
1016
tableName: string,
11-
dataSource: DataSource
17+
dataSource: DataSource,
18+
config: StarbaseDBConfiguration
1219
): Promise<any[] | null> {
1320
try {
1421
// Verify if the table exists
15-
const tableExistsResult = await executeOperation([{ sql: `SELECT name FROM sqlite_master WHERE type='table' AND name=?;`, params: [tableName] }], dataSource)
22+
const tableExistsResult = await executeOperation([{ sql: `SELECT name FROM sqlite_master WHERE type='table' AND name=?;`, params: [tableName] }], dataSource, config)
1623

1724
if (tableExistsResult.length === 0) {
1825
return null;
1926
}
2027

2128
// Get table data
22-
const dataResult = await executeOperation([{ sql: `SELECT * FROM ${tableName};` }], dataSource)
29+
const dataResult = await executeOperation([{ sql: `SELECT * FROM ${tableName};` }], dataSource, config)
2330
return dataResult;
2431
} catch (error: any) {
2532
console.error('Table Data Fetch Error:', error);

src/export/json.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
import { getTableData, createExportResponse } from './index';
22
import { createResponse } from '../utils';
33
import { DataSource } from '../types';
4+
import { StarbaseDBConfiguration } from '../handler';
45

56
export async function exportTableToJsonRoute(
67
tableName: string,
7-
dataSource: DataSource
8+
dataSource: DataSource,
9+
config: StarbaseDBConfiguration
810
): Promise<Response> {
911
try {
10-
const data = await getTableData(tableName, dataSource);
12+
const data = await getTableData(tableName, dataSource, config);
1113

1214
if (data === null) {
1315
return createResponse(undefined, `Table '${tableName}' does not exist.`, 404);

src/handler.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ export class StarbaseDB {
155155

156156
if (this.getFeature("export")) {
157157
app.get("/export/dump", this.isInternalSource, async () => {
158-
return dumpDatabaseRoute(this.dataSource);
158+
return dumpDatabaseRoute(this.dataSource, this.config);
159159
});
160160

161161
app.get(
@@ -164,7 +164,7 @@ export class StarbaseDB {
164164
this.hasTableName,
165165
async (c) => {
166166
const tableName = c.req.valid("param").tableName;
167-
return exportTableToJsonRoute(tableName, this.dataSource);
167+
return exportTableToJsonRoute(tableName, this.dataSource, this.config);
168168
}
169169
);
170170

@@ -174,14 +174,14 @@ export class StarbaseDB {
174174
this.hasTableName,
175175
async (c) => {
176176
const tableName = c.req.valid("param").tableName;
177-
return exportTableToCsvRoute(tableName, this.dataSource);
177+
return exportTableToCsvRoute(tableName, this.dataSource, this.config);
178178
}
179179
);
180180
}
181181

182182
if (this.getFeature("import")) {
183183
app.post("/import/dump", this.isInternalSource, async (c) => {
184-
return importDumpRoute(c.req.raw, this.dataSource);
184+
return importDumpRoute(c.req.raw, this.dataSource, this.config);
185185
});
186186

187187
app.post(
@@ -190,7 +190,7 @@ export class StarbaseDB {
190190
this.hasTableName,
191191
async (c) => {
192192
const tableName = c.req.valid("param").tableName;
193-
return importTableFromJsonRoute(tableName, request, this.dataSource);
193+
return importTableFromJsonRoute(tableName, request, this.dataSource, this.config);
194194
}
195195
);
196196

@@ -200,7 +200,7 @@ export class StarbaseDB {
200200
this.hasTableName,
201201
async (c) => {
202202
const tableName = c.req.valid("param").tableName;
203-
return importTableFromCsvRoute(tableName, request, this.dataSource);
203+
return importTableFromCsvRoute(tableName, request, this.dataSource, this.config);
204204
}
205205
);
206206
}

src/import/csv.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { createResponse } from '../utils';
22
import { DataSource } from '../types';
33
import { executeOperation } from '../export';
4+
import { StarbaseDBConfiguration } from '../handler';
45

56
interface ColumnMapping {
67
[key: string]: string;
@@ -14,7 +15,8 @@ interface CsvData {
1415
export async function importTableFromCsvRoute(
1516
tableName: string,
1617
request: Request,
17-
dataSource: DataSource
18+
dataSource: DataSource,
19+
config: StarbaseDBConfiguration
1820
): Promise<Response> {
1921
try {
2022
if (!request.body) {
@@ -67,7 +69,7 @@ export async function importTableFromCsvRoute(
6769
const statement = `INSERT INTO ${tableName} (${columns.join(', ')}) VALUES (${placeholders})`;
6870

6971
try {
70-
await executeOperation([{ sql: statement, params: values }], dataSource)
72+
await executeOperation([{ sql: statement, params: values }], dataSource, config)
7173
successCount++;
7274
} catch (error: any) {
7375
failedStatements.push({

src/import/dump.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { createResponse } from '../utils';
22
import { DataSource } from '../types';
33
import { executeOperation } from '../export';
4+
import { StarbaseDBConfiguration } from '../handler';
45

56
function parseSqlStatements(sqlContent: string): string[] {
67
const lines = sqlContent.split('\n');
@@ -31,7 +32,8 @@ function parseSqlStatements(sqlContent: string): string[] {
3132

3233
export async function importDumpRoute(
3334
request: Request,
34-
dataSource: DataSource
35+
dataSource: DataSource,
36+
config: StarbaseDBConfiguration
3537
): Promise<Response> {
3638
if (request.method !== 'POST') {
3739
return createResponse(undefined, 'Method not allowed', 405);
@@ -66,7 +68,7 @@ export async function importDumpRoute(
6668
const results = [];
6769
for (const statement of sqlStatements) {
6870
try {
69-
const result = await executeOperation([{ sql: statement }], dataSource)
71+
const result = await executeOperation([{ sql: statement }], dataSource, config)
7072
results.push({ statement, success: true, result });
7173
} catch (error: any) {
7274
console.error(`Error executing statement: ${statement}`, error);

src/import/json.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { createResponse } from '../utils';
22
import { executeOperation } from '../export';
33
import { DataSource } from '../types';
4+
import { StarbaseDBConfiguration } from '../handler';
45

56
interface ColumnMapping {
67
[key: string]: string;
@@ -14,7 +15,8 @@ interface JsonData {
1415
export async function importTableFromJsonRoute(
1516
tableName: string,
1617
request: Request,
17-
dataSource: DataSource
18+
dataSource: DataSource,
19+
config: StarbaseDBConfiguration
1820
): Promise<Response> {
1921
try {
2022
if (!request.body) {
@@ -60,7 +62,7 @@ export async function importTableFromJsonRoute(
6062
const statement = `INSERT INTO ${tableName} (${columns.join(', ')}) VALUES (${placeholders})`;
6163

6264
try {
63-
await executeOperation([{ sql: statement, params: values }], dataSource)
65+
await executeOperation([{ sql: statement, params: values }], dataSource, config)
6466
successCount++;
6567
} catch (error: any) {
6668
failedStatements.push({

0 commit comments

Comments
 (0)