-2

I'm looking for some sample code using the AEM JCR_SQL2 API in server-side Javascript (NOT Java), i.e. code that starts with use(function() { ... }) and is loaded via data-sly-use=${...}.

All Google results are 100% Java based examples.

What I've already tried: Google "JCR-SQL2 js example" and variations.

Expected result: sample code in Javascript.

Actual result: lots of Java code :-(

Alex R
  • 11,364
  • 15
  • 100
  • 180
  • Mind me asking, why JS? If you’re using it for a Use model, why not go with Java? Also, post a simple java example and I might be able to convert it to JS. – Ahmed Musallam Jul 18 '18 at 00:01
  • @AhmedMusallam the advantages of JS are well documented, for example here https://forums.adobe.com/thread/2330311 – Alex R Jul 18 '18 at 01:33
  • I dont know about “advantages” in this specific case. A use API model should not be used to make queries or have complex logic. You’d be better off creating an OSGI service the does the querying and call that service from within your JS model. – Ahmed Musallam Jul 18 '18 at 14:24
  • @AhmedMusallam do you recommend using an OSGI service for simple retrieval of a string value to be used in the presentation logic of a single, non-reusable AEM component? – Alex R Jul 18 '18 at 19:25
  • off course not, my comment was general, just like your question was general :) also, my comment included the word "complex", in case you missed it. if you're doing this for a "single, non-reusable AEM component" (new information) then sure, add that logic in the JS, it's your implementation after all. – Ahmed Musallam Jul 18 '18 at 19:32

2 Answers2

5

If you wanna use server-side JS (what I don't recommend), then you only have to convert the syntax of the Java examples. You interact with the Java-Objects anyway. So the API is the same for JS as for Java. In case you have a HTL component and calling the JS via the Use-API, then several objects are already defined in your JS scope.

https://helpx.adobe.com/experience-manager/htl/using/global-objects.html

Here is an JS example to search all core components with a SQL-2 query:

use(function () {
    var pageName = currentPage.name;
    var title = currentPage.properties.get("jcr:title");
    var resourceName = granite.resource.name;
    var resourceTitle = properties.get("jcr:title");

    var componentList = [];
    var componentIter = resolver.findResources("SELECT * FROM [cq:Component] AS c WHERE ISDESCENDANTNODE(c, '/apps/core/wcm')", "JCR-SQL2");
    while (componentIter.hasNext()) {
        var compoenentRes = componentIter.next();
        componentList.push(compoenentRes.getPath());
    }

    return {
        pageName: pageName,
        title: title,
        resourceName: resourceName,
        componentList: componentList
    };
});

The component HTL code to use it, would be:

<div data-sly-use.info="info.js">
    <p>page name: ${info.pageName}</p>
    <p>title: ${info.title}</p>
    <p>resourceName: ${info.resourceName}</p>
    <p>core components: </p>
    <ul data-sly-list.component="${info.componentList}">
        <li>${component}
    </ul>
</div>

PS: You probably know, but here you find the Use-API for JS: https://helpx.adobe.com/experience-manager/htl/using/use-api-javascript.html

Alexander Berndt
  • 1,628
  • 9
  • 17
0

I'm not aware of any Web-API for JCR_SQL2 queries. You would have to implement your own Servlet in AEM (with Java), which would accept queries from external HTTP requests. Then you could call your servlet from your JS code via Ajax, and execute the query inside AEM with Java.

Maybe the Query Builder API is interesting for you. This query-language is already available from the outside, and can be called via Ajax. For testing and developing your queries you can use the Query Debugger:

Adobe Documentation:

https://helpx.adobe.com/experience-manager/6-3/sites/developing/using/querybuilder-api.html

Query Debugger:

http://localhost:4502/libs/cq/search/content/querydebug.html

Query Debuger with Sample Query:

http://localhost:4502/libs/cq/search/content/querydebug.html?charset=UTF-8&query=type%3Dcq%3APage%0D%0Aorderby%3D%40jcr%3Acontent%2Fcq%3AlastModified%0D%0Aorderby.sort%3Ddesc

Sample Query called directly:

http://localhost:4502/bin/querybuilder.json?orderby=%40jcr%3acontent%2fcq%3alastModified&orderby.sort=desc&type=cq%3aPage

Alexander Berndt
  • 1,628
  • 9
  • 17