Skip to content

Commit

Permalink
feat(basemodel): compute refs from the added columns
Browse files Browse the repository at this point in the history
  • Loading branch information
thetutlage committed Oct 7, 2019
1 parent 212b5e9 commit dc79df7
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/Orm/BaseModel/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ export class BaseModel implements ModelContract {
Object.defineProperty(this, '$columns', { value: new Map() })
Object.defineProperty(this, '$computed', { value: new Map() })
Object.defineProperty(this, '$relations', { value: new Map() })
Object.defineProperty(this, '$refs', { value: {} })
Object.defineProperty(this, '_mappings', {
value: {
cast: new Map(),
Expand Down Expand Up @@ -219,6 +220,7 @@ export class BaseModel implements ModelContract {

this.$columns.set(name, column)
this._mappings.cast.set(column.castAs!, name)
this.$refs[name] = column.castAs
}

/**
Expand Down
Binary file added test-helpers/tmp/db.sqlite
Binary file not shown.
16 changes: 16 additions & 0 deletions test/orm/base-model.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,22 @@ test.group('Base model | boot', (group) => {
assert.deepEqual(mapToObj(User.$columns), {})
assert.deepEqual(mapToObj(User.$relations), {})
assert.deepEqual(mapToObj(User.$computed), {})
assert.deepEqual(User.$refs, {})
})

test('compute refs from the added columns', async (assert) => {
class User extends BaseModel {
public static $increments = false

@column({ primary: true })
public id: number

@column()
public userName: string
}

User.$boot()
assert.deepEqual(User.$refs, { id: 'id', userName: 'user_name' })
})
})

Expand Down

0 comments on commit dc79df7

Please sign in to comment.