[ad_1]
|
I’ve also tried posting them in two other subs and both pulled them down as NSFW. 😂 submitted by /u/strikecat18 |
[ad_2]
/u/strikecat18
Source link
Humor | ReportWire publishes the latest breaking U.S. and world news, trending topics and developing stories from around globe.
[ad_1]
|
I’ve also tried posting them in two other subs and both pulled them down as NSFW. 😂 submitted by /u/strikecat18 |
[ad_2]
/u/strikecat18
Source link
[ad_1]
While I’m not hugely fond of ORMs (I’d argue that relations and objects don’t map neatly to each other, and any ORM is going to be a very leaky abstraction for all but trivial cases), that’s not because I love writing SQL. I’m a big fan of query-builder tools; describe your query programatically, and have an API that generates the required SQL as a result. This cuts down on developer error, and also hopefully handles all the weird little dialects that every database has.
For example, did you know Postgres has an @> operator? It’s a contains operation, which returns true if an array, range, or JSON dictionary contains your search term. Basically, an advanced “in” operation.
Gretchen‘s team is using the Knex library, which doesn’t have a built-in method for constructing those kinds of queries. But that’s fine, because it does offer a whereRaw method, which allows you to supply raw SQL. The nice thing about this is that you can still parameterize your query, and Knex will handle all the fun things, like transforming an array into a string.
Or you could just not use that, and write the code yourself:
exports.buildArrayString = jsArray => {
let arrayString = '{';
for(let i = 0; i < jsArray.length; i++) {
arrayString += jsArray[i];
if(i + 1 < jsArray.length) {
arrayString += ','
}
}
arrayString += '}';
return arrayString;
}
There’s the string munging we know and love. This constructs a Postgres array, which is wrapped in curly braces.
Also, little pro-tip for generating comma separated code, and this is just a real tiny optimization: before the loop append item zero, start the loop with item 1, and then you can unconditionally prepend a comma, removing any conditional logic from your loop. That’s not a WTF, but I’ve seen so much otherwise good code make that mistake I figured I’d bring it up.
exports.buildArrayContainsQuery = (key, values) => {
let returnQueryParams = [];
returnQueryParams.push(`${key} @> ?`);
returnQueryParams.push(exports.buildArrayString(values));
return returnQueryParams;
}
And here’s where it’s used. “do we need input safety checks here?” is never a comment I like to see as a TODO. That said, because we are still using Knex’s parameter handling, I’d hope it handles escaping correctly so that the answer to this question is “no”. If the answer is “yes” for some reason, I’d stop using this library!
That said, all of this code becomes superfluous, especially when you read the comments in this function. I could just directly run query.whereRaw('_tags @> ?', myArray); I don’t need to munge the string myself. I don’t need to write a function which returns an array of parameters that I have to split back up to pass to the query I want to call.
Here’s the worst part of all of this: these functions exist in a file called sqlUtils.js, which is just a pile of badly re-invented wheels, and the only thing they have in common is that they’re vaguely related to database operations.
[ad_2]
Remy Porter
Source link
[ad_1]
Arguably, the worst moment for date times was the shift from Julian to Gregorian calendars. The upgrade took a long time, too, as some countries were using the Julian calendar over 300 years from the official changeover, famously featured in the likely aprochryphal story about Russia arriving late for the Olympics.
At least that change didn’t involve adding any extra months, unlike some of the Julian reforms, which involved adding multiple “intercalary months” to get the year back in sync after missing a pile of leap years.
Speaking of adding months, Will J sends us this “calendar” enum:
enum Calendar
{
April = 0,
August = 1,
December = 2,
February = 3,
Friday = 4,
January = 5,
July = 6,
June = 7,
March = 8,
May = 9,
Monday = 10,
November = 11,
October = 12,
PublicHoliday = 13,
Saturday = 14,
Sunday = 15,
September = 16,
Thursday = 17,
Tuesday = 18,
Wednesday = 19
}
Honestly, the weather in PublicHoliday is usually a bit too cold for my tastes. A little later into the spring, like Saturday, is usually a nicer month.
Will offers the hypothesis that some clever developer was trying to optimize compile times: obviously, emitting code for one enum has to be more efficient than emitting code for many enums. I think it more likely that someone just wanted to shove all the calendar stuff into one bucket.
Will further adds:
One of my colleagues points out that the only thing wrong with this enum is that September should be before Sunday.
Yes, arguably, since this enum clearly was meant to be sorted in alphabetical order, but that raises the question of: should it really?
[ad_2]
Remy Porter
Source link