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.
281 lines • 10.7 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.MixinCombinations = void 0;
exports.WithTimestamps = WithTimestamps;
exports.WithSoftDelete = WithSoftDelete;
exports.WithAuditFields = WithAuditFields;
exports.WithVersioning = WithVersioning;
exports.WithId = WithId;
exports.WithMessage = WithMessage;
exports.composeMixins = composeMixins;
exports.DisableSwagger = DisableSwagger;
exports.DisableValidation = DisableValidation;
exports.hasSwaggerDisabled = hasSwaggerDisabled;
exports.hasValidationDisabled = hasValidationDisabled;
const swagger_1 = require("@nestjs/swagger");
let IsOptional, IsDateString, IsNumber, IsBoolean, IsString, MaxLength;
try {
const classValidator = require('class-validator');
IsOptional = classValidator.IsOptional;
IsDateString = classValidator.IsDateString;
IsNumber = classValidator.IsNumber;
IsBoolean = classValidator.IsBoolean;
IsString = classValidator.IsString;
MaxLength = classValidator.MaxLength;
}
catch (error) {
IsOptional =
IsDateString =
IsNumber =
IsBoolean =
IsString =
() => () => { };
MaxLength = () => () => { };
}
const defaultMixinConfig = {
swagger: {
enabled: true,
includeExamples: true,
includeDescriptions: true,
},
validation: {
enabled: true,
optional: true,
},
};
function mergeMixinConfig(userConfig) {
return {
swagger: {
...defaultMixinConfig.swagger,
...userConfig?.swagger,
},
validation: {
...defaultMixinConfig.validation,
...userConfig?.validation,
},
};
}
function applyDecorators(target, propertyKey, mixinConfig, apiPropertyOptions) {
if (mixinConfig.swagger?.enabled !== false) {
(0, swagger_1.ApiProperty)(apiPropertyOptions)(target, propertyKey);
}
if (mixinConfig.validation?.enabled !== false) {
IsOptional()(target, propertyKey);
}
}
function WithTimestamps(Base, config) {
const mixinConfig = mergeMixinConfig(config);
class TimestampsMixin extends Base {
}
if (mixinConfig.swagger?.enabled !== false) {
(0, swagger_1.ApiProperty)({
description: 'Creation timestamp',
example: '2023-01-01T00:00:00.000Z',
type: () => Date,
required: false,
})(TimestampsMixin.prototype, 'createdAt');
}
if (mixinConfig.validation?.enabled !== false) {
IsOptional()(TimestampsMixin.prototype, 'createdAt');
IsDateString()(TimestampsMixin.prototype, 'createdAt');
}
if (mixinConfig.swagger?.enabled !== false) {
(0, swagger_1.ApiProperty)({
description: 'Last update timestamp',
example: '2023-01-01T12:00:00.000Z',
type: () => Date,
required: false,
})(TimestampsMixin.prototype, 'updatedAt');
}
if (mixinConfig.validation?.enabled !== false) {
IsOptional()(TimestampsMixin.prototype, 'updatedAt');
IsDateString()(TimestampsMixin.prototype, 'updatedAt');
}
return TimestampsMixin;
}
function WithSoftDelete(Base, config) {
const mixinConfig = mergeMixinConfig(config);
class SoftDeleteMixin extends Base {
}
if (mixinConfig.swagger?.enabled !== false) {
(0, swagger_1.ApiProperty)({
description: 'Soft deletion timestamp',
example: '2023-01-01T18:00:00.000Z',
type: () => Date,
required: false,
})(SoftDeleteMixin.prototype, 'deletedAt');
}
if (mixinConfig.validation?.enabled !== false) {
IsOptional()(SoftDeleteMixin.prototype, 'deletedAt');
IsDateString()(SoftDeleteMixin.prototype, 'deletedAt');
}
if (mixinConfig.swagger?.enabled !== false) {
(0, swagger_1.ApiProperty)({
description: 'Whether the record is active (not soft deleted)',
example: true,
default: true,
type: () => Boolean,
required: false,
})(SoftDeleteMixin.prototype, 'isActive');
}
if (mixinConfig.validation?.enabled !== false) {
IsOptional()(SoftDeleteMixin.prototype, 'isActive');
IsBoolean()(SoftDeleteMixin.prototype, 'isActive');
}
return SoftDeleteMixin;
}
function WithAuditFields(Base, config) {
const mixinConfig = mergeMixinConfig(config);
class AuditFieldsMixin extends Base {
}
if (mixinConfig.swagger?.enabled !== false) {
(0, swagger_1.ApiProperty)({
description: 'ID of user who created this record',
example: 1,
type: () => Number,
required: false,
})(AuditFieldsMixin.prototype, 'createdBy');
}
if (mixinConfig.validation?.enabled !== false) {
IsOptional()(AuditFieldsMixin.prototype, 'createdBy');
IsNumber()(AuditFieldsMixin.prototype, 'createdBy');
}
if (mixinConfig.swagger?.enabled !== false) {
(0, swagger_1.ApiProperty)({
description: 'ID of user who last updated this record',
example: 2,
type: () => Number,
required: false,
})(AuditFieldsMixin.prototype, 'updatedBy');
}
if (mixinConfig.validation?.enabled !== false) {
IsOptional()(AuditFieldsMixin.prototype, 'updatedBy');
IsNumber()(AuditFieldsMixin.prototype, 'updatedBy');
}
return AuditFieldsMixin;
}
function WithVersioning(Base, config) {
const mixinConfig = mergeMixinConfig(config);
class VersioningMixin extends Base {
}
if (mixinConfig.swagger?.enabled !== false) {
(0, swagger_1.ApiProperty)({
description: 'Version number for optimistic locking',
example: 1,
default: 1,
type: () => Number,
required: false,
})(VersioningMixin.prototype, 'version');
}
if (mixinConfig.validation?.enabled !== false) {
IsOptional()(VersioningMixin.prototype, 'version');
IsNumber()(VersioningMixin.prototype, 'version');
}
return VersioningMixin;
}
function WithId(Base, config) {
const mixinConfig = mergeMixinConfig(config);
class IdMixin extends Base {
}
if (mixinConfig.swagger?.enabled !== false) {
(0, swagger_1.ApiProperty)({
description: 'Unique identifier',
example: 1,
type: () => Number,
required: false,
})(IdMixin.prototype, 'id');
}
if (mixinConfig.validation?.enabled !== false) {
IsOptional()(IdMixin.prototype, 'id');
IsNumber()(IdMixin.prototype, 'id');
}
return IdMixin;
}
function WithMessage(Base, config) {
const mixinConfig = mergeMixinConfig(config);
const fieldName = config?.fieldName || 'message';
class MessageMixin extends Base {
}
if (mixinConfig.swagger?.enabled !== false) {
(0, swagger_1.ApiProperty)({
description: 'Response message providing additional context',
example: config?.defaultMessage || 'Operation completed successfully',
type: () => String,
required: false,
maxLength: config?.maxLength || 500,
})(MessageMixin.prototype, fieldName);
}
if (mixinConfig.validation?.enabled !== false) {
IsOptional()(MessageMixin.prototype, fieldName);
IsString()(MessageMixin.prototype, fieldName);
if (config?.maxLength) {
MaxLength(config.maxLength)(MessageMixin.prototype, fieldName);
}
}
if (fieldName !== 'message') {
Object.defineProperty(MessageMixin.prototype, fieldName, {
value: undefined,
writable: true,
enumerable: true,
configurable: true,
});
}
return MessageMixin;
}
function composeMixins(Base, ...mixins) {
return mixins.reduce((acc, mixin) => mixin(acc), Base);
}
class MixinCombinations {
static WithFullAuditTrail(Base, config) {
return composeMixins(Base, (base) => WithTimestamps(base, config), (base) => WithAuditFields(base, config), (base) => WithVersioning(base, config));
}
static WithFullAuditTrailAndMessage(Base, config) {
return composeMixins(Base, (base) => WithTimestamps(base, config), (base) => WithAuditFields(base, config), (base) => WithVersioning(base, config), (base) => WithMessage(base, config));
}
static WithSoftDeleteAndTimestamps(Base, config) {
return composeMixins(Base, (base) => WithTimestamps(base, config), (base) => WithSoftDelete(base, config));
}
static WithSoftDeleteTimestampsAndMessage(Base, config) {
return composeMixins(Base, (base) => WithTimestamps(base, config), (base) => WithSoftDelete(base, config), (base) => WithMessage(base, config));
}
static WithStandardEntity(Base, config) {
return composeMixins(Base, (base) => WithId(base, config), (base) => WithTimestamps(base, config));
}
static WithStandardEntityAndMessage(Base, config) {
return composeMixins(Base, (base) => WithId(base, config), (base) => WithTimestamps(base, config), (base) => WithMessage(base, config));
}
static WithCompleteEntity(Base, config) {
return composeMixins(Base, (base) => WithId(base, config), (base) => WithTimestamps(base, config), (base) => WithAuditFields(base, config), (base) => WithSoftDelete(base, config), (base) => WithVersioning(base, config));
}
static WithCompleteEntityAndMessage(Base, config) {
return composeMixins(Base, (base) => WithId(base, config), (base) => WithTimestamps(base, config), (base) => WithAuditFields(base, config), (base) => WithSoftDelete(base, config), (base) => WithVersioning(base, config), (base) => WithMessage(base, config));
}
static WithResponseEntity(Base, config) {
return composeMixins(Base, (base) => WithId(base, config), (base) => WithTimestamps(base, config), (base) => WithMessage(base, config));
}
static WithMinimalResponse(Base, config) {
return composeMixins(Base, (base) => WithId(base, config), (base) => WithMessage(base, config));
}
}
exports.MixinCombinations = MixinCombinations;
function DisableSwagger(Base) {
var _a;
return _a = class extends Base {
},
_a.__disableSwagger = true,
_a;
}
function DisableValidation(Base) {
var _a;
return _a = class extends Base {
},
_a.__disableValidation = true,
_a;
}
function hasSwaggerDisabled(target) {
return target.__disableSwagger === true;
}
function hasValidationDisabled(target) {
return target.__disableValidation === true;
}
//# sourceMappingURL=dto-mixins.js.map