Skip to content

Commit

Permalink
added upload file
Browse files Browse the repository at this point in the history
  • Loading branch information
Ishu Singh committed Oct 21, 2024
1 parent d113400 commit b58db8f
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/tables/tablesApiClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,14 @@ export default abstract class TablesApiClient {
* @throws {MindsDbError} - Something went wrong deleting the table.
*/
abstract deleteTable(name: string, integration: string): Promise<void>;

/**
* Uploads a file to a specified table in an integration.
* @param {string} filePath - Path to the file to be uploaded.
* @param {string} tableName - Name of the table to upload the file to.
* @param {string} integration - Name of the integration the table is a part of.
* @throws {MindsDbError} - Something went wrong uploading the file.
*/
abstract uploadFile(filePath: string, tableName: string, integration: string): Promise<void>;

}
27 changes: 27 additions & 0 deletions src/tables/tablesRestApiClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import Table from './table';
import TablesApiClient from './tablesApiClient';
import mysql from 'mysql';
import { MindsDbError } from '../errors';
import path from 'path';

/** Implementation of TablesApiClient that goes through the REST API */
export default class TablesRestApiClient extends TablesApiClient {
Expand Down Expand Up @@ -87,4 +88,30 @@ export default class TablesRestApiClient extends TablesApiClient {
throw new MindsDbError(sqlQueryResult.error_message);
}
}

/**
* Uploads a file to a specified table in an integration.
* @param {string} filePath - Path to the file to be uploaded.
* @param {string} tableName - Name of the table to upload the file to.
* @param {string} integration - Name of the integration the table is a part of.
* @throws {MindsDbError} - Something went wrong uploading the file.
*/
override async uploadFile(filePath: string, tableName: string, integration: string): Promise<void> {
const fileName = path.basename(filePath);

const sqlQuery = `
LOAD DATA LOCAL INFILE '${fileName}'
INTO TABLE ${mysql.escapeId(integration)}.${mysql.escapeId(tableName)}
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
LINES TERMINATED BY '\\n'
IGNORE 1 ROWS;
`;

const sqlQueryResult = await this.sqlClient.runQuery(sqlQuery);
if (sqlQueryResult.error_message) {
throw new MindsDbError(sqlQueryResult.error_message);
}
}

}

0 comments on commit b58db8f

Please sign in to comment.