Skip to main content

Goal

Use the State module to share data between Functions without a separate database.

Steps

1. Enable the State module

iii-config.yaml
modules:
  - class: modules::state::StateModule
    config:
      adapter:
        class: modules::state::adapters::KvStore
        config:
          store_method: file_based  # Options: in_memory, file_based
          file_path: ./data/state_store.db  # required for file_based

2. Write state

state-writer.ts
import { registerWorker, Logger } from 'iii-sdk'

const iii = registerWorker(process.env.III_URL ?? 'ws://localhost:49134')

iii.registerFunction({ id: 'users::create' }, async (input) => {
  const logger = new Logger()
  const userId = crypto.randomUUID()
  const user = { id: userId, name: input.name, email: input.email }

  await iii.trigger({
    function_id: 'state::set',
    payload: { scope: 'users', key: userId, value: user },
  })

  logger.info('User saved to state', { userId })
  return { userId }
})

// Then call from another function or worker
const { userId } = await iii.trigger({
  function_id: 'users::create',
  payload: { name: 'Alice', email: 'alice@example.com' },
})
logger.info('Created user', { userId })

3. Read state

state-reader.ts
iii.registerFunction({ id: 'users::get' }, async (input) => {
  const logger = new Logger()

  const user = await iii.trigger({
    function_id: 'state::get',
    payload: { scope: 'users', key: input.userId },
  })

  logger.info('User retrieved', { userId: input.userId })
  return user
})

// Then call from another function or worker
const user = await iii.trigger({
  function_id: 'users::get',
  payload: { userId: 'some-user-id' },
})
logger.info('Retrieved user', user)

4. Atomic updates

Use state::update with update operations for safe concurrent modifications:
state-update.ts
iii.registerFunction({ id: 'users::record-login' }, async (input) => {
  const logger = new Logger()

  await iii.trigger({
    function_id: 'state::update',
    payload: {
      scope: 'users',
      key: input.userId,
      ops: [
        { type: 'set', path: 'lastLogin', value: new Date().toISOString() },
        { type: 'increment', path: 'loginCount', by: 1 },
      ],
    },
  })

  logger.info('User login recorded', { userId: input.userId })
  return { updated: true }
})

// Then call from another function or worker
await iii.trigger({
  function_id: 'users::record-login',
  payload: { userId: 'some-user-id' },
})
logger.info('Login recorded')

Result

State is shared across all Functions in the system. Any Function can read or write to any scope/key pair. The Engine handles consistency and persistence based on the configured adapter.