agentleFS
Google CloudKnown publisherGoogleCloudPlatform/devrel-demos/other/modernizing-expressjs/.agents/skills/scaffolding-data-layer/SKILL.md

scaffolding-data-layer

Skill378 starsChanged 4 months agoApache-2.0View on GitHub

Reads Data_Models.md artifacts and generates the equivalent modern ORM schema or native MongoDB/Zod layer. Use when following a legacy audit artifact to scaffold out the data access layer for a Next.js app.

  • Reads credentials

The file

We split this skill into the rules it gives an agent

7 rules. About 0 are common, 2 are distinctive, and 5 are very specific to this project.

  • ≈0

    Common · estimate

    Many files like this have a rule of this kind.

  • ≈2

    Distinctive · estimate

    This author's own choices. Worth a look.

  • 5

    Very specific

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

  • Common
  • Distinctive
  • Very specific
  1. Distinctive:

    Before writing code, you MUST ensure the modern schemas are compatible with the existing legacy data.

    line 32
  2. Distinctive:

    Strategy: Run the new Zod validation schemas against *actual* records in the legacy database.

    line 33
  3. Very specific:

    Create src/lib/db/mongodb.ts (or the appropriate client).

    line 37
  4. Very specific:

    Configuration: Ensure the connection string (usually via MONGODB_URI in .env.local) points to the legacy-app's database.

    line 38
  5. Very specific:

    Create src/lib/models/schema.ts (Zod) and src/lib/models/types.ts (TypeScript).

    line 53
  6. Very specific:

    Type Inference: Use z.infer<typeof Schema> to generate your TypeScript types automatically.

    line 55
  7. Very specific:

    Create src/lib/models/utils.ts for logic that was previously buried in the ORM (e.g., password hashing, complex queries, virtual field …

    line 59

≈ “Common” is an estimate: at least 1 in 5 of the 213,897 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

Scaffolding the Modern Data Layer

Translate legacy data models (Mongoose, Sequelize, etc.) into a modern, type-safe data access layer using TypeScript and Zod.

Objective

Recreate the legacy data structures in a way that provides runtime validation and full TypeScript support, while accounting for Schema Evolution and potential "Dirty Data" in the legacy system.

Instructions for the Scaffold Subagent

Copy this checklist and track your progress:

Task Progress:
- [ ] Step 1: Analyze Audit Specs
- [ ] Step 2: Perform Data Integrity Audit (Draft)
- [ ] Step 3: Initialize Database Connection
- [ ] Step 4: Define Zod Schemas & TypeScript Types
- [ ] Step 5: Implement Data Access Utilities

Step 1. Analyze Audit Specs

Locate and read docs/legacy-audit/Data_Models.md. Identify:

  • Primary Entities and their relationships (One-to-Many, Many-to-Many).
  • Validation Rules: Required fields, regex patterns, min/max values.
  • Middleware Hooks: Legacy pre-save or post-remove logic that needs to be manually ported to the service layer.

Step 2. Perform Data Integrity Audit

Before writing code, you MUST ensure the modern schemas are compatible with the existing legacy data.

  • Strategy: Run the new Zod validation schemas against actual records in the legacy database.
  • Outcome: Identify "Dirty Data" gaps (e.g., missing required fields in old records, inconsistent types) and use z.optional() or .nullable() to maintain compatibility with legacy data patterns.

Step 3. Connect to Legacy Database

Create src/lib/db/mongodb.ts (or the appropriate client).

  • Configuration: Ensure the connection string (usually via MONGODB_URI in .env.local) points to the legacy-app's database.
  • Pattern: Export a cached client promise (Singleton pattern) to avoid opening too many connections during HMR (Hot Module Replacement) in Next.js development.
// Example: src/lib/db/mongodb.ts
import { MongoClient } from "mongodb";

if (!process.env.MONGODB_URI) {
  throw new Error('Invalid/Missing environment variable: "MONGODB_URI"');
}

const uri = process.env.MONGODB_URI;
// ... (Rest of singleton implementation)

Step 4. Define Zod Schemas & TypeScript Types

Create src/lib/models/schema.ts (Zod) and src/lib/models/types.ts (TypeScript).

  • Zod First: Define the schema using Zod for runtime validation.
  • Type Inference: Use z.infer<typeof Schema> to generate your TypeScript types automatically.
  • Relationship Mapping: Convert legacy ORM relationships (e.g., Mongoose Refs) into manual link logic using ObjectId from the native driver.

Step 5. Implement Data Access Utilities

Create src/lib/models/utils.ts for logic that was previously buried in the ORM (e.g., password hashing, complex queries, virtual field calculation).

  • Static Utilities: Native drivers lack "Model Methods." All logic must be extracted into pure functions that take the data object as an argument.

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.