xiaohongshu
xiaohongshu
create-mockdb-repository
Create MockDB (in-memory) repository implementation for development and testing. Use when implementing repository interface with mock database, creating in-memory data store, or setting up test fixtures. Triggers on "mockdb repository", "mock repository", "in-memory repository", "test repository ...
Full skill instructions
Creates an in-memory repository implementation using an array as the data store. Ideal for development, testing, and prototyping.
Location: src/repositories/mockdb/{entity-name}.mockdb.repository.ts
Naming: {entity-name}.mockdb.repository.ts (e.g., note.mockdb.repository.ts)
Create src/repositories/mockdb/{entity-name}.mockdb.repository.ts
import { v4 as uuidv4 } from "uuid";
import type {
{Entity}Type,
Create{Entity}Type,
Update{Entity}Type,
{Entity}QueryParamsType,
{Entity}IdType,
} from "@/schemas/{entity-name}.schema";
import {
DEFAULT_LIMIT,
DEFAULT_PAGE,
type PaginatedResultType,
} from "@/schemas/shared.schema";
import type { I{Entity}Repository } from "@/repositories/{entity-name}.repository";
import type { UserIdType } from "@/schemas/user.schemas";
export class MockDb{Entity}Repository implements I{Entity}Repository {
private {entities}: {Entity}Type[] = [];
// Helper method for filtering, pagination, and sorting
private applyQueryParams(
{entities}: {Entity}Type[],
params: {Entity}QueryParamsType,
): PaginatedResultType<{Entity}Type> {
let filtered = {entities};
// Apply entity-specific filters
if (params.createdBy) {
filtered = filtered.filter((item) => item.createdBy === params.createdBy);
}
// Apply search filter
const searchTerm = params.search?.toLowerCase().trim();
if (searchTerm) {
filtered = filtered.filter((item) =>
item.{searchableField}.toLowerCase().includes(searchTerm)
);
}
// Pagination
const page = params.page ?? DEFAULT_PAGE;
const limit = params.limit ?? DEFAULT_LIMIT;
const skip = (page - 1) * limit;
let paginated = filtered.slice(skip, skip + limit);
const totalPages = Math.ceil(filtered.length / limit);
// Sorting
const sortBy = (params.sortBy ?? "createdAt") as keyof {Entity}Type;
const sortOrder = params.sortOrder;
if (sortBy) {
paginated = paginated.sort((a, b) => {
if (sortOrder === "asc") {
return a[sortBy]?.toString().localeCompare(b[sortBy]?.toString() ?? "") ?? 0;
} else if (sortOrder === "desc") {
return b[sortBy]?.toString().localeCompare(a[sortBy]?.toString() ?? "") ?? 0;
}
return 0;
});
}
return {
data: paginated,
total: filtered.length,
page,
limit,
totalPages,
};
}
async findAll(params: {Entity}QueryParamsType): Promise<PaginatedResultType<{Entity}Type>> {
return this.applyQueryParams(this.{entities}, params);
}
async findById(id: {Entity}IdType): Promise<{Entity}Type | null> {
const item = this.{entities}.find((e) => e.id === id);
return item || null;
}
async findAllByIds(
ids: {Entity}IdType[],
params: {Entity}QueryParamsType,
): Promise<PaginatedResultType<{Entity}Type>> {
const filtered = this.{entities}.filter((item) => ids.includes(item.id));
return this.applyQueryParams(filtered, params);
}
async create(
data: Create{Entity}Type,
createdByUserId: UserIdType,
): Promise<{Entity}Type> {
const now = new Date();
const new{Entity}: {Entity}Type = {
id: uuidv4(),
...data,
createdBy: createdByUserId,
createdAt: now,
updatedAt: now,
};
this.{entities}.push(new{Entity});
return new{Entity};
}
async update(id: {Entity}IdType, data: Update{Entity}Type): Promise<{Entity}Type | null> {
const index = this.{entities}.findIndex((e) => e.id === id);
if (index === -1) {
return null;
}
const existing = this.{entities}[index];
const updated = {
...existing,
...data,
updatedAt: new Date(),
};
this.{entities}[index] = updated;
return updated;
}
async remove(id: {Entity}IdType): Promise<boolean> {
const initialLength = this.{entities}.length;
this.{entities} = this.{entities}.filter((e) => e.id !== id);
return this.{entities}.length < initialLength;
}
// Helper method for testing: clear all data
clear(): void {
this.{entities} = [];
}
}
MockDb{Entity}Repository (e.g., MockDbNoteRepository)I{Entity}Repository interfaceprivate {entities}: {Entity}Type[] = [];
Use a descriptive plural name for the array (e.g., notes, courses, users).
Use uuid for generating unique IDs:
import { v4 as uuidv4 } from "uuid";
// In create method:
id: uuidv4(),
Always set timestamps in create and update:
// Create
const now = new Date();
createdAt: now,
updatedAt: now,
// Update
updatedAt: new Date(),
applyQueryParams HelperThis private method handles:
Always include a clear() method for test cleanup:
clear(): void {
this.{entities} = [];
}
See REFERENCE.md for a full implementation example including:
MockDbNoteRepository classfindAllByIds)clear() helperclear() helper - tests need itxiaohongshu
technical spec
product ux expert
database patterns
Conduct multi-agent task orchestration and workflow coordination.
Initialize project with Conductor artifacts (product definition,
Expert in web animations, transitions, and motion design using Framer Motion and CSS
Creates Mermaid and ASCII diagrams for flowcharts, architecture, ERDs, state machines, mindmaps, and more. Use when user mentions diagram, flowchart, mermaid, ASCII diagram, text diagram, terminal diagram, visualize, C4, mindmap, architecture diagram, sequence diagram, ERD, or needs visual docume...
PostgreSQL bindings for H3 hexagonal grid system. Use when working with H3 cells in Postgres, including spatial indexing, geometry/geography integration, and raster analysis.
Context-Driven Development skill for projects using Conductor. Use this skill when you detect a `conductor/` directory in the project, when working on tasks defined in a `plan.md` file, or when the user asks about tracks, specs, or plans. Automatically applies TDD workflow, tracks task completion...
Display project status, active tracks, and next actions
Official Stakpak application containerization standard operating procedure, a step-by-step guidline to properly dockerize applications. This is a rule book curated by the Stakpak Team.
Generate, edit, and beat-sync AI video with leading models in one workspace.
The world's fastest calendar for remote work
Transform Your Design with AI Designer by ImgCreator.ai
Revolutionizing Video Production with AI-Powered Creativity
Extend an image past the frame and let AI fill the new aspect ratio.
Discover your celebrity doppelgänger with StarByFace!
ChainClarity explains 700+ crypto whitepapers in plain English, with layered summaries, comparisons, research tools, alerts, and a $4.99 Pro plan.
Opus.ai: Revolutionize Your Web Experience