1. Sample service
Let's see how a full REST Service and JavaScript API can be created in just 4 lines of code.
Five main steps:
1. Use you favorite SQL client to create SQL statement(s)
2. Copy-paste your SQL into WEB-INF/lightlink/*/*.js.sql
3. Use JavaScript code prefixed with --%
to mark conditional parts of that SQL
4. Use :p.parameterName
syntax to bind input parameters.
5. Smile and test the REST service ou JavaScript API that you have just developped.
So, let's start:
Preparation.
a) Place lightlink JAR to your web application's WEB-INF/lib
b) Place the following initial configuration into your web.xml
:
<filter> <filter-name>linkFilter</filter-name> <filter-class>io.lightlink.spring.LightLinkFilter</filter-class> </filter> <filter-mapping> <filter-name>linkFilter</filter-name> <url-pattern>/lightlink/*</url-pattern> </filter-mapping> <filter-mapping> <filter-name>linkFilter</filter-name> <url-pattern>/rest/*</url-pattern> </filter-mapping> <!-- ... --> <servlet> <servlet-name>restServlet</servlet-name> <servlet-class>io.lightlink.servlet.RestServlet</servlet-class> <init-param> <param-name>No-CSRF-token-check</param-name> <param-value>true</param-value> </init-param> </servlet> <servlet> <servlet-name>JsProxyServlet</servlet-name> <servlet-class>io.lightlink.servlet.JsProxyServlet</servlet-class> </servlet> <servlet> <servlet-name>jsApiDefinitionServlet</servlet-name> <servlet-class>io.lightlink.servlet.JsMethodsDefinitionServlet</servlet-class> </servlet> <servlet> <servlet-name>DebugApiDefinitionServlet</servlet-name> <servlet-class>io.lightlink.servlet.debug.DebugMethodsDefinitionServlet</servlet-class> </servlet> <servlet> <servlet-name>DebugFacadesProxyServlet</servlet-name> <servlet-class>io.lightlink.servlet.debug.DebugFacadesProxyServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>restServlet</servlet-name><url-pattern>/rest/*</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>JsProxyServlet</servlet-name> <url-pattern>/lightlink/*</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>jsApiDefinitionServlet</servlet-name> <url-pattern>/lightlink-api/jsapi.js</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>DebugApiDefinitionServlet</servlet-name> <url-pattern>/lightlink-debug-src/*</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>DebugFacadesProxyServlet</servlet-name> <url-pattern>/lightlink-debug-proxy/*</url-pattern> </servlet-mapping>
1. Create "WEB-INF/lightlink/MyApp/getCountries.js.sql
" file in your webapp directory.
SELECT * FROM COUNTRIES --% if (p.name) { WHERE NAME=:p.name --% }
Alternativaly you can place it in java resources directory under "lightlink" package. If you use Maven the correct location would be /src/main/resources/lightlnk/MyApp/getCountries.js.sql
, so that it will get copied in the classpath after compilation(WEB-INF/classes/lightlink/MyApp/getCountries.js.sql
)
2. Create "WEB-INF/lightlink/MyApp/config.js
" file (or place it in /src/main/resources/lightlink/MyApp/config.js) that will contain global configuration for the given package and all eventual subpackages
sql.setConnection("<driverClassName>", "<url>", "<login>", "<password>")
or
sql.setDataSourceJndi("<jndiName>")
If you put those files in classpath resources after compilation with your IDE or Maven they will be copied to WEB-INF/classes/MyApp/getCountries.js.sql
2. REST Services
Run your local development web/application server
access
http://localhost:[port][/WAR-context-path]/rest/MyApp/getCountries
or
http://localhost:[port][/WAR-context-path]/rest/MyApp/getCountries?name=Canada
3. Direct JS API for a Web Page
You web page add :
<script src="/lightlink-api/jsapi.js"></script>
Or even better if it’s a JSP page :
<script src="/<%=request.getContextPath()%>/lightlink-api/jsapi.js"></script>
Immediately after the page refresh, you get the JavaScript API available that you cal call like this :
MyApp.getCountries({},function(res){ console.log(res.resultSet); });
or
MyApp.getCountries({name:"Canada"},function(res){ console.log(res.resultSet); });
4. Complex dynamic query example
SELECT * FROM employees WHERE 1=1 --% if (p.firstName) { AND FIRST_NAME = :p.firstName --% } --% if (p.lastName) { AND LAST_NAME = :p.lastName --% } --% if (p.minHireDate) { AND HIRE_DATE >= :(date)p.minHireDate --% } --% if (p.maxHireDate) { AND HIRE_DATE <= :(date)p.maxHireDate --% } ORDER BY EMPLOYEE_ID
5. Stored Procedure Call
5.1. MySQL Example :
CALL TEST_SIMPLE_SP( :(inout)(date)p.date , :(inout)(number)p.number , :p.name , :(out)p.helloString )