Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added upload file #90

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 = `
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@setohe0909 Does this query actually upload the file from local? Shouldn't we use multipart upload?

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);
}
}

}
Loading