Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add
@Unique
decorator, support specifying multiple unique ind…
…exes on the same attribute (#15342)
- Loading branch information
Showing
13 changed files
with
281 additions
and
109 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
export type PropertyOrGetterDescriptor = ( | ||
target: Object, | ||
propertyName: string | symbol, | ||
propertyDescriptor?: PropertyDescriptor, | ||
) => void; | ||
|
||
export interface ParameterizedPropertyDecorator<T> { | ||
(options: T): PropertyOrGetterDescriptor; | ||
|
||
(target: Object, propertyName: string | symbol, propertyDescriptor?: PropertyDescriptor): void; | ||
} | ||
|
||
/** | ||
* Makes a decorator that can optionally receive a parameter | ||
* | ||
* @param defaultValue The value to use if no parameter is provided. | ||
* @param callback The callback that will be executed once the decorator is applied. | ||
*/ | ||
export function makeParameterizedPropertyDecorator<T>( | ||
defaultValue: T, | ||
callback: ( | ||
option: T, | ||
target: Object, | ||
propertyName: string | symbol, | ||
propertyDescriptor: PropertyDescriptor | undefined, | ||
) => void, | ||
): ParameterizedPropertyDecorator<T> { | ||
return function decorator(...args: [options: T] | Parameters<PropertyOrGetterDescriptor>) { | ||
if (args.length === 1) { | ||
return function parameterizedDecorator( | ||
target: Object, | ||
propertyName: string | symbol, | ||
propertyDescriptor?: PropertyDescriptor | undefined, | ||
) { | ||
callback(args[0], target, propertyName, propertyDescriptor ?? Object.getOwnPropertyDescriptor(target, propertyName)); | ||
}; | ||
} | ||
|
||
callback(defaultValue, args[0], args[1], args[2] ?? Object.getOwnPropertyDescriptor(args[0], args[1])); | ||
|
||
// this method only returns something if args.length === 1, but typescript doesn't understand it | ||
return undefined as unknown as PropertyOrGetterDescriptor; | ||
}; | ||
} |
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
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.