@kenniy/godeye-data-contracts
Version:
Enterprise-grade base repository architecture for GOD-EYE microservices with zero overhead and maximum code reuse
174 lines (173 loc) • 6.06 kB
JavaScript
;
/**
* Query DTOs for Clean, Fluent Repository API
* Optimized developer experience with type safety
*/
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.QueryBuilder = exports.FileQueryDto = exports.UserQueryDto = exports.BaseQueryDto = void 0;
const class_validator_1 = require("class-validator");
const class_transformer_1 = require("class-transformer");
const types_1 = require("../types");
/**
* Base Query DTO - Common query parameters
*/
class BaseQueryDto {
constructor() {
this.limit = 20;
this.page = 1;
}
}
exports.BaseQueryDto = BaseQueryDto;
__decorate([
(0, class_validator_1.IsOptional)(),
(0, class_validator_1.IsNumber)(),
(0, class_validator_1.Min)(1),
(0, class_validator_1.Max)(1000),
(0, class_transformer_1.Type)(() => Number),
__metadata("design:type", Number)
], BaseQueryDto.prototype, "limit", void 0);
__decorate([
(0, class_validator_1.IsOptional)(),
(0, class_validator_1.IsNumber)(),
(0, class_validator_1.Min)(1),
(0, class_transformer_1.Type)(() => Number),
__metadata("design:type", Number)
], BaseQueryDto.prototype, "page", void 0);
__decorate([
(0, class_validator_1.IsOptional)(),
(0, class_validator_1.IsString)(),
__metadata("design:type", String)
], BaseQueryDto.prototype, "search", void 0);
__decorate([
(0, class_validator_1.IsOptional)(),
(0, class_validator_1.IsArray)(),
(0, class_validator_1.IsString)({ each: true }),
(0, class_transformer_1.Transform)(({ value }) => typeof value === 'string' ? value.split(',') : value),
__metadata("design:type", Array)
], BaseQueryDto.prototype, "include", void 0);
__decorate([
(0, class_validator_1.IsOptional)(),
(0, class_validator_1.IsArray)(),
(0, class_validator_1.IsString)({ each: true }),
(0, class_transformer_1.Transform)(({ value }) => typeof value === 'string' ? value.split(',') : value),
__metadata("design:type", Array)
], BaseQueryDto.prototype, "select", void 0);
/**
* User Query DTO - Auto-typed for User entity
*/
class UserQueryDto extends BaseQueryDto {
constructor() {
super(...arguments);
this.sortBy = types_1.SortDirection.DESC;
this.sortField = 'createdAt';
}
}
exports.UserQueryDto = UserQueryDto;
__decorate([
(0, class_validator_1.IsOptional)(),
(0, class_validator_1.IsString)(),
__metadata("design:type", String)
], UserQueryDto.prototype, "status", void 0);
__decorate([
(0, class_validator_1.IsOptional)(),
(0, class_validator_1.IsString)(),
__metadata("design:type", String)
], UserQueryDto.prototype, "userType", void 0);
__decorate([
(0, class_validator_1.IsOptional)(),
(0, class_validator_1.IsString)(),
__metadata("design:type", String)
], UserQueryDto.prototype, "email", void 0);
__decorate([
(0, class_validator_1.IsOptional)(),
(0, class_validator_1.IsEnum)(types_1.SortDirection),
__metadata("design:type", Object)
], UserQueryDto.prototype, "sortBy", void 0);
__decorate([
(0, class_validator_1.IsOptional)(),
(0, class_validator_1.IsString)(),
__metadata("design:type", String)
], UserQueryDto.prototype, "sortField", void 0);
/**
* File Query DTO - Auto-typed for File entity
*/
class FileQueryDto extends BaseQueryDto {
}
exports.FileQueryDto = FileQueryDto;
__decorate([
(0, class_validator_1.IsOptional)(),
(0, class_validator_1.IsString)(),
__metadata("design:type", String)
], FileQueryDto.prototype, "userId", void 0);
__decorate([
(0, class_validator_1.IsOptional)(),
(0, class_validator_1.IsString)(),
__metadata("design:type", String)
], FileQueryDto.prototype, "status", void 0);
__decorate([
(0, class_validator_1.IsOptional)(),
(0, class_validator_1.IsString)(),
__metadata("design:type", String)
], FileQueryDto.prototype, "fileType", void 0);
__decorate([
(0, class_validator_1.IsOptional)(),
(0, class_validator_1.IsNumber)(),
(0, class_transformer_1.Type)(() => Number),
__metadata("design:type", Number)
], FileQueryDto.prototype, "minSize", void 0);
__decorate([
(0, class_validator_1.IsOptional)(),
(0, class_validator_1.IsNumber)(),
(0, class_transformer_1.Type)(() => Number),
__metadata("design:type", Number)
], FileQueryDto.prototype, "maxSize", void 0);
/**
* Generic Query Builder for any entity
*/
class QueryBuilder {
constructor() {
this.criteria = {};
}
where(conditions) {
this.criteria.where = { ...this.criteria.where, ...conditions };
return this;
}
include(relations) {
this.criteria.relations = relations;
return this;
}
select(fields) {
this.criteria.select = fields;
return this;
}
sort(field, direction = types_1.SortDirection.DESC) {
this.criteria.sort = { [field]: direction };
return this;
}
limit(count) {
this.criteria.limit = count;
return this;
}
paginate(page, limit = 20) {
this.criteria.page = page;
this.criteria.limit = limit;
return this;
}
search(term, fields) {
this.criteria.search = { term, fields };
return this;
}
build() {
return this.criteria;
}
}
exports.QueryBuilder = QueryBuilder;