BunSane AI Agent Guide
This guide provides comprehensive instructions for AI agents to effectively work with the BunSane framework. Copy these documents to your project's AI context for optimal AI-assisted development.
Framework Overview
BunSane is a TypeScript backend framework for Bun that uses an Entity-Component-System (ECS) architecture pattern adapted for web API development. Data is stored in PostgreSQL.
Core Concepts
| Concept | Description |
|---|---|
| Entity | A unique identifier (UUID) that serves as a container for components |
| Component | A data structure attached to entities. Each component type has its own database table |
| Archetype | A structured view that groups related components and auto-generates GraphQL types |
| Service | Contains business logic and exposes GraphQL/REST endpoints |
| Query | Finds entities by their attached components with filtering support |
Architecture Pattern
┌─────────────────────────────────────────────────────────────────┐
│ GraphQL / REST │
├─────────────────────────────────────────────────────────────────┤
│ Services │
│ ┌──────────────┐ ┌──────────────┐ ┌──────────────┐ │
│ │ UserService │ │ OrderService │ │ AuthService │ │
│ └──────────────┘ └──────────────┘ └──────────────┘ │
├─────────────────────────────────────────────────────────────────┤
│ Archetypes │
│ ┌──────────────┐ ┌──────────────┐ ┌──────────────┐ │
│ │ UserArcheType│ │OrderArcheType│ │ ... │ │
│ └──────────────┘ └──────────────┘ └──────────────┘ │
├─────────────────────────────────────────────────────────────────┤
│ Entity + Components │
│ ┌────────┐ ┌──────────────┐ ┌─────────────┐ ┌────────────┐ │
│ │ Entity │─│NameComponent │─│EmailComponent│─│PasswordComp│ │
│ └────────┘ └──────────────┘ └─────────────┘ └────────────┘ │
├─────────────────────────────────────────────────────────────────┤
│ PostgreSQL Database │
└─────────────────────────────────────────────────────────────────┘
Module Import Reference
All BunSane imports follow the pattern bunsane/<module>. Below is a comprehensive reference organized by module.
Core - Entity & Components
import { Entity } from "bunsane/core/Entity";
import {
BaseComponent,
CompData,
Component,
ComponentRegistry,
type ComponentGetter,
type ComponentDataType
} from "bunsane/core/components";
Core - ArcheType
import {
ArcheType,
ArcheTypeField,
ArcheTypeFunction,
BaseArcheType,
type ArcheTypeOwnProperties,
HasOne,
HasMany,
BelongsTo,
asEnumType
} from "bunsane/core/ArcheType";
Core - Application
import App from "bunsane/core/App";
Core - Utilities
import { logger } from "bunsane/core/Logger";
import { responseError, handleGraphQLError } from "bunsane/core/ErrorHandler";
import { BatchLoader } from "bunsane/core/BatchLoader";
Core - Metadata (Enums)
import { Enum } from "bunsane/core/metadata";
Query
import { Query, or } from "bunsane/query";
// Advanced query features
import {
QueryContext,
QueryNode,
type QueryResult,
OrQuery,
OrNode,
type FilterBuilder,
FilterBuilderRegistry,
buildJSONPath
} from "bunsane/query";
Service
import {
BaseService,
ServiceRegistry,
Get,
Post,
Put,
Delete,
Patch,
Options,
Head
} from "bunsane/service";
GraphQL
import {
GraphQLOperation,
GraphQLObjectType,
GraphQLField,
GraphQLScalarType,
isFieldRequested
} from "bunsane/gql";
import { GraphQLSubscription } from "bunsane/gql/Generator";
Database
import db from "bunsane/database";
Types
import type { GraphQLContext, GraphQLInfo } from "bunsane/types/graphql.types";
import type { ScheduleInterval, ScheduledTaskOptions } from "bunsane/types/scheduler.types";
Hooks
import {
ComponentTargetHook,
EntityHook,
ComponentHook,
LifecycleHook,
registerDecoratedHooks
} from "bunsane/core/decorators/EntityHooks";
import type {
EntityCreatedEvent,
EntityUpdatedEvent,
EntityDeletedEvent,
ComponentAddedEvent,
ComponentUpdatedEvent,
ComponentRemovedEvent
} from "bunsane/core/events/EntityLifecycleEvents";
Scheduler
import {
ScheduledTask,
registerScheduledTasks,
ScheduleInterval
} from "bunsane/scheduler";
Swagger/OpenAPI
import { ApiDocs, ApiTags } from "bunsane/swagger";
Plugins
import BasePlugin from "bunsane/plugins";
Upload System
import {
UploadManager,
FileValidator,
StorageProvider,
LocalStorageProvider,
UploadComponent,
ImageMetadataComponent,
UploadHelper,
Upload,
UploadField,
BatchUpload,
RequiredUpload,
QuickSetup,
initializeUploadSystem
} from "bunsane/upload";
External Dependencies
// Validation (Zod)
import { z } from "zod";
// GraphQL Error Handling
import { GraphQLError } from "graphql";
Project Structure Convention
project/
├── src/
│ ├── components/ # Component definitions
│ │ ├── UserComponent.ts
│ │ └── OrderComponent.ts
│ ├── archetypes/ # Archetype definitions
│ │ ├── UserArcheType.ts
│ │ └── OrderArcheType.ts
│ ├── services/ # Service classes
│ │ ├── UserService.ts
│ │ └── OrderService.ts
│ ├── utilities/ # Helper functions
│ └── App.ts # Application entry
└── index.ts
Quick Decision Guide
When to Create a New Component
Create a new component when:
- The data represents a distinct concept (e.g.,
EmailComponent,AddressComponent) - The data needs to be queried independently
- The data has different lifecycle from other fields
When to Add Fields to Existing Component
Add fields to existing component when:
- The fields are always accessed together
- The fields have the same lifecycle (created/updated together)
- The fields represent a single logical unit
When to Use Tags vs Data Components
- Tags: Empty components used for categorization/filtering (e.g.,
UserTag,AdminTag,DeletedTag) - Data Components: Components with
@CompData()fields that store actual data
Document Index
- Component Best Practices - How to design optimal components
- Query Optimization - Efficient data retrieval patterns
- Service Patterns - Service implementation guidelines
- Common Patterns - Reusable code patterns
- Quick Reference - Copy-paste code snippets and Minimal App Setup guide
Critical Rules for AI Agents
- Components must be atomic - No nested objects within
@CompData()fields - Always call
entity.save()after modifications - Use transactions for operations involving multiple entity modifications
- Register field resolvers for archetypes with computed fields
- Validate input with Zod schemas before processing
- Handle errors by returning
GraphQLErrorwith appropriate codes