diff --git a/src/tables/table.ts b/src/tables/table.ts index 0286fe6..741b820 100644 --- a/src/tables/table.ts +++ b/src/tables/table.ts @@ -54,6 +54,15 @@ export default class Table { await this.tablesApiClient.deleteTable(this.name, this.integration); } + /** + * Deletes specific row (or multiple rows) from the table present in the given integration. + * @param {string} select - select statement to specify which rows should be deleted. + * @throws {MindsDbError} - Something went wrong deleting the data from the table. + */ + async deleteFromTable(select?:string):Promise { + await this.tablesApiClient.deleteFromTable(this.name,this.integration,select); + } + /** * Insert data into this table. * @param {string} select - SELECT query to insert data from. diff --git a/src/tables/tablesApiClient.ts b/src/tables/tablesApiClient.ts index 02d684a..622d142 100644 --- a/src/tables/tablesApiClient.ts +++ b/src/tables/tablesApiClient.ts @@ -42,6 +42,15 @@ export default abstract class TablesApiClient { abstract deleteTable(name: string, integration: string): Promise; /** + * Deletes specific row (or multiple rows) from the table present in the given integration. + * @param {string} name - Name of the table from which data is to be deleted. + * @param {string} integration - Name of the integration the table is a part of. + * @param {string} select - select statement to specify which rows should be deleted. + * @throws {MindsDbError} - Something went wrong deleting the data from the table. + */ + abstract deleteFromTable(name: string, integration: string, select?: string): Promise; + + /* * Insert data into this table. * @param {string} name - Name of the table to be deleted. * @param {string} integration - Name of the integration the table to be deleted is a part of. @@ -49,6 +58,7 @@ export default abstract class TablesApiClient { * @throws {MindsDbError} - Something went wrong inserting data into the table. */ abstract insertTable(name: string, integration: string, select: string): Promise; + /** * Deletes a file from the files integration. * @param {string} name - Name of the file to be deleted. diff --git a/src/tables/tablesRestApiClient.ts b/src/tables/tablesRestApiClient.ts index 01c49cb..21af99c 100644 --- a/src/tables/tablesRestApiClient.ts +++ b/src/tables/tablesRestApiClient.ts @@ -89,6 +89,27 @@ export default class TablesRestApiClient extends TablesApiClient { } /** + * Deletes specific row (or multiple rows) from the table present in the given integration. + * @param {string} name - Name of the table from which data is to be deleted. + * @param {string} integration - Name of the integration the table is a part of. + * @param {string} select - select statement to specify which rows should be deleted. + * @throws {MindsDbError} - Something went wrong deleting the data from the table. + */ + override async deleteFromTable(name: string, integration: string,select?:string): Promise { + /** + If select parameter is not passed then entire data from the table is deleted. + */ + const sqlQuery = select ?? `DELETE FROM TABLE ${mysql.escapeId( + integration + )}.${mysql.escapeId(name)}`; + const sqlQueryResult = await this.sqlClient.runQuery(sqlQuery); + if (sqlQueryResult.error_message) { + throw new MindsDbError(sqlQueryResult.error_message); + } + } + + + /* * Insert data into this table. * @param {Array> | string} data - A 2D array of values to insert, or a SELECT query to insert data from. * @throws {MindsDbError} - Something went wrong inserting data into the table. @@ -113,6 +134,7 @@ export default class TablesRestApiClient extends TablesApiClient { override async deleteFile(name: string): Promise { const sqlQuery = `DROP TABLE files.${mysql.escapeId(name)}`; + const sqlQueryResult = await this.sqlClient.runQuery(sqlQuery); if (sqlQueryResult.error_message) { throw new MindsDbError(sqlQueryResult.error_message);