cca-user-module
v0.2.12
Published
User module
Downloads
237
Readme
cca-user-module
cca-user-module is a TypeScript module that provides use cases, a controller, and a container to manage users in your application. It supports creating, retrieving, updating, and deleting users and is designed to be integrated into a larger system that provides database and HTTP wiring.
Installation
Install the module via npm:
npm install cca-user-moduleFeatures
- Create User: Create a new user with required fields such as name and email.
- Get User: Retrieve user details by ID.
- Get All Users: Retrieve a list of all users (cached for a short TTL).
- Update User: Update existing user information.
- Delete User: Delete a user by ID.
Usage Example
Below is an example of how to integrate the UserController provided by cca-user-module into your Express application.
import express from 'express';
import { createUserContainer, UserController } from 'cca-user-module';
import { BaseDatabase } from 'cca-core';
const app = express();
app.use(express.json());
// Create the container using a BaseDatabase instance provided by the host system
const database = new BaseDatabase(/* ... your TypeORM data source ... */);
const { userController } = createUserContainer(database);
const controller: UserController = userController;
// Define user routes
app.post('/users', controller.createUser);
app.get('/users/:id', controller.getUser);
app.get('/users', controller.getAllUsers);
app.put('/users/:id', controller.updateUser);
app.delete('/users/:id', controller.deleteUser);
// Error handling middleware
app.use((err, req, res, next) => {
res.status(500).json({ error: err.message });
});
app.listen(3000, () => console.log('Server running on port 3000'));API Endpoints
1. Create a User
- URL:
/users - Method:
POST - Description: Creates a new user.
- Request Body:
- User details (e.g.,
name,email).
- User details (e.g.,
- Response:
- Code:
201 Created - Content: JSON object with the newly created user details.
- Code:
2. Get a Single User
- URL:
/users/:id - Method:
GET - Description: Retrieves a user by their ID.
- Response:
- Code:
200 OK - Content: JSON object with user details.
- Code:
3. Get All Users
- URL:
/users - Method:
GET - Description: Retrieves all users.
- Response:
- Code:
200 OK - Content: JSON array containing user objects.
- Code:
4. Update a User
- URL:
/users/:id - Method:
PUT - Description: Updates an existing user.
- Request Body: JSON object with the fields to update.
- Response:
- Code:
200 OK - Content: JSON object with the updated user details.
- Code:
5. Delete a User
- URL:
/users/:id - Method:
DELETE - Description: Deletes a user by their ID.
- Response:
- Code:
204 No Content
- Code:
Error Handling
Ensure your Express application includes error-handling middleware to capture and process errors from the controller methods:
app.use((err, req, res, next) => {
res.status(500).json({ error: err.message });
});Scripts
npm run build:tsupnpm run testNotes
- Input validation is performed for
id,name, andemailfields. Get All Usersuses a short-lived cache (TTL 300 seconds). Cache is invalidated on create/update/remove.
License
This project is licensed under the MIT License.
