Repository Template
This template provides a standard structure for creating new repositories in the PRS system, extending the BaseRepository class.
Usage Instructions
- Replace
EntityName with your entity name
- Implement the methods below
- Register the repository in the container
Template Code
| JavaScript |
|---|
| /**
* Template for creating a new repository
*
* Instructions:
* 1. Replace EntityName with your entity name
* 2. Implement the methods below
* 3. Register the repository in the container
*/
const BaseRepository = require('../../infra/repositories/baseRepository');
class EntityNameRepository extends BaseRepository {
constructor({ db }) {
super(db.entityNameModel);
this.db = db;
this.Sequelize = db.Sequelize;
}
/**
* Find all entities with optional filtering and pagination
*
* @param {Object} options - Query options
* @returns {Promise<Object>} - Paginated list of entities
*/
async findAll(options = {}) {
// Add any custom logic for finding all entities
return super.findAll(options);
}
/**
* Find one entity by criteria
*
* @param {Object} options - Query options
* @returns {Promise<Object>} - Entity object
*/
async findOne(options = {}) {
// Add any custom logic for finding one entity
return super.findOne(options);
}
/**
* Create a new entity
*
* @param {Object} data - Entity data
* @param {Object} options - Query options
* @returns {Promise<Object>} - Created entity
*/
async create(data, options = {}) {
// Add any custom logic for creating an entity
return super.create(data, options);
}
/**
* Update an entity
*
* @param {Object} where - Criteria for finding entity to update
* @param {Object} data - Updated entity data
* @param {Object} options - Query options
* @returns {Promise<Array>} - [rowsUpdated, updatedEntities]
*/
async update(where, data, options = {}) {
// Add any custom logic for updating an entity
return super.update(where, data, options);
}
/**
* Delete an entity
*
* @param {Object} where - Criteria for finding entity to delete
* @param {Object} options - Query options
* @returns {Promise<number>} - Number of deleted entities
*/
async destroy(where, options = {}) {
// Add any custom logic for deleting an entity
return super.destroy(where, options);
}
// Custom query methods...
}
|
Key Features
- Inheritance: Extends BaseRepository for common functionality
- Sequelize Integration: Uses Sequelize for database operations
- Customizable Methods: Override base methods for custom behavior
- Advanced Queries: Support for complex queries and aggregations
- Association Handling: Support for including related data
Common Methods
- findAll: Retrieve all entities with optional filtering and pagination
- findOne: Retrieve a single entity by criteria
- create: Create a new entity
- update: Update an existing entity
- destroy: Delete an entity
Advanced Query Examples
| JavaScript |
|---|
| async findByStatus(status) {
return this.findAll({
where: { status },
include: [
{
model: this.db.userModel,
as: 'createdByUser',
attributes: ['id', 'firstName', 'lastName', 'username'],
},
],
order: [['createdAt', 'DESC']],
paginate: false, // Return all results without pagination
});
}
|
Complex Conditions
| JavaScript |
|---|
| async findWithComplexConditions(filters) {
const { status, createdBy, fromDate, toDate } = filters;
const where = {};
if (status) {
where.status = status;
}
if (createdBy) {
where.createdBy = createdBy;
}
if (fromDate || toDate) {
where.createdAt = {};
if (fromDate) {
where.createdAt[this.Sequelize.Op.gte] = new Date(fromDate);
}
if (toDate) {
where.createdAt[this.Sequelize.Op.lte] = new Date(toDate);
}
}
return this.findAll({
where,
include: [
// Add associations as needed
],
order: [['createdAt', 'DESC']],
});
}
|
Aggregation Queries
| JavaScript |
|---|
| async getAggregatedData(filters) {
const { groupBy, status } = filters;
const where = {};
if (status) {
where.status = status;
}
const attributes = [
[this.Sequelize.fn('COUNT', this.Sequelize.col('id')), 'count'],
];
if (groupBy === 'status') {
attributes.unshift('status');
return this.model.findAll({
attributes,
where,
group: ['status'],
raw: true,
});
}
// Additional aggregation options...
}
|
Best Practices
- Extend BaseRepository for common functionality
- Use Sequelize operators for complex conditions
- Include related data using associations
- Use transactions for write operations
- Implement custom query methods for specific business needs