The Elegance SDK is an SDK for quickly building real-time AI full-stack JavaScript applications using SingleStoreDB with support for MySQL and Kai (support for Mongo APIs) connection types and the OpenAI API. It provides a set of ready-made tools for impl
npm install @singlestore/elegance-sdkThe Elegance SDK is an SDK for quickly building real-time AI full-stack JavaScript applications using SingleStoreDB with support for MySQL and Kai (support for Mongo APIs) connection types and the OpenAI API. It provides a set of ready-made tools for implementing various types of functionality when you need to transact, analyze, and contextualize data in real-time or build real-time AI apps.
- Dot product search
- Chat completions
- File embeddings generation (csv, pdf)
- SQL and aggregate queries
- SQL and Kai (MongoDB) database connections support
- Ready-to-use Node.js controllers and React.js hooks
``sh`
npm install @singlestore/elegance-sdk
This guide will show you how to use the SDK in Express.js and React.js.
1. Create a eleganceServerClient.ts filecreateEleganceServerClient
2. Import the function from @singlestore/elegance-sdk/server
`tsx
import { createEleganceServerClient } from "@singlestore/elegance-sdk/server";
export const eleganceServerClient = createEleganceServerClient("mysql", {
connection: {
host: "
user: "
password: "
database: "
},
ai: {
openai: {
apiKey: "
}
}
});
`
In case if you don't want to use OpenAI, you can replace the existing AI SDK logic with customizers:
`tsx
...
import {
EmbeddingInput,
CreateEmbeddingResult,
CreateChatCompletionParams,
CreateChatCompletionResult
} from "@singlestore/elegance-sdk/types";
...
ai: {
customizers: {
createEmbedding: async (input: EmbeddingInput): Promise
const embedding = await customFn(input);
return embedding;
},
createChatCompletion: async (params: CreateChatCompletionParams): Promise
const chatCompletion = await customFn(params);
return chatCompletion;
}
}
}
...
`
3. Create a route handler for elegance/:route (using Express.js as an example).
`tsx
import express from "express";
import type { Routes } from "@singlestore/elegance-sdk/server";
import { eleganceServerClient } from "@/services/eleganceServerClient";
export const eleganceRouter = express.Router();
eleganceRouter.post("/elegance/:route", async (req, res) => {
try {
const route = req.params.route as Routes;
const result = await eleganceServerClient.handleRoute(route, req.body);
return res.status(200).json(result);
} catch (error: any) {
return res.status(error.status).json(error);
}
});
`
4. Import the eleganceRouter into the ./server/index.ts
`tsx
import express from "express";
import { eleganceRouter } from "./routes/elegance";
const app = express();
app.use(eleganceRouter);
app.listen(4000, () => {
console.log(Server started on port: ${4000});`
});
5. Run the server
6. Create a eleganceClient.ts filecreateEleganceClient
7. Import the function from @singlestore/elegance-sdk
`tsx
import { createEleganceClient } from "@singlestore/elegance-sdk";
export const eleganceClient = createEleganceClient("mysql", {
baseURL: "http://localhost:4000",
});
`
7. Import the eleganceClient to your component
`tsx
import { useEffect } from "react";
import { eleganceClient } from "@/services/eleganceClient";
export function ExampleComponent() {
const query = eleganceClient.hooks.useQuery<{ name: string }[]>({
initialValue: [],
initialIsLoading: true,
});
const { execute: executeQuery } = query;
useEffect(() => {
executeQuery({ query: "SELECT name FROM table LIMIT 3" });
}, [executeQuery]);
if (query.isLoading) return "Loading...";
return (
8. Run your client
Templates
You can find templates using the Elegance SDK here:
- Next.js Template
- Express.js Template
Example apps
You can find example apps using the Elegance SDK here:
API
#### createEleganceServerClient
Creates an EleganceServerClient instance for a server.
Parameters:
-
connectionType: "kai" | "mysql"
- `tsx
config: {
connection: KaiConnectionConfig | MySQLConnectionConfig;
ai?: {
openai?: OpenAIConfig;
customizers?: {
createEmbedding?: (input: EmbeddingInput) => Promise;
createChatCompletion?: (params: CreateChatCompletionParams) => Promise;
} // You can use your own functions to create an embedding or a chat completion.
};
}
`Returns:
eleganceServerClient#### eleganceServerClient
Server client that includes a database connection, controllers and AI client. It's used on the server to handle requests from the client and execute logic.
#### eleganceServerClient.connection
MySQL or MongoDB connection to interact with a database
#### eleganceServerClient.handleRoute
Accepts a route and executes the controller for that route.
Parameters:
-
route: string - controller route name
- body: object - controller body#### eleganceServerClient.controllers.insertOne\
Inserts one record.
Parameters:
Kai
`tsx
body: {
db?: string;
collection: string;
generateId?: boolean;
value: MongoOptionalUnlessRequiredId;
options?: MongoInsertOneOptions;
}
`MySQL
`tsx
body: {
db?: string;
collection: string;
generateId?: boolean;
value: T;
}
`Returns:
T#### eleganceServerClient.controllers.insertMany>
Inserts many records.
Parameters:
Kai
`tsx
body: {
db?: string;
collection: string;
values: Array>;
generateId?: boolean;
options?: MongoBulkWriteOptions;
}
`MySQL
`tsx
body: {
db?: string;
collection: string;
generateId?: boolean;
values: Array;
}
`Returns:
Array#### eleganceServerClient.controllers.updateMany>
Updates many records.
Parameters:
Kai
`tsx
body: {
db?: string;
collection: string;
filter: MongoFilter;
update: MongoUpdateFilter;
options?: MongoUpdateOptions;
updatedFilter?: MongoFilter;
}
`MySQL
`tsx
body: {
db?: string;
collection: string;
where: MySQLWhere;
set: MySQLSet;
updatedWhere: MySQLWhere;
}
`Returns:
Array#### eleganceServerClient.controllers.deleteMany\
Deletes many records.
Parameters:
Kai
`tsx
body: {
db?: string;
collection: string;
filter: MongoFilter;
options?: MongoDeleteOptions;
}
`MySQL
`tsx
body: {
db?: string;
collection: string;
where: MySQLWhere;
}
`Returns:
{ message: string }#### eleganceServerClient.controllers.findOne\
Gets one record.
Parameters:
Kai
`tsx
body: {
db?: string;
collection: string;
filter?: MongoFilter;
options?: MongoFindOptions;
}
`MySQL
`tsx
body: {
db?: string;
collection: string;
columns?: string[];
where?: MySQLWhere;
extra?: string;
}
`Returns:
T#### eleganceServerClient.controllers.findMany>
Gets many records.
Parameters:
Kai
`tsx
body: {
db?: string;
collection: string;
filter?: MongoFilter;
options?: MongoFindOptions;
}
`MySQL
`tsx
body: {
db?: string;
collection: string;
columns?: string[];
where?: MySQLWhere;
skip?: number;
limit?: number;
extra?: string;
}
`Returns:
Array