← 返回 Skills 市场
qwe123sddfsdfs

Batch Processing (DataLoader)

作者 qwe123sddfsdfs · GitHub ↗ · v1.0.0
cross-platform ✓ 安全检测通过
339
总下载
0
收藏
2
当前安装
1
版本数
在 OpenClaw 中安装
/install batch-processing
功能描述
DataLoader pattern for batch processing to solve N+1 query problems. Reduces database/API calls from N+1 to 2 by batching and caching.
使用说明 (SKILL.md)

Batch Processing with DataLoader

Solve N+1 query problems using the DataLoader pattern. This skill provides utilities for batching database queries, API calls, or any expensive operations.

Problem: N+1 Query

// BAD: N+1 queries (1 for list + N for each item)
const users = await db.query('SELECT * FROM users');
for (const user of users) {
  const posts = await db.query('SELECT * FROM posts WHERE user_id = ?', [user.id]);
  user.posts = posts;
}
// Total queries: 1 + N (where N = number of users)

Solution: DataLoader Pattern

// GOOD: 2 queries total using batching
const DataLoader = require('./batch-processing/dataloader.js');

const userLoader = new DataLoader(async (userIds) => {
  // Batch load all users in one query
  const users = await db.query('SELECT * FROM users WHERE id IN (?)', [userIds]);
  return userIds.map(id => users.find(u => u.id === id));
});

const postLoader = new DataLoader(async (userIdss) => {
  // Batch load posts for multiple users
  const allPosts = await db.query('SELECT * FROM posts WHERE user_id IN (?)', [userIdss.flat()]);
  return userIdss.map(ids => allPosts.filter(p => ids.includes(p.user_id)));
});

// Usage - automatically batches concurrent requests
const users = await Promise.all(userIds.map(id => userLoader.load(id)));
const posts = await Promise.all(userIds.map(id => postLoader.load(id)));
// Total queries: 2 (regardless of N)

Core API

DataLoader Constructor

const loader = new DataLoader(batchLoadFn, options);

batchLoadFn: async (keys) => values

  • Receives array of keys
  • Must return array of values in same order
  • Can return null/undefined for missing keys

options (optional):

  • maxBatchSize: Maximum keys per batch (default: 100)
  • batchScheduleMs: Delay to collect batch (default: 0, immediate)
  • cache: Enable caching (default: true)
  • cacheKeyFn: Custom cache key function

Methods

  • load(key): Load a single key (returns Promise)
  • loadMany(keys): Load multiple keys (returns Promise\x3CArray>)
  • prime(key, value): Manually prime cache
  • clear(key): Clear cache for key
  • clearAll(): Clear entire cache

Usage Examples

Database Batching

const userLoader = new DataLoader(async (ids) => {
  const rows = await db.query(
    'SELECT * FROM users WHERE id IN (?)',
    [ids]
  );
  return ids.map(id => rows.find(r => r.id === id) || null);
});

// Concurrent loads are automatically batched
const [user1, user2, user3] = await Promise.all([
  userLoader.load(1),
  userLoader.load(2),
  userLoader.load(3)
]);

API Batching

const apiLoader = new DataLoader(async (urls) => {
  const responses = await Promise.all(
    urls.map(url => fetch(url).then(r => r.json()))
  );
  return responses;
});

// Batch multiple API calls
const [data1, data2] = await Promise.all([
  apiLoader.load('https://api.example.com/users/1'),
  apiLoader.load('https://api.example.com/users/2')
]);

Nested Batching (GraphQL-style)

const postLoader = new DataLoader(async (userIds) => {
  const posts = await db.query(
    'SELECT * FROM posts WHERE user_id IN (?)',
    [userIds]
  );
  return userIds.map(id => posts.filter(p => p.user_id === id));
});

const commentLoader = new DataLoader(async (postIds) => {
  const comments = await db.query(
    'SELECT * FROM comments WHERE post_id IN (?)',
    [postIds.flat()]
  );
  return postIds.map(ids => comments.filter(c => ids.includes(c.post_id)));
});

// Resolve nested data efficiently
const users = await userLoader.loadMany(userIds);
for (const user of users) {
  user.posts = await postLoader.load(user.id);
  for (const post of user.posts) {
    post.comments = await commentLoader.load(post.id);
  }
}

Performance Comparison

Scenario Without DataLoader With DataLoader
Load 100 users 101 queries 1 query
Load 100 users + posts 201 queries 2 queries
Load 100 users + posts + comments 301 queries 3 queries

Best Practices

  1. Create loaders per request: Don't share loaders across requests to avoid cache pollution
  2. Batch size matters: Tune maxBatchSize based on your database/API limits
  3. Handle errors gracefully: Return null/undefined for missing keys, don't throw
  4. Use with Promise.all: Concurrent loads trigger batching
  5. Clear cache when needed: Use clear() for mutations that affect cached data

Files

  • dataloader.js - Core DataLoader implementation
  • examples/ - Usage examples for different scenarios
  • benchmarks/ - Performance comparison scripts

Installation

This skill is self-contained. Import the DataLoader:

const DataLoader = require('./batch-processing/dataloader.js');

When to Use

✅ Multiple database queries in a loop ✅ Fetching related data for multiple items ✅ API calls that can be batched ✅ GraphQL resolvers ✅ Any N+1 query pattern

❌ Single queries ❌ Real-time streaming data ❌ When order matters and can't be preserved

安全使用建议
This package appears coherent and safe for evaluation. Before using in production: 1) Review and test the batchLoadFn implementations you supply (ensure they use parameterized queries to avoid SQL injection and actually batch work rather than making per-id calls). 2) Note the DataLoader caches Promises immediately — if a batch promise rejects, the rejected Promise remains cached (consider clearing cache on error). 3) Follow the SKILL.md best practice to create loaders per request to avoid cache pollution. 4) Run the provided examples/benchmarks in a sandboxed environment first. 5) If you need provenance, confirm the repository/source (package.json repo URL is present but source was listed as unknown).
功能分析
Type: OpenClaw Skill Name: batch-processing Version: 1.0.0 The skill bundle provides a DataLoader utility for batch processing, aiming to solve N+1 query problems. All code files (`dataloader.js`, `benchmark.js`, `examples.js`) are well-documented and implement the stated functionality using mock database and API interactions. There are no actual network calls, database connections, or file system operations beyond module imports. Neither `SKILL.md` nor `README.md` contain any prompt injection attempts or instructions for the AI agent to perform unauthorized actions. The `package.json` lists no external dependencies. The entire package is self-contained and aligns perfectly with its described purpose without any high-risk behaviors.
能力评估
Purpose & Capability
Name, description, SKILL.md, package.json and the three JS files all consistently implement a DataLoader pattern for batching and caching; nothing in the bundle asks for unrelated services, credentials, or system access.
Instruction Scope
SKILL.md and examples focus on creating loaders that call db.query or fetch; these are example usages rather than instructions to read system files or secrets. Note: example code references network URLs and a fetch-like API only in examples (mocked in examples.js) — the core implementation itself does not perform external network I/O.
Install Mechanism
No install spec is present (instruction-only install), and the package is pure JS with zero external dependencies. Nothing is downloaded from arbitrary URLs or written to system paths as part of installation.
Credentials
The skill declares no required environment variables, no credentials, and the code does not access process.env or system config paths. Requested privileges are minimal and appropriate for a library.
Persistence & Privilege
Flags such as always and disable-model-invocation are default/normal. The skill does not request persistent agent privileges or attempt to modify other skills or global configuration.
如何使用
  1. 确保已安装 OpenClaw(本地或 Docker 部署)
  2. 在对话框中输入安装命令:/install batch-processing
  3. 安装完成后,直接呼叫该 Skill 的名称或使用 /batch-processing 触发
  4. 根据 Skill 的参数说明提供必要输入,即可获得结构化输出
版本历史
v1.0.0
- Initial release of the batch-processing skill featuring a DataLoader utility to solve N+1 query problems. - Batches and caches database/API calls, reducing calls from N+1 to 2 in typical scenarios. - Provides a DataLoader constructor with configurable batching, cache, and schedule options. - Includes clear API documentation and best practices for integrating with databases, APIs, and GraphQL. - Offers usage examples, performance comparisons, and details on file structure for quick adoption.
元数据
Slug batch-processing
版本 1.0.0
许可证
累计安装 2
当前安装数 2
历史版本数 1
常见问题

Batch Processing (DataLoader) 是什么?

DataLoader pattern for batch processing to solve N+1 query problems. Reduces database/API calls from N+1 to 2 by batching and caching. 它是一个面向 Claude Code / OpenClaw 的 AI Agent Skill 插件,目前累计下载 339 次。

如何安装 Batch Processing (DataLoader)?

在 OpenClaw 或 Claude Code 对话框中运行命令「/install batch-processing」即可一键安装,无需额外配置。

Batch Processing (DataLoader) 是免费的吗?

是的,Batch Processing (DataLoader) 完全免费(开源免费),可自由下载、安装和使用。

Batch Processing (DataLoader) 支持哪些平台?

Batch Processing (DataLoader) 跨平台运行,可在任意部署了 OpenClaw / Claude Code 的环境中使用(cross-platform)。

谁开发了 Batch Processing (DataLoader)?

由 qwe123sddfsdfs(@qwe123sddfsdfs)开发并维护,当前版本 v1.0.0。

💬 留言讨论