miolo-model 3.0.0-beta.221 → 3.0.0-beta.224
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/CacheMixin.mjs +56 -0
- package/src/MioloArray.mjs +90 -0
- package/src/MioloModel.mjs +98 -6
package/package.json
CHANGED
package/src/CacheMixin.mjs
CHANGED
|
@@ -1,11 +1,25 @@
|
|
|
1
1
|
export default (BaseClass = class {}) =>
|
|
2
2
|
class extends BaseClass {
|
|
3
|
+
/**
|
|
4
|
+
* Reset the model's cache.
|
|
5
|
+
* @public
|
|
6
|
+
*/
|
|
3
7
|
reset_cache() {
|
|
4
8
|
this.__cache__ = {}
|
|
5
9
|
}
|
|
6
10
|
|
|
11
|
+
/**
|
|
12
|
+
* Reset the model's cache.
|
|
13
|
+
* @public
|
|
14
|
+
*/
|
|
7
15
|
resetCache = this.reset_cache
|
|
8
16
|
|
|
17
|
+
/**
|
|
18
|
+
* Get a value from the model's cache.
|
|
19
|
+
* @param {string} key - The key to get the value from.
|
|
20
|
+
* @returns {any} The value from the cache.
|
|
21
|
+
* @public
|
|
22
|
+
*/
|
|
9
23
|
get_from_cache(key) {
|
|
10
24
|
if (this.__cache__ === undefined) {
|
|
11
25
|
return undefined
|
|
@@ -13,8 +27,20 @@ export default (BaseClass = class {}) =>
|
|
|
13
27
|
return this.__cache__[key]
|
|
14
28
|
}
|
|
15
29
|
|
|
30
|
+
/**
|
|
31
|
+
* Get a value from the model's cache.
|
|
32
|
+
* @param {string} key - The key to get the value from.
|
|
33
|
+
* @returns {any} The value from the cache.
|
|
34
|
+
* @public
|
|
35
|
+
*/
|
|
16
36
|
getFromCache = this.get_from_cache
|
|
17
37
|
|
|
38
|
+
/**
|
|
39
|
+
* Set a value in the model's cache.
|
|
40
|
+
* @param {string} key - The key to set the value to.
|
|
41
|
+
* @param {any} value - The value to set.
|
|
42
|
+
* @public
|
|
43
|
+
*/
|
|
18
44
|
set_to_cache(key, value) {
|
|
19
45
|
if (this.__cache__ === undefined) {
|
|
20
46
|
this.__cache__ = {}
|
|
@@ -22,8 +48,21 @@ export default (BaseClass = class {}) =>
|
|
|
22
48
|
this.__cache__[key] = value
|
|
23
49
|
}
|
|
24
50
|
|
|
51
|
+
/**
|
|
52
|
+
* Set a value in the model's cache.
|
|
53
|
+
* @param {string} key - The key to set the value to.
|
|
54
|
+
* @param {any} value - The value to set.
|
|
55
|
+
* @public
|
|
56
|
+
*/
|
|
25
57
|
setToCache = this.set_to_cache
|
|
26
58
|
|
|
59
|
+
/**
|
|
60
|
+
* Get a value from the model's cache or make it if it doesn't exist.
|
|
61
|
+
* @param {string} cache_key - The key to get the value from.
|
|
62
|
+
* @param {Function} make_callback - The function to make the value if it doesn't exist.
|
|
63
|
+
* @returns {any} The value from the cache.
|
|
64
|
+
* @public
|
|
65
|
+
*/
|
|
27
66
|
get_from_cache_or_make(cache_key, make_callback) {
|
|
28
67
|
if (this.__cache__ === undefined) {
|
|
29
68
|
this.__cache__ = {}
|
|
@@ -35,13 +74,30 @@ export default (BaseClass = class {}) =>
|
|
|
35
74
|
return this.__cache__[cache_key]
|
|
36
75
|
}
|
|
37
76
|
|
|
77
|
+
/**
|
|
78
|
+
* Get a value from the model's cache or make it if it doesn't exist.
|
|
79
|
+
* @param {string} cache_key - The key to get the value from.
|
|
80
|
+
* @param {Function} make_callback - The function to make the value if it doesn't exist.
|
|
81
|
+
* @returns {any} The value from the cache.
|
|
82
|
+
* @public
|
|
83
|
+
*/
|
|
38
84
|
getFromCacheOrMake = this.get_from_cache_or_make
|
|
39
85
|
|
|
86
|
+
/**
|
|
87
|
+
* Invalidate a value in the model's cache.
|
|
88
|
+
* @param {string} cache_key - The key to invalidate.
|
|
89
|
+
* @public
|
|
90
|
+
*/
|
|
40
91
|
invalidate_cache(cache_key) {
|
|
41
92
|
if (this.__cache__ !== undefined) {
|
|
42
93
|
delete this.__cache__[cache_key]
|
|
43
94
|
}
|
|
44
95
|
}
|
|
45
96
|
|
|
97
|
+
/**
|
|
98
|
+
* Invalidate a value in the model's cache.
|
|
99
|
+
* @param {string} cache_key - The key to invalidate.
|
|
100
|
+
* @public
|
|
101
|
+
*/
|
|
46
102
|
invalidateCache = this.invalidate_cache
|
|
47
103
|
}
|
package/src/MioloArray.mjs
CHANGED
|
@@ -1,6 +1,12 @@
|
|
|
1
1
|
import CacheMixin from "./CacheMixin.mjs"
|
|
2
2
|
|
|
3
3
|
export default class MioloArray extends CacheMixin(Array) {
|
|
4
|
+
/**
|
|
5
|
+
* Create a new MioloArray.
|
|
6
|
+
* @param {Function} itemClass - The class of the items in the array.
|
|
7
|
+
* @param {Array<MioloModel> | Array<Object>} items - The items to initialize the array with.
|
|
8
|
+
* @param {...any} extra - Extra parameters to pass to the item class constructor.
|
|
9
|
+
*/
|
|
4
10
|
constructor(itemClass, items = [], ...extra) {
|
|
5
11
|
// Arrays can be inited with a number
|
|
6
12
|
if (typeof items === "number") {
|
|
@@ -25,18 +31,46 @@ export default class MioloArray extends CacheMixin(Array) {
|
|
|
25
31
|
})
|
|
26
32
|
}
|
|
27
33
|
|
|
34
|
+
/**
|
|
35
|
+
* Returns the last item in the array.
|
|
36
|
+
* @returns {MioloModel} The last item in the array.
|
|
37
|
+
* @public
|
|
38
|
+
*/
|
|
28
39
|
last() {
|
|
29
40
|
return this[this.length - 1]
|
|
30
41
|
}
|
|
31
42
|
|
|
43
|
+
/**
|
|
44
|
+
* Returns an Object data containing both:
|
|
45
|
+
* - the instance inner data
|
|
46
|
+
* - data from attriobutes of type MioloModel or MioloArray
|
|
47
|
+
* (attributes with prefix "__" are ignored).
|
|
48
|
+
* @returns {Object} All the data of the instance.
|
|
49
|
+
* @public
|
|
50
|
+
*/
|
|
32
51
|
get_data() {
|
|
33
52
|
return [...this].map((i) => i.get_data())
|
|
34
53
|
}
|
|
35
54
|
|
|
55
|
+
/**
|
|
56
|
+
* Returns an Object data containing both:
|
|
57
|
+
* - the instance inner data
|
|
58
|
+
* - data from attriobutes of type MioloModel or MioloArray
|
|
59
|
+
* (attributes with prefix "__" are ignored).
|
|
60
|
+
* @returns {Object} All the data of the instance.
|
|
61
|
+
* @public
|
|
62
|
+
*/
|
|
36
63
|
getData() {
|
|
37
64
|
return [...this].map((i) => i.getData())
|
|
38
65
|
}
|
|
39
66
|
|
|
67
|
+
/**
|
|
68
|
+
* Find the index of the first item in the array that has the given field and value.
|
|
69
|
+
* @param {string} field - The field to search for.
|
|
70
|
+
* @param {any} value - The value to search for.
|
|
71
|
+
* @returns {number} The index of the first item that has the given field and value, or -1 if not found.
|
|
72
|
+
* @public
|
|
73
|
+
*/
|
|
40
74
|
find_index_by_field(field, value) {
|
|
41
75
|
if (this.length >= 0) {
|
|
42
76
|
const fidx = this.findIndex((elem) => {
|
|
@@ -50,10 +84,24 @@ export default class MioloArray extends CacheMixin(Array) {
|
|
|
50
84
|
return -1
|
|
51
85
|
}
|
|
52
86
|
|
|
87
|
+
/**
|
|
88
|
+
* Find the index of the first item in the array that has the given field and value.
|
|
89
|
+
* @param {string} field - The field to search for.
|
|
90
|
+
* @param {any} value - The value to search for.
|
|
91
|
+
* @returns {number} The index of the first item that has the given field and value, or -1 if not found.
|
|
92
|
+
* @public
|
|
93
|
+
*/
|
|
53
94
|
findIndexByField(field, value) {
|
|
54
95
|
return this.find_index_by_field(field, value)
|
|
55
96
|
}
|
|
56
97
|
|
|
98
|
+
/**
|
|
99
|
+
* Find the first item in the array that has the given field and value.
|
|
100
|
+
* @param {string} field - The field to search for.
|
|
101
|
+
* @param {any} value - The value to search for.
|
|
102
|
+
* @returns {MioloModel} The first item in the array that has the given field and value, or undefined if not found.
|
|
103
|
+
* @public
|
|
104
|
+
*/
|
|
57
105
|
find_by_field(field, value) {
|
|
58
106
|
if (this.length >= 0) {
|
|
59
107
|
const filt = this.filter((elem) => {
|
|
@@ -67,18 +115,43 @@ export default class MioloArray extends CacheMixin(Array) {
|
|
|
67
115
|
return undefined
|
|
68
116
|
}
|
|
69
117
|
|
|
118
|
+
/**
|
|
119
|
+
* Find the first item in the array that has the given field and value.
|
|
120
|
+
* @param {string} field - The field to search for.
|
|
121
|
+
* @param {any} value - The value to search for.
|
|
122
|
+
* @returns {MioloModel} The first item in the array that has the given field and value, or undefined if not found.
|
|
123
|
+
* @public
|
|
124
|
+
*/
|
|
70
125
|
findByField(field, value) {
|
|
71
126
|
return this.find_by_field(field, value)
|
|
72
127
|
}
|
|
73
128
|
|
|
129
|
+
/**
|
|
130
|
+
* Find the first item in the array that has the given id.
|
|
131
|
+
* @param {string} id - The id to search for.
|
|
132
|
+
* @returns {MioloModel} The first item in the array that has the given id, or undefined if not found.
|
|
133
|
+
* @public
|
|
134
|
+
*/
|
|
74
135
|
find_by_id(id) {
|
|
75
136
|
return this.find_by_field("id", id)
|
|
76
137
|
}
|
|
77
138
|
|
|
139
|
+
/**
|
|
140
|
+
* Find the first item in the array that has the given id.
|
|
141
|
+
* @param {string} id - The id to search for.
|
|
142
|
+
* @returns {MioloModel} The first item in the array that has the given id, or undefined if not found.
|
|
143
|
+
* @public
|
|
144
|
+
*/
|
|
78
145
|
findById(id) {
|
|
79
146
|
return this.find_by_id(id)
|
|
80
147
|
}
|
|
81
148
|
|
|
149
|
+
/**
|
|
150
|
+
* Remove the first item in the array that has the given field and value.
|
|
151
|
+
* @param {string} field - The field to search for.
|
|
152
|
+
* @param {any} value - The value to search for.
|
|
153
|
+
* @public
|
|
154
|
+
*/
|
|
82
155
|
remove_by_field(field, value) {
|
|
83
156
|
const fidx = this.find_index_by_field(field, value)
|
|
84
157
|
if (fidx >= 0) {
|
|
@@ -86,10 +159,22 @@ export default class MioloArray extends CacheMixin(Array) {
|
|
|
86
159
|
}
|
|
87
160
|
}
|
|
88
161
|
|
|
162
|
+
/**
|
|
163
|
+
* Remove the first item in the array that has the given field and value.
|
|
164
|
+
* @param {string} field - The field to search for.
|
|
165
|
+
* @param {any} value - The value to search for.
|
|
166
|
+
* @public
|
|
167
|
+
*/
|
|
89
168
|
removeByField(field, value) {
|
|
90
169
|
return this.remove_by_field(field, value)
|
|
91
170
|
}
|
|
92
171
|
|
|
172
|
+
/**
|
|
173
|
+
* Push a new item to the end of the array.
|
|
174
|
+
* @param {Object} data - The data to push to the array.
|
|
175
|
+
* @returns {MioloModel | Object} The new item.
|
|
176
|
+
* @public
|
|
177
|
+
*/
|
|
93
178
|
push(data) {
|
|
94
179
|
const item =
|
|
95
180
|
data !== undefined && data instanceof this.itemClass ? data : new this.itemClass(data)
|
|
@@ -97,6 +182,11 @@ export default class MioloArray extends CacheMixin(Array) {
|
|
|
97
182
|
return item
|
|
98
183
|
}
|
|
99
184
|
|
|
185
|
+
/**
|
|
186
|
+
* Creates a shallow clone of the model.
|
|
187
|
+
* @returns {MioloArray} A shallow clone of the model.
|
|
188
|
+
* @public
|
|
189
|
+
*/
|
|
100
190
|
clone() {
|
|
101
191
|
const currentData = this.getData()
|
|
102
192
|
const clonedData = JSON.parse(JSON.stringify(currentData))
|
package/src/MioloModel.mjs
CHANGED
|
@@ -2,13 +2,27 @@ import CacheMixin from "./CacheMixin.mjs"
|
|
|
2
2
|
import MioloArray from "./MioloArray.mjs"
|
|
3
3
|
|
|
4
4
|
export default class MioloModel extends CacheMixin() {
|
|
5
|
+
/**
|
|
6
|
+
* Initialize a new MioloModel.
|
|
7
|
+
* Notice the attributes you set to your instance may be handled by MioloModel:
|
|
8
|
+
* - if you set an attribute of type MioloModel or MioloArray, it will be taken by get_data() / getData() methods
|
|
9
|
+
* - if you want an attribute of type MioloModel or MioloArray to be ignored by get_data() / getData() methods, you can prefix it with "__" (e.g. __my_attr)
|
|
10
|
+
* @param {Object} data - The data to initialize the model with.
|
|
11
|
+
*/
|
|
5
12
|
constructor(data) {
|
|
6
13
|
super()
|
|
7
14
|
this.data = data
|
|
8
15
|
this.reset_cache()
|
|
9
16
|
}
|
|
10
17
|
|
|
11
|
-
|
|
18
|
+
/**
|
|
19
|
+
* Get a value from the model's data.
|
|
20
|
+
* @param {string} field - The field to get the value from.
|
|
21
|
+
* @param {any} def - The default value to return if the field is not found.
|
|
22
|
+
* @returns {any} The value from the model's data.
|
|
23
|
+
* @public
|
|
24
|
+
*/
|
|
25
|
+
get_value(field, def) {
|
|
12
26
|
if (this.data !== undefined) {
|
|
13
27
|
if (this.data[field] !== undefined && this.data[field] !== null) {
|
|
14
28
|
return this.data[field]
|
|
@@ -17,14 +31,49 @@ export default class MioloModel extends CacheMixin() {
|
|
|
17
31
|
return def
|
|
18
32
|
}
|
|
19
33
|
|
|
20
|
-
|
|
34
|
+
/**
|
|
35
|
+
* Get a value from the model's data.
|
|
36
|
+
* @param {string} field - The field to get the value from.
|
|
37
|
+
* @param {any} def - The default value to return if the field is not found.
|
|
38
|
+
* @returns {any} The value from the model's data.
|
|
39
|
+
* @public
|
|
40
|
+
* @deprecated Use get_value() instead.
|
|
41
|
+
*/
|
|
42
|
+
_get(field, def) {
|
|
43
|
+
return this.get_value(field, def)
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Set a value in the model's data.
|
|
48
|
+
* @param {string} field - The field to set the value to.
|
|
49
|
+
* @param {any} val - The value to set.
|
|
50
|
+
* @public
|
|
51
|
+
*/
|
|
52
|
+
set_value(field, val) {
|
|
21
53
|
if (this.data === undefined) {
|
|
22
54
|
this.data = {}
|
|
23
55
|
}
|
|
24
56
|
this.data[field] = val
|
|
25
57
|
}
|
|
26
58
|
|
|
27
|
-
|
|
59
|
+
/**
|
|
60
|
+
* Set a value in the model's data.
|
|
61
|
+
* @param {string} field - The field to set the value to.
|
|
62
|
+
* @param {any} val - The value to set.
|
|
63
|
+
* @public
|
|
64
|
+
* @deprecated Use set_value() instead.
|
|
65
|
+
*/
|
|
66
|
+
_set(field, val) {
|
|
67
|
+
this.set_value(field, val)
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* Returns an Object data corresponding to MioloModel and MioloArray instances attributes of this instance.
|
|
72
|
+
* Attributes with prefix "__" are ignored.
|
|
73
|
+
* @returns {Object} The extra data from the model.
|
|
74
|
+
* @private
|
|
75
|
+
*/
|
|
76
|
+
_get_extra_data() {
|
|
28
77
|
const data = {}
|
|
29
78
|
for (const [key, value] of Object.entries(this)) {
|
|
30
79
|
if (key.startsWith("__")) {
|
|
@@ -39,7 +88,13 @@ export default class MioloModel extends CacheMixin() {
|
|
|
39
88
|
return data
|
|
40
89
|
}
|
|
41
90
|
|
|
42
|
-
|
|
91
|
+
/**
|
|
92
|
+
* Returns an Object data corresponding to MioloModel and MioloArray instances attributes of this instance.
|
|
93
|
+
* Attributes with prefix "__" are ignored.
|
|
94
|
+
* @returns {Object} The extra data from the model.
|
|
95
|
+
* @private
|
|
96
|
+
*/
|
|
97
|
+
_getExtraData() {
|
|
43
98
|
const data = {}
|
|
44
99
|
for (const [key, value] of Object.entries(this)) {
|
|
45
100
|
if (key.startsWith("__")) {
|
|
@@ -54,22 +109,46 @@ export default class MioloModel extends CacheMixin() {
|
|
|
54
109
|
return data
|
|
55
110
|
}
|
|
56
111
|
|
|
112
|
+
/**
|
|
113
|
+
* Returns an Object data containing both:
|
|
114
|
+
* - the instance inner data
|
|
115
|
+
* - data from attriobutes of type MioloModel or MioloArray
|
|
116
|
+
* (attributes with prefix "__" are ignored).
|
|
117
|
+
* @returns {Object} All the data of the instance.
|
|
118
|
+
* @public
|
|
119
|
+
*/
|
|
57
120
|
get_data() {
|
|
58
|
-
const extra = this.
|
|
121
|
+
const extra = this._get_extra_data() || {}
|
|
59
122
|
return {
|
|
60
123
|
...this.data,
|
|
61
124
|
...extra
|
|
62
125
|
}
|
|
63
126
|
}
|
|
64
127
|
|
|
128
|
+
/**
|
|
129
|
+
* Returns an Object data containing both:
|
|
130
|
+
* - the instance inner data
|
|
131
|
+
* - data from attriobutes of type MioloModel or MioloArray
|
|
132
|
+
* (attributes with prefix "__" are ignored).
|
|
133
|
+
* @returns {Object} All the data of the instance.
|
|
134
|
+
* @public
|
|
135
|
+
*/
|
|
65
136
|
getData() {
|
|
66
|
-
const extra = this.
|
|
137
|
+
const extra = this._getExtraData() || {}
|
|
67
138
|
return {
|
|
68
139
|
...this.data,
|
|
69
140
|
...extra
|
|
70
141
|
}
|
|
71
142
|
}
|
|
72
143
|
|
|
144
|
+
/**
|
|
145
|
+
* Update the model's inner data with changes.
|
|
146
|
+
* It resetes model's inner cache.
|
|
147
|
+
* Does the same (updating data and resetting cache) for nested MioloModel or MioloArray attributes
|
|
148
|
+
* (ignoring those prefixed with "__").
|
|
149
|
+
* @param {Object} changes - The changes to apply to the model.
|
|
150
|
+
* @public
|
|
151
|
+
*/
|
|
73
152
|
update(changes) {
|
|
74
153
|
this.reset_cache()
|
|
75
154
|
|
|
@@ -93,6 +172,14 @@ export default class MioloModel extends CacheMixin() {
|
|
|
93
172
|
}
|
|
94
173
|
}
|
|
95
174
|
|
|
175
|
+
/**
|
|
176
|
+
* Update the model's inner data by merging another model's data with it.
|
|
177
|
+
* It resetes model's inner cache.
|
|
178
|
+
* Does the same (updating data and resetting cache) for nested MioloModel or MioloArray attributes
|
|
179
|
+
* (ignoring those prefixed with "__").
|
|
180
|
+
* @param {Object} model - The model to merge with.
|
|
181
|
+
* @public
|
|
182
|
+
*/
|
|
96
183
|
merge(model) {
|
|
97
184
|
this.update(model.getData())
|
|
98
185
|
for (const [key, value] of Object.entries(model)) {
|
|
@@ -107,6 +194,11 @@ export default class MioloModel extends CacheMixin() {
|
|
|
107
194
|
}
|
|
108
195
|
}
|
|
109
196
|
|
|
197
|
+
/**
|
|
198
|
+
* Creates a shallow clone of the model.
|
|
199
|
+
* @returns {MioloModel} A shallow clone of the model.
|
|
200
|
+
* @public
|
|
201
|
+
*/
|
|
110
202
|
clone() {
|
|
111
203
|
const currentData = this.getData()
|
|
112
204
|
const clonedData = JSON.parse(JSON.stringify(currentData))
|