SingleStore Client
The SingleStore Client is a package designed for interacting with the SingleStore API in Node.js environments.
Table of Contents
Installation
npm install @singlestore/client
Example Apps
Usage
Initialization
The SingleStoreClient can be initialized in multiple ways, depending on your needs. Below are examples of how to initialize the client in various scenarios.
Default
Use this method if you don’t need Management API access or AI integration.
import { SingleStoreClient } from "@singlestore/client";
const client = new SingleStoreClient();
With Management API Access
This method is used when you need to access SingleStore's management API.
import { SingleStoreClient } from "@singlestore/client";
const client = new SingleStoreClient({ apiKey: "<SINGLESTORE_API_KEY>" });
With AI Functionality
If you want to integrate AI features, use this method. You need to pass an AI instance with the required API key.
npm install @singlestore/ai
import { AI } from "@singlestore/ai";
import { SingleStoreClient } from "@singlestore/client";
const ai = new AI({ openAIApiKey: "<OPENAI_API_KEY>" });
const client = new SingleStoreClient({ ai });
Additional Notes
- The SingleStoreClient class is flexible, allowing you to pass only the features you need (e.g., AI, API key). It will automatically configure the services based on the provided options.
- You can also use custom LLMs instead of the pre-installed OpenAI. To do this, see the
@singlestore/ai
package documentation.
Organization
Get Current Organization
Returns the current organization if an API key was provided during initialization.
const organization = await client.organization.get();
Workspace Group
Get Workspace Group
All Workspace Groups
const workspaceGroups = await client.workspaceGroup.get();
By Workspace Group ID
const workspaceGroup = await client.workspaceGroup.get({
where: { id: "<WORKSPACE_GROUP_ID>" },
});
By Workspace Group Name
const workspaceGroup = await client.workspaceGroup.get({
where: { name: "<WORKSPACE_GROUP_NAME>" },
});
Additional Notes
- To include terminated workspace groups, add the
includeTerminated: true
parameter to the workspaceGroup.get
options object. - To select specific fields from a workspace group, add the
select: ['<FIELD_NAME_TO_SELECT>']
parameter to the workspaceGroup.get
options object.
Create Workspace Group
const { workspaceGroup, adminPassword } = await client.workspaceGroup.create({
name: "<WORKSPACE_GROUP_NAME>",
regionName: "US West 2 (Oregon)",
adminPassword: "<WORKSPACE_GROUP_PASSWORD>",
allowAllTraffic: false,
firewallRanges: ["IP_ADDRESS"],
dataBucketKMSKeyID: "<ID>",
backupBucketKMSKeyID: "<ID>",
updateWindow: { day: "mo", hour: 12 },
expiresAt: new Date("2025-01-01"),
});
Additional Notes
- Only the
name
and regionName
fields are required to create a workspace group. All other fields are optional. - If the
adminPassword
value is not provided, a generated password is returned. - To find all available
regionName
values, refer to this link.
Update Workspace Group
You can update a workspace group by specifying the workspace group ID or by calling the update
method on a selected Workspace Group instance.
By Workspace Group ID
await client.workspaceGroup.update("<WORKSPACE_GROUP_ID>", {
name: "<NEW_WORKSPACE_GROUP_NAME>",
adminPassword: "<NEW_WORKSPACE_GROUP_PASSWORD>",
allowAllTraffic: true,
firewallRanges: ["<NEW_IP_ADDRESS>"],
updateWindow: { day: "mo", hour: 12 },
expiresAt: new Date("2025-01-01"),
});
Selected Workspace Group
Updates the currently selected workspace group.
await workspaceGroup.update({
name: "<NEW_WORKSPACE_GROUP_NAME>",
adminPassword: "<NEW_WORKSPACE_GROUP_PASSWORD>",
allowAllTraffic: true,
firewallRanges: ["<NEW_IP_ADDRESS>"],
updateWindow: { day: "mo", hour: 12 },
expiresAt: new Date("2025-01-01"),
});
Additional Notes
- All fields are optional when updating a workspace group.
Delete Workspace Group
You can delete a workspace group by specifying the workspace group ID or by calling the delete
method on a selected Workspace Group instance.
By Workspace Group ID
await client.workspaceGroup.delete("<WORKSPACE_GROUP_ID>", false);
Selected Workspace Group
Deletes the currently selected workspace group.
await workspaceGroup.delete(false);
Additional Notes
- To forcibly delete a workspace group, set the optional
force
argument to true
.
Get Metrics
const metrics = await workspaceGroup.getMetrics();
Get Private Connections
const privateConnections = await workspaceGroup.getPrivateConnections();
Workspace
Connect Workspace
Using Client
const connection = client.connect({
host: "<WORKSPACE_HOST>",
user: "<WORKSPACE_USER>",
password: "<WORKSPACE_PASSWORD>",
port: <WORKSPACE_PORT>
});
Using Workspace Group
const workspace = await workspaceGroup.workspace.get({
where: { id: "<WORKSPACE_ID>" },
});
if (workspace) {
const connection = workspace.connect({
user: "<WORKSPACE_USER>",
password: "<WORKSPACE_PASSWORD>",
port: <WORKSPACE_PORT>,
});
}
Free Tier
To connect to a free tier workspace, download the SSL certificate from this link, set the port
and database
, and place the SSL certificate in the root directory of your project.
const connection = client.connect({
host: "<WORKSPACE_HOST>",
user: "<WORKSPACE_USER>",
password: "<WORKSPACE_PASSWORD>",
port: <WORKSPACE_PORT>
database: "<DATABASE_NAME>",
ssl: { ca: readFileSync(resolve(process.cwd(), "singlestore_bundle.pem")) },
});
Get Workspace
All Workspaces
const workspace = await workspaceGroup.workspace.get();
By Workspace ID
const workspace = await workspaceGroup.workspace.get({
where: { id: "<WORKSPACE_ID>" },
});
By Workspace Name
const workspace = await workspaceGroup.workspace.get({
where: { name: "<WORKSPACE_NAME>" },
});
Additional Notes
- To include terminated workspaces, add the
includeTerminated: true
parameter to the workspaceGroup.workspace.get
options object. - To select specific fields from a workspace group, add the
select: ['<FIELD_NAME_TO_SELECT>']
parameter to the workspaceGroup.workspace.get
options object.
Create Workspace
const workspace = await workspaceGroup.workspace.create({
name: "WORKSPACE_NAME",
size: "S-00",
enableKai: true,
cacheConfig: 1,
scaleFactor: 1,
autoSuspend: {
suspendType: "SCHEDULED",
suspendAfterSeconds: 1200,
},
});
Additional Notes
- Only the
name
field is required to create a workspace. All other fields are optional. - To find all available
size
values, refer to the SingleStore Helios Pricing page.
Update Workspace
By Workspace ID
await workspaceGroup.workspace.update("<WORKSPACE_ID>", {
size: "S-00",
enableKai: true,
cacheConfig: 1,
scaleFactor: 1,
deploymentType: "PRODUCTION",
autoSuspend: {
suspendType: "SCHEDULED",
suspendAfterSeconds: 1200,
},
});
Selected Workspace
await workspace.update({
size: "S-00",
enableKai: true,
cacheConfig: 1,
scaleFactor: 1,
deploymentType: "PRODUCTION",
autoSuspend: {
suspendType: "SCHEDULED",
suspendAfterSeconds: 1200,
},
});
Additional Notes
- All fields are optional when updating a workspace.
Delete Workspace
By Workspace ID
await workspaceGroup.workspace.delete("<WORKSPACE_ID>");
Selected Workspace
await workspace.delete();
Get Workspace State
By Workspace ID
const state = await workspaceGroup.workspace.getState("<WORKSPACE_ID>");
Selected Workspace
const state = await workspace.getState();
Resume Workspace
By Workspace ID
await workspaceGroup.workspace.resume("<WORKSPACE_ID>", { disableAutoSuspend: false });
Selected Workspace
await workspace.resume({ disableAutoSuspend: false });
Additional Notes
- The
disableAutoSuspend
parameter is optional.
Suspend Workspace
By Workspace ID
await workspaceGroup.workspace.suspend("<WORKSPACE_ID>");
Selected Workspace
await workspace.suspend();
Database
Use Database
The use
method allows you to interact with a specific database within the connection. You can optionally provide a generic DatabaseSchema
to describe the database schema and its tables.
interface DatabaseSchema extends DatabaseType {
name: "<DATABASE_NAME>";
tables: {
users: {
id: number;
};
};
}
const database = connection.database.use<DatabaseSchema>("<DATABASE_NAME>");
Create Database
The create
method allows you to create a database within the connection. You can optionally provide a generic DatabaseSchema
to describe the database schema and its tables.
interface DatabaseSchema extends DatabaseType {
name: "<DATABASE_NAME>";
tables: {
users: {
id: number;
};
};
}
const database = await connection.database.create<DatabaseSchema>({
name: "<DATABASE_NAME>",
tables: {
users: {
columns: {
id: {
type: "BIGINT",
autoIncrement: true,
primaryKey: true,
nullable: false,
default: 0,
clauses: ["<CUSTOM_CLAUSE>"],
},
},
clauses: ["<CUSTOM_CLAUSE>"],
fulltextKeys: ["<COLUMN_NAME>"],
primaryKeys: ["<COLUMN_NAME>"],
},
},
});
Drop Database
By Database Name
await connection.database.drop("<DATABASE_NAME>");
Selected Database
await database.drop();
Query Database
The query
method allows you to execute a MySQL query on the database and retrieve the result. The query result is returned as an array of rows, where each row is represented as an object with column names as keys and the corresponding values.
type RowType = { [K: string]: any }[];
const [rows] = await database.query<RowType>("<MYSQL_QUERY>");
Additional Notes
- Ensure that the query string is properly formatted to prevent SQL errors.
- The
RowType
is a flexible type that can accommodate various column structures in the query result.
Describe Database
const info = await database.describe();
Show Database Info
The showInfo
method allows you to retrieve information about the database. You can optionally request extended information by setting the isExtended
argument to true
.
const info = await database.showInfo(true);
Show Database Tables Info
The showTablesInfo
method allows you to retrieve information about the database tables. You can optionally request extended information by setting the isExtended
argument to true
.
const tablesInfo = await database.showTablesInfo(true);
Table
Use Table
The use
method allows you to access a specific table within the database. It optionally accepts a table name and schema, providing an interface to interact with the table for querying and manipulation.
type TableName = "<TABLE_NAME>";
type TableSchema = { [K: string]: any };
const table = database.table.use<TableName, TableSchema>("<TABLE_NAME>");
Create Table
The create
method allows you to create a new table in the database. You can define the table name and schema, specifying columns and their properties such as data types, constraints, and default values.
type TableName = "<TABLE_NAME>";
type TableSchema = { id: number };
const table = await database.table.create<TableName, TableSchema>({
name: "<TABLE_NAME>",
columns: {
id: {
type: "BIGINT",
autoIncrement: true,
primaryKey: true,
nullable: false,
default: 0,
clauses: ["<CUSTOM_CLAUSE>"],
},
},
});
Drop Table
By Table Name
await database.table.drop("<TABLE_NAME>");
Selected Table
await table.drop();
Truncate Table
By Table Name
await database.table.truncate("<TABLE_NAME>");
Selected Table
await table.truncate();
Rename Table
By Table Name
await database.table.rename("<TABLE_NAME>", "<TABLE_NEW_NAME>");
Selected Table
await table.rename("<TABLE_NEW_NAME>");
Show Table Info
The showInfo
method allows you to retrieve information about the table. You can optionally request extended information by setting the isExtended
argument to true
.
const tableInfo = await table.showInfo(true);
Show Table Columns Info
The showInfo
method allows you to retrieve information about the table columns.
const tableColumnsInfo = await table.showColumnsInfo();
Insert Table Value
The insert
method allows you to insert data into a table. You can insert a single value or multiple values at once by providing an object or an array of objects that map column names to values.
Single Value
await table.insert({columnName: <VALUE>})
Multiple Values
await table.insert([{columnName: <VALUE>}, {columnName: <VALUE_2>}])
Find Table Values
The find
method allows you to retrieve values from a table, with optional support for conditions, joins, grouping, ordering, and pagination. You can either fetch all values from a table or apply conditions to narrow down th
Find All Values
Retrieves all values from the table without any conditions.
const values = await table.find();
Find Values By Condition
Retrieves values from the table based on the specified conditions. You can customize the query with select, join, where, groupBy, orderBy, limit, and offset options.
const values = await table.find({
select: ["<COLUMN_NAME>", "COUNT(*) AS count"],
join: [
{
type: "FULL",
table: "<JOIN_TABLE_NAME>",
as: "<JOIN_TABLE_AS>",
on: [
"<COLUMN_NAME>",
"=",
"<JOIN_COLUMN_NAME>",
],
},
],
where: { columnName: "<COLUMN_VALUE>" },
groupBy: ["<COLUMN_NAME>"],
orderBy: {
columnName: "asc",
},
limit: 10,
offset: 10,
});
Additional Notes
- The
COUNT(*) AS count
pattern follows the clause AS alias
structure, where COUNT(*)
is the clause
and count
is the alias
. - Ensure that joins, conditions, and selected columns are appropriate for the table schema and the data you're trying to retrieve.
Update Table Values
The update
method allows you to modify existing values in the table. You provide the new values to update, along with a condition to specify which rows should be updated.
await table.update(
{ columnName: "<NEW_COLUMN_VALUE>" },
{ columnName: "<COLUMN_VALUE>" },
);
Delete Table Values
The delete
method allows you to remove rows from the table that match a specified condition.
await table.delete({ columnName: "<COLUMN_VALUE>" });
Additional Notes
- Be cautious when using the
delete
method, especially if the where condition is broad, as it could result in the removal of multiple rows. - If no where condition is provided, all rows in the table will be deleted. It’s best practice to always provide a where clause to avoid accidental data loss.
Table Vector Search
The vectorSearch
method allows you to perform searches using vector-based embeddings in a specified column. This is particularly useful for tasks such as semantic search, where results are based on the similarity of vector representations of text or data.
Basic
Performs a vector search based on a prompt, returning rows from the table that match the vector similarity.
const rows = await table.vectorSearch({
prompt: "<PROMPT>",
vectorColumn: "<VECTOR_COLUMN_NAME>",
embeddingParams: {
model: "<MODEL_NAME>",
dimensions: "<VECTOR_DIMENSTION>",
},
});
With Conditions
Performs a vector search with additional conditions such as selected columns, joins, filtering, grouping, ordering, and pagination.
const rows = await table.vectorSearch(
{
prompt: "<PROMPT>",
vectorColumn: "<VECTOR_COLUMN_NAME>",
embeddingParams: {
model: "<MODEL_NAME>",
dimensions: "<VECTOR_DIMENSTION>",
},
},
{
select: ["<COLUMN_NAME>"],
join: [
{
type: "FULL",
table: "<JOIN_TABLE_NAME>",
as: "<JOIN_TABLE_AS>",
on: [
"<COLUMN_NAME>",
"=",
"<JOIN_COLUMN_NAME>",
],
},
],
where: { columnName: "<COLUMN_VALUE>" },
groupBy: ["<COLUMN_NAME>"],
orderBy: {
columnName: "asc",
},
limit: 10,
offset: 10,
},
);
Additional Notes
- The
vectorSearch
method returns both the table rows and a v_score
field, which reflects the similarity score of each row to the search prompt. - Conditions such as
select
, join
, where
, and others can be used to refine the results further, similar to standard SQL queries.
Create Table Chat Completion
The createChatCompletion
method allows you to generate chat completions based on a vector search within a table. Depending on the stream
option, you can retrieve the results either as a complete string or in a streamed fashion, with optional custom tools for enhancing functionality.
As String
Performs a chat completion based on a vector search and returns the result as a complete string.
const chatCompletion = await table.createChatCompletion(
{
model: "<MODEL_NAME>",
prompt: "<PROMPT>",
systemRole: "<SYSTEM_ROLE>",
vectorColumn: "<VECTOR_COLUMN_NAME>",
stream: false,
temperature: 0,
embeddingParams: {
model: "<MODEL_NAME>",
dimensions: "<VECTOR_DIMENSTION>",
},
},
{
select: ["<COLUMN_NAME>"],
where: { columnName: "<COLUMN_VALUE>" },
limit: 1,
},
);
As Stream
Performs a chat completion and returns the result as a stream of data chunks.
const stream = await table.createChatCompletion(
{
stream: true,
...
},
);
const chatCompletion = await ai.chatCompletions.handleStream(stream, (chunk) => {
console.log(chunk);
});
With Custom Tools
You can also integrate custom tools to extend the functionality of the chat completion.
import { ChatCompletionTool } from "@singlestore/ai/chat-completions";
import { z } from "zod";
const customTool = new ChatCompletionTool({
name: "<TOOL_NAME>",
description: "<TOOL_DESCRIPTION>",
params: z.object({ paramName: z.string().describe("<PARAM_DESCRIPTION>") }),
call: async (params) => {
const value = await anyFnCall(params);
return { name: "<TOOL_NAME>", params, value: JSON.stringify(value) };
},
});
await table.createChatCompletion({
tools: [customTool],
...
});
Additional Notes
- The second argument of the
createChatCompletion
method accepts the same options as the second argument of the vectorSearch
method, such as select
, where
, and limit
. - When using
stream: true
, the handleStream
function is used to process the stream. It accepts a callback as the second argument, which handles each new chunk of data as it arrives.
Column
Use Column
const column = table.column.use("<COLUMN_NAME>");
Add Column
const column = await table.column.add({
name: "<NEW_COLUMN_NAME>",
type: "BIGINT",
autoIncrement: false,
primaryKey: false,
nullable: true,
default: 0,
clauses: ["<CUSTOM_CLAUSE>"],
});
Modify Column
By Column Name
await table.column.modify("<COLUMN_NAME>", {
type: "BIGINT",
autoIncrement: false,
primaryKey: false,
nullable: true,
default: 0,
clauses: ["<CUSTOM_CLAUSE>"],
});
Selected Column
await column.modify(...)
Rename Column
By Column Name
await table.column.rename("<COLUMN_NAME>", "<NEW_COLUMN_NAME>");
Selected Column
await column.modify("<NEW_COLUMN_NAME>");
Drop Column
By Column Name
await table.column.drop("<COLUMN_NAME>");
Selected Column
await column.drop();
Show Column Info
By Column Name
await table.column.showInfo("<COLUMN_NAME>");
Selected Column
await column.showInfo();
Billing
Get Billing
const billing = await client.billing.get({
metric: "ComputeCredit",
startTime: new Date("2024-01-01"),
endTime: new Date("2024-01-09"),
aggregateBy: "month",
});
Region
Get Region
Get All Regions
const regions = await client.region.get();
Get Region By ID
const region = await client.region.get({ id: "<REGION_ID>" });
Get Region By Name
const region = await client.region.get({ name: "<REGION_NAME>" });
Additional Notes
- To find all available region names, follow this link
Team
Get Team
Get All Teams
const teams = await client.team.get();
Get Team By ID
const team = await client.team.get({ id: "<TEAM_ID>" });
Get Team By Name
const team = await client.team.get({ name: "<TEAM_NAME>" });
Get Team By Description
const team = await client.team.get({ description: "<TEAM_DESCRIPTION>" });
Create Team
const team = await client.team.create({
name: "<TEAM_NAME>",
description: "<TEAM_DESCRIPTION>",
memberTeams: ["<TEAM_ID>"],
memberUsers: ["<USER_ID>"],
});
Update Team
By Team ID
await client.team.update("<TEAM_ID>", {
name: "<NEW_TEAM_NAME>",
description: "<NEW_TEAM_DESCRIPTION>",
});
Selected Team
await team.update(...);
Delete Team
By Team ID
await client.team.delete("<TEAM_ID>");
Selected Team
await team.delete();
Add Team Member
Add Team
By Team ID
await client.team.addMemberTeams("<TEAM_ID>", ["<ADD_TEAM_ID>"]);
Selected Team
await team.addMemberTeams(["<ADD_TEAM_ID>"]);
Add User
By Team ID
await client.team.addMemberUsers("<TEAM_ID>", ["<ADD_USER_ID>"]);
Selected Team
await team.addMemberUsers(["<ADD_USER_ID>"]);
Remove Team Member
Remove Team
By Team ID
await client.team.removeMemberTeams("<TEAM_ID>", ["<REMOVE_TEAM_ID>"]);
Selected Team
await team.removeMemberTeams(["<REMOVE_TEAM_ID>"]);
Remove User
By Team ID
await client.team.removeMemberUsers("<TEAM_ID>", ["<REMOVE_USER_ID>"]);
Selected Team
await team.removeMemberUsers(["<REMOVE_USER_ID>"]);
Job
Get Job
const job = client.job.get("<JOB_ID>");
Create Job
const job = await client.job.create({
name: "<JOB_NAME>",
description: "<JOB_DESCRIPTION>",
executionConfig: {
notebookPath: "<NOTEBOOK_NAME.ipynb>",
createSnapshot: true,
runtimeName: "notebooks-cpu-small",
},
schedule: {
mode: "Recurring",
executionIntervalInMinutes: 60,
startAt: new Date("2025-01-01"),
},
targetConfig: {
databaseName: "<DATABASE_NAME>",
resumeTarget: true,
targetID: "<TARGET_ID>",
targetType: "Workspace",
},
});
Delete Job
By Job ID
await client.job.delete("<JOB_ID>");
Selected Job
await job.delete();
Get Job Executions
By Job ID
const executions = await client.job.getExecutions("<JOB_ID>", 1, 10);
Selected Job
const executions = await job.getExecutions(1, 10);
Get Job Parameters
By Job ID
const parameters = await client.job.getParameters("<JOB_ID>");
Selected Job
const parameters = await job.getParameters();
Get Job Runtimes
const runtimes = await client.job.getRuntimes();
Secret
Get Secret
Get All Secrets
const secrets = await client.secret.get();
Get By Secret ID
const secret = await client.secret.get({ id: "<SECRET_ID>" });
Get By Secret Name
const secret = await client.secret.get({ name: "<SECRET_NAME>" });
Create Secret
const secret = await client.secret.create({
name: "<SECRET_NAME>",
value: "<SECRET_VALUE>",
});
Update Secret
By Secret ID
const secret = await client.secret.update("<SECRET_ID>", "<NEW_SECRET_VALUE>");
Selected Secret
const secret = await secret.update("<NEW_SECRET_VALUE>");
Delete Secret
By Secret ID
await client.secret.delete("<SECRET_ID>");
Selected Secret
await secret.delete();
Stage
- Folder path example:
folderName/
- File path example:
folderName/fileName.json
Get All Stage
const stage = await workspaceGroup.stage.get();
Get Stage Folder
const stage = await workspaceGroup.stage.get("<STAGE_PATH>/");
const nextStage = await stage.get("<STAGE_PATH>/");
Get Stage File
const stage = await workspaceGroup.stage.get("<STAGE_PATH>");
const nextStage = await stage.get("<STAGE_PATH>");
Update Stage
By Stage Path
await workspaceGroup.stage.update("<STAGE_PATH>", { newPath: "<NEW_STAGE_PATH>" });
Selected Stage
await stage.update({ newPath: "<NEW_STAGE_PATH>" });
Additional Notes
Create Stage Folder
In Stage Path
const newStage = await workspaceGroup.stage.createFolder("<NEW_STAGE_PATH>", "NEW_STAGE_NAME");
In Selected Stage
const newStage = stage.createFolder(
"<NEW_STAGE_NAME>",
"<NEW_STAGE_PATH>",
);
Delete Stage
By Stage Path
await workspaceGroup.stage.delete("<STAGE_PATH>");
Selected Stage
await stage.delete();
Storage
Get Storage Regions
const regions = await workspaceGroup.storage.getRegions();
Get Storage Status
const status = await workspaceGroup.storage.getStatus();
Connection
Execute Custom Query
const result = await connection.client.execute("<SQL_QUERY>");