Apiato.js
A powerful and flexible API library for Node.js that supports both SQL (Sequelize) and NoSQL (Mongoose) databases, with REST and Socket.IO implementations.
Features
- Support for both SQL (Sequelize) and NoSQL (Mongoose) databases
- REST API implementation
- Socket.IO implementation
- TypeScript support
- Validation
- Pagination
- Sorting
- Filtering
- Population/Include relations
- Room-based communication (Socket.IO)
- Broadcasting (Socket.IO)
Installation
bun install apiato
Examples
This repository includes four example implementations:
- REST API with Sequelize (SQLite)
- REST API with Mongoose (MongoDB)
- Socket.IO with Sequelize (SQLite)
- Socket.IO with Mongoose (MongoDB)
Running the Examples
Each example is in its own directory under examples/
. To run an example:
Navigate to the example directory:
cd examples/[example-name]
Install dependencies:
bun install
Start the development server:
bun run dev
Example Ports
- Sequelize REST API: http://localhost:3000
- Mongoose REST API: http://localhost:3001
- Mongoose Socket.IO: http://localhost:3002
- Sequelize Socket.IO: http://localhost:3003
Usage
REST API with Sequelize
import { ApiatoSQL } from 'apiato/typescript';
import User from './models/User';
const apiato = new ApiatoSQL();
// Create routes
router.post('/', apiato.createOne(User));
router.get('/', apiato.getMany(User));
router.get('/:id', apiato.getOneById(User));
router.put('/:id', apiato.updateById(User));
router.delete('/:id', apiato.findIdAndDelete(User));
REST API with Mongoose
import { ApiatoNoSQL } from 'apiato/typescript';
import User from './models/User';
const apiato = new ApiatoNoSQL();
// Create routes
router.post('/', apiato.createOne(User));
router.get('/', apiato.getMany(User));
router.get('/:id', apiato.getOneById(User));
router.put('/:id', apiato.updateById(User));
router.delete('/:id', apiato.findIdAndDelete(User));
Socket.IO with Sequelize/Mongoose
import { ApiatoSocket } from 'apiato/typescript';
import { Server } from 'socket.io';
import User from './models/User';
const io = new Server(httpServer);
const userSocket = new ApiatoSocket(io, User);
// Available events:
// - create
// - getMany
// - getOneById
// - updateById
// - deleteById
// Example client usage:
socket.emit('create', JSON.stringify({
body: {
name: "John Doe",
email: "john@example.com",
age: 30
},
responseType: "private" // or "broadcast" or "room"
}));
API Documentation
REST API Endpoints
POST /
: Create a new recordGET /
: Get all records (with pagination, sorting, filtering)GET /:id
: Get a record by IDPUT /:id
: Update a record by IDDELETE /:id
: Delete a record by ID
Socket.IO Events
create
: Create a new recordgetMany
: Get all recordsgetOneById
: Get a record by IDupdateById
: Update a record by IDdeleteById
: Delete a record by ID
Query Parameters (REST API)
where
: Filter records by field valueslike
: Filter records using partial matchesselect
: Select specific fieldspaginate
: Paginate results (page, limit)sort
: Sort results by fieldspopulate/include
: Include related records
Socket.IO Request Format
{
body?: any; // Data for create/update operations
id?: number | string; // Record ID for single-record operations
query?: { // Query parameters
where?: any; // Filter conditions
attributes?: string[]; // Fields to select (Sequelize)
select?: any; // Fields to select (Mongoose)
include?: any[]; // Relations to include
sort?: any; // Sort conditions
paginate?: { // Pagination
page: number;
limit: number;
}
};
responseType?: 'private' | 'broadcast' | 'room'; // Response type
room?: string; // Room name for room-based responses
tag?: string; // Custom tag for response tracking
}
License
MIT