Change README for 3.0.0.

This commit is contained in:
Madeorsk 2024-10-04 14:58:00 +02:00
parent e43e27e2e1
commit aa07f362ac
Signed by: Madeorsk
GPG key ID: 677E51CA765BB79F

139
README.md
View file

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