The easiest city to relocate to in Europe revealed as UK emigration numbers jump more than six per cent
Katherine Ryan breaks her years-long silence on the father of her daughter, 16, as she claims he 'never pays for anything, hardly makes contact and doesn't make any sacrifices for her'
I spent £50 successfully proving my parking ticket was unfair. Can I get my expenses back? DEAN DUNHAM replies
Mother whose sea view was blocked by illegal wall is shocked as council back developers who built it
Suella Braverman vows to abolish the Equalities Office if Reform win power and warns Britain is being 'ripped apart' by diversity policies
Zara and Mike Tindall 'will support' William and Kate over Beatrice and Eugenie: Royal insider reveals why Princess Anne's daughter will 'naturally' side with the Future King and Queen
Lindsey Vonn's Winter Olympics ski crash injury is 'a lot more severe than a broken leg' with her 'leg in pieces' after specialists suggested she may need amputation
PETER HITCHENS: 72 years after Lord of the Flies shocked Britain, its warning still rings true - where fathers are absent, boys are a growing danger in our violent society
Florence Pugh sports a huge ring on her engagement finger after sparking rumours she is set to marry her Peaky Blinders star boyfriend Finn Cole
Outrage as three MILLION dogs to be 'massacred' by firing squad ahead of the World Cup
Emilia Jones exudes elegance in a pale pink dress as she joins chic Marisa Abela at the dunhill pre-BAFTA Filmmakers dinner
Wealthy HSBC banker dodged £5,900 in train fares after using 'doughnutting' ticket scam on journeys into London from his £2m home: Fraudster is banned from railway
Tourists are ordered to leave Italian and Swiss Alps as villages are evacuated because there's too much SNOW
Moment palace intruder who announced he was 'the King' is handcuffed and put in police van after he was caught roaming around royal residence
10 skiers missing after devastating avalanche near Lake Tahoe as rescue teams desperately search for survivors
Sydney Sweeney indulges in 'midnight snack' with male model for her raunchiest SYRN ad yet
CodeSOD: Contains Some Bad Choices
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 => { // postgres has some different array syntax // [1,2] => '{1,2}' 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) => { // TODO: do we need to do input safety checks here? // console.log("buildArrayContainsQuery"); // build the postgres 'contains' query to compare arrays // ex: to fetch files by the list of tags //WORKS: //select * from files where _tags @> '{2}'; //query.whereRaw('_tags @> ?', '{2}') let returnQueryParams = []; returnQueryParams.push(`${key} @> ?`); returnQueryParams.push(exports.buildArrayString(values)); // console.log(returnQueryParams); 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.
.comment {border: none; }