@kaspernj/api-maker 1.0.268 → 1.0.270
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 +65 -24
- package/src/cache-key-generator.mjs +65 -0
- package/src/collection.mjs +39 -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.270",
|
|
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"
|
|
@@ -142,10 +143,21 @@ export default class BaseModel {
|
|
|
142
143
|
return reflections
|
|
143
144
|
}
|
|
144
145
|
|
|
146
|
+
static reflection(name) {
|
|
147
|
+
const foundReflection = this.reflections().find((reflection) => reflection.name() == name)
|
|
148
|
+
|
|
149
|
+
if (!foundReflection) {
|
|
150
|
+
throw new Error(`No such reflection: ${name} in ${this.reflections().map((reflection) => reflection.name()).join(", ")}`)
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
return foundReflection
|
|
154
|
+
}
|
|
155
|
+
|
|
145
156
|
constructor (args = {}) {
|
|
146
157
|
this.changes = {}
|
|
147
158
|
this.newRecord = args.isNewRecord
|
|
148
159
|
this.relationshipsCache = {}
|
|
160
|
+
this.relationships = {}
|
|
149
161
|
|
|
150
162
|
if (args && args.data && args.data.a) {
|
|
151
163
|
this._readModelDataFromArgs(args)
|
|
@@ -207,6 +219,7 @@ export default class BaseModel {
|
|
|
207
219
|
|
|
208
220
|
clone.abilities = {...this.abilities}
|
|
209
221
|
clone.modelData = {...this.modelData}
|
|
222
|
+
clone.relationships = {...this.relationships}
|
|
210
223
|
clone.relationshipsCache = {...this.relationshipsCache}
|
|
211
224
|
|
|
212
225
|
return clone
|
|
@@ -237,6 +250,12 @@ export default class BaseModel {
|
|
|
237
250
|
}
|
|
238
251
|
}
|
|
239
252
|
|
|
253
|
+
fullCacheKey() {
|
|
254
|
+
const cacheKeyGenerator = new CacheKeyGenerator(this)
|
|
255
|
+
|
|
256
|
+
return cacheKeyGenerator.cacheKey()
|
|
257
|
+
}
|
|
258
|
+
|
|
240
259
|
static all () {
|
|
241
260
|
return this.ransack()
|
|
242
261
|
}
|
|
@@ -385,6 +404,12 @@ export default class BaseModel {
|
|
|
385
404
|
return false
|
|
386
405
|
}
|
|
387
406
|
|
|
407
|
+
isAssociationPresent (associationName) {
|
|
408
|
+
if (this.isAssociationLoaded(associationName)) return true
|
|
409
|
+
if (associationName in this.relationships) return true
|
|
410
|
+
return false
|
|
411
|
+
}
|
|
412
|
+
|
|
388
413
|
static parseValidationErrors ({error, model, options}) {
|
|
389
414
|
if (!(error instanceof ValidationError)) return
|
|
390
415
|
if (!error.args.response.validation_errors) return
|
|
@@ -489,6 +514,7 @@ export default class BaseModel {
|
|
|
489
514
|
|
|
490
515
|
setNewModel (model) {
|
|
491
516
|
this.setNewModelData(model)
|
|
517
|
+
this.relationships = digg(model, "relationships")
|
|
492
518
|
this.relationshipsCache = digg(model, "relationshipsCache")
|
|
493
519
|
}
|
|
494
520
|
|
|
@@ -643,7 +669,8 @@ export default class BaseModel {
|
|
|
643
669
|
{
|
|
644
670
|
args: {
|
|
645
671
|
query_params: this.collection && this.collection.params(),
|
|
646
|
-
save: objectData
|
|
672
|
+
save: objectData,
|
|
673
|
+
simple_model_errors: options?.simpleModelErrors
|
|
647
674
|
},
|
|
648
675
|
command: `${this.modelClassData().collectionName}-update`,
|
|
649
676
|
collectionName: this.modelClassData().collectionName,
|
|
@@ -697,6 +724,7 @@ export default class BaseModel {
|
|
|
697
724
|
|
|
698
725
|
preloadRelationship (relationshipName, model) {
|
|
699
726
|
this.relationshipsCache[BaseModel.snakeCase(relationshipName)] = model
|
|
727
|
+
this.relationships[BaseModel.snakeCase(relationshipName)] = model
|
|
700
728
|
}
|
|
701
729
|
|
|
702
730
|
uniqueKey () {
|
|
@@ -792,7 +820,9 @@ export default class BaseModel {
|
|
|
792
820
|
}
|
|
793
821
|
|
|
794
822
|
async _loadBelongsToReflection (args, queryArgs = {}) {
|
|
795
|
-
if (args.reflectionName in this.
|
|
823
|
+
if (args.reflectionName in this.relationships) {
|
|
824
|
+
return this.relationships[args.reflectionName]
|
|
825
|
+
} else if (args.reflectionName in this.relationshipsCache) {
|
|
796
826
|
return this.relationshipsCache[args.reflectionName]
|
|
797
827
|
} else {
|
|
798
828
|
const collection = new Collection(args, queryArgs)
|
|
@@ -803,21 +833,24 @@ export default class BaseModel {
|
|
|
803
833
|
}
|
|
804
834
|
|
|
805
835
|
_readBelongsToReflection ({reflectionName}) {
|
|
806
|
-
if (
|
|
807
|
-
|
|
808
|
-
|
|
836
|
+
if (reflectionName in this.relationships) {
|
|
837
|
+
return this.relationships[reflectionName]
|
|
838
|
+
} else if (reflectionName in this.relationshipsCache) {
|
|
839
|
+
return this.relationshipsCache[reflectionName]
|
|
840
|
+
}
|
|
809
841
|
|
|
810
|
-
|
|
811
|
-
const modelClassName = digg(this.modelClassData(), "name")
|
|
842
|
+
if (this.isNewRecord()) return null
|
|
812
843
|
|
|
813
|
-
|
|
814
|
-
|
|
844
|
+
const loadedRelationships = Object.keys(this.relationshipsCache)
|
|
845
|
+
const modelClassName = digg(this.modelClassData(), "name")
|
|
815
846
|
|
|
816
|
-
|
|
847
|
+
throw new NotLoadedError(`${modelClassName}#${reflectionName} hasn't been loaded yet. Only these were loaded: ${loadedRelationships.join(", ")}`)
|
|
817
848
|
}
|
|
818
849
|
|
|
819
850
|
async _loadHasManyReflection (args, queryArgs = {}) {
|
|
820
|
-
if (args.reflectionName in this.
|
|
851
|
+
if (args.reflectionName in this.relationships) {
|
|
852
|
+
return this.relationships[args.reflectionName]
|
|
853
|
+
} else if (args.reflectionName in this.relationshipsCache) {
|
|
821
854
|
return this.relationshipsCache[args.reflectionName]
|
|
822
855
|
}
|
|
823
856
|
|
|
@@ -830,7 +863,9 @@ export default class BaseModel {
|
|
|
830
863
|
}
|
|
831
864
|
|
|
832
865
|
async _loadHasOneReflection (args, queryArgs = {}) {
|
|
833
|
-
if (args.reflectionName in this.
|
|
866
|
+
if (args.reflectionName in this.relationships) {
|
|
867
|
+
return this.relationships[args.reflectionName]
|
|
868
|
+
} else if (args.reflectionName in this.relationshipsCache) {
|
|
834
869
|
return this.relationshipsCache[args.reflectionName]
|
|
835
870
|
} else {
|
|
836
871
|
const collection = new Collection(args, queryArgs)
|
|
@@ -843,17 +878,20 @@ export default class BaseModel {
|
|
|
843
878
|
}
|
|
844
879
|
|
|
845
880
|
_readHasOneReflection ({reflectionName}) {
|
|
846
|
-
if (
|
|
847
|
-
|
|
848
|
-
|
|
849
|
-
|
|
850
|
-
|
|
851
|
-
const modelClassName = digg(this.modelClassData(), "name")
|
|
881
|
+
if (reflectionName in this.relationships) {
|
|
882
|
+
return this.relationships[reflectionName]
|
|
883
|
+
} else if (reflectionName in this.relationshipsCache) {
|
|
884
|
+
return this.relationshipsCache[reflectionName]
|
|
885
|
+
}
|
|
852
886
|
|
|
853
|
-
|
|
887
|
+
if (this.isNewRecord()) {
|
|
888
|
+
return null
|
|
854
889
|
}
|
|
855
890
|
|
|
856
|
-
|
|
891
|
+
const loadedRelationships = Object.keys(this.relationshipsCache)
|
|
892
|
+
const modelClassName = digg(this.modelClassData(), "name")
|
|
893
|
+
|
|
894
|
+
throw new NotLoadedError(`${modelClassName}#${reflectionName} hasn't been loaded yet. Only these were loaded: ${loadedRelationships.join(", ")}`)
|
|
857
895
|
}
|
|
858
896
|
|
|
859
897
|
_readModelDataFromArgs (args) {
|
|
@@ -893,19 +931,22 @@ export default class BaseModel {
|
|
|
893
931
|
|
|
894
932
|
if (!relationshipData) {
|
|
895
933
|
this.relationshipsCache[relationshipName] = null
|
|
934
|
+
this.relationships[relationshipName] = null
|
|
896
935
|
} else if (Array.isArray(relationshipData)) {
|
|
897
|
-
|
|
936
|
+
this.relationshipsCache[relationshipName] = []
|
|
937
|
+
this.relationships[relationshipName] = []
|
|
898
938
|
|
|
899
939
|
for (const relationshipId of relationshipData) {
|
|
900
940
|
const model = preloaded.getModel(relationshipType, relationshipId)
|
|
901
941
|
|
|
902
|
-
|
|
942
|
+
this.relationshipsCache[relationshipName].push(model)
|
|
943
|
+
this.relationships[relationshipName].push(model)
|
|
903
944
|
}
|
|
904
|
-
|
|
905
|
-
this.relationshipsCache[relationshipName] = result
|
|
906
945
|
} else {
|
|
907
946
|
const model = preloaded.getModel(relationshipType, relationshipData)
|
|
947
|
+
|
|
908
948
|
this.relationshipsCache[relationshipName] = model
|
|
949
|
+
this.relationships[relationshipName] = model
|
|
909
950
|
}
|
|
910
951
|
}
|
|
911
952
|
}
|
|
@@ -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,44 @@ export default class ApiMakerCollection {
|
|
|
95
95
|
return this.args.model.relationshipsCache[this.args.reflectionName]
|
|
96
96
|
}
|
|
97
97
|
|
|
98
|
+
loaded () {
|
|
99
|
+
const {model, reflectionName} = this.args
|
|
100
|
+
|
|
101
|
+
if (reflectionName in model.relationships) {
|
|
102
|
+
return model.relationships[reflectionName]
|
|
103
|
+
} else if (reflectionName in model.relationshipsCache) {
|
|
104
|
+
return model.relationshipsCache[reflectionName]
|
|
105
|
+
} else if (model.isNewRecord()) {
|
|
106
|
+
const reflectionNameUnderscore = inflection.underscore(reflectionName)
|
|
107
|
+
|
|
108
|
+
// Initialize as empty and try again to return the empty result
|
|
109
|
+
this.set([])
|
|
110
|
+
|
|
111
|
+
return digg(model.relationships, reflectionNameUnderscore)
|
|
112
|
+
} else {
|
|
113
|
+
throw new Error(`${reflectionName} hasnt been loaded yet`)
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
// Replaces the relationships with the given new collection.
|
|
118
|
+
set(newCollection) {
|
|
119
|
+
this.args.model.relationships[this.args.reflectionName] = newCollection
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
// Pushes another model onto the given collection.
|
|
123
|
+
push(newModel) {
|
|
124
|
+
if (!(this.args.reflectionName in this.args.model.relationships)) {
|
|
125
|
+
this.args.model.relationships[this.args.reflectionName] = []
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
this.args.model.relationships[this.args.reflectionName].push(newModel)
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
// Array shortcuts
|
|
132
|
+
find = (...args) => this.loaded().find(...args)
|
|
133
|
+
forEach = (...args) => this.loaded().forEach(...args)
|
|
134
|
+
map = (...args) => this.loaded().map(...args)
|
|
135
|
+
|
|
98
136
|
preload (preloadValue) {
|
|
99
137
|
return this._merge({preload: preloadValue})
|
|
100
138
|
}
|