This SDK provides a JavaScript interface for interacting with MindsDB, making it easy to manage minds, datasources, and perform queries through MindsDB's REST API. It simplifies API calls, manages errors, and provides an intuitive interface for creating, listing, updating, and deleting minds and datasources.
- Minds Management: Create, retrieve, list, and delete MindsDB minds.
- Datasources Management: Set up, retrieve, list, and remove datasources.
- API Interaction Handling: Easy-to-use methods for common HTTP operations (GET, POST, PATCH, DELETE).
- Error Handling: Custom error handling for common HTTP status responses (e.g., 404 Not Found, 403 Forbidden, 401 Unauthorized, and generic server errors).
Install the SDK using npm:
npm install minds-js-sdkThe guide below provides details on initializing and managing minds and datasources with the SDK.
To use the SDK, import the Client class and create an instance with your MindsDB API key:
import Client from 'minds-js-sdk';
// Initialize the MindsDB client
const client = new Client('YOUR_API_KEY');The SDK allows you to configure and interact with "minds" — AI models linked with datasources and prompts.
Set up a new mind with a unique name, model details, linked datasources, and a prompt:
const mind = await client.minds.create('my_mind', {
modelName: 'gpt-3',
provider: 'OpenAI',
datasources: ['datasource1', 'datasource2'],
promptTemplate: 'You are a coding assistant',
});
console.log('Mind created:', mind);Retrieve a list of all minds associated with your account:
const minds = await client.minds.list();
console.log('Available minds:', minds);Access details of a specific mind by its name:
const mind = await client.minds.get('my_mind');
console.log('Mind details:', mind);Delete an existing mind by providing its name:
await client.minds.drop('my_mind');
console.log('Mind deleted');The SDK also allows for seamless management of datasources.
Add a datasource by specifying its configuration, which includes connection details and tables:
const datasourceConfig = {
name: 'my_datasource',
engine: 'postgres',
description: 'Sample Postgres datasource',
connectionData: {
user: 'demo_user',
password: 'demo_password',
host: 'samples.mindsdb.com',
port: 5432,
database: 'demo',
schema: 'demo_data',
},
tables: ['table1', 'table2'],
};
const datasource = await client.datasources.create(datasourceConfig);
console.log('Datasource created:', datasource);Get a list of all datasources available with your MindsDB account:
const datasources = await client.datasources.list();
console.log('Available datasources:', datasources);Fetch the details of a particular datasource by name:
const datasource = await client.datasources.get('my_datasource');
console.log('Datasource details:', datasource);Remove a datasource by specifying its name:
await client.datasources.drop('my_datasource');
console.log('Datasource deleted');- Error Handling: The SDK handles API errors internally, enabling better error management.
- Asynchronous API: All SDK methods return promises, allowing you to use
async/awaitfor smoother asynchronous handling.
The testing suite uses Jest for unit testing and axios-mock-adapter to mock HTTP requests.
- Dependencies:
- Ensure Jest and Axios are installed. Run
npm install jest axios axios-mock-adapterif they’re not already inpackage.json.
- Ensure Jest and Axios are installed. Run
- Scripts:
- Run all tests with:
npm test
- Run all tests with:
The project includes a set of centralized mock utility functions to streamline testing and improve code readability. These functions are located in mock.test.js and include:
mockGet- Mocks a successfulGETrequest.mockPost- Mocks a successfulPOSTrequest.mockDelete- Mocks a successfulDELETErequest.mockError- Mocks a failed request with a specific status and error message.
You can generate a test coverage report to ensure adequate test coverage:
npm test -- --coverageThe report will be in the coverage/ folder. You can open coverage/lcov-report/index.html to view a detailed report in your browser.
This test file verifies that the APIClient service can handle successful and error responses properly. Each endpoint is tested with specific HTTP responses.
- Successful GET request:
- Checks if
APIClient.get()returns the expected response data.
- Checks if
- Error handling:
- Verifies if
APIClientthrows specific exceptions (ObjectNotFound,Forbidden,Unauthorized,UnknownError) based on HTTP error codes like404,403,401, and500.
- Verifies if
The dataservice.test.js file tests the DatasourcesService class for managing data sources in the project. Using mockGet, mockPost, and mockDelete from mock.test.js, each service method is tested for correctness and error handling.
- Creating a Datasource:
- Tests if
create()properly posts data to/datasourcesand returns the expected object.
- Tests if
- Listing Datasources:
- Uses
mockGetto simulate fetching an array of datasources.
- Uses
- Getting a Datasource by Name:
- Uses
mockGetto fetch a single datasource by its name.
- Uses
- Deleting a Datasource:
- Simulates a successful deletion using
mockDelete.
- Simulates a successful deletion using
- Handling Unsupported Datasource Types:
- Tests if an unsupported datasource type throws an
ObjectNotSupportederror.
- Tests if an unsupported datasource type throws an
The minds.test.js file focuses on MindsService, which manages "minds" (models) in the system. These tests use the same mock.test.js utility functions.
- Creating a Mind:
- Uses
mockPostto test ifcreate()correctly posts data and returns the created mind.
- Uses
- Listing Minds:
- Uses
mockGetto simulate listing all available minds.
- Uses
- Getting a Mind by Name:
- Simulates fetching a mind by name and verifies the correct return data.
- Deleting a Mind:
- Uses
mockDeleteto test deletion functionality.
- Uses
- Handling Creation Error:
- Uses
mockErrorto test if an error during mind creation is handled correctly and throws the appropriate error message.
- Uses
If you’d like to contribute, please follow these steps:
- Fork the repository.
- Create a feature branch:
git checkout -b feature-name
- Commit your changes:
git commit -m "Add some feature" - Push to the branch:
git push origin feature-name
- Create a new Pull Request.
Ensure your code follows the existing code style and passes all tests before submitting.