query to cfscript properly in coldfusion? - sql

CLARIFICATION: The question here is, "how do I stop using <cfquery> to retrieve data and replace it with a call to an API?"
I have a sql query where I need help to convert it to cfscript. However as you can see I have tried converting it. But, I need some verification if I am on the right path to convert from sql query to cfscript. if not, can anyone help me by converting from sql query to cfscript? thanks for the help. here is my code.
CFSCRIPT:
<cfset jsonDatas = fileRead("c:\Users\Desktop\MyApi.json" )>      
<cfset jsonData = deserializeJSON(jsonDatas) />       
<cfif arrayLen(jsonData)>  
<cfloop array="#jsonData#" index="prop">       
<cfoutput>  
<cfscript>  
// writedump(jsonData);for (item in jsonData[1])   {  
  if (#prop.payGrade# == 0) {
         #prop.divisionNbr#;        
 #prop.probationBeginDate#;        
 #prop.legacyStatus#;        
 #prop.payStep#;        
 #prop.creationDate#;           
  } 

I am going to go for broke here. Do you have data in a DB and you just want to return a JSON representation of it? As in
<cfquery name="result">
SELECT ...
FROM whereever
</cfquery>
<cfoutput>#SerializeJSON(result, 'struct')#</cfoutput>
And then something else tries to consume this data? I have been looking and your questions, and I don't know if we are on the generating data side of the world, or in the consuming data side of the world.

You are asking about two different things.
SQL defines the query run on the database.
CFSCRIPT is just a script style syntax for ColdFusion code (as opposed to tags).
Your example code is
Here is some SQL
Here I am reading JSON and converting it to a ColdFusion struct using script style code instead of tags.
Are you trying to go from currently calling a database, which returns data as a ColdFusion query object, to calling an API, which returns data as a JSON packet? You need to then convert the JSON data into the same or similar CF structures you currently use?
UPDATE: If you want to replace the existing query with an API call,
Does the API already exist?
Does it return the same data as the current query?
Is your team moving to APIs to decouple existing code?
Do you need to know HOW to convert a <cfquery> call to an API call?

This is really a comment, but I need a lot of space to write it.
Are you looking to get data from a remote source and display it on a web page. Are you looking to do something like this?
I think we need to clarify which are client side technologies and which are server side technologies.
Update based on comments**
Consider a VueJS solution
<!-- Showing stuff on screen -->
<div id="app">
{{ info }}
</div>
I can show stuff on the screen. This is similar, but not identical to #info#
<!-- Getting data -->
<cfscript>
new Vue({
el: '#app',
data () {
return {
info: null
}
},
mounted () {
axios
.get('https://api.coindesk.com/v1/bpi/currentprice.json')
.then(response => (this.info = response))
}
})
<cfscript>
And this is getting data from a remote source and putting that data into a javascript variable.
Explaination
So why am I doing this with Javascript rather than ColdFusion? Javascript runs on the browser; ColdFusion runs on the server. If you want to consume an API on the browser, you have to use browser based technologies.
My example is in VueJS, but Angular and React are also options. It is a bit dated, but jQuery can do this kind of stuff too.
Code Source: https://v2.vuejs.org/v2/cookbook/using-axios-to-consume-apis.html

Related

Vue.js and data storing in text/script files

I need to store my data in .txt or .json files.
And it's good to load file with axios, for example, and then output the JSON.parsed data.
For example, I have file "data/tweets.json", and there are tons of tweets in json format. Well, I use axios.get ... and then I output content to needed component.
Well, and I cannot use things like import tweets from "#/data/tweets.ts", I need to make something db shaped, but without db engines.
Is there some traditional, easy way to make it?
Maybe there is some kind of node_nodule for the task?
Is there some config for Vue app for the case?
Make an HTTP request to get the static data from your server using Axios or fetch.
axios.get('/data/tweets.json').then((res) => {
// bind your data in Vue
this.tweets = JSON.parse(res.data)
})

How to work with local versions of data objects within a Vue.js application?

I'm using Vue.js and vuex to work with data from a backend store that I access via and api.
My question may be more about semantics and how to actually ask this question and search for better answers because I don't even know the correct terms to be searching on. So here goes...
Within my vue code I make a call to my back end api and it returns a json blob with a block of data that i want to work with in the ui. No problems there. But then I need to slightly modify that data to present it to the user. this entails assessing a few fields and changing them (say, if the value is 0 put in 'n/a', etc.), maybe doing some math on a few fields and so on. Then I present that to the user in a grid/table. I want the user to be able to sort and search on the fields as well. So I may display a field named fullName that comes from the back end but allow the user to sort on a field named lastName that is not actually in the grid.
With that said, here is my question - should I be creating a single 'transformer/reducer' in code to translate the data object coming from the api into what I need for the ui? Or should I be writing multiple filters (by this I mean Vue.js filters that go directly in the html with the '|' pipe) to just modify the fields directly in the html template?
My concern is the once i modify the object coming back from the api what if I have to post/patch/put back some kind of update - then I need a 'reverse' transformer to get back to an object that my api wants to see.
Generically, I suppose my question could be boiled down to "how do I locally handle an api response object so that I can maintain its 'purity'?"
Or something along those lines. I'm obviously fumbling for the correct terms and that is why i'm struggling to even google for info on this topic. For that I apologize and any help would be appreciated.
Also, I realize there is nothing Vue-specific about my question as this issue could apply to any data-driven ui framework.
I think I get what you're asking now. You just want to know if you need to modify the response before making it consumable by the UI. Let's say you're using axios. What I would do in Vue is use the response.data object as the data to inject into the vue data property. For example, your response.data returns an array of objects. You would hardcode a data variable in Vue like this:
data () {
return {
apiData: []
}
}
and with the API response, set the data array like this:
const response = await axios.get(...);
this.apiData = response.data.filter(item => return item.foo === bar);
Now apiData holds a filtered version of the response data, which was asynchronously retrieved for processing, including the ObjectId's for each object. Note: I would call this during created(){} hook to allow time for the GET request to process prior to DOM mounting.You can reference the array objects by the ObjectID within the UI, like this:
<div v-for="item in apiData" :key='item_id' :item="item">
<p> {{item.whateverProperty}} </p>
</div>

Reducing Parse Server to only Parse Cloud

I'm currently using a self hosted Parse Server up to date but I'm facing some security issues.
At the moment, calls done to the route /classes can retrieve any object in any table and, even though I might want an object to be public readable, I wouldn't like to show all the parameters of that object. Briefly I don't want the database to be retrieved in any case, I would like to disable "everything" except the Parse Cloud code. So that is, I would be able to run calls to my own functions, but not able to use clients (Android, iOS, C#, Javascript...) to retrieve data.
Is there any way to do this? I've been searching deeply for this, trying to debug some Controllers but I don't have any clue.
Thank you very much in advance.
tl;dr: set the ACL for all objects to be only readable when using the master key and then tell the query in Cloud Code to use the MK when querying your data
So without changing Parse Server itself you could make use of ACL and only allow a specific user to access objects. You would then "login" as that user in your Cloud Code and be able to access all objects.
As the old method, Parse.Cloud.useMasterKey() isn't available in the OS Parse Server you will have to pass the parameter useMasterKey to the query you are running which should do the trick for this particular request and will bypass ACL/CLP. There is an example in the Wiki of Parse Server as well.
For convenience, here is a short code example from the Wiki:
Parse.Cloud.define('getTotalMessageCount', function(request, response) {
var query = new Parse.Query('Messages');
query.count({
useMasterKey: true
}) // count() will use the master key to bypass ACLs
.then(function(count) {
response.success(count);
});
});

Is it possible to add additional logic to the Parse Server?

I want to perform some actions on X value depending on input from received in Y value. Can I perform such actions writing server side code in parse server?
Any pointers will be helpful.
Thanks.
Custom server side code can be achieved via cloud code. Cloud code allows you to create custom functions that are written in NodeJS and those functions can do various operations like: query from database, integrate with other solutions like: social, sending emails and more. The big advantage in parse-server is that you can use any npm module that you like from within the cloud code function and because there are millions of modules out there you have unlimited options.
Another very cool features of cloud code is the server side hooks
server side hooks allows you to write a code that will be triggered by parse-server core when an object is being saved or deleted. such events can be:
beforeSave - do something before the object is being saved to the database
afterSave - do something after the object is being saved
beforeDelete - do something before deleting
and more and more..
in order to define new cloud code function you will need to use the following code:
Parse.Cloud.define("{YOUR_FUNCTION_NAME}", function (request, response) {
// write your code, require some npm module and more...
});
In order to create server side hook you can write the following code:
Parse.Cloud.beforeSave("{PARSE_OBJECT_NAME}", function (request, response) {
// write your code and handle before saving an object
});
Triggering cloud code functions can be done easily via parse-server REST API or via parse-server client SDK's (iOS,Android,JavaScript and more)
There is a great guide on cloud code in here:
http://parseplatform.github.io/docs/cloudcode/guide/
Good Luck :)

Custom http query string for server-side sorting in dojox.grid.datagrid

I use MongoDB as application data storage. To sort retrieved documents, I write the following code (in perl):
$db->user->find({})->sort({ username => -1 })
where { username => -1 } means that the result of the query should be sorted by attribute username in descending order.
On the client-side I use DojoX DataGrid as main element of user management interface. After clicking the column header, DataGrid sends a GET request like:
/api/user/?sort(-username)
The question is: how can I change that GET query to look like:
/api/user/?sort=&username=-1
I'd be happy to do that because my server-side framework Mojolicious offers tools for parsing key-value query strings, not the strings looking like some kind of function call.
Dojo uses RQL which looks like the Mongo Query Language. They both use JSON and are both designed for document oriented databases. You can read more about here RQL at this Sitepen article.
Since RQL is so close to MQL, It might be easier to create an adaptor on the server side than to modify Dojo's behaviour.