-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpasted_text_1726177322806.txt
224 lines (191 loc) · 4.92 KB
/
pasted_text_1726177322806.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
skip to:contentpackage searchsign in
❤
Pro
Teams
Pricing
Documentation
npm
Search packages
Search
@singlestore/client
TypeScript icon, indicating that this package has built-in type declarations
0.0.35 • Public • Published 7 days ago
SingleStore Client
The SingleStore Client is a package designed for interacting with the SingleStore API in Node.js environments.
Table of Contents
Installation
Usage Examples
Create an Instance
Connect to a Workspace
Create a Database
Use a Database
Create a Table
Insert Values
Find Values
Find Values by a Condition
Update Values by a Condition
Add AI Functionality
Perform a Vector Search
Create a Chat Completion Stream
Advanced Usage
Installation
To install the @singlestore/client package, run the following command:
npm install @singlestore/client
Usage Examples
Create an Instance
Instantiate the SingleStoreClient to interact with the SingleStore.
import { SingleStoreClient } from "@singlestore/client";
const client = new SingleStoreClient();
Connect to a Workspace
Connect to a specific workspace using your workspace credentials.
const workspace = client.workspace({
host: "<DATABASE_HOST>",
user: "<DATABASE_USER>",
password: "<DATABASE_PASSWORD>",
});
Create a Database
Create a new database within the connected workspace.
const database = await workspace.createDatabase({ name: "my_database" });
Use a Database
Select and use an existing database.
const database = await workspace.database("my_database");
Create a Table
Create a table within the selected database with specified columns and attributes.
const usersTable = await database.createTable({
name: "users",
columns: {
id: { type: "BIGINT", autoIncrement: true, primaryKey: true },
name: { type: "VARCHAR(32)" },
role: { type: "VARCHAR(32)" },
},
});
Insert Values
Insert multiple records into a table.
await usersTable.insert([
{ name: "User 1", role: "admin" },
{ name: "User 2", role: "visitor" },
]);
Find Values
Retrieve all records from a table.
const users = await usersTable.find();
Find Values by a Condition
Retrieve records that match specific conditions.
const admins = await usersTable.find({ where: { role: "admin" } });
Update Values by a Condition
Update records that meet certain conditions.
await usersTable.update({ role: "admin" }, { name: "User 2" });
Add AI Functionality
Integrate AI capabilities using the SingleStore AI package.
import { AI } from "@singlestore/ai";
import { SingleStoreClient } from "@singlestore/client";
const ai = new AI({ openAIApiKey: "<OPENAI_API_KEY>" });
const client = new SingleStoreClient({ ai });
Perform a Vector Search
Execute a vector search on a table to find relevant records.
const results = await usersTable.vectorSearch(
{ prompt: "A 4k monitor", vectorColumn: "description_v" },
{ select: ["name", "description", "price"], limit: 1 },
);
Create a Chat Completion Stream
Generate a chat completion or stream based on prompt and vector search results.
const stream = await usersTable.createChatCompletion(
{ prompt: "Find a 4k monitor", vectorColumn: "description_v", stream: true },
{ select: ["name", "description", "price"], limit: 1 },
);
const onChunk: OnChatCompletionChunk = (chunk) => console.log(chunk);
const chatCompletion = await ai.chatCompletions.handleStream(stream, onChunk);
console.log(chatCompletion);
Advanced Usage
A more complex example demonstrating advanced queries and table joins.
interface StoreDatabase {
name: "store_database";
tables: {
products: {
name: "products";
columns: {
id: number;
name: string;
description: string;
price: number;
category_id: number;
description_v: string;
};
};
categories: {
name: "categories";
columns: {
id: number;
name: string;
};
};
};
}
const client = new SingleStoreClient({ ai });
const workspace = client.workspace<{
databases: { store_database: StoreDatabase };
}>({
host: "<DATABASE_HOST>",
user: "<DATABASE_USER>",
password: "<DATABASE_PASSWORD>",
});
const database = workspace.database("store_database");
const products = await database.table("products").find({
select: ["id", "name", "description", "price", "category.name"],
join: [
{
table: "categories",
as: "category",
on: ["category_id", "=", "id"],
},
],
where: {
"category.id": 1,
"price": { gte: 500 },
},
orderBy: { price: "desc" },
limit: 5,
});
console.log(products);
Readme
Keywords
singlestore
Package Sidebar
Install
npm i @singlestore/client
Repository
github.com/singlestore-labs/singlestore
Homepage
github.com/singlestore-labs/singlestore
Weekly Downloads
180
Version
0.0.35
License
Apache-2.0
Unpacked Size
199 kB
Total Files
8
Last publish
7 days ago
Collaborators
lneves_singlestore
ydemenskyi
martaimv
Try on RunKit
Report malware
Footer
Support
Help
Advisories
Status
Contact npm
Company
About
Blog
Press
Terms & Policies
Policies
Terms of Use
Code of Conduct
Privacy