3 ms·
Perhaps it depends what you're doing? IMO: .where('column_a', '=', 'value1') .and(q => q.isNull('column_b').orWhere('column_b', '=', 'value2'))) is a
by nicoburns 3y ago
Perhaps it depends what you're doing?
IMO:
.where('column_a', '=', 'value1')
.and(q => q.isNull('column_b').orWhere('column_b', '=', 'value2')))
is a lot less readable than:
WHERE
column_a = 'value1'
AND (column_b IS NULL OR column_b = 'value2')
- DangitBobby 3y agoIn Django that would be .filter(column_a='value1', Q(column_b__isnull=True)|Q(column_b='value2')) And obviously you can use whatever indentations you like.
- nicoburns 3y agoAh, that's quite a bit nicer. You can't do that in JavaScript on two counts: - No keyword arguments - No operator overloading (so you can't override | to get the nice "or" syntax)
- DangitBobby 3y agoI actually messed it up a little because I'm not sure you can mix positional and kwargs in filter, and you definitely can't use kwargs first. Still, the idea is there. In JS, theoretically you could design an API like .where({column_a: 1}, Q(column_b__isnull=True).or({column_b: 2})) Which really isn't bad IMO
- squeaky-clean 3y agoIt's been a bit since I've used Django, but I believe you can just swap the order so the kwarg comes last. filter( Q(column_b__isnull=True)|Q(column_b='value2'), column_a='value1', ) Or just turn it into another Q .filter(Q(column_a='value1'), Q(column_b__isnull=True)|Q(column_b='value2'))
- Izkata 3y agoThe docs show both of those as working examples, but Q supports using & for "and", and can be combined in any way - you could also do this: .filter(Q(column_a='value1') & (Q(column_b__isnull=True) | Q(column_b='value2')))
- LinXitoW 3y agoOne of these my IDE can typecheck and apply code hightlighting, the other is just a blob of text.
- pjmlp 3y agoOn my case that would be SQL, as I use nice SQL aware IDEs for Oracle and SQL Server.