📅 You're Invited: Meet the Socket team at RSAC (April 28 – May 1).RSVP
Socket
Sign inDemoInstall
Socket

vs-express-api-builder

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

vs-express-api-builder

A powerful, simple, and effective Express.js API builder that helps you create RESTful APIs with minimal code

1.0.3
latest
Source
npm
Version published
Weekly downloads
10
-56.52%
Maintainers
1
Weekly downloads
 
Created
Source

vs-express-api-builder

A powerful, simple, and effective Express.js API builder that helps you create RESTful APIs with minimal code.

Why Choose vs-express-api-builder?

Our package is different from others because it focuses on simplicity and effectiveness:

  • Inline Model Definition - Define your models directly in your code, no need for separate schema files
  • Automatic CRUD Operations - Get a full REST API with just a few lines of code
  • Built-in Validation - Type-safe validation using Zod
  • Powerful Query Builder - Chain methods to build complex queries
  • Lifecycle Hooks - Easily extend functionality with before/after hooks
  • Simple Middleware Management - Configure middleware with a single object
  • Seamless Database Integration - Connect to MongoDB with minimal setup

Getting Started

New to APIs?

If you're new to building APIs, check out our Beginner's Guide for step-by-step instructions.

Quick Start

npm install vs-express-api-builder

Basic Example

import express from 'express';
import { Model, DatabaseConnection, APICreator } from 'vs-express-api-builder';

// Define your model inline
const UserModel = new Model({
  name: { type: String, required: true },
  email: { type: String, required: true, unique: true },
  age: { type: Number, min: 0 }
});

// Create Express app
const app = express();
app.use(express.json());

// Connect to MongoDB
await DatabaseConnection.getInstance().connect({
  url: 'mongodb://localhost:27017/myapp'
});

// Create API endpoints
const userAPI = new APICreator({
  path: '/users',
  model: UserModel
});

// Use the API routes
app.use('/api', userAPI.getRouter());

// Start the server
app.listen(3000, () => {
  console.log('Server running on http://localhost:3000');
});

That's it! You now have a fully functional REST API with the following endpoints:

  • GET /api/users - List all users
  • GET /api/users/:id - Get a specific user
  • POST /api/users - Create a new user
  • PUT /api/users/:id - Update a user
  • DELETE /api/users/:id - Delete a user

Features

Model Definition

Define your models inline with validation rules:

const ProductModel = new Model({
  name: { type: String, required: true },
  price: { type: Number, required: true, min: 0 },
  category: { type: String, enum: ['electronics', 'clothing', 'books'] },
  inStock: { type: Boolean, default: true }
});

Relationships

Define relationships between models:

const OrderModel = new Model({
  user: { type: String, ref: 'User', required: true },
  products: [{ type: String, ref: 'Product' }],
  total: { type: Number, required: true }
});

Validation

Add validation schemas using Zod:

import { z } from 'zod';

const productAPI = new APICreator({
  path: '/products',
  model: ProductModel,
  validation: {
    create: z.object({
      name: z.string().min(2),
      price: z.number().min(0),
      category: z.enum(['electronics', 'clothing', 'books']),
      inStock: z.boolean().optional()
    })
  }
});

Query Builder

Build complex queries with a fluent interface:

// Find all products in the electronics category that are in stock
const products = await ProductModel
  .where('category').equals('electronics')
  .where('inStock').equals(true)
  .sort('price', 'asc')
  .limit(10)
  .exec();

// Find orders with populated user and products
const orders = await OrderModel
  .where('total').gt(100)
  .populate('user')
  .populate('products')
  .exec();

Lifecycle Hooks

Add hooks to extend functionality:

const userAPI = new APICreator({
  path: '/users',
  model: UserModel,
  hooks: {
    beforeCreate: async (req, res, next) => {
      // Hash password before creating user
      req.body.password = await hashPassword(req.body.password);
      next();
    },
    afterCreate: async (req, res, next) => {
      // Send welcome email after user is created
      await sendWelcomeEmail(res.locals.user.email);
      next();
    }
  }
});

Middleware Management

Configure middleware with a single object:

const api = new APICreator({
  path: '/api',
  model: MyModel,
  middleware: {
    auth: true, // Enable authentication
    rateLimit: true, // Enable rate limiting
    cache: true, // Enable response caching
    cors: true // Enable CORS
  }
});

Examples

Check out our examples directory for more usage examples:

Documentation

For more detailed documentation, visit our documentation site.

License

MIT

Keywords

express

FAQs

Package last updated on 10 Apr 2025

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts