Open stubailo opened 8 years ago
Do you guys think you can create a helper query function that does ...myQuery.fetch()[0].field
in a safe way? As I'm looking at the React Todos example, I'm seeing that call here and there, which can be potentially error-throwing if an empty array is returned.
Perhaps something that wraps (...myQuery.fetch()[0] || {}).field
, assuming that .fetch()
always returns an array even if it's empty?
Edit: I noticed that Bookshelf has a fetchOne
, which could work, however, since SQL gives you the ability to just give you one value that is 'columnless', like a COUNT
query, perhaps something that does .fetchValue()
or something like that, i.e. Lists.select('incomplete_todos').where({ id:1 }).fetchValue()
?
Isn't this something that could also be useful for Mongo? Like, Todos.findOne().field
will also throw an error.
I guess this is one place where the CoffeeScript ?.
comes in super handy.
I think this would be a great thing to open a new issue for, since it's not technically related to filling in parts of existing Knex syntax.
Yeah, it would be helpful for Mongo too. I find myself writing a lot of (Todos.findOne() || {}).field
or singleTodo && singleTodo.field
, which is so annoying.
I moved it to #9.
Here's an initial list: