Skip to content

Commit

Permalink
Merge branch 'mindsdb:main' into skills
Browse files Browse the repository at this point in the history
  • Loading branch information
md-abid-hussain authored Nov 18, 2024
2 parents 40de90d + 446e698 commit 6ff3d6c
Show file tree
Hide file tree
Showing 7 changed files with 149 additions and 6 deletions.
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ const SQL = new SQLModule.SqlRestApiClient(
const Databases = new DatabasesModule.DatabasesRestApiClient(SQL);
const Models = new ModelsModule.ModelsRestApiClient(SQL);
const Projects = new ProjectsModule.ProjectsRestApiClient(
SQL,
defaultAxiosInstance,
httpAuthenticator
);
Expand Down
23 changes: 20 additions & 3 deletions src/projects/project.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,22 @@
/** Structure of a MindsDB Project. */
export default interface Project {
/** Name of the project. */
import ProjectsApiClient from './projectsApiClient';

/**
* Represents a MindsDB project and all supported operations.
*/
export default class Project {
/** API client to use for executing mlEngine operations. */
projectApiClient: ProjectsApiClient;

/** Name of the mlEngine. */
name: string;

/**
*
* @param {ProjectsApiClient} projectApiClient - API client to use for executing project operations.
* @param {string} name - Name of the project.
*/
constructor(projectApiClient: ProjectsApiClient, name: string) {
this.projectApiClient = projectApiClient;
this.name = name;
}
}
13 changes: 13 additions & 0 deletions src/projects/projectsApiClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,17 @@ export default abstract class ProjectsApiClient {
* @returns {Promise<Array<Project>>} - All projects.
*/
abstract getAllProjects(): Promise<Array<Project>>;

/**
* Creates a new MindsDB project.
* @param {string} name - Name of the project.
* @returns {Promise<Project>} - The created project.
*/
abstract createProject(name: string): Promise<Project>;

/**
*
* @param {string} name - Name of the project to delete.
*/
abstract deleteProject(name: string): Promise<void>;
}
42 changes: 40 additions & 2 deletions src/projects/projectsRestApiClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,30 @@ import Constants from '../constants';
import Project from './project';
import HttpAuthenticator from '../httpAuthenticator';
import { MindsDbError } from '../errors';
import SqlApiClient from '../sql/sqlApiClient';

/** Implementation of ProjectsApiClient that goes through the REST API. */
export default class ProjectsRestApiClient extends ProjectsApiClient {
/** SQL API client to send all SQL query requests. */
sqlClient: SqlApiClient;

/** Axios client to send all HTTP requests. */
client: Axios;

/** Authenticator to use for reauthenticating if needed. */
/** Authenticator to use for reauthenticate if needed. */
authenticator: HttpAuthenticator;

/**
* Constructor for Projects API client.
* @param {Axios} client - Axios instance to send all HTTP requests.
*/
constructor(client: Axios, authenticator: HttpAuthenticator) {
constructor(
sqlClient: SqlApiClient,
client: Axios,
authenticator: HttpAuthenticator
) {
super();
this.sqlClient = sqlClient;
this.client = client;
this.authenticator = authenticator;
}
Expand Down Expand Up @@ -52,4 +61,33 @@ export default class ProjectsRestApiClient extends ProjectsApiClient {
throw MindsDbError.fromHttpError(error, projectsUrl);
}
}

/**
*
* @param name - Name of the project to create.
* @returns {Promise<Project>} - The created project.
*/
override async createProject(name: string): Promise<Project> {
const sqlQuery = `CREATE PROJECT ${name}`;

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

return new Project(this, name);
}

/**
*
* @param {string} name - Name of the project to delete.
*/
override async deleteProject(name: string): Promise<void> {
const sqlQuery = `DROP PROJECT ${name}`;

const sqlQueryResult = await this.sqlClient.runQuery(sqlQuery);
if (sqlQueryResult.error_message) {
throw new MindsDbError(sqlQueryResult.error_message);
}
}
}
17 changes: 16 additions & 1 deletion src/tables/table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,13 @@ export default class Table {
this.name = name;
this.integration = integration;
}

/**
* Removes this table from its integration.
* @throws {MindsDbError} - Something went wrong removing this table.
*/
async removeTable(): Promise<void> {
await this.tablesApiClient.removeTable(this.name, this.integration);
}
/**
* Creates a table in an integration from a given SELECT statement.
* @param {string} select - SELECT statement to use for populating the new table with data.
Expand All @@ -47,4 +53,13 @@ export default class Table {
async delete(): Promise<void> {
await this.tablesApiClient.deleteTable(this.name, this.integration);
}

/**
* Insert data into this table.
* @param {string} select - SELECT query to insert data from.
* @throws {MindsDbError} - Something went wrong inserting data into the table.
*/
async insert(select: string): Promise<void> {
await this.tablesApiClient.insertTable(this.name, this.integration, select);
}
}
18 changes: 18 additions & 0 deletions src/tables/tablesApiClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,28 @@ export default abstract class TablesApiClient {
*/
abstract deleteTable(name: string, integration: string): Promise<void>;

/**
* 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.
* @param {string} select - SELECT query to insert data from.
* @throws {MindsDbError} - Something went wrong inserting data into the table.
*/
abstract insertTable(name: string, integration: string, select: string): Promise<void>;
/**
* Deletes a file from the files integration.
* @param {string} name - Name of the file to be deleted.
* @throws {MindsDbError} - Something went wrong deleting the file.
*/
abstract deleteFile(name: string): Promise<void>;
}

/**
* Removes a table from its integration.
* @param {string} name - Name of the table to be removed.
* @param {string} integration - Name of the integration the table belongs to.
* @returns {Promise<void>} - Resolves when the table is successfully removed.
* @throws {MindsDbError} - Something went wrong removing the table.
*/
abstract removeTable(name: string, integration: string): Promise<void>;

41 changes: 41 additions & 0 deletions src/tables/tablesRestApiClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,23 @@ export default class TablesRestApiClient extends TablesApiClient {
}
}

/**
* Insert data into this table.
* @param {Array<Array<any>> | 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.
*/
override async insertTable(name: string, integration: string, select: string): Promise<void> {
try {
const sqlQuery = `INSERT INTO ${mysql.escapeId(integration)}.${mysql.escapeId(name)} (${select})`;
const sqlQueryResult = await this.sqlClient.runQuery(sqlQuery);
if (sqlQueryResult.error_message) {
throw new MindsDbError(sqlQueryResult.error_message);
}
} catch(error) {
throw new MindsDbError(`Insert into a table failed: ${error}`);
}
}

/**
* Deletes a file from the files integration.
* @param {string} name - Name of the file to be deleted.
Expand All @@ -102,3 +119,27 @@ export default class TablesRestApiClient extends TablesApiClient {
}
}
}

/**
* Removes a table from its integration.
* @param {string} name - Name of the table to be removed.
* @param {string} integration - Name of the integration the table belongs to.
* @returns {Promise<void>} - Resolves when the table is successfully removed.
* @throws {MindsDbError} - Something went wrong removing the table.
*/
override async removeTable(
name: string,
integration: string
): Promise<void> {
// Construct the SQL query to drop the table
const sqlQuery = `DROP TABLE ${mysql.escapeId(integration)}.${mysql.escapeId(name)}`;

// Execute the SQL query using the sqlClient
const sqlQueryResult = await this.sqlClient.runQuery(sqlQuery);

// Check for errors
if (sqlQueryResult.error_message) {
throw new MindsDbError(sqlQueryResult.error_message);
}
}

0 comments on commit 6ff3d6c

Please sign in to comment.