Primitives.org.ai

forEach

Iterate over entities with a callback

Process each entity of a type with an async callback.

Import

import { DB } from 'ai-database'

const { db } = DB({
  Post: { title: 'string' },
  Persona: { name: 'string' },
})

Syntax

db.forEach(options, callback)
db.forEach({ type: 'Type' }, callback)
db.forEach({ type: 'Type', where: {} }, callback)

Examples

// Process all posts
await db.forEach({ type: 'Post' }, async (post) => {
  console.log(post.title)
})

// With filtering
await db.forEach(
  { type: 'Post', where: { published: true } },
  async (post) => {
    await notifySubscribers(post)
  }
)

Use Cases

Generate Content for Each Entity

// Generate posts for each persona
await db.forEach({ type: 'Persona' }, async (persona) => {
  await db.generate({
    type: 'Post',
    count: 10,
    data: { persona: persona.$id },
  })
})

Batch Processing

// Update all posts
await db.forEach({ type: 'Post' }, async (post) => {
  await db.Post.update(post.$id, {
    slug: slugify(post.title),
  })
})

Data Migration

// Migrate old format to new
await db.forEach({ type: 'User' }, async (user) => {
  await db.User.update(user.$id, {
    displayName: user.name,  // rename field
    name: undefined,         // remove old field
  })
})

Options

OptionTypeDescription
typestringEntity type to iterate
whereobjectFilter criteria
orderBystringSort field
order'asc' | 'desc'Sort direction
limitnumberMax entities to process
concurrencynumberParallel callbacks (default: 1)

Concurrency

// Process 5 at a time
await db.forEach(
  { type: 'Post', concurrency: 5 },
  async (post) => {
    await processPost(post)
  }
)

Progress Tracking

let processed = 0
const total = await db.Post.count()

await db.forEach({ type: 'Post' }, async (post) => {
  await processPost(post)
  processed++
  console.log(`${processed}/${total}`)
})

Error Handling

await db.forEach({ type: 'Post' }, async (post) => {
  try {
    await processPost(post)
  } catch (error) {
    console.error(`Failed: ${post.$id}`, error)
    // continues to next entity
  }
})
Was this page helpful?

On this page