nestjs-prisma-base
Version:
A comprehensive NestJS package providing base classes, utilities, and decorators for building CRUD APIs with Prisma ORM integration, featuring pagination, search, filtering, relation loading, configurable DTOs, and modular composition capabilities.
113 lines • 4.18 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.ConfigurableBaseResponseDto = exports.ConfigurableBaseUpdateDto = exports.ConfigurableBaseCreateDto = void 0;
exports.configureDTOs = configureDTOs;
exports.getDTOConfig = getDTOConfig;
exports.registerDTOConfigChangeCallback = registerDTOConfigChangeCallback;
let triggerSwaggerUpdate = null;
let globalDTOConfig = {
includeTimestamps: true,
includeId: true,
includeMessage: false,
swaggerEnabled: false,
timestampFields: {
createdAt: 'createdAt',
updatedAt: 'updatedAt',
},
messageField: {
fieldName: 'message',
defaultValue: undefined,
maxLength: 500,
},
};
function configureDTOs(config) {
if (config.messageField) {
if (config.messageField.maxLength !== undefined && config.messageField.maxLength <= 0) {
throw new Error('messageField.maxLength must be a positive number');
}
if (config.messageField.fieldName !== undefined && typeof config.messageField.fieldName !== 'string') {
throw new Error('messageField.fieldName must be a string');
}
if (config.messageField.fieldName !== undefined && config.messageField.fieldName.trim() === '') {
throw new Error('messageField.fieldName cannot be empty');
}
if (config.messageField.defaultValue !== undefined && typeof config.messageField.defaultValue !== 'string') {
throw new Error('messageField.defaultValue must be a string');
}
}
const previousConfig = { ...globalDTOConfig };
globalDTOConfig = { ...globalDTOConfig, ...config };
if (triggerSwaggerUpdate &&
(previousConfig.includeMessage !== globalDTOConfig.includeMessage ||
JSON.stringify(previousConfig.messageField) !== JSON.stringify(globalDTOConfig.messageField))) {
triggerSwaggerUpdate();
}
}
function getDTOConfig() {
return { ...globalDTOConfig };
}
function registerDTOConfigChangeCallback(callback) {
triggerSwaggerUpdate = callback;
}
class ConfigurableBaseCreateDto {
static configure(config) {
this.dtoConfig = { ...this.dtoConfig, ...config };
}
static getConfig() {
return { ...globalDTOConfig, ...this.dtoConfig };
}
}
exports.ConfigurableBaseCreateDto = ConfigurableBaseCreateDto;
ConfigurableBaseCreateDto.dtoConfig = {};
class ConfigurableBaseUpdateDto {
static configure(config) {
this.dtoConfig = { ...this.dtoConfig, ...config };
}
static getConfig() {
return { ...globalDTOConfig, ...this.dtoConfig };
}
}
exports.ConfigurableBaseUpdateDto = ConfigurableBaseUpdateDto;
ConfigurableBaseUpdateDto.dtoConfig = {};
class ConfigurableBaseResponseDto {
static configure(config) {
this.dtoConfig = { ...this.dtoConfig, ...config };
}
static getConfig() {
return { ...globalDTOConfig, ...this.dtoConfig };
}
static shouldIncludeTimestamps() {
const config = this.getConfig();
return config.includeTimestamps ?? true;
}
static shouldIncludeId() {
const config = this.getConfig();
return config.includeId ?? true;
}
static shouldIncludeMessage() {
const config = this.getConfig();
return config.includeMessage ?? false;
}
static isSwaggerEnabled() {
const config = this.getConfig();
return config.swaggerEnabled ?? false;
}
static getTimestampFields() {
const config = this.getConfig();
return {
createdAt: config.timestampFields?.createdAt ?? 'createdAt',
updatedAt: config.timestampFields?.updatedAt ?? 'updatedAt',
};
}
static getMessageFieldConfig() {
const config = this.getConfig();
return {
fieldName: config.messageField?.fieldName ?? 'message',
defaultValue: config.messageField?.defaultValue,
maxLength: config.messageField?.maxLength ?? 500,
};
}
}
exports.ConfigurableBaseResponseDto = ConfigurableBaseResponseDto;
ConfigurableBaseResponseDto.dtoConfig = {};
//# sourceMappingURL=configurable-dtos.js.map