agentleFS
Workshop-VenturesWorkshop-Ventures/skills/backend-scaffolding/SKILL.md

backend-scaffolding

Skill1 starsChanged 10 months agoMITView on GitHub

Scaffold a Koa-based backend server with standard structure including config, logging, routes, models, and database setup. Use when asked to "create a backend", "scaffold backend", "set up an API server", or "initialize backend structure".

  • Reads credentials
  • Installs packages

The file

We split this skill into the rules it gives an agent

4 rules. About 0 are common, 0 are distinctive, and 4 are very specific to this project.

  • ≈0

    Common · estimate

    Many files like this have a rule of this kind.

  • ≈0

    Distinctive · estimate

    This author's own choices. Worth a look.

  • 4

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Very specific:

    Config: Always use config object, never process.env directly

    line 147
  2. Very specific:

    Logging: Use createLogger('module-name'), never console.log

    line 148
  3. Very specific:

    Routes: Use koa-zod-router with Zod schemas for validation

    line 149
  4. Very specific:

    Models: Use generateId() for IDs, stripId transform for JSON output

    line 150

≈ “Common” is an estimate: at least 1 in 5 of the 350,495 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md

Backend Scaffolding

Scaffold a Koa-based backend server with configuration, logging, routing with Zod validation, Mongoose models, and database connectivity.

Structure Overview

apps/backend/src/
├── config/
│   └── index.ts          # Centralized configuration (env vars)
├── lib/
│   └── logger/
│       └── index.ts      # Pino logger with pretty printing
├── models/
│   ├── _utils.ts         # Model utilities (generateId, stripId)
│   ├── db.ts             # MongoDB connection
│   └── {Model}.ts        # Mongoose models
├── routes/
│   └── {resource}.ts     # koa-zod-router routes
└── main.ts               # Application entry point

Installation

npm install koa @koa/cors koa-bodyparser koa-mount koa-static koa-zod-router mongoose pino zod uuid
npm install -D @types/koa @types/koa__cors @types/koa-bodyparser @types/koa-mount @types/koa-static pino-pretty

For WebSocket support: npm install koa-websocket @types/koa-websocket

Implementation Steps

1. Config Module (config/index.ts)

Centralize all environment variables. Never use process.env directly outside this file.

const isDev = process.env.NODE_ENV !== 'production';

const config = {
  isDev,
  port: process.env.PORT || 3333,
  mongo: {
    database: process.env.MONGO_DATABASE || 'myapp',
    protocol: process.env.MONGO_PROTOCOL || 'mongodb',
    host: process.env.MONGO_HOST || 'localhost:27017',
    username: process.env.MONGO_USERNAME || '',
    password: process.env.MONGO_PASSWORD || '',
  },
  logging: {
    level: process.env.LOG_LEVEL || (isDev ? 'debug' : 'info'),
    routeLogging: process.env.ROUTE_LOGGING !== 'false',
  },
};

export default config;

2. Logger Module (lib/logger/index.ts)

import pino from 'pino';
import config from '../../config';

export const logger = pino({
  level: config.logging.level,
  transport: config.isDev
    ? { target: 'pino-pretty', options: { colorize: true, translateTime: 'SYS:standard', ignore: 'pid,hostname' } }
    : undefined,
});

export const createLogger = (name: string) => logger.child({ name });
export default logger;

3. Model Utilities (models/_utils.ts)

import { v4 as uuidv4 } from 'uuid';

export const generateId = (): string => uuidv4();

export const stripId = (_doc: unknown, ret: Record<string, unknown>) => {
  delete ret._id;
  return ret;
};

4. Database Connection (models/db.ts)

import mongoose from 'mongoose';
import config from '../config';
import { createLogger } from '../lib/logger';

const log = createLogger('db');

export async function init() {
  const user = config.mongo.username && config.mongo.password
    ? `${encodeURIComponent(config.mongo.username)}:${encodeURIComponent(config.mongo.password)}@`
    : '';
  const connectionString = `${config.mongo.protocol}://${user}${config.mongo.host}/${config.mongo.database}`;
  await mongoose.connect(connectionString);
  log.info({ database: config.mongo.database }, 'Connected to MongoDB');
}

5. Models, Routes, and Main Entry Point

See references/examples.md for complete code examples including:

  • Example Mongoose model with proper typing
  • Example routes with Zod validation (CRUD operations)
  • Main entry point with middleware setup
  • Shared types definition

Environment Variables

PORT=3333
NODE_ENV=development
MONGO_DATABASE=myapp
MONGO_PROTOCOL=mongodb
MONGO_HOST=localhost:27017
MONGO_USERNAME=
MONGO_PASSWORD=
LOG_LEVEL=debug
ROUTE_LOGGING=true

Checklist

  1. Install dependencies: npm install
  2. Start MongoDB: docker compose up -d or local MongoDB
  3. Set environment: Copy .env.example to .env.local, run direnv allow
  4. Start backend: just serve-backend
  5. Test health: curl http://localhost:3333/api
  6. Test CRUD: See examples in references/examples.md

Best Practices

  1. Config: Always use config object, never process.env directly
  2. Logging: Use createLogger('module-name'), never console.log
  3. Routes: Use koa-zod-router with Zod schemas for validation
  4. Models: Use generateId() for IDs, stripId transform for JSON output
  5. Types: Define shared types in libs/types, import via @{project}/types
  6. Errors: Return structured error responses with appropriate status codes

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.