-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat(fieldconfig): decorator is added #76
Merged
Merged
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
51 changes: 51 additions & 0 deletions
51
src/models/common/decorators/fieldConfig/classFieldConfig.decorator.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import 'reflect-metadata'; | ||
import { getFieldConfig, IPropFieldConfigInfo } from './fieldConfig.decorator'; | ||
|
||
const fieldConfigMetadataKey = Symbol('fieldconfigclass'); | ||
type KeyValueDict = Record<string, unknown>; | ||
const target = {}; | ||
|
||
function getFieldConfigs(object: KeyValueDict): IPropFieldConfigInfo[] { | ||
const ret = []; | ||
for (const prop in object) { | ||
const fieldConfigMap = getFieldConfig(object, prop); | ||
if (fieldConfigMap) { | ||
ret.push({ | ||
prop: prop, | ||
...fieldConfigMap, | ||
}); | ||
} | ||
} | ||
return ret; | ||
} | ||
|
||
function getFieldConfigClassesInfo(): IFieldConfigClassInfo[] | undefined { | ||
return Reflect.getMetadata(fieldConfigMetadataKey, target) as IFieldConfigClassInfo[]; | ||
} | ||
|
||
export interface IFieldConfigClassInfo { | ||
name: string; | ||
fields: IPropFieldConfigInfo[]; | ||
} | ||
|
||
export function fieldConfigClass(): ClassDecorator { | ||
// eslint-disable-next-line @typescript-eslint/ban-types | ||
return <TFunction extends Function>(classCtr: TFunction): TFunction => { | ||
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-unsafe-call | ||
const classInstance = new (classCtr as any)(); | ||
cl-shaharshavit-rnd marked this conversation as resolved.
Show resolved
Hide resolved
|
||
const classData: IFieldConfigClassInfo = { | ||
fields: getFieldConfigs(classInstance), | ||
name: classCtr.name, | ||
}; | ||
|
||
const classDataList = getFieldConfigClassesInfo() ?? []; | ||
classDataList.push(classData); | ||
Reflect.defineMetadata(fieldConfigMetadataKey, classDataList, target); | ||
return classCtr; | ||
}; | ||
} | ||
|
||
export function getFieldConfigClassInfo(className: string): IFieldConfigClassInfo { | ||
const classInfos = Reflect.getMetadata(fieldConfigMetadataKey, target) as IFieldConfigClassInfo[]; | ||
return classInfos.find((classInfo) => classInfo.name === className) as IFieldConfigClassInfo; | ||
} |
32 changes: 32 additions & 0 deletions
32
src/models/common/decorators/fieldConfig/fieldConfig.decorator.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import 'reflect-metadata'; | ||
import { IDescribeTsType } from '../../../layerMetadata'; | ||
import { IFieldConfigClassInfo } from './classFieldConfig.decorator'; | ||
|
||
const fieldConfigMetadataKey = Symbol('fieldconfig'); | ||
|
||
export enum FieldCategory { | ||
MAIN = 'MAIN', | ||
GENERAL = 'GENERAL', | ||
GEO_INFO = 'GEO_INFO', | ||
} | ||
|
||
export interface IFieldConfigInfo { | ||
category: FieldCategory; // field category | ||
complexType?: IDescribeTsType; // complex type subfields | ||
subFields?: IFieldConfigClassInfo; // complex type subfields | ||
isManuallyEditable?: boolean; // is field might be edited after creation | ||
isFilterable?: boolean; // is field might participate in filter/search params | ||
isSortable?: boolean; // is field might participate in sorting | ||
} | ||
|
||
export interface IPropFieldConfigInfo extends IFieldConfigInfo { | ||
prop: string; | ||
} | ||
|
||
export function fieldConfig(fieldConfigInfo: IFieldConfigInfo): PropertyDecorator { | ||
return Reflect.metadata(fieldConfigMetadataKey, fieldConfigInfo); | ||
} | ||
|
||
export function getFieldConfig<T>(target: T, propertyKey: string): IFieldConfigInfo | undefined { | ||
return Reflect.getMetadata(fieldConfigMetadataKey, target, propertyKey) as IFieldConfigInfo; | ||
} |
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
export * from './decorators/property/graphql.decorator'; | ||
export * from './decorators/property/classGraphql.decorator'; | ||
export * from '../layerMetadata/link'; | ||
export * from './decorators/graphQL/graphql.decorator'; | ||
export * from './decorators/graphQL/classGraphql.decorator'; | ||
export * from './interfaces/ormCatalog.interface'; | ||
export * from './interfaces/propCatalogDBMapping.interface'; | ||
export * from './interfaces/propGraphQLMapping.interface'; | ||
export * from '../layerMetadata/link'; | ||
export * from './decorators/fieldConfig/fieldConfig.decorator'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I should have thought about it earlier but since target is static and global it can be used to store the data directly without using reflect metadata.
it will make the code bit simpler to understand.
up to you if u want to change it, its ok either way by my standard (for this use case)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done, reflection is removed, more clean code