-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #54 from dsdavis4/custom_id
Add support for custom id fields
- Loading branch information
Showing
26 changed files
with
842 additions
and
119 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import type DynaRecord from "../../DynaRecord"; | ||
import Metadata from "../../metadata"; | ||
|
||
/** | ||
* An optional decorator for marking class fields as the primary identifier (ID) within the context of a single-table design entity. | ||
* | ||
* By default, if no IdAttribute is specified on an entity, then a uuid is generated for each entity | ||
* | ||
* Use this decorator to specify the field you want to use as the id for each entity. For example, if you wanted a users email to be their id. | ||
* | ||
* This decorator registers the decorated field as the unique identifier for the entity, ensuring proper entity identity management within the ORM. | ||
* | ||
* IMPORTANT! - The ID field is a required field and must be a unique identifier for each entity instance. It cannot be applied to nullable attributes | ||
* | ||
* @template T The entity the decorator is applied to, extending {@link DynaRecord}. | ||
* @template K The type of the field being marked as the ID. | ||
* @param _value This parameter is unused but required for compatibility with the class field decorator structure. | ||
* @param context Provides metadata about the field being decorated, including its name and class. | ||
* @returns A class field decorator function that registers the decorated field as the entity's ID within the ORM metadata system. | ||
* | ||
* Usage example: | ||
* ```typescript | ||
* class User extends TableClass { | ||
* @IdAttribute | ||
* @StringAttribute({ alias: "Email" }) | ||
* public readonly email: string; | ||
* } | ||
* ``` | ||
* | ||
* Here, `@IdAttribute` decorates `email` of `User` as the primary identifier, ensuring it is registered and managed as the entity's unique key. | ||
*/ | ||
function IdAttribute<T extends DynaRecord, K extends string>( | ||
_value: undefined, | ||
context: ClassFieldDecoratorContext<T, K> | ||
) { | ||
return function (this: T, value: K) { | ||
Metadata.addEntityIdField(this.constructor.name, context.name.toString()); | ||
return value; | ||
}; | ||
} | ||
|
||
export default IdAttribute; |
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
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.