August 14, 2020
Quick Tip: Returning a query as an array of structures in ColdFusion 2018 using reduce() and the fat arrow operator
Comments
(4)
August 14, 2020
Quick Tip: Returning a query as an array of structures in ColdFusion 2018 using reduce() and the fat arrow operator
Newbie 49 posts
Followers: 41 people
(4)

In this quick tip, I’m going to show you how to use the reduce() function in tandem with the fat arrow operator in ColdFusion 2018 to return a query as an array of structures.  In Lucee, returning an array of structs from a query is as simple as adding a returntype=”array” attribute to the <cfquery> tag.  This doesn’t exist in ColdFusion.  This example executes a query like normal, but applies the reduce() function on the return to convert the query object into an array of structures.

public array function getUser (numeric id) {
    local.SQL = "SELECT users.* FROM users WHERE (1 = 1)";
    local.Parameters = {};

if (!isNull(arguments.id)) {
local.SQL &= ” AND (id = :id)”;
local.Parameters.insert( ‘id’, { cfsqltype: ‘integer’, value: arguments.id } );
}

local.Result = queryExecute(local.SQL, local.Parameters);

return local.Result.reduce((result, row) => {
result.append(row);
return result;
}, []);
}

Please note; the fat arrow operator was introduces in ColdFusion 2018 update 5 and ColdFusion 2016 update 12.  Installations before this hot fix will not work.

4 Comments
2020-12-04 21:24:33
2020-12-04 21:24:33

Hey, David, I just thought to test that code against CF2016 (latest current update, 15) and it did NOT work. You mentioned that you thought it was in both CF2018 (post update 5) and CF2016 (post update 12), but I’m afraid that does not seem to be the case. All the looking into fat arrows/lambda’s in the CF docs seem to mention only CF2018.

Just clarifying for readers, as much as anything. But again, great stuff for those on CF2018 (until they get to CF2021 when it’s still easier with returntype=”array” on cfquery/queryexecute).

Like
(1)
2020-12-04 20:53:53
2020-12-04 20:53:53

Great stuff, David. For readers, we should note that in CF2021, Adobe HAS added the returntype=”array” (and even “json/array”) for cfquery and queryexecute. Of course, CF2021 was released 3 months after this post, and the public beta would start for another week.

But for those on CF2018 or 2016, this is indeed a cool alternative until they may upgrade.

And if anyone may want to see a working example of this approach, I have a working example at the cffiddle site. We can’t use a real cfquery there, so I use the querynew function to create a “fake” query. (And I don’t bother with implementation as a function or the additional query parameter insertion.)

So it’s just a few lines of code, like David shows above.

Like
(1)
(2)
>
Charlie Arehart
's comment
2023-02-28 19:41:00
2023-02-28 19:41:00
>
Charlie Arehart
's comment

Charlie, your link to CFFiddle is now broken.  I highly recommend sharing code using a non-Adobe service (like a Github gist) as Adobe has a history of munging links, dropping user names, reformatting code (so it’s not even valid anymore) and inactivating/deleting threads.  (I also add links to CFFiddle & TryCF within the source of the shared self-contained code so that it can be easily tested online.)

What does returnType=”struct” do?  Adobe doesn’t appear to have documented its usage or benefit.  It doesn’t behave the same as Lucee & appears to add an unnecessary struct container around the “array” returntype.

Like
>
James Moberg
's comment
2023-03-01 16:37:39
2023-03-01 16:37:39
>
James Moberg
's comment

James, this is simply an instance of cffiddle.org being offline entirely, not any of the untoward behaviors you have experience with. I’ve just reached out to them to ask about it (as anyone could, via the cfsup@adobe.com address they have shared in various places).

As for using an alternative, sure, if the goal was just to show the code. Of course the fiddle allows RUNNING it. So, too, does trycf.com (which also runs lucee). I just default to cffiddle.org because it’s always worked for me and others, and even the cf docs offer links to examples there. So I suspect this outage will be resolved.

(To be clear, I do realize that YOU already know most of what I have shared here. I’ve added the clarifications for others who will read it.)

As for the question on the struct returntype, I’m writing this on a phone so not at my computer to test things (and the UIs for cfiddle and trycf don’t work well on mobile.). But if it does what you say, that’s indeed lamentable..and you know what to do to report that. 🙂 If you may open a ticket at tracker.adobe.com (whether a bug report or feature request), please share it here so we can add votes and track the progress.

Finally, if you may be motivated to work a code example, please do share a gist here. I’m sure many would appreciate it, especially while cfiddle is down.

Like
Add Comment