pikku-mongodb

$npx mdskill add pikkujs/pikku/pikku-mongodb

Initialize MongoDB services for Pikku apps with integrated storage.

  • Enables persistent storage for channels, workflows, secrets, and deployments.
  • Depends on @pikku/mongodb package and MongoDB client libraries.
  • Triggers only when PikkuMongoDB or specific store services are requested.
  • Returns initialized database instances ready for application queries.

SKILL.md

.github/skills/pikku-mongodbView on GitHub ↗
---
name: pikku-mongodb
description: 'Use when setting up MongoDB database services in a Pikku app. Covers PikkuMongoDB connection, channel stores, workflow services, secret services, AI storage, agent runs, and deployment services.
TRIGGER when: code uses PikkuMongoDB, MongoDBChannelStore, MongoDBWorkflowService, MongoDBSecretService, or user asks about MongoDB setup with Pikku.
DO NOT TRIGGER when: user asks about SQL databases (use pikku-kysely) or Redis (use pikku-redis).'
---

# Pikku MongoDB

`@pikku/mongodb` provides MongoDB-backed implementations of Pikku's core service interfaces.

## Installation

```bash
yarn add @pikku/mongodb
```

## API Reference

### `PikkuMongoDB` (Connection Wrapper)

```typescript
import { PikkuMongoDB } from '@pikku/mongodb'

const mongo = new PikkuMongoDB(
  logger: Logger,
  clientOrUri: MongoClient | string,
  dbName: string,
  options?: MongoClientOptions
)

await mongo.init()
mongo.db  // Db instance for queries
await mongo.close()
```

### Available Services

| Service | Interface | Purpose |
|---------|-----------|---------|
| `MongoDBChannelStore` | `ChannelStore` | WebSocket channel state persistence |
| `MongoDBEventHubStore` | `EventHubStore` | Event hub state persistence |
| `MongoDBWorkflowService` | `PikkuWorkflowService` | Workflow definition storage |
| `MongoDBWorkflowRunService` | `WorkflowRunService` | Workflow execution tracking |
| `MongoDBDeploymentService` | `DeploymentService` | Deployment state management |
| `MongoDBAIStorageService` | `AIStorageService, AIRunStateService` | AI conversation/run storage |
| `MongoDBAgentRunService` | `AgentRunService` | Agent execution tracking |
| `MongoDBSecretService` | `SecretService` | Encrypted secret storage (envelope encryption) |

All services take a `Db` instance in their constructor and have an `init()` method that creates collections/indexes.

### Secret Service

```typescript
import { MongoDBSecretService } from '@pikku/mongodb'

const secrets = new MongoDBSecretService(mongo.db, {
  kekSecret: 'your-key-encryption-key',
  salt: 'your-salt',
})
await secrets.init()

await secrets.setSecretJSON('api-key', { key: 'sk-...' })
const value = await secrets.getSecretJSON<{ key: string }>('api-key')
await secrets.rotateKEK()
```

## Usage Patterns

### Full Setup

```typescript
import { PikkuMongoDB, MongoDBChannelStore, MongoDBWorkflowService } from '@pikku/mongodb'

const createSingletonServices = pikkuServices(async (config) => {
  const logger = new PinoLogger()
  const mongo = new PikkuMongoDB(logger, config.mongoUri, 'myapp')
  await mongo.init()

  const channelStore = new MongoDBChannelStore(mongo.db)
  await channelStore.init()

  const workflowService = new MongoDBWorkflowService(mongo.db)
  await workflowService.init()

  return { config, logger, database: mongo, channelStore, workflowService }
})
```

More from pikkujs/pikku

SkillDescription
pikku-addon'Use when creating or consuming reusable function packages (addons) in Pikku. Covers wireAddon, addon(), pikkuAddonServices, pikkuAddonWireServices, addon package structure, and cross-project function sharing.
pikku-ai-agent'Use when building AI agents, chatbots, or LLM-powered assistants with Pikku. Covers pikkuAIAgent, tool registration, memory, streaming, and agent invocation.
pikku-ai-vercel'Use when setting up AI agent execution with the Vercel AI SDK in a Pikku app. Covers VercelAIAgentRunner for streaming and non-streaming AI agent steps.
pikku-ai-voice'Use when adding voice input (speech-to-text) or voice output (text-to-speech) to AI agents in a Pikku app. Covers voiceInput/voiceOutput middleware hooks and STT/TTS service interfaces.
pikku-auth-js'Use when integrating Auth.js (NextAuth) with a Pikku app. Covers createAuthHandler, createAuthRoutes, and Auth.js configuration.
pikku-aws'Use when setting up AWS services (S3, SQS, Secrets Manager) in a Pikku app. Covers S3Content for file storage, SQSQueueService for queues, and AWSSecrets for secret management.
pikku-backblaze'Use when setting up Backblaze B2 file storage in a Pikku app. Covers B2Content for file uploads, downloads, and signed URLs.
pikku-cli'Use when building CLI commands with Pikku. Covers wireCLI, pikkuCLICommand, subcommands, options, parameters, custom renderers, and nested command groups.
pikku-concepts'Foundational guide to Pikku framework concepts. Use this skill when working with any Pikku codebase, starting a new Pikku project, or migrating a backend to Pikku. Covers the core mental model, function types, project structure, code generation, testing, and how Pikku maps to traditional backend patterns.
pikku-config'Use when managing secrets, environment variables, config, or OAuth2 credentials in a Pikku app. Covers wireSecret, wireVariable, wireOAuth2Credential, and typed config access.