@kaspernj/api-maker 1.0.123 → 1.0.127

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.
Files changed (58) hide show
  1. package/.eslintrc.js +39 -33
  2. package/__tests__/cable-connection-pool.test.js +4 -4
  3. package/__tests__/custom-error.test.js +13 -0
  4. package/__tests__/model-name.test.js +16 -10
  5. package/__tests__/routes-native.test.js +15 -33
  6. package/__tests__/support/task.js +5 -5
  7. package/__tests__/support/user.js +5 -5
  8. package/index.js +0 -4
  9. package/package.json +6 -5
  10. package/src/api.cjs +15 -15
  11. package/src/base-model.cjs +85 -80
  12. package/src/cable-connection-pool.cjs +11 -11
  13. package/src/cable-subscription-pool.cjs +34 -34
  14. package/src/cable-subscription.cjs +2 -2
  15. package/src/can-can-loader.jsx +7 -7
  16. package/src/can-can.cjs +17 -16
  17. package/src/collection.cjs +36 -36
  18. package/src/command-submit-data.cjs +10 -11
  19. package/src/commands-pool.cjs +13 -13
  20. package/src/custom-error.cjs +20 -14
  21. package/src/deserializer.cjs +7 -7
  22. package/src/devise.cjs +15 -15
  23. package/src/error-logger.cjs +9 -9
  24. package/src/event-connection.jsx +6 -6
  25. package/src/event-created.jsx +8 -8
  26. package/src/event-destroyed.jsx +6 -6
  27. package/src/event-emitter-listener.jsx +6 -6
  28. package/src/event-listener.jsx +8 -8
  29. package/src/event-model-class.jsx +6 -6
  30. package/src/event-updated.jsx +10 -10
  31. package/src/instance-of-class-name.cjs +5 -7
  32. package/src/key-value-store.cjs +21 -18
  33. package/src/logger.cjs +4 -4
  34. package/src/merge.cjs +4 -4
  35. package/src/model-events.cjs +6 -5
  36. package/src/model-name.cjs +6 -5
  37. package/src/model-prop-type.cjs +11 -13
  38. package/src/model-recipes-loader.cjs +4 -4
  39. package/src/model-recipes-model-loader.cjs +45 -15
  40. package/src/models-response-reader.cjs +10 -10
  41. package/src/money-formatter.cjs +11 -11
  42. package/src/params.cjs +7 -7
  43. package/src/preloaded.cjs +6 -6
  44. package/src/resource-route.cjs +14 -12
  45. package/src/resource-routes.jsx +9 -4
  46. package/src/result.cjs +7 -7
  47. package/src/routes-native.cjs +6 -6
  48. package/src/routes.cjs +1 -1
  49. package/src/serializer.cjs +9 -9
  50. package/src/services.cjs +3 -3
  51. package/src/session-status-updater.cjs +14 -14
  52. package/src/source-maps-loader.cjs +11 -11
  53. package/src/translated-collections.cjs +2 -1
  54. package/src/updated-attribute.jsx +17 -14
  55. package/src/validation-error.cjs +5 -8
  56. package/src/validation-errors.cjs +16 -16
  57. package/src/event-location-changed.jsx +0 -21
  58. package/src/history-listener.jsx +0 -20
@@ -1,7 +1,7 @@
1
1
  const debounce = require("debounce")
2
2
  const ModelEvents = require("./model-events.cjs")
3
3
  const PropTypes = require("prop-types")
4
- const PropTypesExact = require("prop-types-exact")
4
+ const propTypesExact = require("prop-types-exact")
5
5
  const React = require("react")
6
6
 
7
7
  export default class ApiMakerEventUpdated extends React.PureComponent {
@@ -9,7 +9,7 @@ export default class ApiMakerEventUpdated extends React.PureComponent {
9
9
  active: true
10
10
  }
11
11
 
12
- static propTypes = PropTypesExact({
12
+ static propTypes = propTypesExact({
13
13
  active: PropTypes.bool.isRequired,
14
14
  debounce: PropTypes.oneOfType([
15
15
  PropTypes.bool,
@@ -20,16 +20,16 @@ export default class ApiMakerEventUpdated extends React.PureComponent {
20
20
  onUpdated: PropTypes.func.isRequired
21
21
  })
22
22
 
23
- constructor(props) {
23
+ constructor (props) {
24
24
  super(props)
25
25
  this.onConnected = this.onConnected.bind(this)
26
26
  }
27
27
 
28
- componentDidMount() {
28
+ componentDidMount () {
29
29
  this.connect()
30
30
  }
31
31
 
32
- componentWillUnmount() {
32
+ componentWillUnmount () {
33
33
  if (this.connectUpdated) {
34
34
  this.connectUpdated.unsubscribe()
35
35
  }
@@ -39,7 +39,7 @@ export default class ApiMakerEventUpdated extends React.PureComponent {
39
39
  }
40
40
  }
41
41
 
42
- connect() {
42
+ connect () {
43
43
  const {model, onConnected} = this.props
44
44
 
45
45
  this.connectUpdated = ModelEvents.connectUpdated(model, (...args) => this.onUpdated(...args))
@@ -49,7 +49,7 @@ export default class ApiMakerEventUpdated extends React.PureComponent {
49
49
  }
50
50
  }
51
51
 
52
- debounce() {
52
+ debounce () {
53
53
  if (!this.debounceInstance) {
54
54
  if (typeof this.props.debounce == "number") {
55
55
  this.debounceInstance = debounce(this.props.onUpdated, this.props.debounce)
@@ -61,11 +61,11 @@ export default class ApiMakerEventUpdated extends React.PureComponent {
61
61
  return this.debounceInstance
62
62
  }
63
63
 
64
- onConnected() {
64
+ onConnected () {
65
65
  this.props.onConnected()
66
66
  }
67
67
 
68
- onUpdated(...args) {
68
+ onUpdated (...args) {
69
69
  if (!this.props.active) {
70
70
  return
71
71
  }
@@ -77,7 +77,7 @@ export default class ApiMakerEventUpdated extends React.PureComponent {
77
77
  }
78
78
  }
79
79
 
80
- render() {
80
+ render () {
81
81
  return null
82
82
  }
83
83
  }
@@ -1,13 +1,11 @@
1
1
  const {digg} = require("diggerize")
2
2
 
3
- const instanceOfClassName = (expectedClassName) => {
4
- return (props, propName, componentName) => {
5
- const prop = digg(props, propName)
6
- const className = digg(prop, "constructor", "name")
3
+ const instanceOfClassName = (expectedClassName) => (props, propName, componentName) => {
4
+ const prop = digg(props, propName)
5
+ const className = digg(prop, "constructor", "name")
7
6
 
8
- if (className != expectedClassName) {
9
- return new Error(`Invalid prop '${propName}' passed to '${componentName}'. Expected a class name of '${expectedClassName}' but got '${className}'.`)
10
- }
7
+ if (className != expectedClassName) {
8
+ return new Error(`Invalid prop '${propName}' passed to '${componentName}'. Expected a class name of '${expectedClassName}' but got '${className}'.`)
11
9
  }
12
10
  }
13
11
 
@@ -1,22 +1,22 @@
1
1
  const Params = require("./params.cjs")
2
2
 
3
3
  module.exports = class KeyValueStore {
4
- static current() {
4
+ static current () {
5
5
  if (!global.currentKeyValueStore)
6
6
  global.currentKeyValueStore = new KeyValueStore()
7
7
 
8
8
  return global.currentKeyValueStore
9
9
  }
10
10
 
11
- static async get(key) {
12
- return await KeyValueStore.current().get(key)
11
+ static get (key) {
12
+ return KeyValueStore.current().get(key)
13
13
  }
14
14
 
15
- static async set(key, value) {
16
- return await KeyValueStore.current().set(key, value)
15
+ static set (key, value) {
16
+ return KeyValueStore.current().set(key, value)
17
17
  }
18
18
 
19
- static async getCachedParams(paramName, args = {}) {
19
+ static async getCachedParams (paramName, args = {}) {
20
20
  const oldQuery = await KeyValueStore.get(paramName)
21
21
  const params = Params.parse()
22
22
 
@@ -29,34 +29,37 @@ module.exports = class KeyValueStore {
29
29
  }
30
30
  }
31
31
 
32
- static async setCachedParams(paramName, qParams) {
33
- return await KeyValueStore.set(paramName, qParams)
32
+ static setCachedParams (paramName, qParams) {
33
+ return KeyValueStore.set(paramName, qParams)
34
34
  }
35
35
 
36
- constructor() {
36
+ constructor () {
37
37
  this.database = new Dexie("KeyValueStoreDatabase")
38
38
  this.database.version(1).stores({
39
39
  keyValues: "++id, key, value"
40
40
  })
41
41
  }
42
42
 
43
- async get(key) {
44
- const row = await this.database.keyValues.where("key").equals(key).first()
43
+ async get (key) {
44
+ const row = await this.database.keyValues
45
+ .where("key")
46
+ .equals(key)
47
+ .first()
45
48
 
46
49
  if (row)
47
50
  return row.value
48
51
  }
49
52
 
50
- async set(key, value) {
51
- const row = await this.database.keyValues.where("key").equals(key).first()
53
+ async set (key, value) {
54
+ const row = await this.database.keyValues
55
+ .where("key")
56
+ .equals(key)
57
+ .first()
52
58
 
53
59
  if (row) {
54
- await this.database.keyValues.update(row.id, {value: value})
60
+ await this.database.keyValues.update(row.id, {value})
55
61
  } else {
56
- await this.database.keyValues.add({
57
- key: key,
58
- value: value
59
- })
62
+ await this.database.keyValues.add({key, value})
60
63
  }
61
64
 
62
65
  return true
package/src/logger.cjs CHANGED
@@ -1,5 +1,5 @@
1
1
  module.exports = class ApiMakerLogger {
2
- static current() {
2
+ static current () {
3
3
  if (!global.apiMakerLogger) {
4
4
  global.apiMakerLogger = new ApiMakerLogger()
5
5
  // global.apiMakerLogger.setDebug(true)
@@ -8,16 +8,16 @@ module.exports = class ApiMakerLogger {
8
8
  return global.apiMakerLogger
9
9
  }
10
10
 
11
- static log(message) {
11
+ static log (message) {
12
12
  ApiMakerLogger.current().log(message)
13
13
  }
14
14
 
15
- log(message) {
15
+ log (message) {
16
16
  if (this.debug)
17
17
  console.log("ApiMaker", message)
18
18
  }
19
19
 
20
- setDebug(value) {
20
+ setDebug (value) {
21
21
  this.debug = value
22
22
  }
23
23
  }
package/src/merge.cjs CHANGED
@@ -1,4 +1,4 @@
1
- function isPlainObject(input) {
1
+ const isPlainObject = (input) => {
2
2
  if (input && typeof input === "object" && !Array.isArray(input)) {
3
3
  return true
4
4
  }
@@ -6,7 +6,7 @@ function isPlainObject(input) {
6
6
  return false
7
7
  }
8
8
 
9
- function merge(firstObject, ...objects) {
9
+ const merge = (firstObject, ...objects) => {
10
10
  for (const object of objects) {
11
11
  mergeObjectsInto(firstObject, object)
12
12
  }
@@ -14,7 +14,7 @@ function merge(firstObject, ...objects) {
14
14
  return firstObject
15
15
  }
16
16
 
17
- function mergeArraysInto(mergeIntoValue, ...arrays) {
17
+ const mergeArraysInto = (mergeIntoValue, ...arrays) => {
18
18
  for (const array of arrays) {
19
19
  for (const value of array) {
20
20
  if (!mergeIntoValue.includes(value)) {
@@ -24,7 +24,7 @@ function mergeArraysInto(mergeIntoValue, ...arrays) {
24
24
  }
25
25
  }
26
26
 
27
- function mergeObjectsInto(mergeInto, object) {
27
+ const mergeObjectsInto = (mergeInto, object) => {
28
28
  for (const key in object) {
29
29
  const value = object[key]
30
30
 
@@ -1,35 +1,36 @@
1
1
  const CableConnectionPool = require("./cable-connection-pool.cjs")
2
+ const {digg} = require("diggerize")
2
3
 
3
4
  module.exports = class ModelEvents {
4
- static connect(model, eventName, callback) {
5
+ static connect (model, eventName, callback) {
5
6
  const modelClassName = digg(model.modelClassData(), "name")
6
7
  const cableSubscription = CableConnectionPool.current().connectEvent(modelClassName, model.primaryKey(), eventName, callback)
7
8
 
8
9
  return cableSubscription
9
10
  }
10
11
 
11
- static connectModelClass(modelClass, eventName, callback) {
12
+ static connectModelClass (modelClass, eventName, callback) {
12
13
  const modelClassName = digg(modelClass.modelClassData(), "name")
13
14
  const cableSubscription = CableConnectionPool.current().connectModelClassEvent(modelClassName, eventName, callback)
14
15
 
15
16
  return cableSubscription
16
17
  }
17
18
 
18
- static connectCreated(modelClass, callback) {
19
+ static connectCreated (modelClass, callback) {
19
20
  const modelClassName = digg(modelClass.modelClassData(), "name")
20
21
  const cableSubscription = CableConnectionPool.current().connectCreated(modelClassName, callback)
21
22
 
22
23
  return cableSubscription
23
24
  }
24
25
 
25
- static connectDestroyed(model, callback) {
26
+ static connectDestroyed (model, callback) {
26
27
  const modelClassName = digg(model.modelClassData(), "name")
27
28
  const cableSubscription = CableConnectionPool.current().connectDestroyed(modelClassName, model.primaryKey(), callback)
28
29
 
29
30
  return cableSubscription
30
31
  }
31
32
 
32
- static connectUpdated(model, callback) {
33
+ static connectUpdated (model, callback) {
33
34
  const modelClassName = digg(model.modelClassData(), "name")
34
35
  const cableSubscription = CableConnectionPool.current().connectUpdate(modelClassName, model.primaryKey(), callback)
35
36
 
@@ -1,15 +1,16 @@
1
1
  module.exports = class ModelName {
2
- constructor(data) {
2
+ constructor (data) {
3
3
  this.data = data
4
4
  }
5
5
 
6
- human(args) {
7
- if (!args)
8
- args = {count: 1}
6
+ human (args) {
7
+ let argsToUse = args
8
+
9
+ if (!argsToUse) argsToUse = {count: 1}
9
10
 
10
11
  let countKey
11
12
 
12
- if (args.count > 1 || args.count < 0) {
13
+ if (argsToUse.count > 1 || argsToUse.count < 0) {
13
14
  countKey = "other"
14
15
  } else {
15
16
  countKey = "one"
@@ -2,7 +2,7 @@ const {digg} = require("diggerize")
2
2
  const Inflection = require("inflection")
3
3
 
4
4
  module.exports = class ApiMakerModelPropType {
5
- static ofModel(modelClass) {
5
+ static ofModel (modelClass) {
6
6
  const modelPropTypeInstance = new ApiMakerModelPropType()
7
7
 
8
8
  modelPropTypeInstance.withModelType(modelClass)
@@ -10,13 +10,13 @@ module.exports = class ApiMakerModelPropType {
10
10
  return modelPropTypeInstance
11
11
  }
12
12
 
13
- constructor() {
13
+ constructor () {
14
14
  this.isNotRequired = this.isNotRequired.bind(this)
15
15
  this.isRequired = this.isRequired.bind(this)
16
16
  this._withLoadedAssociations = {}
17
17
  }
18
18
 
19
- isNotRequired(props, propName, _componentName) {
19
+ isNotRequired (props, propName, _componentName) {
20
20
  const model = props[propName]
21
21
 
22
22
  if (model) {
@@ -24,7 +24,7 @@ module.exports = class ApiMakerModelPropType {
24
24
  }
25
25
  }
26
26
 
27
- isRequired(props, propName, _componentName) {
27
+ isRequired (props, propName, _componentName) {
28
28
  const model = props[propName]
29
29
 
30
30
  if (!model) return new Error(`${propName} was required but not given`)
@@ -32,21 +32,21 @@ module.exports = class ApiMakerModelPropType {
32
32
  return this.validate({model, propName})
33
33
  }
34
34
 
35
- previous() {
35
+ previous () {
36
36
  if (!this._previousModelPropType) throw new Error("No previous model prop type set")
37
37
 
38
38
  return this._previousModelPropType
39
39
  }
40
40
 
41
- setPreviousModelPropType(previousModelPropType) {
41
+ setPreviousModelPropType (previousModelPropType) {
42
42
  this._previousModelPropType = previousModelPropType
43
43
  }
44
44
 
45
- withModelType(modelClass) {
45
+ withModelType (modelClass) {
46
46
  this._withModelType = modelClass
47
47
  }
48
48
 
49
- validate({model, propName}) {
49
+ validate ({model, propName}) {
50
50
  if (this._withModelType && this._withModelType.name != model.constructor.name)
51
51
  return new Error(`Expected ${propName} to be of type ${this._withModelType.name} but it wasn't: ${model.constructor.name}`)
52
52
 
@@ -69,8 +69,6 @@ module.exports = class ApiMakerModelPropType {
69
69
 
70
70
  const associationCache = digg(model.relationshipsCache, underscoreAssociationName)
71
71
 
72
- let associationModel
73
-
74
72
  // Find a model to run sub-model-prop-type-validations on
75
73
  if (Array.isArray(associationCache)) {
76
74
  for (const preloadedModel of associationCache) {
@@ -103,13 +101,13 @@ module.exports = class ApiMakerModelPropType {
103
101
  }
104
102
  }
105
103
 
106
- withLoadedAbilities(arrayOfAbilities) {
104
+ withLoadedAbilities (arrayOfAbilities) {
107
105
  this._withLoadedAbilities = arrayOfAbilities
108
106
 
109
107
  return this
110
108
  }
111
109
 
112
- withLoadedAssociation(associationName) {
110
+ withLoadedAssociation (associationName) {
113
111
  const associationModelPropType = new ApiMakerModelPropType()
114
112
 
115
113
  associationModelPropType.setPreviousModelPropType(this)
@@ -118,7 +116,7 @@ module.exports = class ApiMakerModelPropType {
118
116
  return associationModelPropType
119
117
  }
120
118
 
121
- withLoadedAttributes(arrayOfAttributes) {
119
+ withLoadedAttributes (arrayOfAttributes) {
122
120
  this._withLoadedAttributes = arrayOfAttributes
123
121
 
124
122
  return this
@@ -1,17 +1,17 @@
1
- const {digs} = require("diggerize")
1
+ const {digg, digs} = require("diggerize")
2
2
  const ModelRecipesModelLoader = require("./model-recipes-model-loader.cjs")
3
3
 
4
4
  module.exports = class ModelRecipesLoader {
5
- constructor({recipes}) {
5
+ constructor ({recipes}) {
6
6
  this.modelClasses = {}
7
7
  this.recipes = recipes
8
8
  }
9
9
 
10
- getModelClass(name) {
10
+ getModelClass (name) {
11
11
  return digg(this, "modelClasses", name)
12
12
  }
13
13
 
14
- load() {
14
+ load () {
15
15
  const {recipes} = digs(this, "recipes")
16
16
  const {models} = digs(recipes, "models")
17
17
 
@@ -1,17 +1,17 @@
1
1
  const BaseModel = require("./base-model.cjs")
2
2
  const Collection = require("./collection.cjs")
3
- const {digg} = require("diggerize")
3
+ const {digg, digs} = require("diggerize")
4
4
  const inflection = require("inflection")
5
5
 
6
6
  module.exports = class ApiMakerModelRecipesModelLoader {
7
- constructor({modelRecipe, modelRecipesLoader}) {
7
+ constructor ({modelRecipe, modelRecipesLoader}) {
8
8
  if (!modelRecipe) throw new Error("No 'modelRecipe' was given")
9
9
 
10
10
  this.modelRecipesLoader = modelRecipesLoader
11
11
  this.modelRecipe = modelRecipe
12
12
  }
13
13
 
14
- createClass() {
14
+ createClass () {
15
15
  const {modelRecipe} = digs(this, "modelRecipe")
16
16
  const {
17
17
  attributes,
@@ -42,14 +42,17 @@ module.exports = class ApiMakerModelRecipesModelLoader {
42
42
  return ModelClass
43
43
  }
44
44
 
45
- addAttributeMethodsToModelClass(ModelClass, attributes) {
45
+ addAttributeMethodsToModelClass (ModelClass, attributes) {
46
46
  for (const attributeName in attributes) {
47
47
  const attribute = attributes[attributeName]
48
48
  const {name} = digs(attribute, "name")
49
49
  const methodName = inflection.camelize(name, true)
50
50
  const hasMethodName = inflection.camelize(`has_${name}`, true)
51
51
 
52
- ModelClass.prototype[methodName] = function () { return this.readAttributeUnderscore(attributeName) }
52
+ ModelClass.prototype[methodName] = function () {
53
+ return this.readAttributeUnderscore(attributeName)
54
+ }
55
+
53
56
  ModelClass.prototype[hasMethodName] = function () {
54
57
  const value = this[methodName]()
55
58
 
@@ -58,7 +61,7 @@ module.exports = class ApiMakerModelRecipesModelLoader {
58
61
  }
59
62
  }
60
63
 
61
- addCollectionCommandsToModelClass(ModelClass, collectionCommands) {
64
+ addCollectionCommandsToModelClass (ModelClass, collectionCommands) {
62
65
  for (const collectionCommandName in collectionCommands) {
63
66
  const methodName = inflection.camelize(collectionCommandName, true)
64
67
 
@@ -76,7 +79,7 @@ module.exports = class ApiMakerModelRecipesModelLoader {
76
79
  }
77
80
  }
78
81
 
79
- addMemberCommandsToModelClass(ModelClass, memberCommands) {
82
+ addMemberCommandsToModelClass (ModelClass, memberCommands) {
80
83
  for (const memberCommandName in memberCommands) {
81
84
  const methodName = inflection.camelize(memberCommandName, true)
82
85
 
@@ -95,7 +98,7 @@ module.exports = class ApiMakerModelRecipesModelLoader {
95
98
  }
96
99
  }
97
100
 
98
- addRelationshipsToModelClass(ModelClass, modelClassData, relationships) {
101
+ addRelationshipsToModelClass (ModelClass, modelClassData, relationships) {
99
102
  const {modelRecipesLoader} = digs(this, "modelRecipesLoader")
100
103
 
101
104
  for (const relationshipName in relationships) {
@@ -145,6 +148,7 @@ module.exports = class ApiMakerModelRecipesModelLoader {
145
148
  modelMethodName,
146
149
  modelRecipesLoader,
147
150
  optionsAs,
151
+ optionsPrimaryKey,
148
152
  optionsThrough,
149
153
  relationshipName,
150
154
  resourceName
@@ -169,13 +173,13 @@ module.exports = class ApiMakerModelRecipesModelLoader {
169
173
  }
170
174
  }
171
175
 
172
- defineBelongsToGetMethod({ModelClass, modelMethodName, relationshipName}) {
176
+ defineBelongsToGetMethod ({ModelClass, modelMethodName, relationshipName}) {
173
177
  ModelClass.prototype[modelMethodName] = function () {
174
178
  return this._readBelongsToReflection({reflectionName: relationshipName})
175
179
  }
176
180
  }
177
181
 
178
- defineBelongsToLoadMethod({foreignKey, klassPrimaryKey, ModelClass, modelRecipesLoader, loadMethodName, optionsPrimaryKey, relationshipName, resourceName}) {
182
+ defineBelongsToLoadMethod ({foreignKey, klassPrimaryKey, ModelClass, modelRecipesLoader, loadMethodName, optionsPrimaryKey, relationshipName, resourceName}) {
179
183
  ModelClass.prototype[loadMethodName] = function () {
180
184
  const foreignKeyMethodName = inflection.camelize(foreignKey, true)
181
185
 
@@ -195,7 +199,19 @@ module.exports = class ApiMakerModelRecipesModelLoader {
195
199
  }
196
200
  }
197
201
 
198
- defineHasManyGetMethod({activeRecordName, className, foreignKey, ModelClass, modelMethodName, modelRecipesLoader, optionsAs, optionsThrough, relationshipName, resourceName}) {
202
+ defineHasManyGetMethod ({
203
+ activeRecordName,
204
+ className,
205
+ foreignKey,
206
+ ModelClass,
207
+ modelMethodName,
208
+ modelRecipesLoader,
209
+ optionsAs,
210
+ optionsPrimaryKey,
211
+ optionsThrough,
212
+ relationshipName,
213
+ resourceName
214
+ }) {
199
215
  ModelClass.prototype[modelMethodName] = function () {
200
216
  const id = this.primaryKey()
201
217
  const modelClass = modelRecipesLoader.getModelClass(resourceName)
@@ -224,8 +240,12 @@ module.exports = class ApiMakerModelRecipesModelLoader {
224
240
  }
225
241
  } else {
226
242
  const ransack = {}
243
+ const primaryKeyColumnName = optionsPrimaryKey || digg(ModelClass.modelClassData(), "primaryKey")
244
+ const primaryKeyMethodName = inflection.camelize(primaryKeyColumnName, true)
245
+
246
+ if (!(primaryKeyMethodName in this)) throw new Error(`No such primary key method: ${primaryKeyMethodName}`)
227
247
 
228
- ransack[`${foreignKey}_eq`] = this.primaryKey()
248
+ ransack[`${foreignKey}_eq`] = this[primaryKeyMethodName]()
229
249
 
230
250
  if (optionsAs) {
231
251
  ransack[`${optionsAs}_type_eq`] = activeRecordName
@@ -238,7 +258,7 @@ module.exports = class ApiMakerModelRecipesModelLoader {
238
258
  }
239
259
  }
240
260
 
241
- defineHasManyLoadMethod({foreignKey, loadMethodName, ModelClass, modelClassData, modelRecipesLoader, optionsThrough, relationshipName, resourceName}) {
261
+ defineHasManyLoadMethod ({foreignKey, loadMethodName, ModelClass, modelClassData, modelRecipesLoader, optionsThrough, relationshipName, resourceName}) {
242
262
  ModelClass.prototype[loadMethodName] = function () {
243
263
  const id = this.primaryKey()
244
264
  const modelClass = modelRecipesLoader.getModelClass(resourceName)
@@ -279,13 +299,23 @@ module.exports = class ApiMakerModelRecipesModelLoader {
279
299
  }
280
300
  }
281
301
 
282
- defineHasOneGetMethd({ModelClass, modelMethodName, relationshipName}) {
302
+ defineHasOneGetMethd ({ModelClass, modelMethodName, relationshipName}) {
283
303
  ModelClass.prototype[modelMethodName] = function () {
284
304
  return this._readHasOneReflection({reflectionName: relationshipName})
285
305
  }
286
306
  }
287
307
 
288
- defineHasOneLoadMethod({activeRecordPrimaryKey, foreignKey, loadMethodName, ModelClass, modelClassData, modelRecipesLoader, optionsThrough, relationshipName, resourceName}) {
308
+ defineHasOneLoadMethod ({
309
+ activeRecordPrimaryKey,
310
+ foreignKey,
311
+ loadMethodName,
312
+ ModelClass,
313
+ modelClassData,
314
+ modelRecipesLoader,
315
+ optionsThrough,
316
+ relationshipName,
317
+ resourceName
318
+ }) {
289
319
  ModelClass.prototype[loadMethodName] = function () {
290
320
  const primaryKeyMethodName = inflection.camelize(activeRecordPrimaryKey, true)
291
321
 
@@ -3,36 +3,36 @@ const inflection = require("inflection")
3
3
  const Preloaded = require("./preloaded.cjs")
4
4
 
5
5
  module.exports = class ModelsResponseReader {
6
- static first(response) {
6
+ static first (response) {
7
7
  return ModelsResponseReader.collection(response)[0]
8
8
  }
9
9
 
10
- static collection(response) {
11
- const reader = new ModelsResponseReader({response: response})
10
+ static collection (response) {
11
+ const reader = new ModelsResponseReader({response})
12
12
  return reader.models()
13
13
  }
14
14
 
15
- constructor(args) {
15
+ constructor (args) {
16
16
  this.collection = args.collection
17
17
  this.response = args.response
18
18
  }
19
19
 
20
- models() {
20
+ models () {
21
21
  const preloaded = new Preloaded(this.response)
22
22
  const models = []
23
23
 
24
- for(const modelType in this.response.data) {
24
+ for (const modelType in this.response.data) {
25
25
  const modelClassName = inflection.classify(modelType)
26
- const modelClass = digg(require("@kaspernj/api-maker/src/models"), modelClassName)
27
- const collectionName = modelClass.modelClassData().collectionName
26
+ const ModelClass = digg(require("@kaspernj/api-maker/src/models"), modelClassName)
27
+ const collectionName = ModelClass.modelClassData().collectionName
28
28
 
29
- for(const modelId of this.response.data[modelType]) {
29
+ for (const modelId of this.response.data[modelType]) {
30
30
  const modelData = this.response.preloaded[collectionName][modelId]
31
31
 
32
32
  if (!modelData)
33
33
  throw new Error(`Couldn't find model data for ${collectionName}(${modelId})`)
34
34
 
35
- const model = new modelClass({
35
+ const model = new ModelClass({
36
36
  collection: this.collection,
37
37
  data: modelData,
38
38
  isNewRecord: false