Skip to main content
Version: 0.5.0 (Previous)

๐Ÿ› ๏ธ 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:

  1. Pure functions โ€“ no global state, every dependency is passed explicitly (great for testing!).
  2. Composable โ€“ reuse/compose features between services or build your own.
  3. Database-agnostic โ€“ only a Collection interface is required; swap Mongo for a stub in tests.

๐Ÿ“‘ Available Servicesโ€‹

ServiceNPM importResponsibilityDocs
AuthenticationauthServiceUser authentication (register, login, logout, JWT tokens)Docs ยป
UseruserServiceManage application users (CRUD, lock/unlock)Docs ยป
OrganizationorganizationServiceCreate & manage organizations/workspacesDocs ยป
ProductproductServiceComplex product management inc. batch & copy opsDocs ยป
CategorycategoryServiceMaintain product or content categoriesDocs ยป
AttributeattributeServiceManage key-value attributes and attribute groupsDocs ยป
OrderorderServiceE-commerce order management with items and pricingDocs ยป
ChatchatServiceReal-time messaging with channels, subscriptions, and messagesDocs ยป

โ„น๏ธ 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!