@kaspernj/api-maker 1.0.268 → 1.0.269
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 +2 -1
- package/src/base-model.mjs +55 -24
- package/src/cache-key-generator.mjs +65 -0
- package/src/collection.mjs +30 -1
package/package.json
CHANGED
|
@@ -16,7 +16,7 @@
|
|
|
16
16
|
]
|
|
17
17
|
},
|
|
18
18
|
"name": "@kaspernj/api-maker",
|
|
19
|
-
"version": "1.0.
|
|
19
|
+
"version": "1.0.269",
|
|
20
20
|
"type": "module",
|
|
21
21
|
"description": "",
|
|
22
22
|
"main": "index.js",
|
|
@@ -52,6 +52,7 @@
|
|
|
52
52
|
"on-location-changed": ">= 1.0.7",
|
|
53
53
|
"qs": ">= 6.9.3",
|
|
54
54
|
"replaceall": ">= 0.1.6",
|
|
55
|
+
"spark-md5": "^3.0.2",
|
|
55
56
|
"strftime": ">= 0.10.0",
|
|
56
57
|
"uniqunize": "^1.0.1",
|
|
57
58
|
"wake-event": ">= 0.0.1"
|
package/src/base-model.mjs
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import Attribute from "./base-model/attribute.mjs"
|
|
2
2
|
import AttributeNotLoadedError from "./attribute-not-loaded-error.mjs"
|
|
3
|
+
import CacheKeyGenerator from "./cache-key-generator.mjs"
|
|
3
4
|
import Collection from "./collection.mjs"
|
|
4
5
|
import CommandsPool from "./commands-pool.mjs"
|
|
5
6
|
import Config from "./config.mjs"
|
|
@@ -146,6 +147,7 @@ export default class BaseModel {
|
|
|
146
147
|
this.changes = {}
|
|
147
148
|
this.newRecord = args.isNewRecord
|
|
148
149
|
this.relationshipsCache = {}
|
|
150
|
+
this.relationships = {}
|
|
149
151
|
|
|
150
152
|
if (args && args.data && args.data.a) {
|
|
151
153
|
this._readModelDataFromArgs(args)
|
|
@@ -207,6 +209,7 @@ export default class BaseModel {
|
|
|
207
209
|
|
|
208
210
|
clone.abilities = {...this.abilities}
|
|
209
211
|
clone.modelData = {...this.modelData}
|
|
212
|
+
clone.relationships = {...this.relationships}
|
|
210
213
|
clone.relationshipsCache = {...this.relationshipsCache}
|
|
211
214
|
|
|
212
215
|
return clone
|
|
@@ -237,6 +240,12 @@ export default class BaseModel {
|
|
|
237
240
|
}
|
|
238
241
|
}
|
|
239
242
|
|
|
243
|
+
fullCacheKey() {
|
|
244
|
+
const cacheKeyGenerator = new CacheKeyGenerator(this)
|
|
245
|
+
|
|
246
|
+
return cacheKeyGenerator.cacheKey()
|
|
247
|
+
}
|
|
248
|
+
|
|
240
249
|
static all () {
|
|
241
250
|
return this.ransack()
|
|
242
251
|
}
|
|
@@ -385,6 +394,12 @@ export default class BaseModel {
|
|
|
385
394
|
return false
|
|
386
395
|
}
|
|
387
396
|
|
|
397
|
+
isAssociationPresent (associationName) {
|
|
398
|
+
if (this.isAssociationLoaded(associationName)) return true
|
|
399
|
+
if (associationName in this.relationships) return true
|
|
400
|
+
return false
|
|
401
|
+
}
|
|
402
|
+
|
|
388
403
|
static parseValidationErrors ({error, model, options}) {
|
|
389
404
|
if (!(error instanceof ValidationError)) return
|
|
390
405
|
if (!error.args.response.validation_errors) return
|
|
@@ -489,6 +504,7 @@ export default class BaseModel {
|
|
|
489
504
|
|
|
490
505
|
setNewModel (model) {
|
|
491
506
|
this.setNewModelData(model)
|
|
507
|
+
this.relationships = digg(model, "relationships")
|
|
492
508
|
this.relationshipsCache = digg(model, "relationshipsCache")
|
|
493
509
|
}
|
|
494
510
|
|
|
@@ -643,7 +659,8 @@ export default class BaseModel {
|
|
|
643
659
|
{
|
|
644
660
|
args: {
|
|
645
661
|
query_params: this.collection && this.collection.params(),
|
|
646
|
-
save: objectData
|
|
662
|
+
save: objectData,
|
|
663
|
+
simple_model_errors: options?.simpleModelErrors
|
|
647
664
|
},
|
|
648
665
|
command: `${this.modelClassData().collectionName}-update`,
|
|
649
666
|
collectionName: this.modelClassData().collectionName,
|
|
@@ -697,6 +714,7 @@ export default class BaseModel {
|
|
|
697
714
|
|
|
698
715
|
preloadRelationship (relationshipName, model) {
|
|
699
716
|
this.relationshipsCache[BaseModel.snakeCase(relationshipName)] = model
|
|
717
|
+
this.relationships[BaseModel.snakeCase(relationshipName)] = model
|
|
700
718
|
}
|
|
701
719
|
|
|
702
720
|
uniqueKey () {
|
|
@@ -792,7 +810,9 @@ export default class BaseModel {
|
|
|
792
810
|
}
|
|
793
811
|
|
|
794
812
|
async _loadBelongsToReflection (args, queryArgs = {}) {
|
|
795
|
-
if (args.reflectionName in this.
|
|
813
|
+
if (args.reflectionName in this.relationships) {
|
|
814
|
+
return this.relationships[args.reflectionName]
|
|
815
|
+
} else if (args.reflectionName in this.relationshipsCache) {
|
|
796
816
|
return this.relationshipsCache[args.reflectionName]
|
|
797
817
|
} else {
|
|
798
818
|
const collection = new Collection(args, queryArgs)
|
|
@@ -803,21 +823,24 @@ export default class BaseModel {
|
|
|
803
823
|
}
|
|
804
824
|
|
|
805
825
|
_readBelongsToReflection ({reflectionName}) {
|
|
806
|
-
if (
|
|
807
|
-
|
|
808
|
-
|
|
826
|
+
if (reflectionName in this.relationships) {
|
|
827
|
+
return this.relationships[reflectionName]
|
|
828
|
+
} else if (reflectionName in this.relationshipsCache) {
|
|
829
|
+
return this.relationshipsCache[reflectionName]
|
|
830
|
+
}
|
|
809
831
|
|
|
810
|
-
|
|
811
|
-
const modelClassName = digg(this.modelClassData(), "name")
|
|
832
|
+
if (this.isNewRecord()) return null
|
|
812
833
|
|
|
813
|
-
|
|
814
|
-
|
|
834
|
+
const loadedRelationships = Object.keys(this.relationshipsCache)
|
|
835
|
+
const modelClassName = digg(this.modelClassData(), "name")
|
|
815
836
|
|
|
816
|
-
|
|
837
|
+
throw new NotLoadedError(`${modelClassName}#${reflectionName} hasn't been loaded yet. Only these were loaded: ${loadedRelationships.join(", ")}`)
|
|
817
838
|
}
|
|
818
839
|
|
|
819
840
|
async _loadHasManyReflection (args, queryArgs = {}) {
|
|
820
|
-
if (args.reflectionName in this.
|
|
841
|
+
if (args.reflectionName in this.relationships) {
|
|
842
|
+
return this.relationships[args.reflectionName]
|
|
843
|
+
} else if (args.reflectionName in this.relationshipsCache) {
|
|
821
844
|
return this.relationshipsCache[args.reflectionName]
|
|
822
845
|
}
|
|
823
846
|
|
|
@@ -830,7 +853,9 @@ export default class BaseModel {
|
|
|
830
853
|
}
|
|
831
854
|
|
|
832
855
|
async _loadHasOneReflection (args, queryArgs = {}) {
|
|
833
|
-
if (args.reflectionName in this.
|
|
856
|
+
if (args.reflectionName in this.relationships) {
|
|
857
|
+
return this.relationships[args.reflectionName]
|
|
858
|
+
} else if (args.reflectionName in this.relationshipsCache) {
|
|
834
859
|
return this.relationshipsCache[args.reflectionName]
|
|
835
860
|
} else {
|
|
836
861
|
const collection = new Collection(args, queryArgs)
|
|
@@ -843,17 +868,20 @@ export default class BaseModel {
|
|
|
843
868
|
}
|
|
844
869
|
|
|
845
870
|
_readHasOneReflection ({reflectionName}) {
|
|
846
|
-
if (
|
|
847
|
-
|
|
848
|
-
|
|
849
|
-
|
|
850
|
-
|
|
851
|
-
const modelClassName = digg(this.modelClassData(), "name")
|
|
871
|
+
if (reflectionName in this.relationships) {
|
|
872
|
+
return this.relationships[reflectionName]
|
|
873
|
+
} else if (reflectionName in this.relationshipsCache) {
|
|
874
|
+
return this.relationshipsCache[reflectionName]
|
|
875
|
+
}
|
|
852
876
|
|
|
853
|
-
|
|
877
|
+
if (this.isNewRecord()) {
|
|
878
|
+
return null
|
|
854
879
|
}
|
|
855
880
|
|
|
856
|
-
|
|
881
|
+
const loadedRelationships = Object.keys(this.relationshipsCache)
|
|
882
|
+
const modelClassName = digg(this.modelClassData(), "name")
|
|
883
|
+
|
|
884
|
+
throw new NotLoadedError(`${modelClassName}#${reflectionName} hasn't been loaded yet. Only these were loaded: ${loadedRelationships.join(", ")}`)
|
|
857
885
|
}
|
|
858
886
|
|
|
859
887
|
_readModelDataFromArgs (args) {
|
|
@@ -893,19 +921,22 @@ export default class BaseModel {
|
|
|
893
921
|
|
|
894
922
|
if (!relationshipData) {
|
|
895
923
|
this.relationshipsCache[relationshipName] = null
|
|
924
|
+
this.relationships[relationshipName] = null
|
|
896
925
|
} else if (Array.isArray(relationshipData)) {
|
|
897
|
-
|
|
926
|
+
this.relationshipsCache[relationshipName] = []
|
|
927
|
+
this.relationships[relationshipName] = []
|
|
898
928
|
|
|
899
929
|
for (const relationshipId of relationshipData) {
|
|
900
930
|
const model = preloaded.getModel(relationshipType, relationshipId)
|
|
901
931
|
|
|
902
|
-
|
|
932
|
+
this.relationshipsCache[relationshipName].push(model)
|
|
933
|
+
this.relationships[relationshipName].push(model)
|
|
903
934
|
}
|
|
904
|
-
|
|
905
|
-
this.relationshipsCache[relationshipName] = result
|
|
906
935
|
} else {
|
|
907
936
|
const model = preloaded.getModel(relationshipType, relationshipData)
|
|
937
|
+
|
|
908
938
|
this.relationshipsCache[relationshipName] = model
|
|
939
|
+
this.relationships[relationshipName] = model
|
|
909
940
|
}
|
|
910
941
|
}
|
|
911
942
|
}
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import SparkMD5 from "spark-md5"
|
|
2
|
+
|
|
3
|
+
export default class CacheKeyGenerator {
|
|
4
|
+
constructor(model) {
|
|
5
|
+
this.allModels = [model]
|
|
6
|
+
this.readModels = {}
|
|
7
|
+
this.recordModelType(model.modelClassData().name)
|
|
8
|
+
this.recordModel(model.modelClassData().name, model)
|
|
9
|
+
this.fillModels(model)
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
recordModelType(relationshipType) {
|
|
13
|
+
if (!(relationshipType in this.readModels)) {
|
|
14
|
+
this.readModels[relationshipType] = {}
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
recordModel(relationshipType, model) {
|
|
19
|
+
this.allModels.push(model)
|
|
20
|
+
this.readModels[relationshipType][model.id() || model.uniqueKey()] = true
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
isModelRecorded(relationshipType, model) {
|
|
24
|
+
if (model.id() in this.readModels[relationshipType]) {
|
|
25
|
+
return true
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
fillModels(model) {
|
|
30
|
+
for (const relationshipType in model.relationships) {
|
|
31
|
+
this.recordModelType(relationshipType)
|
|
32
|
+
|
|
33
|
+
for (const anotherModel of model.relationships[relationshipType]) {
|
|
34
|
+
if (this.isModelRecorded(relationshipType, anotherModel)) {
|
|
35
|
+
continue
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
this.recordModel(relationshipType, anotherModel)
|
|
39
|
+
this.fillModels(anotherModel)
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
cacheKey() {
|
|
45
|
+
const md5 = new SparkMD5()
|
|
46
|
+
|
|
47
|
+
for (const model of this.allModels) {
|
|
48
|
+
md5.append("-model-")
|
|
49
|
+
md5.append(model.modelClassData().name)
|
|
50
|
+
md5.append("-unique-key-")
|
|
51
|
+
md5.append(model.id() || model.uniqueKey())
|
|
52
|
+
md5.append("-attributes-")
|
|
53
|
+
|
|
54
|
+
const attributes = model.attributes()
|
|
55
|
+
|
|
56
|
+
for (const attributeName in attributes) {
|
|
57
|
+
md5.append(attributeName)
|
|
58
|
+
md5.append("-attribute-")
|
|
59
|
+
md5.append(`${model.readAttributeUnderscore(attributeName)}`)
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
return md5.end()
|
|
64
|
+
}
|
|
65
|
+
}
|
package/src/collection.mjs
CHANGED
|
@@ -87,7 +87,7 @@ export default class ApiMakerCollection {
|
|
|
87
87
|
return this._merge({limit: amount})
|
|
88
88
|
}
|
|
89
89
|
|
|
90
|
-
|
|
90
|
+
preloaded () {
|
|
91
91
|
if (!(this.args.reflectionName in this.args.model.relationshipsCache)) {
|
|
92
92
|
throw new Error(`${this.args.reflectionName} hasnt been loaded yet`)
|
|
93
93
|
}
|
|
@@ -95,6 +95,35 @@ export default class ApiMakerCollection {
|
|
|
95
95
|
return this.args.model.relationshipsCache[this.args.reflectionName]
|
|
96
96
|
}
|
|
97
97
|
|
|
98
|
+
loaded () {
|
|
99
|
+
if (this.args.reflectionName in this.args.model.relationships) {
|
|
100
|
+
return this.args.model.relationships[this.args.reflectionName]
|
|
101
|
+
} else if (this.args.reflectionName in this.args.model.relationshipsCache) {
|
|
102
|
+
return this.args.model.relationshipsCache[this.args.reflectionName]
|
|
103
|
+
} else {
|
|
104
|
+
throw new Error(`${this.args.reflectionName} hasnt been loaded yet`)
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
// Replaces the relationships with the given new collection.
|
|
109
|
+
set(newCollection) {
|
|
110
|
+
this.args.model.relationships[this.args.reflectionName] = newCollection
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
// Pushes another model onto the given collection.
|
|
114
|
+
push(newModel) {
|
|
115
|
+
if (!(this.args.reflectionName in this.args.model.relationships)) {
|
|
116
|
+
this.args.model.relationships[this.args.reflectionName] = []
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
this.args.model.relationships[this.args.reflectionName].push(newModel)
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
// Array shortcuts
|
|
123
|
+
find = (...args) => this.loaded().find(...args)
|
|
124
|
+
forEach = (...args) => this.loaded().forEach(...args)
|
|
125
|
+
map = (...args) => this.loaded().map(...args)
|
|
126
|
+
|
|
98
127
|
preload (preloadValue) {
|
|
99
128
|
return this._merge({preload: preloadValue})
|
|
100
129
|
}
|