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 e373efdd0a
Signed by: Madeorsk
GPG key ID: 677E51CA765BB79F
3 changed files with 18 additions and 3 deletions

View file

@ -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

View file

@ -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",

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"];
/**
* Identifier name type.
*/
export type IdentifierNameType<Shape> = Shape extends ModelShape ? keyof Shape : unknown;
/**
* Interface of a Sharkitek model definition.
*/
@ -64,6 +69,11 @@ export interface ModelDefinition<Shape extends ModelShape, IdentifierType, Model
*/
getIdentifier(): IdentifierType;
/**
* Get model identifier name.
*/
getIdentifierName(): IdentifierNameType<Shape>;
/**
* Serialize the model.
*/
@ -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>
@ -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.