0.1.2 • Published 10 months ago

@ferjssilva/fast-crud-api v0.1.2

Weekly downloads
-
License
ISC
Repository
-
Last release
10 months ago

Fast CRUD API

A lightweight and flexible REST API generator for Fastify and MongoDB. Create fully-featured CRUD APIs with minimal configuration.

Quick Install

npm install @ferjssilva/fast-crud-api

Dependencies

  • fastify (peer dependency)
  • mongoose (peer dependency)
  • fastify-plugin

Features

  • šŸš€ Full CRUD operations out of the box
  • šŸ“„ Automatic pagination
  • šŸ” Text search across string fields
  • šŸ”— Reference population support
  • šŸ“± Nested routes for relationships
  • šŸŽÆ Method restrictions per model
  • šŸ›  Query building with filtering and sorting
  • ⚔ MongoDB integration with proper document transformation
  • šŸ”„ Clean REST API endpoints
  • 🚨 Comprehensive error handling
  • āœ… 100% Test Coverage

How to Use

Basic Setup

const fastify = require('fastify')()
const mongoose = require('mongoose')
const fastCrudApi = require('@ferjssilva/fast-crud-api')

// Your mongoose models
const User = require('./models/User')
const Post = require('./models/Post')

// Register the plugin
fastify.register(fastCrudApi, {
  prefix: '/api',
  models: [User, Post],
  methods: {
    // Optional: restrict methods per model
    users: ['GET', 'POST', 'PUT', 'DELETE'],
    posts: ['GET', 'POST']
  }
})

API Usage

List Resources

GET /api/users?page=1&limit=10
GET /api/users?sort={"createdAt":-1}
GET /api/users?name=John&age=25
GET /api/users?search=john

Get Single Resource

GET /api/users/:id
GET /api/users/:id?populate=posts

Create Resource

POST /api/users
Content-Type: application/json

{
  "name": "John Doe",
  "email": "john@example.com"
}

Update Resource

PUT /api/users/:id
Content-Type: application/json

{
  "name": "John Updated"
}

Delete Resource

DELETE /api/users/:id

Nested Routes

GET /api/users/:userId/posts

Response Format

List Response

{
  "data": [...],
  "pagination": {
    "total": 100,
    "page": 1,
    "limit": 10,
    "pages": 10
  }
}

Single Resource Response

{
  "id": "...",
  "field1": "value1",
  "field2": "value2"
}

Error Response

{
  "error": "ErrorType",
  "message": "Error description",
  "details": [] // Optional validation details
}

Project Structure

The library is organized in a modular structure for better maintainability:

src/
ā”œā”€ā”€ index.js               # Main plugin module
ā”œā”€ā”€ utils/
│   ā”œā”€ā”€ document.js        # Document transformation utilities
│   └── query.js           # Query building utilities
ā”œā”€ā”€ middleware/
│   └── error-handler.js   # Error handling middleware
ā”œā”€ā”€ routes/
│   ā”œā”€ā”€ crud.js            # CRUD route handlers
│   └── nested.js          # Nested route handlers
└── validators/
    └── method.js          # Method validation utilities

Issues and Contact

If you encounter any issues or have suggestions for improvements, please open an issue on our GitHub repository. We appreciate your feedback and contributions!

Open an Issue

Testing

The library includes comprehensive unit tests to ensure the correct functioning of all components:

# Run all tests
npm test

# Run tests with coverage report
npm run test:coverage

# Run tests in watch mode (useful during development)
npm run test:watch

Code coverage results:

  • Lines of code: 100%
  • Functions: 100%
  • Branches: 100%
  • Statements: 100%

We've achieved complete coverage for all components:

  • Utils: 100%
  • Validators: 100%
  • Middleware: 100%
  • Routes: 100%

License

ISC License

0.1.2

10 months ago

0.1.1

10 months ago

0.1.0

10 months ago

0.0.5

11 months ago

0.0.3

11 months ago

0.0.2

11 months ago

0.0.1

11 months ago