Add a way to get the identifier name of a model.
This commit is contained in:
parent
4eb8b7d3bc
commit
e373efdd0a
3 changed files with 18 additions and 3 deletions
|
@ -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
|
||||
|
|
|
@ -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",
|
||||
|
|
|
@ -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.
|
||||
|
|
Loading…
Reference in a new issue