1
1
# Sharkitek Core
2
2
3
+ ![ Version 3.0.0] ( https://img.shields.io/badge/version-3.0.0-blue )
4
+
3
5
## Introduction
4
6
5
7
Sharkitek is a Javascript / TypeScript library designed to ease development of client-side models.
6
8
7
9
With Sharkitek, you define the architecture of your models by specifying their properties and their types.
8
- Then, you can use the defined methods like ` serialize ` , ` deserialize ` or ` serializeDiff ` .
10
+ Then, you can use the defined methods like ` serialize ` , ` deserialize ` , ` save ` or ` serializeDiff ` .
9
11
10
12
``` typescript
11
- class Example extends Model <Example >
13
+ class Example extends s .model ({
14
+ id: s .property .numeric (),
15
+ name: s .property .string (),
16
+ })
12
17
{
13
- id: number ;
14
- name: string ;
15
-
16
- protected SDefinition(): ModelDefinition <Example >
17
- {
18
- return {
19
- id: SDefine (SNumeric ),
20
- name: SDefine (SString ),
21
- };
22
- }
23
18
}
24
19
```
25
20
@@ -31,60 +26,44 @@ class Example extends Model<Example>
31
26
/**
32
27
* A person.
33
28
*/
34
- class Person extends Model <Person >
29
+ class Person extends s .model ({
30
+ id: s .property .numeric (),
31
+ name: s .property .string (),
32
+ firstName: s .property .string (),
33
+ email: s .property .string (),
34
+ createdAt: s .property .date (),
35
+ active: s .property .boolean (),
36
+ }, " id" )
35
37
{
36
- id: number ;
37
- name: string ;
38
- firstName: string ;
39
- email: string ;
40
- createdAt: Date ;
41
38
active: boolean = true ;
42
-
43
- protected SIdentifier(): ModelIdentifier <Person >
44
- {
45
- return " id" ;
46
- }
47
-
48
- protected SDefinition(): ModelDefinition <Person >
49
- {
50
- return {
51
- name: SDefine (SString ),
52
- firstName: SDefine (SString ),
53
- email: SDefine (SString ),
54
- createdAt: SDefine (SDate ),
55
- active: SDefine (SBool ),
56
- };
57
- }
58
39
}
59
40
```
60
41
61
42
``` typescript
62
43
/**
63
44
* An article.
64
45
*/
65
- class Article extends Model <Article >
46
+ class Article extends s .model ({
47
+ id: s .property .numeric (),
48
+ title: s .property .string (),
49
+ authors: s .property .array (s .property .model (Author )),
50
+ text: s .property .string (),
51
+ evaluation: s .property .decimal (),
52
+ tags: s .property .array (
53
+ s .property .object ({
54
+ name: s .property .string (),
55
+ })
56
+ ),
57
+ }, " id" )
66
58
{
67
59
id: number ;
68
60
title: string ;
69
61
authors: Author [] = [];
70
62
text: string ;
71
63
evaluation: number ;
72
-
73
- protected SIdentifier(): ModelIdentifier <Article >
74
- {
75
- return " id" ;
76
- }
77
-
78
- protected SDefinition(): ModelDefinition <Article >
79
- {
80
- return {
81
- id: SDefine (SNumeric ),
82
- title: SDefine (SString ),
83
- authors: SDefine (SArray (SModel (Author ))),
84
- text: SDefine (SString ),
85
- evaluation: SDefine (SDecimal ),
86
- };
87
- }
64
+ tags: {
65
+ name: string ;
66
+ }[];
88
67
}
89
68
```
90
69
@@ -102,53 +81,42 @@ Sharkitek defines some basic types by default, in these classes:
102
81
- ` DecimalType ` : number in the model, formatted string in the serialized object.
103
82
- ` DateType ` : date in the model, ISO formatted date in the serialized object.
104
83
- ` ArrayType ` : array in the model, array in the serialized object.
84
+ - ` ObjectType ` : object in the model, object in the serialized object.
105
85
- ` ModelType ` : instance of a specific class in the model, object in the serialized object.
106
86
107
- When you are defining a Sharkitek property , you must provide its type by instantiating one of these classes.
87
+ When you are defining a property of a Sharkitek model , you must provide its type by instantiating one of these classes.
108
88
109
89
``` typescript
110
- class Example extends Model <Example >
90
+ class Example extends s .model ({
91
+ foo: s .property .define (new StringType ()),
92
+ })
111
93
{
112
94
foo: string ;
113
-
114
- protected SDefinition(): ModelDefinition <Example >
115
- {
116
- return {
117
- foo: new Definition (new StringType ()),
118
- };
119
- }
120
95
}
121
96
```
122
97
123
- To ease the use of these classes and reduce read complexity, some constant variables and functions are defined in the library,
124
- following a certain naming convention: "S{type_name}".
125
-
126
- - ` BoolType ` => ` SBool `
127
- - ` StringType ` => ` SString `
128
- - ` NumericType ` => ` SNumeric `
129
- - ` DecimalType ` => ` SDecimal `
130
- - ` DateType ` => ` SDate `
131
- - ` ArrayType ` => ` SArray `
132
- - ` ModelType ` => ` SModel `
98
+ To ease the use of these classes and reduce read complexity,
99
+ properties of each type are easily definable with a function for each type.
133
100
134
- When the types require parameters, the constant is defined as a function. If there is no parameter, then a simple
135
- variable is enough.
101
+ - ` BoolType ` => ` s.property.boolean `
102
+ - ` StringType ` => ` s.property.string `
103
+ - ` NumericType ` => ` s.property.numeric `
104
+ - ` DecimalType ` => ` s.property.decimal `
105
+ - ` DateType ` => ` s.property.date `
106
+ - ` ArrayType ` => ` s.property.array `
107
+ - ` ObjectType ` => ` s.property.object `
108
+ - ` ModelType ` => ` s.property.model `
136
109
137
- Type implementers should provide a corresponding variable or function for each defined type. They can even provide
138
- multiple functions or constants when predefined parameters. (For example, we could define ` SStringArray ` which would
139
- be a variable similar to ` SArray(SString )` .)
110
+ Type implementers should provide a corresponding function for each defined type. They can even provide
111
+ multiple functions or constants with predefined parameters.
112
+ (For example, we could define ` s.property.stringArray() ` which would be similar to ` s.property.array(s.property.string() )` .)
140
113
141
114
``` typescript
142
- class Example extends Model <Example >
115
+ class Example extends s .model ({
116
+ foo: s .property .string (),
117
+ })
143
118
{
144
- foo: string = undefined ;
145
-
146
- protected SDefinition(): ModelDefinition <Example >
147
- {
148
- return {
149
- foo: SDefine (SString ),
150
- };
151
- }
119
+ foo: string ;
152
120
}
153
121
```
154
122
@@ -208,7 +176,6 @@ const result = model.serializeDiff();
208
176
// result = { id: 5, title: "A new title for a new world" }
209
177
// if `id` is not defined as the model identifier:
210
178
// result = { title: "A new title for a new world" }
211
-
212
179
```
213
180
214
181
#### ` resetDiff() `
@@ -238,7 +205,6 @@ const result = model.serializeDiff();
238
205
// result = { id: 5 }
239
206
// if `id` is not defined as the model identifier:
240
207
// result = {}
241
-
242
208
```
243
209
244
210
#### ` save() `
@@ -265,5 +231,4 @@ const result = model.save();
265
231
// result = { id: 5, title: "A new title for a new world" }
266
232
// if `id` is not defined as the model identifier:
267
233
// result = { title: "A new title for a new world" }
268
-
269
234
```
0 commit comments