Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

toJSON fix #211

Merged
merged 5 commits into from
Nov 8, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions src/Lucid/Model/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,8 @@ class Model extends BaseModel {
static async create (payload, trx) {
const modelInstance = new this()
modelInstance.fill(payload)
modelInstance.$visible = this.visible
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be moved to _instantiate method.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And will be nice to have a small test around it

modelInstance.$hidden = this.hidden
await modelInstance.save(trx)
return modelInstance
}
Expand Down Expand Up @@ -1326,13 +1328,13 @@ class Model extends BaseModel {
}

if (!this.isNew) {
const attributes = await this.constructor.find(this.primaryKeyValue)
if (!attributes) {
const newInstance = await this.constructor.find(this.primaryKeyValue)
if (!newInstance) {
throw GE
.RuntimeException
.invoke(`Cannot reload model since row with ${this.constructor.primaryKey} ${this.primaryKeyValue} has been removed`)
}
this.newUp(attributes)
this.newUp(newInstance.$attributes)
}
}
}
Expand Down
28 changes: 28 additions & 0 deletions test/unit/lucid-serializer.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -223,4 +223,32 @@ test.group('Relations | Serializer', (group) => {
assert.equal(json.perPage, 20)
assert.equal(json.lastPage, 1)
})

test('test toJSON with visible()', async (assert) => {
class User extends Model {
static get visible () {
return ['id', 'username']
}
}
User._bootIfNotBooted()
await ioc.use('Database').table('users').insert([{ username: 'virk' }, { username: 'nikk' }])

// Test 1 - find in DB
const a = await User.find(1)
assert.deepEqual(a.toJSON(), {id: 1, username: 'virk'})

// // test 2 - find in db then reload
const b = await User.find(1)
await b.reload()
assert.deepEqual(b.toJSON(), {id: 1, username: 'virk'})

// // test 3 - create
const c = await User.create({username: 'ben'})
assert.deepEqual(c.toJSON(), {id: 3, username: 'ben'})

// // test 4 - create then reload from db
const d = await User.create({username: 'simon'})
await d.reload()
assert.deepEqual(d.toJSON(), {id: 4, username: 'simon'})
})
})