๐ ๏ธ Service
Nodeblocks backend is composed of modular services, each responsible for a specific piece of business logic and data.
Every service follows the same conventions so that integrating, extending or swapping them remains straightforward.
๐ What is a Service?โ
A service is a thin wrapper around one or more features (use-case bundles that contain routes, schemas and handlers).
At runtime the wrapper wires dependencies (e.g. a MongoDB collection) and exposes a typed interface you can call from code or automatically map to HTTP endpoints through the provided router utilities.
โโโโโโโโโโโโโโโ
โ Service โ userService(db)
โโโโโโโโโโโโโโโค
โ Features โท createUserFeature
โ โท getUserFeatures
โ โท editUserFeatures
โ โท ...
โโโโโโโโโโโโโโโ
Key take-aways:
- Pure functions โ no global state, every dependency is passed explicitly (great for testing!).
- Composable โ reuse/compose features between services or build your own.
- Database-agnostic โ only a
Collection
interface is required; swap Mongo for a stub in tests.
๐ Available Servicesโ
Service | NPM import | Responsibility | Docs |
---|---|---|---|
Authentication | authService | User authentication (register, login, logout, JWT tokens) | Docs ยป |
User | userService | Manage application users (CRUD, lock/unlock) | Docs ยป |
Organization | organizationService | Create & manage organizations/workspaces | Docs ยป |
Product | productService | Complex product management inc. batch & copy ops | Docs ยป |
Category | categoryService | Maintain product or content categories | Docs ยป |
Attribute | attributeService | Manage key-value attributes and attribute groups | Docs ยป |
Order | orderService | E-commerce order management with items and pricing | Docs ยป |
Chat | chatService | Real-time messaging with channels, subscriptions, and messages | Docs ยป |
โน๏ธ All services have been tested with live API endpoints and include comprehensive Bruno test suites. More services will be added to the SDK over time. Feel free to open an issue or PR for suggestions!
๐งโ๐ป Consuming Services Programmaticallyโ
All services share the same signature: they are factory functions that take a database object with named collections, configuration, and optional drivers (mail/file storage/OAuth) and return an Express router.
import express from 'express';
import { MongoClient } from 'mongodb';
import { middlewares, services } from '@nodeblocks/backend-sdk';
const { nodeBlocksErrorMiddleware } = middlewares;
const { userService } = services;
const client = new MongoClient('mongodb://localhost:27017').db('dev');
express()
.use(
userService(
{
users: client.collection('users'),
identity: client.collection('identity'),
},
{
authSecrets: {
authEncSecret: 'your-encryption-secret',
authSignSecret: 'your-signing-secret',
},
},
{
// Optional drivers (when needed)
// mailService,
// fileStorageDriver,
// googleOAuthDriver,
}
)
)
.use(nodeBlocksErrorMiddleware())
.listen(8089, () => console.log('Server running'));
๐ Design Principlesโ
โข Single responsibility โ each service targets one domain entity.
โข Extensibility first โ compose, override, or decorate features without forking the codebase.
โก๏ธ Nextโ
Start with the Authentication Service for user authentication, or explore other services based on your needs. All services follow the same patterns and conventions!