Skip to content

Commit

Permalink
Add a way to get the identifier name of a model.
Browse files Browse the repository at this point in the history
  • Loading branch information
Madeorsk committed Oct 5, 2024
1 parent 4eb8b7d commit e373efd
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 3 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
</p>

<p align="center">
<img alt="Version 3.1.0" src="https://img.shields.io/badge/version-3.1.0-blue" />
<img alt="Version 3.2.0" src="https://img.shields.io/badge/version-3.2.0-blue" />
</p>

## Introduction
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@sharkitek/core",
"version": "3.1.0",
"version": "3.2.0",
"description": "TypeScript library for well-designed model architectures.",
"keywords": [
"deserialization",
Expand Down
17 changes: 16 additions & 1 deletion src/Model/Model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ export type ModelClass<ModelType extends Model<Shape, IdentifierType<Shape, Iden
*/
export type IdentifierType<Shape extends ModelShape, K extends keyof Shape> = Shape[K]["_sharkitek"];

/**
* Identifier name type.
*/
export type IdentifierNameType<Shape> = Shape extends ModelShape ? keyof Shape : unknown;

/**
* Interface of a Sharkitek model definition.
*/
Expand All @@ -64,6 +69,11 @@ export interface ModelDefinition<Shape extends ModelShape, IdentifierType, Model
*/
getIdentifier(): IdentifierType;

/**
* Get model identifier name.
*/
getIdentifierName(): IdentifierNameType<Shape>;

/**
* Serialize the model.
*/
Expand Down Expand Up @@ -103,7 +113,7 @@ export interface ModelDefinition<Shape extends ModelShape, IdentifierType, Model
* @param shape Model shape definition.
* @param identifier Identifier property name.
*/
export function model<ModelType extends Model<Shape, IdentifierType<Shape, Identifier>>, Shape extends ModelShape, Identifier extends keyof Shape = any>(
export function model<ModelType extends Model<Shape, IdentifierType<Shape, Identifier>>, Shape extends ModelShape, Identifier extends IdentifierNameType<Shape> = any>(
shape: Shape,
identifier?: Identifier,
): ModelClass<ModelType, Shape, Identifier>
Expand Down Expand Up @@ -160,6 +170,11 @@ export function model<ModelType extends Model<Shape, IdentifierType<Shape, Ident
return (this as PropertiesModel<Shape>)?.[identifier];
}

getIdentifierName(): IdentifierNameType<Shape>
{
return identifier;
}

serialize(): SerializedModel<Shape>
{
// Creating an empty (=> partial) serialized object.
Expand Down

0 comments on commit e373efd

Please sign in to comment.