Compare commits

..

No commits in common. "main" and "v2.1.1" have entirely different histories.
main ... v2.1.1

19 changed files with 466 additions and 475 deletions

View File

@ -1,6 +1,6 @@
MIT License
Copyright (c) 2024 Zeptotech
Copyright (c) <year> <copyright holders>
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

139
README.md
View File

@ -1,20 +1,25 @@
# Sharkitek Core
![Version 3.0.0](https://img.shields.io/badge/version-3.0.0-blue)
## Introduction
Sharkitek is a Javascript / TypeScript library designed to ease development of client-side models.
With Sharkitek, you define the architecture of your models by specifying their properties and their types.
Then, you can use the defined methods like `serialize`, `deserialize`, `save` or `serializeDiff`.
Then, you can use the defined methods like `serialize`, `deserialize` or `serializeDiff`.
```typescript
class Example extends s.model({
id: s.property.numeric(),
name: s.property.string(),
})
class Example extends Model<Example>
{
id: number;
name: string;
protected SDefinition(): ModelDefinition<Example>
{
return {
id: SDefine(SNumeric),
name: SDefine(SString),
};
}
}
```
@ -26,16 +31,30 @@ class Example extends s.model({
/**
* A person.
*/
class Person extends s.model({
id: s.property.numeric(),
name: s.property.string(),
firstName: s.property.string(),
email: s.property.string(),
createdAt: s.property.date(),
active: s.property.boolean(),
}, "id")
class Person extends Model<Person>
{
id: number;
name: string;
firstName: string;
email: string;
createdAt: Date;
active: boolean = true;
protected SIdentifier(): ModelIdentifier<Person>
{
return "id";
}
protected SDefinition(): ModelDefinition<Person>
{
return {
name: SDefine(SString),
firstName: SDefine(SString),
email: SDefine(SString),
createdAt: SDefine(SDate),
active: SDefine(SBool),
};
}
}
```
@ -43,27 +62,29 @@ class Person extends s.model({
/**
* An article.
*/
class Article extends s.model({
id: s.property.numeric(),
title: s.property.string(),
authors: s.property.array(s.property.model(Author)),
text: s.property.string(),
evaluation: s.property.decimal(),
tags: s.property.array(
s.property.object({
name: s.property.string(),
})
),
}, "id")
class Article extends Model<Article>
{
id: number;
title: string;
authors: Author[] = [];
text: string;
evaluation: number;
tags: {
name: string;
}[];
protected SIdentifier(): ModelIdentifier<Article>
{
return "id";
}
protected SDefinition(): ModelDefinition<Article>
{
return {
id: SDefine(SNumeric),
title: SDefine(SString),
authors: SDefine(SArray(SModel(Author))),
text: SDefine(SString),
evaluation: SDefine(SDecimal),
};
}
}
```
@ -81,42 +102,53 @@ Sharkitek defines some basic types by default, in these classes:
- `DecimalType`: number in the model, formatted string in the serialized object.
- `DateType`: date in the model, ISO formatted date in the serialized object.
- `ArrayType`: array in the model, array in the serialized object.
- `ObjectType`: object in the model, object in the serialized object.
- `ModelType`: instance of a specific class in the model, object in the serialized object.
When you are defining a property of a Sharkitek model, you must provide its type by instantiating one of these classes.
When you are defining a Sharkitek property, you must provide its type by instantiating one of these classes.
```typescript
class Example extends s.model({
foo: s.property.define(new StringType()),
})
class Example extends Model<Example>
{
foo: string;
protected SDefinition(): ModelDefinition<Example>
{
return {
foo: new Definition(new StringType()),
};
}
}
```
To ease the use of these classes and reduce read complexity,
properties of each type are easily definable with a function for each type.
To ease the use of these classes and reduce read complexity, some constant variables and functions are defined in the library,
following a certain naming convention: "S{type_name}".
- `BoolType` => `s.property.boolean`
- `StringType` => `s.property.string`
- `NumericType` => `s.property.numeric`
- `DecimalType` => `s.property.decimal`
- `DateType` => `s.property.date`
- `ArrayType` => `s.property.array`
- `ObjectType` => `s.property.object`
- `ModelType` => `s.property.model`
- `BoolType` => `SBool`
- `StringType` => `SString`
- `NumericType` => `SNumeric`
- `DecimalType` => `SDecimal`
- `DateType` => `SDate`
- `ArrayType` => `SArray`
- `ModelType` => `SModel`
Type implementers should provide a corresponding function for each defined type. They can even provide
multiple functions or constants with predefined parameters.
(For example, we could define `s.property.stringArray()` which would be similar to `s.property.array(s.property.string())`.)
When the types require parameters, the constant is defined as a function. If there is no parameter, then a simple
variable is enough.
Type implementers should provide a corresponding variable or function for each defined type. They can even provide
multiple functions or constants when predefined parameters. (For example, we could define `SStringArray` which would
be a variable similar to `SArray(SString)`.)
```typescript
class Example extends s.model({
foo: s.property.string(),
})
class Example extends Model<Example>
{
foo: string;
foo: string = undefined;
protected SDefinition(): ModelDefinition<Example>
{
return {
foo: SDefine(SString),
};
}
}
```
@ -176,6 +208,7 @@ const result = model.serializeDiff();
// result = { id: 5, title: "A new title for a new world" }
// if `id` is not defined as the model identifier:
// result = { title: "A new title for a new world" }
```
#### `resetDiff()`
@ -205,6 +238,7 @@ const result = model.serializeDiff();
// result = { id: 5 }
// if `id` is not defined as the model identifier:
// result = {}
```
#### `save()`
@ -231,4 +265,5 @@ const result = model.save();
// result = { id: 5, title: "A new title for a new world" }
// if `id` is not defined as the model identifier:
// result = { title: "A new title for a new world" }
```

View File

@ -1,6 +1,6 @@
{
"name": "@sharkitek/core",
"version": "3.0.0",
"version": "2.1.1",
"description": "Sharkitek core models library.",
"keywords": [
"sharkitek",

34
src/Model/Definition.ts Normal file
View File

@ -0,0 +1,34 @@
import {Type} from "./Types/Type";
/**
* Options of a definition.
*/
export interface DefinitionOptions<SerializedType, SharkitekType>
{ //TODO implement some options, like `mandatory`.
}
/**
* A Sharkitek model property definition.
*/
export class Definition<SerializedType, SharkitekType>
{
/**
* Initialize a property definition with the given type and options.
* @param type - The model property type.
* @param options - Property definition options.
*/
constructor(
public type: Type<SerializedType, SharkitekType>,
public options: DefinitionOptions<SerializedType, SharkitekType> = {},
) {}
}
/**
* Initialize a property definition with the given type and options.
* @param type - The model property type.
* @param options - Property definition options.
*/
export function SDefine<SerializedType, SharkitekType>(type: Type<SerializedType, SharkitekType>, options: DefinitionOptions<SerializedType, SharkitekType> = {}): Definition<SerializedType, SharkitekType>
{
return new Definition<SerializedType, SharkitekType>(type, options);
}

View File

@ -1,231 +1,217 @@
import {Definition} from "./PropertyDefinition";
import {Definition} from "./Definition";
/**
* Type definition of a model constructor.
* Model properties definition type.
*/
export type ConstructorOf<T extends object> = { new(): T; };
export type ModelDefinition<T> = Partial<Record<keyof T, Definition<unknown, unknown>>>;
/**
* Model identifier type.
*/
export type ModelIdentifier<T> = keyof T;
/**
* Unknown property definition.
* A Sharkitek model.
*/
export type UnknownDefinition = Definition<unknown, unknown>;
/**
* A model shape.
*/
export type ModelShape = Record<string, UnknownDefinition>;
/**
* Properties of a model based on its shape.
*/
export type PropertiesModel<Shape extends ModelShape> = {
[k in keyof Shape]: Shape[k]["_sharkitek"];
};
/**
* Serialized object type based on model shape.
*/
export type SerializedModel<Shape extends ModelShape> = {
[k in keyof Shape]: Shape[k]["_serialized"];
};
/**
* Type of a model object.
*/
export type Model<Shape extends ModelShape, IdentifierType = unknown> = ModelDefinition<Shape, IdentifierType> & PropertiesModel<Shape>;
/**
* Identifier type.
*/
export type IdentifierType<Shape extends ModelShape, K extends keyof Shape> = Shape[K]["_sharkitek"];
/**
* Interface of a Sharkitek model definition.
*/
export interface ModelDefinition<Shape extends ModelShape, IdentifierType>
export abstract class Model<THIS>
{
/**
* Model properties definition function.
*/
protected abstract SDefinition(): ModelDefinition<THIS>;
/**
* Return the name of the model identifier property.
*/
protected SIdentifier(): ModelIdentifier<THIS>
{
return undefined;
}
/**
* Get given property definition.
* @protected
*/
protected getPropertyDefinition(propertyName: string): Definition<unknown, unknown>
{
return (this.SDefinition() as any)?.[propertyName];
}
/**
* Get the list of the model properties.
* @protected
*/
protected getProperties(): string[]
{
return Object.keys(this.SDefinition());
}
/**
* Calling a function for a defined property.
* @param propertyName - The property for which to check definition.
* @param callback - The function called when the property is defined.
* @param notProperty - The function called when the property is not defined.
* @protected
*/
protected propertyWithDefinition(propertyName: string, callback: (propertyDefinition: Definition<unknown, unknown>) => void, notProperty: () => void = () => {}): unknown
{
// Getting the current property definition.
const propertyDefinition = this.getPropertyDefinition(propertyName);
if (propertyDefinition)
// There is a definition for the current property, calling the right callback.
return callback(propertyDefinition);
else
// No definition for the given property, calling the right callback.
return notProperty();
}
/**
* Calling a function for each defined property.
* @param callback - The function to call.
* @protected
*/
protected forEachModelProperty(callback: (propertyName: string, propertyDefinition: Definition<unknown, unknown>) => unknown): any|void
{
for (const propertyName of this.getProperties())
{ // For each property, checking that its type is defined and calling the callback with its type.
const result = this.propertyWithDefinition(propertyName, (propertyDefinition) => {
// If the property is defined, calling the function with the property name and definition.
const result = callback(propertyName, propertyDefinition);
// If there is a return value, returning it directly (loop is broken).
if (typeof result !== "undefined") return result;
});
// If there is a return value, returning it directly (loop is broken).
if (typeof result !== "undefined") return result;
}
}
/**
* The original properties values.
* @protected
*/
protected _originalProperties: Record<string, any> = {};
/**
* The original (serialized) object.
* @protected
*/
protected _originalObject: any = null;
/**
* Determine if the model is new or not.
*/
isNew(): boolean
{
return !this._originalObject;
}
/**
* Determine if the model is dirty or not.
*/
isDirty(): boolean
{
return this.forEachModelProperty((propertyName, propertyDefinition) => (
// For each property, checking if it is different.
propertyDefinition.type.propertyHasChanged(this._originalProperties[propertyName], (this as any)[propertyName])
// There is a difference, we should return false.
? true
// There is no difference, returning nothing.
: undefined
)) === true;
}
/**
* Get model identifier.
*/
getIdentifier(): IdentifierType;
getIdentifier(): unknown
{
return (this as any)[this.SIdentifier()];
}
/**
* Serialize the model.
*/
serialize(): SerializedModel<Shape>;
/**
* Deserialize the model.
* @param obj Serialized object.
*/
deserialize(obj: SerializedModel<Shape>): Model<Shape, IdentifierType>;
/**
* Find out if the model is new (never deserialized) or not.
*/
isNew(): boolean;
/**
* Find out if the model is dirty or not.
*/
isDirty(): boolean;
/**
* Serialize the difference between current model state and the original one.
*/
serializeDiff(): Partial<SerializedModel<Shape>>;
/**
* Set current properties values as original values.
*/
resetDiff(): void;
resetDiff()
{
this.forEachModelProperty((propertyName, propertyDefinition) => {
// For each property, set its original value to its current property value.
this._originalProperties[propertyName] = (this as any)[propertyName];
propertyDefinition.type.resetDiff((this as any)[propertyName]);
});
}
/**
* Serialize the difference between current model state and original one.
*/
serializeDiff(): any
{
// Creating a serialized object.
const serializedDiff: any = {};
this.forEachModelProperty((propertyName, propertyDefinition) => {
// For each defined model property, adding it to the serialized object if it has changed.
if (this.SIdentifier() == propertyName
|| propertyDefinition.type.propertyHasChanged(this._originalProperties[propertyName], (this as any)[propertyName]))
// Adding the current property to the serialized object if it is the identifier or its value has changed.
serializedDiff[propertyName] = propertyDefinition.type.serializeDiff((this as any)[propertyName]);
})
return serializedDiff; // Returning the serialized object.
}
/**
* Get difference between original values and current ones, then reset it.
* Similar to call `serializeDiff()` then `resetDiff()`.
*/
save(): Partial<SerializedModel<Shape>>;
}
/**
* Define a Sharkitek model.
* @param shape Model shape definition.
* @param identifier Identifier property name.
*/
export function model<Shape extends ModelShape, Identifier extends keyof Shape = any>(
shape: Shape,
identifier?: Identifier,
): ConstructorOf<Model<Shape, IdentifierType<Shape, Identifier>>>
{
// Get shape entries.
const shapeEntries = Object.entries(shape) as [keyof Shape, UnknownDefinition][];
return class GenericModel implements ModelDefinition<Shape, IdentifierType<Shape, Identifier>>
save(): any
{
constructor()
{
// Initialize properties to undefined.
Object.assign(this,
// Build empty properties model from shape entries.
Object.fromEntries(shapeEntries.map(([key]) => [key, undefined])) as PropertiesModel<Shape>
);
}
// Get the difference.
const diff = this.serializeDiff();
/**
* Calling a function for each defined property.
* @param callback - The function to call.
* @protected
*/
protected forEachModelProperty<ReturnType>(callback: (propertyName: keyof Shape, propertyDefinition: UnknownDefinition) => ReturnType): ReturnType
{
for (const [propertyName, propertyDefinition] of shapeEntries)
{ // For each property, checking that its type is defined and calling the callback with its type.
// If the property is defined, calling the function with the property name and definition.
const result = callback(propertyName, propertyDefinition);
// If there is a return value, returning it directly (loop is broken).
if (typeof result !== "undefined") return result;
}
}
// Once the difference has been gotten, reset it.
this.resetDiff();
return diff; // Return the difference.
}
/**
* The original properties values.
* @protected
*/
protected _originalProperties: Partial<PropertiesModel<Shape>> = {};
/**
* Serialize the model.
*/
serialize(): any
{
// Creating a serialized object.
const serializedObject: any = {};
/**
* The original (serialized) object.
* @protected
*/
protected _originalObject: SerializedModel<Shape>|null = null;
this.forEachModelProperty((propertyName, propertyDefinition) => {
// For each defined model property, adding it to the serialized object.
serializedObject[propertyName] = propertyDefinition.type.serialize((this as any)[propertyName]);
});
return serializedObject; // Returning the serialized object.
}
/**
* Special operations on parse.
* @protected
*/
protected parse(): void
{} // Nothing by default. TODO: create an event system to create functions like "beforeDeserialization" or "afterDeserialization".
getIdentifier(): IdentifierType<Shape, Identifier>
{
return (this as PropertiesModel<Shape>)?.[identifier];
}
/**
* Deserialize the model.
*/
deserialize(serializedObject: any): THIS
{
this.forEachModelProperty((propertyName, propertyDefinition) => {
// For each defined model property, assigning its deserialized value to the model.
(this as any)[propertyName] = propertyDefinition.type.deserialize(serializedObject[propertyName]);
});
serialize(): SerializedModel<Shape>
{
// Creating an empty (=> partial) serialized object.
const serializedObject: Partial<SerializedModel<Shape>> = {};
// Reset original property values.
this.resetDiff();
this.forEachModelProperty((propertyName, propertyDefinition) => {
// For each defined model property, adding it to the serialized object.
serializedObject[propertyName] = propertyDefinition.type.serialize((this as PropertiesModel<Shape>)?.[propertyName]);
});
this._originalObject = serializedObject; // The model is not a new one, but loaded from a deserialized one.
return serializedObject as SerializedModel<Shape>; // Returning the serialized object.
}
deserialize(obj: SerializedModel<Shape>): Model<Shape, IdentifierType<Shape, Identifier>>
{
this.forEachModelProperty((propertyName, propertyDefinition) => {
// For each defined model property, assigning its deserialized value.
(this as PropertiesModel<Shape>)[propertyName] = propertyDefinition.type.deserialize(obj[propertyName]);
});
// Reset original property values.
this.resetDiff();
this._originalObject = obj; // The model is not a new one, but loaded from a deserialized one. Storing it.
return this as Model<Shape, IdentifierType<Shape, Identifier>>;
}
isNew(): boolean
{
return !this._originalObject;
}
isDirty(): boolean
{
return this.forEachModelProperty((propertyName, propertyDefinition) => (
// For each property, checking if it is different.
propertyDefinition.type.propertyHasChanged(this._originalProperties[propertyName], (this as PropertiesModel<Shape>)[propertyName])
// There is a difference, we should return false.
? true
// There is no difference, returning nothing.
: undefined
)) === true;
}
serializeDiff(): Partial<SerializedModel<Shape>>
{
// Creating an empty (=> partial) serialized object.
const serializedObject: Partial<SerializedModel<Shape>> = {};
this.forEachModelProperty((propertyName, propertyDefinition) => {
// For each defined model property, adding it to the serialized object if it has changed or if it is the identifier.
if (
identifier == propertyName ||
propertyDefinition.type.propertyHasChanged(this._originalProperties[propertyName], (this as PropertiesModel<Shape>)[propertyName])
) // Adding the current property to the serialized object if it is the identifier or its value has changed.
serializedObject[propertyName] = propertyDefinition.type.serializeDiff((this as PropertiesModel<Shape>)?.[propertyName]);
});
return serializedObject; // Returning the serialized object.
}
resetDiff(): void
{
this.forEachModelProperty((propertyName, propertyDefinition) => {
// For each property, set its original value to its current property value.
this._originalProperties[propertyName] = (this as PropertiesModel<Shape>)[propertyName];
propertyDefinition.type.resetDiff((this as PropertiesModel<Shape>)[propertyName]);
});
}
save(): Partial<SerializedModel<Shape>>
{
// Get the difference.
const diff = this.serializeDiff();
// Once the difference has been obtained, reset it.
this.resetDiff();
return diff; // Return the difference.
}
} as unknown as ConstructorOf<Model<Shape, IdentifierType<Shape, Identifier>>>;
return this as unknown as THIS; // Returning this, after deserialization.
}
}

View File

@ -1,10 +0,0 @@
export {define} from "./PropertyDefinition";
export {array} from "./Types/ArrayType";
export {bool, boolean} from "./Types/BoolType";
export {date} from "./Types/DateType";
export {decimal} from "./Types/DecimalType";
export {model} from "./Types/ModelType";
export {numeric} from "./Types/NumericType";
export {object} from "./Types/ObjectType";
export {string} from "./Types/StringType";

View File

@ -1,26 +0,0 @@
import {Type} from "./Types/Type";
/**
* Property definition class.
*/
export class Definition<SerializedType, ModelType>
{
readonly _sharkitek: ModelType;
readonly _serialized: SerializedType;
/**
* Create a property definer instance.
* @param type Property type.
*/
constructor(public readonly type: Type<SerializedType, ModelType>)
{}
}
/**
* New definition of a property of the given type.
* @param type Type of the property to define.
*/
export function define<SerializedType, ModelType>(type: Type<SerializedType, ModelType>): Definition<SerializedType, ModelType>
{
return new Definition(type);
}

View File

@ -1,5 +1,4 @@
import {Type} from "./Type";
import {define, Definition} from "../PropertyDefinition";
/**
* Type of an array of values.
@ -7,60 +6,60 @@ import {define, Definition} from "../PropertyDefinition";
export class ArrayType<SerializedValueType, SharkitekValueType> extends Type<SerializedValueType[], SharkitekValueType[]>
{
/**
* Initialize a new array type of a Sharkitek model property.
* @param valueDefinition Definition the array values.
* Constructs a new array type of a Sharkitek model property.
* @param valueType - Type of the array values.
*/
constructor(protected valueDefinition: Definition<SerializedValueType, SharkitekValueType>)
constructor(protected valueType: Type<SerializedValueType, SharkitekValueType>)
{
super();
}
serialize(value: SharkitekValueType[]|null|undefined): SerializedValueType[]|null|undefined
serialize(value: SharkitekValueType[]): SerializedValueType[]
{
if (value === undefined) return undefined;
if (value === null) return null;
return value.map((value) => (
// Serializing each value of the array.
this.valueDefinition.type.serialize(value)
this.valueType.serialize(value)
));
}
deserialize(value: SerializedValueType[]|null|undefined): SharkitekValueType[]|null|undefined
deserialize(value: SerializedValueType[]): SharkitekValueType[]
{
if (value === undefined) return undefined;
if (value === null) return null;
return value.map((serializedValue) => (
// Deserializing each value of the array.
this.valueDefinition.type.deserialize(serializedValue)
this.valueType.deserialize(serializedValue)
));
}
serializeDiff(value: SharkitekValueType[]|null|undefined): SerializedValueType[]|null|undefined
serializeDiff(value: SharkitekValueType[]): any
{
if (value === undefined) return undefined;
if (value === null) return null;
// Serializing diff of all elements.
return value.map((value) => this.valueDefinition.type.serializeDiff(value) as SerializedValueType);
return value.map((value) => this.valueType.serializeDiff(value));
}
resetDiff(value: SharkitekValueType[]|null|undefined): void
resetDiff(value: SharkitekValueType[]): void
{
// Do nothing if it is not an array.
if (!Array.isArray(value)) return;
// Reset diff of all elements.
value.forEach((value) => this.valueDefinition.type.resetDiff(value));
value.forEach((value) => this.valueType.resetDiff(value));
}
}
/**
* New array property definition.
* @param valueDefinition Array values type definition.
* Type of an array of values.
* @param valueType - Type of the array values.
*/
export function array<SerializedValueType, SharkitekValueType>(valueDefinition: Definition<SerializedValueType, SharkitekValueType>): Definition<SerializedValueType[], SharkitekValueType[]>
export function SArray<SerializedValueType, SharkitekValueType>(valueType: Type<SerializedValueType, SharkitekValueType>)
{
return define(new ArrayType(valueDefinition));
return new ArrayType<SerializedValueType, SharkitekValueType>(valueType);
}

View File

@ -1,42 +1,22 @@
import {Type} from "./Type";
import {define, Definition} from "../PropertyDefinition";
/**
* Type of any boolean value.
*/
export class BoolType extends Type<boolean, boolean>
{
deserialize(value: boolean|null|undefined): boolean|null|undefined
deserialize(value: boolean): boolean
{
// Keep NULL and undefined values.
if (value === undefined) return undefined;
if (value === null) return null;
return !!value; // ensure bool type.
}
serialize(value: boolean|null|undefined): boolean|null|undefined
serialize(value: boolean): boolean
{
// Keep NULL and undefined values.
if (value === undefined) return undefined;
if (value === null) return null;
return !!value; // ensure bool type.
}
}
/**
* New boolean property definition.
* Type of any boolean value.
*/
export function bool(): Definition<boolean, boolean>
{
return define(new BoolType());
}
/**
* New boolean property definition.
* Alias of bool.
*/
export function boolean(): ReturnType<typeof bool>
{
return bool();
}
export const SBool = new BoolType();

View File

@ -1,32 +1,22 @@
import {Type} from "./Type";
import {define, Definition} from "../PropertyDefinition";
/**
* Type of dates.
*/
export class DateType extends Type<string, Date>
{
deserialize(value: string|null|undefined): Date|null|undefined
deserialize(value: string): Date
{
if (value === undefined) return undefined;
if (value === null) return null;
return new Date(value);
}
serialize(value: Date|null|undefined): string|null|undefined
serialize(value: Date): string
{
if (value === undefined) return undefined;
if (value === null) return null;
return value?.toISOString();
return value.toISOString();
}
}
/**
* New date property definition.
* Type of dates.
*/
export function date(): Definition<string, Date>
{
return define(new DateType());
}
export const SDate = new DateType();

View File

@ -1,32 +1,22 @@
import {Type} from "./Type";
import {define, Definition} from "../PropertyDefinition";
/**
* Type of decimal numbers.
*/
export class DecimalType extends Type<string, number>
{
deserialize(value: string|null|undefined): number|null|undefined
deserialize(value: string): number
{
if (value === undefined) return undefined;
if (value === null) return null;
return parseFloat(value);
}
serialize(value: number|null|undefined): string|null|undefined
serialize(value: number): string
{
if (value === undefined) return undefined;
if (value === null) return null;
return value?.toString();
return value.toString();
}
}
/**
* New decimal property definition.
* Type of decimal numbers.
*/
export function decimal(): Definition<string, number>
{
return define(new DecimalType());
}
export const SDecimal = new DecimalType();

View File

@ -1,49 +1,44 @@
import {Type} from "./Type";
import {define, Definition} from "../PropertyDefinition";
import {ConstructorOf, Model, ModelShape, SerializedModel} from "../Model";
import {Model} from "../Model";
/**
* Type definition of the constructor of a specific type.
*/
export type ConstructorOf<T> = { new(): T; }
/**
* Type of a Sharkitek model value.
*/
export class ModelType<Shape extends ModelShape> extends Type<SerializedModel<Shape>, Model<Shape>>
export class ModelType<M extends Model<M>> extends Type<any, M>
{
/**
* Initialize a new model type of a Sharkitek model property.
* @param modelConstructor Model constructor.
* Constructs a new model type of a Sharkitek model property.
* @param modelConstructor - Constructor of the model.
*/
constructor(protected modelConstructor: ConstructorOf<Model<Shape>>)
constructor(protected modelConstructor: ConstructorOf<M>)
{
super();
}
serialize(value: Model<Shape>|null|undefined): SerializedModel<Shape>|null|undefined
serialize(value: M|null): any
{
if (value === undefined) return undefined;
if (value === null) return null;
// Serializing the given model.
return value?.serialize();
return value ? value.serialize() : null;
}
deserialize(value: SerializedModel<Shape>|null|undefined): Model<Shape>|null|undefined
deserialize(value: any): M|null
{
if (value === undefined) return undefined;
if (value === null) return null;
// Deserializing the given object in the new model.
return (new this.modelConstructor()).deserialize(value) as Model<Shape>;
return value ? (new this.modelConstructor()).deserialize(value) : null;
}
serializeDiff(value: Model<Shape>|null|undefined): Partial<SerializedModel<Shape>>|null|undefined
serializeDiff(value: M): any
{
if (value === undefined) return undefined;
if (value === null) return null;
// Serializing the given model.
return value?.serializeDiff();
return value ? value.serializeDiff() : null;
}
resetDiff(value: Model<Shape>|null|undefined): void
resetDiff(value: M): void
{
// Reset diff of the given model.
value?.resetDiff();
@ -51,10 +46,10 @@ export class ModelType<Shape extends ModelShape> extends Type<SerializedModel<Sh
}
/**
* New model property definition.
* @param modelConstructor Model constructor.
* Type of a Sharkitek model value.
* @param modelConstructor - Constructor of the model.
*/
export function model<Shape extends ModelShape>(modelConstructor: ConstructorOf<Model<Shape>>): Definition<SerializedModel<Shape>, Model<Shape>>
export function SModel<M extends Model<M>>(modelConstructor: ConstructorOf<M>)
{
return define(new ModelType(modelConstructor));
return new ModelType(modelConstructor);
}

View File

@ -1,26 +1,22 @@
import {Type} from "./Type";
import {define, Definition} from "../PropertyDefinition";
/**
* Type of any numeric value.
*/
export class NumericType extends Type<number, number>
{
deserialize(value: number|null|undefined): number|null|undefined
deserialize(value: number): number
{
return value;
}
serialize(value: number|null|undefined): number|null|undefined
serialize(value: number): number
{
return value;
}
}
/**
* New numeric property definition.
* Type of any numeric value.
*/
export function numeric(): Definition<number, number>
{
return define(new NumericType());
}
export const SNumeric = new NumericType();

View File

@ -1,67 +1,66 @@
import {Type} from "./Type";
import {define, Definition} from "../PropertyDefinition";
import {ModelShape, PropertiesModel, SerializedModel, UnknownDefinition} from "../Model";
import {Definition} from "../Definition";
/**
* Type of a custom object.
* Type of a simple object.
*/
export class ObjectType<Shape extends ModelShape> extends Type<SerializedModel<Shape>, PropertiesModel<Shape>>
export class ObjectType<Keys extends symbol|string> extends Type<Record<Keys, any>, Record<Keys, any>>
{
/**
* Initialize a new object type of a Sharkitek model property.
* @param shape
* Constructs a new object type of a Sharkitek model property.
* @param fieldsTypes Object fields types.
*/
constructor(protected readonly shape: Shape)
constructor(protected fieldsTypes: Record<Keys, Definition<unknown, unknown>>)
{
super();
}
deserialize(value: SerializedModel<Shape>|null|undefined): PropertiesModel<Shape>|null|undefined
deserialize(value: Record<Keys, any>): Record<Keys, any>
{
if (value === undefined) return undefined;
if (value === null) return null;
return Object.fromEntries(
// For each defined field, deserialize its value according to its type.
(Object.entries(this.shape) as [keyof Shape, UnknownDefinition][]).map(([fieldName, fieldDefinition]) => (
(Object.entries(this.fieldsTypes) as [Keys, Definition<any, any>][]).map(([fieldName, fieldDefinition]) => (
// Return an entry with the current field name and the deserialized value.
[fieldName, fieldDefinition.type.deserialize(value?.[fieldName])]
))
) as PropertiesModel<Shape>;
) as Record<Keys, any>;
}
serialize(value: PropertiesModel<Shape>|null|undefined): SerializedModel<Shape>|null|undefined
serialize(value: Record<Keys, any>): Record<Keys, any>
{
if (value === undefined) return undefined;
if (value === null) return null;
return Object.fromEntries(
// For each defined field, serialize its value according to its type.
(Object.entries(this.shape) as [keyof Shape, UnknownDefinition][]).map(([fieldName, fieldDefinition]) => (
(Object.entries(this.fieldsTypes) as [Keys, Definition<any, any>][]).map(([fieldName, fieldDefinition]) => (
// Return an entry with the current field name and the serialized value.
[fieldName, fieldDefinition.type.serialize(value?.[fieldName])]
))
) as PropertiesModel<Shape>;
) as Record<Keys, any>;
}
serializeDiff(value: PropertiesModel<Shape>|null|undefined): Partial<SerializedModel<Shape>>|null|undefined
serializeDiff(value: Record<Keys, any>): Record<Keys, any>
{
if (value === undefined) return undefined;
if (value === null) return null;
return Object.fromEntries(
// For each defined field, serialize its diff value according to its type.
(Object.entries(this.shape) as [keyof Shape, UnknownDefinition][]).map(([fieldName, fieldDefinition]) => (
(Object.entries(this.fieldsTypes) as [Keys, Definition<any, any>][]).map(([fieldName, fieldDefinition]) => (
// Return an entry with the current field name and the serialized diff value.
[fieldName, fieldDefinition.type.serializeDiff(value?.[fieldName])]
))
) as PropertiesModel<Shape>;
) as Record<Keys, any>;
}
resetDiff(value: PropertiesModel<Shape>|null|undefined)
resetDiff(value: Record<Keys, any>): void
{
// For each field, reset its diff.
(Object.entries(this.shape) as [keyof Shape, UnknownDefinition][]).forEach(([fieldName, fieldDefinition]) => {
(Object.entries(this.fieldsTypes) as [Keys, Definition<any, any>][]).forEach(([fieldName, fieldDefinition]) => {
// Reset diff of the current field.
fieldDefinition.type.resetDiff(value?.[fieldName]);
});
@ -69,10 +68,10 @@ export class ObjectType<Shape extends ModelShape> extends Type<SerializedModel<S
}
/**
* New object property definition.
* @param shape Shape of the object.
* Type of a simple object.
* @param fieldsTypes Object fields types.
*/
export function object<Shape extends ModelShape>(shape: Shape): Definition<SerializedModel<Shape>, PropertiesModel<Shape>>
export function SObject<Keys extends symbol|string>(fieldsTypes: Record<Keys, Definition<unknown, unknown>>): ObjectType<Keys>
{
return define(new ObjectType(shape));
return new ObjectType<Keys>(fieldsTypes);
}

View File

@ -1,26 +1,22 @@
import {Type} from "./Type";
import {define, Definition} from "../PropertyDefinition";
/**
* Type of any string value.
*/
export class StringType extends Type<string, string>
{
deserialize(value: string|null|undefined): string|null|undefined
deserialize(value: string): string
{
return value;
}
serialize(value: string|null|undefined): string|null|undefined
serialize(value: string): string
{
return value;
}
}
/**
* New string property definition.
* Type of any string value.
*/
export function string(): Definition<string, string>
{
return define(new StringType());
}
export const SString = new StringType();

View File

@ -1,25 +1,25 @@
/**
* Abstract class of a Sharkitek model property type.
*/
export abstract class Type<SerializedType, ModelType>
export abstract class Type<SerializedType, SharkitekType>
{
/**
* Serialize the given value of a Sharkitek model property.
* @param value Value to serialize.
* @param value - Value to serialize.
*/
abstract serialize(value: ModelType|null|undefined): SerializedType|null|undefined;
abstract serialize(value: SharkitekType): SerializedType;
/**
* Deserialize the given value of a serialized Sharkitek model.
* @param value - Value to deserialize.
*/
abstract deserialize(value: SerializedType|null|undefined): ModelType|null|undefined;
abstract deserialize(value: SerializedType): SharkitekType;
/**
* Serialize the given value only if it has changed.
* @param value - Value to deserialize.
*/
serializeDiff(value: ModelType|null|undefined): Partial<SerializedType>|null|undefined
serializeDiff(value: SharkitekType): SerializedType|null
{
return this.serialize(value); // By default, nothing changes.
}
@ -28,7 +28,7 @@ export abstract class Type<SerializedType, ModelType>
* Reset the difference between the original value and the current one.
* @param value - Value for which reset diff data.
*/
resetDiff(value: ModelType|null|undefined): void
resetDiff(value: SharkitekType): void
{
// By default, nothing to do.
}
@ -38,7 +38,7 @@ export abstract class Type<SerializedType, ModelType>
* @param originalValue - Original property value.
* @param currentValue - Current property value.
*/
propertyHasChanged(originalValue: ModelType|null|undefined, currentValue: ModelType|null|undefined): boolean
propertyHasChanged(originalValue: SharkitekType, currentValue: SharkitekType): boolean
{
return originalValue != currentValue;
}
@ -48,7 +48,7 @@ export abstract class Type<SerializedType, ModelType>
* @param originalValue - Original serialized property value.
* @param currentValue - Current serialized property value.
*/
serializedPropertyHasChanged(originalValue: SerializedType|null|undefined, currentValue: SerializedType|null|undefined): boolean
serializedPropertyHasChanged(originalValue: SerializedType, currentValue: SerializedType): boolean
{
return originalValue != currentValue;
}

View File

@ -1,14 +0,0 @@
import * as property from "./Properties";
export { property };
export * from "./Model";
export {Definition} from "./PropertyDefinition";
export {ArrayType} from "./Types/ArrayType";
export {BoolType} from "./Types/BoolType";
export {DateType} from "./Types/DateType";
export {DecimalType} from "./Types/DecimalType";
export {ModelType} from "./Types/ModelType";
export {NumericType} from "./Types/NumericType";
export {ObjectType} from "./Types/ObjectType";
export {StringType} from "./Types/StringType";

View File

@ -1,4 +1,14 @@
import * as s from "./Model";
export * from "./Model";
export { s };
export default s;
export * from "./Model/Model";
export * from "./Model/Definition";
export * from "./Model/Types/Type";
export * from "./Model/Types/ArrayType";
export * from "./Model/Types/BoolType";
export * from "./Model/Types/DateType";
export * from "./Model/Types/DecimalType";
export * from "./Model/Types/ModelType";
export * from "./Model/Types/NumericType";
export * from "./Model/Types/ObjectType";
export * from "./Model/Types/StringType";

View File

@ -1,18 +1,39 @@
import {s} from "../src";
import {
SArray,
SDecimal,
SModel,
SNumeric,
SString,
SDate,
SBool,
Model,
ModelDefinition,
SDefine, ModelIdentifier
} from "../src";
import {SObject} from "../src/Model/Types/ObjectType";
/**
* Another test model.
*/
class Author extends s.model({
name: s.property.string(),
firstName: s.property.string(),
email: s.property.string(),
createdAt: s.property.date(),
active: s.property.bool(),
})
class Author extends Model<Author>
{
name: string;
firstName: string;
email: string;
createdAt: Date;
active: boolean = true;
protected SDefinition(): ModelDefinition<Author>
{
return {
name: SDefine(SString),
firstName: SDefine(SString),
email: SDefine(SString),
createdAt: SDefine(SDate),
active: SDefine(SBool),
};
}
constructor(name: string = "", firstName: string = "", email: string = "", createdAt: Date = new Date())
{
super();
@ -27,18 +48,7 @@ class Author extends s.model({
/**
* A test model.
*/
class Article extends s.model({
id: s.property.numeric(),
title: s.property.string(),
authors: s.property.array(s.property.model(Author)),
text: s.property.string(),
evaluation: s.property.decimal(),
tags: s.property.array(
s.property.object({
name: s.property.string(),
})
),
}, "id")
class Article extends Model<Article>
{
id: number;
title: string;
@ -48,6 +58,27 @@ class Article extends s.model({
tags: {
name: string;
}[];
protected SIdentifier(): ModelIdentifier<Article>
{
return "id";
}
protected SDefinition(): ModelDefinition<Article>
{
return {
id: SDefine(SNumeric),
title: SDefine(SString),
authors: SDefine(SArray(SModel(Author))),
text: SDefine(SString),
evaluation: SDefine(SDecimal),
tags: SDefine(SArray(
SObject({
name: SDefine(SString),
})
)),
};
}
}
it("deserialize", () => {
@ -109,8 +140,8 @@ it("deserialize then save", () => {
id: 1,
title: "this is a test",
authors: [
{ name: "DOE", firstName: "John", email: "test@test.test", createdAt: (new Date()).toISOString(), active: true, },
{ name: "TEST", firstName: "Another", email: "another@test.test", createdAt: (new Date()).toISOString(), active: false, },
{ name: "DOE", firstName: "John", email: "test@test.test", createdAt: new Date(), active: true, },
{ name: "TEST", firstName: "Another", email: "another@test.test", createdAt: new Date(), active: false, },
],
text: "this is a long test.",
evaluation: "25.23",
@ -136,8 +167,8 @@ it("save with modified submodels", () => {
id: 1,
title: "this is a test",
authors: [
{ name: "DOE", firstName: "John", email: "test@test.test", createdAt: (new Date()).toISOString(), active: true, },
{ name: "TEST", firstName: "Another", email: "another@test.test", createdAt: (new Date()).toISOString(), active: false, },
{ name: "DOE", firstName: "John", email: "test@test.test", createdAt: new Date(), active: true, },
{ name: "TEST", firstName: "Another", email: "another@test.test", createdAt: new Date(), active: false, },
],
text: "this is a long test.",
evaluation: "25.23",