Add a way to get the identifier name of a model.

This commit is contained in:
Madeorsk 2024-10-05 14:16:15 +02:00
parent 4eb8b7d3bc
commit 0897a5c048
Signed by: Madeorsk
GPG key ID: 677E51CA765BB79F
3 changed files with 18 additions and 3 deletions

View file

@ -19,7 +19,7 @@
</p> </p>
<p align="center"> <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> </p>
## Introduction ## Introduction

View file

@ -1,6 +1,6 @@
{ {
"name": "@sharkitek/core", "name": "@sharkitek/core",
"version": "3.1.0", "version": "3.2.0",
"description": "TypeScript library for well-designed model architectures.", "description": "TypeScript library for well-designed model architectures.",
"keywords": [ "keywords": [
"deserialization", "deserialization",

View file

@ -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"]; 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. * Interface of a Sharkitek model definition.
*/ */
@ -64,6 +69,11 @@ export interface ModelDefinition<Shape extends ModelShape, IdentifierType, Model
*/ */
getIdentifier(): IdentifierType; getIdentifier(): IdentifierType;
/**
* Get model identifier name.
*/
getIdentifierName(): IdentifierNameType<Shape>;
/** /**
* Serialize the model. * Serialize the model.
*/ */
@ -103,7 +113,7 @@ export interface ModelDefinition<Shape extends ModelShape, IdentifierType, Model
* @param shape Model shape definition. * @param shape Model shape definition.
* @param identifier Identifier property name. * @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, shape: Shape,
identifier?: Identifier, identifier?: Identifier,
): ModelClass<ModelType, Shape, Identifier> ): ModelClass<ModelType, Shape, Identifier>
@ -160,6 +170,11 @@ export function model<ModelType extends Model<Shape, IdentifierType<Shape, Ident
return (this as PropertiesModel<Shape>)?.[identifier]; return (this as PropertiesModel<Shape>)?.[identifier];
} }
getIdentifierName(): IdentifierNameType<Shape>
{
return identifier;
}
serialize(): SerializedModel<Shape> serialize(): SerializedModel<Shape>
{ {
// Creating an empty (=> partial) serialized object. // Creating an empty (=> partial) serialized object.