Learn to develop a JavaScript SQL adapter.
The example in this page shows how to implement an adapter that connects with a MySQL database by using the connectivity facilities that are provided with MobileFirst Server. You can learn more about connectivity in The JavaScript adapter framework and SQL adapter connectionPolicy element.
To connect to an SQL database, JavaScript code needs a JDBC connector driver for the specific database type. You must download the appropriate JDBC connector driver and add it as a dependency in your Maven project. For more information about adding dependencies, see System Dependencies.
There are two parts to the implementation of a JavaScript adapter:
// Invoke stored SQL procedure and return invocation result
function getAccountTransactions2 ( accountId ){
// To run a SQL stored procedure, use the MFP.Server.invokeSQLStoredProcedure method
return MFP . Server . invokeSQLStoredProcedure ({
procedure : "getAccountTransactions" ,
parameters : [ accountId ]
});
}
var getAccountsTransactionsStatement = "SELECT transactionId, fromAccount, toAccount, transactionDate, transactionAmount, transactionType " +
"FROM accounttransactions " +
"WHERE accounttransactions.fromAccount = ? AND accounttransactions.toAccount = ? " +
"ORDER BY transactionDate DESC " +
"LIMIT 20;";
//Invoke prepared SQL query and return invocation result
function getAccountTransactions1(fromAccount, toAccount){
return MFP.Server.invokeSQLStatement({
preparedStatement : getAccountsTransactionsStatement,
parameters : [fromAccount, toAccount]
});
}
fromAccount | toAccount | transactionAmount | transactionDate | transactionId | transactionType |
---|---|---|---|---|---|
"12345" | "54321" | 180.00 | "2009-03-11T11:08:39.000Z" | "W06091500863" | "Funds Transfer" |
"12345" | null | 130.00 | "2009-03-07T11:09:39.000Z" | "W214122\/5337" | "ATM Withdrawal" |
{
"isSuccessful": true,
"resultSet": [{
"fromAccount": "12345",
"toAccount": "54321",
"transactionAmount": 180.00,
"transactionDate": "2009-03-11T11:08:39.000Z",
"transactionId": "W06091500863",
"transactionType": "Funds Transfer"
}, {
"fromAccount": "12345",
"toAccount": null,
"transactionAmount": 130.00,
"transactionDate": "2009-03-07T11:09:39.000Z",
"transactionId": "W214122\/5337",
"transactionType": "ATM Withdrawal"
}]
}
result.invocationResult.resultSet
result.ResultSet