Commit 62d28c7f authored by nanahira's avatar nanahira

add query decorators

parent a40a05d0
...@@ -2,6 +2,7 @@ import { NotWritable } from '../decorators'; ...@@ -2,6 +2,7 @@ import { NotWritable } from '../decorators';
import { SelectQueryBuilder } from 'typeorm'; import { SelectQueryBuilder } from 'typeorm';
import { IsInt, IsPositive } from 'class-validator'; import { IsInt, IsPositive } from 'class-validator';
import { ApiProperty } from '@nestjs/swagger'; import { ApiProperty } from '@nestjs/swagger';
import { reflector } from '../utility/metadata';
export interface PageSettingsWise { export interface PageSettingsWise {
pageCount: number; pageCount: number;
...@@ -16,6 +17,13 @@ export interface QueryWise<T> { ...@@ -16,6 +17,13 @@ export interface QueryWise<T> {
applyQuery(qb: SelectQueryBuilder<T>, entityName: string): void; applyQuery(qb: SelectQueryBuilder<T>, entityName: string): void;
} }
export type QueryCond = <T extends PageSettingsDto>(
obj: T,
qb: SelectQueryBuilder<T>,
entityName: string,
key: keyof T & string,
) => any;
export class PageSettingsDto export class PageSettingsDto
implements PageSettingsWise, PageSettingsFactory, QueryWise<PageSettingsDto> implements PageSettingsWise, PageSettingsFactory, QueryWise<PageSettingsDto>
{ {
...@@ -61,6 +69,13 @@ export class PageSettingsDto ...@@ -61,6 +69,13 @@ export class PageSettingsDto
} }
applyQuery(qb: SelectQueryBuilder<PageSettingsDto>, entityName: string) { applyQuery(qb: SelectQueryBuilder<PageSettingsDto>, entityName: string) {
const queryFields = reflector.getArray('queryConditionFields', this);
for (const field of queryFields) {
const condition = reflector.get('queryCondition', this, field);
if (condition) {
condition(this, qb, entityName, field as keyof PageSettingsDto);
}
}
qb.take(this.getRecordsPerPage()).skip(this.getStartingFrom()); qb.take(this.getRecordsPerPage()).skip(this.getStartingFrom());
} }
} }
import { CreateDateColumn, DeleteDateColumn, UpdateDateColumn } from 'typeorm'; import {
CreateDateColumn,
DeleteDateColumn,
SelectQueryBuilder,
UpdateDateColumn,
} from 'typeorm';
import { NotColumn } from '../decorators'; import { NotColumn } from '../decorators';
import { PageSettingsDto } from './page-settings'; import { PageSettingsDto } from './page-settings';
......
...@@ -3,3 +3,4 @@ export * from './merge'; ...@@ -3,3 +3,4 @@ export * from './merge';
export * from './property'; export * from './property';
export * from './restful'; export * from './restful';
export * from './pipes'; export * from './pipes';
export * from './query';
import { QueryCond } from '../bases';
import { Metadata } from '../utility/metadata';
import {
applyQueryProperty,
applyQueryPropertyLike,
applyQueryPropertySearch,
} from '../utility';
export const QueryCondition = (cond: QueryCond) =>
Metadata.set('queryCondition', cond, 'queryConditionFields');
export const QueryEqual = () => QueryCondition(applyQueryProperty);
export const QueryLike = () => QueryCondition(applyQueryPropertyLike);
export const QuerySearch = () => QueryCondition(applyQueryPropertySearch);
import { MetadataSetter, Reflector } from 'typed-reflector'; import { MetadataSetter, Reflector } from 'typed-reflector';
import { QueryCond } from '../bases';
export interface SpecificFields { interface SpecificFields {
notColumn: boolean; notColumn: boolean;
notWritable: boolean; notWritable: boolean;
notChangeable: boolean; notChangeable: boolean;
} }
type MetadataArrayMap = { [K in keyof SpecificFields as `${K}Fields`]: string };
export const Metadata = new MetadataSetter<SpecificFields, MetadataArrayMap>(); interface MetadataMap extends SpecificFields {
export const reflector = new Reflector<SpecificFields, MetadataArrayMap>(); queryCondition: QueryCond;
}
type FieldsMap = {
[K in keyof MetadataMap as `${K}Fields`]: string;
};
type MetadataArrayMap = FieldsMap;
export const Metadata = new MetadataSetter<MetadataMap, MetadataArrayMap>();
export const reflector = new Reflector<MetadataMap, MetadataArrayMap>();
export function getSpecificFields(obj: any, type: keyof SpecificFields) { export function getSpecificFields(obj: any, type: keyof SpecificFields) {
return reflector return reflector
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment