You need to enable JavaScript to run this app.
导航
FetchCollectionData
最近更新时间:2024.06.14 11:34:51首次发布时间:2024.06.14 11:34:51

概述

根据主键在指定的 Collection 中查询单条或多条数据,单次最多可查询100条数据。
Collection 数据写入/删除后,可以实时查询数据。

请求参数

字段名

类型

是否必传

说明

CollectionName/CollectionAlias

string

数据集名称或数据集别名,二选一

PrimaryKeys

Array<string | number>

主键值,类型对齐定义的主键字段

响应参数
declare class FetchCollectionDataResponse<
  Data extends Record<string, any>,
  PrimaryKey extends GetPrimaryKeys<Data>
> {
  readonly Data: FetchDataInfo<Data, PrimaryKey>[];
  readonly OriginalRequest: string;
  readonly LogId: string;
  constructor(
    Data: FetchDataInfo<Data, PrimaryKey>[],
    OriginalRequest: string,
    LogId: string
  );
}
type GetPrimaryKeys<T extends Record<string, any>> = {
  [K in keyof T]: IsPrimaryKey<T, K> extends true ? K : never;
}[keyof T];

type FetchDataInfo<
  Data extends Record<string, any>,
  PrimaryKey extends GetPrimaryKeys<Data>
> = Data | { [Key in PrimaryKey]: ScalarFieldType2JsType[Data[PrimaryKey]] };
type IsNever<T> = [T] extends [never] ? true : false;

type IsUnion<T, B = T> = IsNever<T> extends true
  ? false
  : T extends B
  ? [B] extends [T]
    ? false
    : true
  : never;
  
enum FieldType {
  Int64 = "int64",
  Float32 = "float32",
  String = "string",
  Boolean = "bool",
  ListString = "list<string>",
  ListInt64 = "list<int64>",
  /** 稠密向量 */
  DenseVector = "vector",
  /** 稀疏向量 */
  SparseVector = "sparse_vector",
  Text = "text",
}  
  
interface ScalarFieldType2JsType {
  [FieldType.Int64]: number;
  [FieldType.Float32]: number;
  [FieldType.String]: string;
  [FieldType.Boolean]: boolean;
  [FieldType.ListString]: string[];
  [FieldType.ListInt64]: Array<number>;
}
  
type IsPrimaryKey<
  Data extends Record<string, any>,
  PrimaryKey extends keyof Data
> = IsUnion<PrimaryKey> extends true
  ? false
  : Data[PrimaryKey] extends ScalarFieldType2JsType[PrimaryKeyFieldType]
  ? true
  : false;

参考示例
import { vikingdb } from '@volcengine/openapi'

declare const service: vikingdb.VikingdbService // 替换为你初始化好的实例

interface Data {
  Id: number,
  Name: string
}

const response = await service.data.FetchCollectionData<Data, 'Id'>({
  CollectionName: 'test_collection_1',
  PrimaryKeys: [1, 2]
})

console.log(response.Data) // 查询的数据集数据 Data