JavaScript 和 TypeScript
最新的 TypeScript 客户端版本是 v3.9.0。
TypeScript 客户端支持用 TypeScript 或 JavaScript 编写的代码。
v3 客户端是当前的 TypeScript 客户端。如果您有为 v2 客户端编写的代码,您应该将其迁移到 v3,因为 v2 客户端不再维护。
v3 客户端支持服务器端开发(Node.js 托管)。如果您的应用程序是基于浏览器的,您可能需要考虑使用 TypeScript 客户端 v2。请记住,v2 客户端已过时且不再官方维护。
安装
本节详细介绍了如何安装和配置 v3 TypeScript 客户端。
安装包
v3 客户端包有一个新的名称,weaviate-client。使用 npm 安装 TypeScript 客户端库包
npm install weaviate-client
导入客户端
v3 客户端使用 ES Modules。文档中的大部分示例代码也使用 ES Module 样式。
如果您的代码需要 CommonJS 兼容性,请使用 CommonJS 导入样式
import weaviate from "weaviate-client";
TypeScript 设置
编辑您的项目配置文件以进行这些更改
- 在
package.json中添加"type": "module" - 将以下代码添加到
tsconfig.json
tsconfig.json 文件
{
"compilerOptions": {
"target": "ESNext",
"module": "NodeNext",
"moduleResolution": "NodeNext",
"esModuleInterop": true,
"skipLibCheck": true,
"allowSyntheticDefaultImports": true,
"strict": true
},
"include": ["src/index.ts"] // this compiles only src/.index.ts, to compile all .ts files, use ["*.ts"]
}
入门
如果您尚未这样做,我们建议您首先完成 快速入门教程,以便充分利用本节。
以下代码演示了如何
- 连接到本地 Weaviate 实例。
- 创建一个新的集合。
- 使用批量导入填充数据库并向量化数据。
- 执行向量搜索。
import weaviate, { WeaviateClient, vectors } from 'weaviate-client';
const client: WeaviateClient = await weaviate.connectToLocal();
await client.collections.create({
name: 'Question',
vectorizers: vectors.text2VecOllama({ // Configure the Ollama embedding integration
apiEndpoint: 'http://ollama:11434', // If using Docker you might need: http://host.docker.internal:11434
model: 'nomic-embed-text', // The model to use
}),
});
// Load data
async function getJsonData() {
const file = await fetch(
'https://raw.githubusercontent.com/weaviate-tutorials/quickstart/main/data/jeopardy_tiny.json'
);
return file.json();
}
// Note: The TS client does not have a `batch` method yet
// We use `insertMany` instead, which sends all of the data in one request
async function importQuestions() {
const questions = client.collections.use('Question');
const data = await getJsonData();
const result = await questions.data.insertMany(data);
console.log('Insertion response: ', result);
}
await importQuestions();
const questions = client.collections.use('Question');
const result = await questions.query.nearText('biology', {
limit: 2,
});
result.objects.forEach((item) => {
console.log(JSON.stringify(item.properties, null, 2));
});
client.close(); // Close the client connection
异步使用
所有客户端 v3 方法,除了 collection.use(),都使用 ES6 Promises 和异步代码。这意味着您必须在函数调用后使用 .then(),或者将您的代码包装在 async/await 块中。
当存在异步代码错误时,promise 会返回特定的错误消息。如果您使用 async 和 await,则拒绝的 promise 就像抛出异常一样。
发布版本
转到 GitHub 发布页面,以查看 TypeScript 客户端库发布的历史记录和变更日志。
点击此处查看 Weaviate 和相应客户端版本的表格
此表列出了最新的五个 Weaviate 数据库版本和相应的客户端库版本。
| Weaviate 数据库 (GitHub) | 首次 发布日期 | Python (GitHub) | TypeScript/ JavaScript (GitHub) | Go (GitHub) | Java (GitHub) |
|---|---|---|---|---|---|
| 1.35.x | 2025-12-17 | 4.19.x | N/A | N/A | N/A |
| 1.34.x | 2025-11-05 | 4.18.x | 3.10.x | 5.6.x | 6.0.0 |
| 1.33.x | 2025-09-25 | 4.17.x | 3.9.x | 5.5.x | 5.5.x |
| 1.32.x | 2025-07-14 | 4.16.x | 3.8.x | 5.3.x | 5.4.x |
| 1.31.x | 2025-05-30 | 4.15.x | 3.6.x | 5.2.x | 5.3.x |
| 1.30.x | 2025-04-03 | 4.12.x | 3.5.x | 5.1.x | 5.2.x |
| 1.29.x | 2025-02-17 | 4.11.x | 3.4.x | 5.0.x | 5.1.x |
旧版本发布
| Weaviate 数据库 (GitHub) | 首次 发布日期 | Python (GitHub) | TypeScript/ JavaScript (GitHub) | Go (GitHub) | Java (GitHub) |
|---|---|---|---|---|---|
| 1.28.x | 2024-12-11 | 4.10.x | 3.3.x | 4.16.x | 5.0.x |
| 1.27.x | 2024-10-16 | 4.9.x | 3.2.x | 4.16.x | 5.0.x 4.9.x |
| 1.26.x | 2024-07-22 | 4.7.x | 3.1.x | 4.15.x | 4.8.x |
| 1.25.x | 2024-05-10 | 4.6.x | 2.1.x | 4.13.x | 4.6.x |
| 1.24.x | 2024-02-27 | 4.5.x | 2.0.x | 4.10.x | 4.4.x |
| 1.23.x | 2023-12-18 | 3.26.x | 1.5.x | 4.10.x | 4.4.x |
| 1.22.x | 2023-10-27 | 3.25.x | 1.5.x | 4.10.x | 4.3.x |
| 1.21.x | 2023-08-17 | 3.22.x | 1.4.x | 4.9.x | 4.2.x |
| 1.20.x | 2023-07-06 | 3.22.x | 1.1.x | 4.7.x | 4.2.x |
| 1.19.x | 2023-05-04 | 3.17.x | 1.1.x1 | 4.7.x | 4.0.x |
| 1.18.x | 2023-03-07 | 3.13.x | 2.14.x | 4.6.x | 3.6.x |
| 1.17.x | 2022-12-20 | 3.9.x | 2.14.x | 4.5.x | 3.5.x |
| 1.16.x | 2022-10-31 | 3.8.x | 2.13.x | 4.4.x | 3.4.x |
| 1.15.x | 2022-09-07 | 3.6.x | 2.12.x | 4.3.x | 3.3.x |
| 1.14.x | 2022-07-07 | 3.6.x | 2.11.x | 4.2.x | 3.2.x |
| 1.13.x | 2022-05-03 | 3.4.x | 2.9.x | 4.0.x | 2.4.x |
| 1.12.x | 2022-04-05 | 3.4.x | 2.8.x | 3.0.x | 2.3.x |
| 1.11.x | 2022-03-14 | 3.2.x | 2.7.x | 2.6.x | 2.3.x |
| 1.10.x | 2022-01-27 | 3.1.x | 2.5.x | 2.4.x | 2.1.x |
| 1.9.x | 2021-12-10 | 3.1.x | 2.4.x | 2.4.x | 2.1.x |
| 1.8.x | 2021-11-30 | 3.1.x | 2.4.x | 2.3.x | 1.1.x |
| 1.7.x | 2021-09-01 | 3.1.x | 2.4.x | 2.3.x | 1.1.x |
| 1.6.x | 2021-08-11 | 2.4.x | 2.3.x | 2.2.x | 1.0.x |
| 1.5.x | 2021-07-13 | 2.2.x | 2.1.x | 2.1.x | 1.0.x |
| 1.4.x | 2021-06-09 | 2.2.x | 2.1.x | 2.1.x | 1.0.x |
| 1.3.x | 2021-04-23 | 2.2.x | 2.1.x | 2.1.x | 1.0.x |
| 1.2.x | 2021-03-15 | 2.2.x | 2.0.x | 1.1.x | - |
| 1.1.x | 2021-02-10 | 2.1.x | - | - | - |
| 1.0.x | 2021-01-14 | 2.0.x | - | - | - |
TypeScript 客户端变更
TypeScript 客户端 于 2023-03-17 替换了 JavaScript 客户端。
Vectorizer API 更改 v3.8.0
从 Weaviate JS/TS 客户端 v3.8.0 开始,在创建集合时对 vectorizer 配置 API 进行了多项更改
configure.vectorizer已被configure.vectors替换- 添加了
configure.multiVectors以使用户能够使用 多向量 configure.vectorizer.none已被configure.vectors.selfProvided替换
JavaScript/TypeScript 客户端 v2 弃用
Weaviate JavaScript/TypeScript 客户端 v2 已被弃用,不应再使用。如果您需要 v2 客户端的文档,请参阅 文档存档。如果您正在从 JavaScript/TypeScript v2 客户端迁移到 v3 客户端,请参阅此 迁移指南。
代码示例和更多资源
可以在 Weaviate 文档中找到有关各种操作和功能的用法信息。
配置压缩、备份、身份验证、授权、数据复制等。
管理集合(CRUD),配置向量化器和索引参数,设置多租户以及执行迁移。
添加新对象、获取现有对象、修改它们以及从集合中删除它们。
从基本的向量和混合搜索到专门的图像查询以及执行数据聚合。
Weaviate API 参考页面中的 搜索 和 REST 也可能是很好的起点。
问题和反馈
如果您有任何问题或反馈,请在 用户论坛 中告诉我们。
