Skip to content

Commit

Permalink
refactor: do not have defaults for relationships, let the end user de…
Browse files Browse the repository at this point in the history
…fine them
  • Loading branch information
thetutlage committed Oct 6, 2019
1 parent 2e18430 commit 6496e70
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 42 deletions.
20 changes: 8 additions & 12 deletions src/Orm/BaseModel/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -703,19 +703,15 @@ export class BaseModel implements ModelContract {
/**
* Returns the related model or default value when model is missing
*/
public $getRelated (key: string, defaultValue: any): any {
if (this.$hasRelated(key)) {
return this.$preloaded[key as string]
}

return defaultValue
public $getRelated (key: string): any {
return this.$preloaded[key]
}

/**
* A boolean to know if relationship has been preloaded or not
*/
public $hasRelated (key: string): boolean {
return this.$preloaded[key as string] !== undefined
return this.$preloaded[key] !== undefined
}

/**
Expand All @@ -739,13 +735,13 @@ export class BaseModel implements ModelContract {
const manyRelationships = ['hasMany', 'manyToMany', 'hasManyThrough']
if (manyRelationships.includes(relation.type)) {
if (!Array.isArray(models)) {
throw new Exception(`
$setRelated accepts an array of related models for ${manyRelationships.join(',')} relationships,
`)
throw new Exception(
`${Model}.${key} must be an array (${manyRelationships.join(',')} relationships)`,
)
}
this.$preloaded[key as string] = models
this.$preloaded[key] = models
} else {
this.$preloaded[key as string] = models as unknown as ModelContract
this.$preloaded[key] = models as unknown as ModelContract
}
}

Expand Down
30 changes: 4 additions & 26 deletions src/Orm/BaseModel/proxyHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,30 +9,7 @@

/// <reference path="../../../adonis-typings/index.ts" />

import { Exception } from '@poppinss/utils'
import { ModelConstructorContract, AvailableRelations } from '@ioc:Adonis/Lucid/Model'

/**
* Value to return when relationship is not preloaded
* with the model instance.
*
* We are re-using the defaults from a static source to avoid creating empty arrays
* everytime someone access the relationship. However, as a downside, if someone
* decides to mutate the array, that will mutate the source and hence we
* freeze the arrays.
*
* The `Object.freeze` however doesn't stop one from defining values for a specific
* index.
*/
const DEFAULTS: {
[P in AvailableRelations]: any
} = {
hasOne: null,
hasMany: Object.freeze([]),
belongsTo: null,
manyToMany: Object.freeze([]),
hasManyThrough: Object.freeze([]),
}
import { ModelConstructorContract } from '@ioc:Adonis/Lucid/Model'

/**
* A proxy trap to add support for custom getters and setters
Expand All @@ -55,7 +32,7 @@ export const proxyHandler = {
*/
const relation = Model.$getRelation(key)
if (relation) {
return target.$getRelated(key, DEFAULTS[relation.type])
return target.$getRelated(key)
}

return Reflect.get(target, key, receiver)
Expand All @@ -79,7 +56,8 @@ export const proxyHandler = {
*/
const relation = Model.$getRelation(key)
if (relation) {
throw new Exception('Cannot set relationships locally', 500, 'E_CANNOT_DEFINE_RELATIONSHIP')
target.$setRelated(key, value)
return true
}

return Reflect.set(target, key, value, receiver)
Expand Down
4 changes: 2 additions & 2 deletions test/orm/base-model.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1114,7 +1114,7 @@ test.group('Base Model | relations', (group) => {
assert.instanceOf(user.$preloaded.profile, Profile)
})

test('return null when relation is not preloaded', (assert) => {
test('return undefined when relation is not preloaded', (assert) => {
class Profile extends BaseModel {
@column()
public username: string
Expand All @@ -1136,7 +1136,7 @@ test.group('Base Model | relations', (group) => {
id: 1,
})

assert.isNull(user.profile)
assert.isUndefined(user.profile)
assert.deepEqual(user.$preloaded, {})
})

Expand Down
2 changes: 1 addition & 1 deletion test/orm/model-belongs-to.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -476,7 +476,7 @@ test.group('Model | BelongsTo', (group) => {
builder.whereNull('username')
}).first()

assert.isNull(profile!.user)
assert.isUndefined(profile!.user)
})

test('preload nested relations', async (assert) => {
Expand Down
2 changes: 1 addition & 1 deletion test/orm/model-has-one.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -446,7 +446,7 @@ test.group('Model | HasOne | preload', (group) => {
builder.whereNull('display_name')
}).where('username', 'virk').first()

assert.isNull(user!.profile)
assert.isUndefined(user!.profile)
})

test('preload nested relations', async (assert) => {
Expand Down

0 comments on commit 6496e70

Please sign in to comment.