Skip to content

findAll

findAll(params): Promise<any[]>

基于 p-map 实现的批量数据获取

包含以下核心功能:

根据 initialPageParam 获取第一页数据,并计算出剩余分页信息,使用 p-map 并发请求获取剩余分页数据,与第一页数据合并后返回 控制并发请求数量 支持与 createIO 创建的 table 对象配合使用 使用限制

  • 每次最多请求 1000 条数据,即 0 < initialPageParam.limit <= 1000,默认为 1000
  • queryFn 的返回结构必须与知晓云的数据列表结构一致,即 {objects: array, meta: object} 点我查看相关资料
  • 使用 sdk 请求数据表时,最好在 select 中配置必须字段,防止数据量过大撑爆云函数内存

参数

NameTypeDescription
paramsObject
params.initialPageParamundefined | object初始请求参数
params.queryFnundefined | (params: { limit: number ; offset: number }) => Promise<{ meta: { total_count: number } ; objects: any[] }>当 table 不满足使用场景时,可自定义 queryFn
params.tableundefined | { count: (options: Pick<TableOperationQueryOptions, "query">) => BaseTableOperationResponse<number> ; create: <TPlain>(data: object, options: Pick<TableOperationQueryOptions, "plain"> & { plain?: TPlain }) => BaseTableOperationResponse<TPlain extends true ? object : Response<object>> ; createMany: (dataList: object[], options: Pick<TableOperationQueryOptions, "enableTrigger">) => BaseTableOperationResponse<CreateManyRecordResponse> ; delete: (options: Pick<TableOperationQueryOptions, "offset" | "limit" | "withCount" | "id" | "enableTrigger">) => BaseTableOperationResponse<any> ; deleteMany: (options: Pick<TableOperationQueryOptions, "query" | "offset" | "limit" | "withCount" | "enableTrigger">) => BaseTableOperationResponse<DeleteManyRecordResponse> ; find: <TPlain>(options: Pick<TableOperationQueryOptions, "expand" | "query" | "offset" | "limit" | "orderBy" | "select" | "withCount" | "plain"> & { plain: TPlain }) => BaseTableOperationResponse<TPlain extends true ? object[] : Response<FindRecordResponse>> ; findAll: (options: Pick<TableOperationQueryOptions, "expand" | "query" | "orderBy" | "select"> & { initialPageParam: Pick<TableOperationQueryOptions, "offset" | "limit"> ; totalLimit: number }) => BaseTableOperationResponse<object[]> ; first: <TPlain>(options: Pick<TableOperationQueryOptions, "expand" | "query" | "offset" | "orderBy" | "select" | "withCount" | "plain"> & { plain: TPlain }) => BaseTableOperationResponse<TPlain extends true ? object : Response<object>> ; get: <TPlain>(options: Pick<TableOperationQueryOptions, "expand" | "select" | "plain" | "id"> & { plain: TPlain }) => BaseTableOperationResponse<TPlain extends true ? object : Response<object>> ; update: <TPlain>(options: Pick<TableOperationQueryOptions, "data" | "query" | "offset" | "limit" | "withCount" | "plain" | "id" | "enableTrigger" | "unset" | "incrementBy" | "append" | "uAppend" | "remove" | "patchObject"> & { plain?: TPlain }) => BaseTableOperationResponse<TPlain extends true ? object : Response<object>> ; updateMany: <TPlain>(options: Pick<TableOperationQueryOptions, "data" | "query" | "offset" | "limit" | "withCount" | "plain" | "enableTrigger" | "unset" | "incrementBy" | "append" | "uAppend" | "remove" | "patchObject"> & { plain?: TPlain }) => BaseTableOperationResponse<TPlain extends true ? UpdateManyRecordResponse : Response<UpdateManyRecordResponse>> }createIO 创建的 table 对象,会调用 io.table.find 方法
params.totalLimitundefined | number表示本次操作允许获取的最大数据总量,默认没有上限

返回值

Promise<any[]>

示例

ts
function getSubscribeLogs(options) {
  const query = io.query
    .arrayContains('tags', options.tags)
    .compare('status', '=', SUBSCRIBE_LOG_STATUS.IDLE)

  if (options.tradeUnionId)
    query.compare('trade_union_id', '=', options.tradeUnionId)

  if (options.tradeUnionHierarchy) {
    query.in('trade_union_hierarchy', options.tradeUnionHierarchy)
  }

  return findAll({
    table: io.subscribeLog,
    initialPageParam: {
      query,
      // 最好是筛选出必须字段,防止数据量过大撑爆云函数内存
      select: 'id,created_by',
    },
  }).then(res => ({
    userIds: res.map(item => item.created_by),
    subscribeLogIds: res.map(item => item.id),
  }))
}

示例

ts
// 自定义 queryFn
function getAllData(options) {
  const queryFn = ({offset, limit}) => {
    // do something to get data list
    return {
      objects: [], // your data list
      meta: {
        total_count: xxxx,
      },
    }
  }
  return findAll({queryFn})
}

示例

ts
// 限制本次获取的最大数据数量
// 一般用于清洗数据之类的操作,可以控制每次清洗操作的数据条目
findAll({table, io.xxxTable, initialPageParam: {offset: 0}, totalLimit: 5000})

// 下次执行时跳过前 5000 条记录
findAll({table, io.xxxTable, initialPageParam: {offset: 5000}, totalLimit: 5000})

源码

packages/faas/find-all.js