diff --git a/src/index.ts b/src/index.ts index e42ce48..19ca649 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,6 +1,7 @@ import ModelsModule from './models/modelsModule'; import DatabasesModule from './databases/databasesModule'; import ProjectsModule from './projects/projectsModule'; +import JobsModule from './jobs/jobsModule'; import SQLModule from './sql/sqlModule'; import ViewsModule from './views/viewsModule'; import Constants from './constants'; @@ -26,6 +27,7 @@ import { MindsDbError } from './errors'; import { BatchQueryOptions, QueryOptions } from './models/queryOptions'; import { FinetuneOptions, TrainingOptions } from './models/trainingOptions'; import Project from './projects/project'; +import Job from './jobs/job'; import SqlQueryResult from './sql/sqlQueryResult'; import Table from './tables/table'; import { JsonPrimitive, JsonValue } from './util/json'; @@ -49,6 +51,11 @@ const Projects = new ProjectsModule.ProjectsRestApiClient( defaultAxiosInstance, httpAuthenticator ); +const Jobs = new JobsModule.JobsRestApiClient( + SQL, + defaultAxiosInstance, + httpAuthenticator +); const Tables = new TablesModule.TablesRestApiClient(SQL); const Views = new ViewsModule.ViewsRestApiClient(SQL); const MLEngines = new MLEnginesModule.MLEnginesRestApiClient( @@ -83,6 +90,7 @@ const connect = async function (options: ConnectionOptions): Promise { const httpClient = getAxiosInstance(options); SQL.client = httpClient; Projects.client = httpClient; + Jobs.client = httpClient; MLEngines.client = httpClient; Callbacks.client = httpClient; @@ -114,6 +122,7 @@ export default { Databases, Models, Projects, + Jobs, Tables, Views, MLEngines, @@ -131,6 +140,7 @@ export { FinetuneOptions, TrainingOptions, Project, + Job, SqlQueryResult, Table, JsonPrimitive, diff --git a/src/jobs/job.ts b/src/jobs/job.ts new file mode 100644 index 0000000..0da44a1 --- /dev/null +++ b/src/jobs/job.ts @@ -0,0 +1,25 @@ +export default class Job { + /** Name of the Job. */ + name: string; + query: string; + if_query: string; + start_at: string; + end_at: string; + schedule_str: string; + + constructor( + name: string, + query: string, + if_query: string, + start_at: string, + end_at: string, + schedule_str: string + ) { + this.name = name; + this.query = query; + this.if_query = if_query; + this.start_at = start_at; + this.end_at = end_at; + this.schedule_str = schedule_str; + } +} diff --git a/src/jobs/jobsApiClient.ts b/src/jobs/jobsApiClient.ts new file mode 100644 index 0000000..5f85ee2 --- /dev/null +++ b/src/jobs/jobsApiClient.ts @@ -0,0 +1,10 @@ +import Job from './job'; + +/** Abstract class outlining Jobs operations supported by the SDK. */ +export default abstract class JobsApiClient { + /** + * Removes a job for the current project + * @returns {Promise} + */ + abstract deleteJob(name: string, project: string): Promise; +} diff --git a/src/jobs/jobsModule.ts b/src/jobs/jobsModule.ts new file mode 100644 index 0000000..0ea5be8 --- /dev/null +++ b/src/jobs/jobsModule.ts @@ -0,0 +1,3 @@ +import JobsRestApiClient from './jobsRestApiClient'; + +export default { JobsRestApiClient }; diff --git a/src/jobs/jobsRestApiClient.ts b/src/jobs/jobsRestApiClient.ts new file mode 100644 index 0000000..48afd3b --- /dev/null +++ b/src/jobs/jobsRestApiClient.ts @@ -0,0 +1,44 @@ +import { Axios } from 'axios'; +import JobsApiClient from './jobsApiClient'; +import mysql from 'mysql'; +import SqlApiClient from '../sql/sqlApiClient'; +import HttpAuthenticator from '../httpAuthenticator'; +import { MindsDbError } from '../errors'; + +/** Implementation of JobsApiClient that goes through the REST API. */ +export default class JobsRestApiClient extends JobsApiClient { + /** Axios client to send all HTTP requests. */ + client: Axios; + + /** Authenticator to use for reauthenticating if needed. */ + authenticator: HttpAuthenticator; + + /** SQL API client to send all SQL query requests. */ + sqlClient: SqlApiClient; + + /** + * Constructor for Jobs API client. + * @param {Axios} client - Axios instance to send all HTTP requests. + */ + constructor(client: Axios, authenticator: HttpAuthenticator, sqlClient: SqlApiClient) { + super(); + this.client = client; + this.authenticator = authenticator; + this.sqlClient = sqlClient; + } + + /** + * Delete a job + * @returns {Promise>} - Drop a job + * @throws {MindsDbError} - Something went wrong fetching projects. + */ + override async deleteJob(name: string, project: string): Promise { + const deleteQuery = `DROP JOB ${mysql.escapeId(project)}.${mysql.escapeId( + name + )}`; + const sqlQueryResult = await this.sqlClient.runQuery(deleteQuery); + if (sqlQueryResult.error_message) { + throw new MindsDbError(sqlQueryResult.error_message); + } + } +}