Skip to content

Commit

Permalink
Merge pull request #98 from md-abid-hussain/project-delete
Browse files Browse the repository at this point in the history
Implemented function to remove a project
  • Loading branch information
mpacheco12 authored Nov 18, 2024
2 parents df58b93 + 65f13f7 commit 446e698
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 5 deletions.
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,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);
}
}
}

0 comments on commit 446e698

Please sign in to comment.