@kaspernj/api-maker 1.0.269 → 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 CHANGED
@@ -16,7 +16,7 @@
16
16
  ]
17
17
  },
18
18
  "name": "@kaspernj/api-maker",
19
- "version": "1.0.269",
19
+ "version": "1.0.270",
20
20
  "type": "module",
21
21
  "description": "",
22
22
  "main": "index.js",
@@ -143,6 +143,16 @@ export default class BaseModel {
143
143
  return reflections
144
144
  }
145
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
+
146
156
  constructor (args = {}) {
147
157
  this.changes = {}
148
158
  this.newRecord = args.isNewRecord
@@ -96,12 +96,21 @@ export default class ApiMakerCollection {
96
96
  }
97
97
 
98
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]
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)
103
112
  } else {
104
- throw new Error(`${this.args.reflectionName} hasnt been loaded yet`)
113
+ throw new Error(`${reflectionName} hasnt been loaded yet`)
105
114
  }
106
115
  }
107
116