Skip to content

Commit b58db8f

Browse files
author
Ishu Singh
committed
added upload file
1 parent d113400 commit b58db8f

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

src/tables/tablesApiClient.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,14 @@ export default abstract class TablesApiClient {
4040
* @throws {MindsDbError} - Something went wrong deleting the table.
4141
*/
4242
abstract deleteTable(name: string, integration: string): Promise<void>;
43+
44+
/**
45+
* Uploads a file to a specified table in an integration.
46+
* @param {string} filePath - Path to the file to be uploaded.
47+
* @param {string} tableName - Name of the table to upload the file to.
48+
* @param {string} integration - Name of the integration the table is a part of.
49+
* @throws {MindsDbError} - Something went wrong uploading the file.
50+
*/
51+
abstract uploadFile(filePath: string, tableName: string, integration: string): Promise<void>;
52+
4353
}

src/tables/tablesRestApiClient.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import Table from './table';
33
import TablesApiClient from './tablesApiClient';
44
import mysql from 'mysql';
55
import { MindsDbError } from '../errors';
6+
import path from 'path';
67

78
/** Implementation of TablesApiClient that goes through the REST API */
89
export default class TablesRestApiClient extends TablesApiClient {
@@ -87,4 +88,30 @@ export default class TablesRestApiClient extends TablesApiClient {
8788
throw new MindsDbError(sqlQueryResult.error_message);
8889
}
8990
}
91+
92+
/**
93+
* Uploads a file to a specified table in an integration.
94+
* @param {string} filePath - Path to the file to be uploaded.
95+
* @param {string} tableName - Name of the table to upload the file to.
96+
* @param {string} integration - Name of the integration the table is a part of.
97+
* @throws {MindsDbError} - Something went wrong uploading the file.
98+
*/
99+
override async uploadFile(filePath: string, tableName: string, integration: string): Promise<void> {
100+
const fileName = path.basename(filePath);
101+
102+
const sqlQuery = `
103+
LOAD DATA LOCAL INFILE '${fileName}'
104+
INTO TABLE ${mysql.escapeId(integration)}.${mysql.escapeId(tableName)}
105+
FIELDS TERMINATED BY ','
106+
ENCLOSED BY '"'
107+
LINES TERMINATED BY '\\n'
108+
IGNORE 1 ROWS;
109+
`;
110+
111+
const sqlQueryResult = await this.sqlClient.runQuery(sqlQuery);
112+
if (sqlQueryResult.error_message) {
113+
throw new MindsDbError(sqlQueryResult.error_message);
114+
}
115+
}
116+
90117
}

0 commit comments

Comments
 (0)