Lucid: Model instance should have toJSON method

Created on 22 Feb 2016  路  9Comments  路  Source: adonisjs/lucid

When getting a record via Model.find(), the returned object ignores the visible/hidden fields declared in the Model declaration. Also; the record itself is inside the attributes key & calling model.toJSON() doesn't work.

Getting the record via model.where().fetch() works fine. However; I can't access the first object of the returned array.

const record = yield Model.where('id', '=', 1).fetch()
console.log(record)
res.send(record[0])

When logging the record object in console, it looks like an array [{ ... }] but getting the first object of that array record[0] does not work. It does work after calling the model.toJSON() method.

  1. Why do find() & where() behave so differently?
  2. Why isn't it possible to get the first object of the array?

Most helpful comment

Yes

All 9 comments

Couple of things here.

First .find method returns the model instance not a collection, so that is the reason .toJSON does not work. Ideally model instance also should have .toJSON method.

Second when you call .fetch method you get a collection back, which is nothing but a lodash wrapper. So in order to get the first record, you have to say.

const record = yield Model.where('id', '=', 1).fetch()
console.log(record.first())

Also i am refactoring Lucid, so in coming future all these inconsistency issues will be resolved.

Any reason why find() is a oh-so-special method that doesn't return a collection?
Was it just forgotten?
What's the point in using Collection, if some methods use it, but others don't?
It creates inconsistencies, so I end up just removing wrappers from everything just to make things work.

And collection also uses a very old version of lodash, so I have to install a separate version...

Collection seems less like something convenient, and more like a pain in the ass...

Coz Collection means collection of multiple rows not collection of a single row.

When you have multiple rows wrapped inside a collection, you can perform operations like map, filter etc. Why would you perform those operations one a single row?

where().first() returns just one row (not inside a list), but it's considered a collection (aka has a lodash wrapper, because that's really all that Collection is...)

For consistency it should either return the row inside a list, or return just a Model instance like find, or make find also return a collection.

If you are on adonis-lucid:^2.0 than yes. In 3.0 it will return the model instance.

Anytime when a single row is fetched intentionally, it will always be an instance otherwise a collection.

Also, I notice that whenever a Collection is used, the wrapped values are not model instances, thus creating another inconsistency.
Is this also fixed in 3.0?

Yes

@thetutlage @marines

const feed =  yield user.feed().fetch()
let index = 0
while( index < _.size(feed.value()) ) {
  // as feed.value() will fetch collection not instance of model
 // i need instance to update data in feed table
  index++
}

All good with Database query update, wanna do with db instance. Please guide.

Thanks!!

Was this page helpful?
0 / 5 - 0 ratings