Servicestack Deserialize Redis Response GetAllItemsFromList - servicestack

So using lists within Servicestack/Redis, when pulling them back from the server I am getting a list of strings (which each the same CLASS just different data in each one).
I did not see a way of using "typed" lists which would allow Servicestack to serialize/deserialize as I add, get items from the List. So my question is:
List<string> resp = rc.GetAllItemsFromList (key);
Gives me back a LIST (Collection) of strings. Each one being a JSON representation of Class ABC.
I'd rather have a list of <ABC> returned. If not, I know I can iterate through the collection of strings deserializing each. But want to know if there is a better way to be doing this than that.

To get a List of Types back you'd use the IRedisTypedClient API and access the Typed List APIs in IRedisList by accessing the Lists[] collection, e.g:
var redisAbc = redis.As<Abc>();
List<Abc> results = redisAbc.Lists[key].GetAll();

Related

How to return an output from javascript function using invoke js in blueprism

I want to read multiple key-value pairs from a webpage and write it to a collection using blueprism.
I want to use javascript.
I am able to read text from webpage but couldnt understand how to write that data into blueprism data item or a collection.
Blue Prism provides no facility to return data directly from a JavaScript call back into the calling Object. Your best bet is to use a script that generates a hidden input element in the DOM and appends the data you want to exfiltrate:
var hiddenElement = document.querySelector('#bp-output');
if (typeof hiddenElement === 'undefined') {
hiddenElement = document.createElement('input');
hiddenElement.type = 'hidden';
hiddenElement.id = 'bp-output';
document.body.appendChild(hiddenElement);
}
hiddenElement.value = /* some functionality to set the value of the newly-created hidden element */;
You'll need to model this element in your object's application modeler, but it's fairly simple to do - you don't need to match on any attributes other than "ID" or "Web ID", and it's a match only to the string bp-output.
From there, you can use a typical Read stage to read the value out of the value attribute of your element.
For more complex data structures like Collections, you will have to utilize some serialization trickery to get to where you want to be. For example, if you're trying to read a table into a Collection via JavaScript, your /* functionality to set the value of the newly-created hidden element */ in the example above may need to leverage some code from this SO thread to serialize the table itself to a CSV string. Once you've read the string from the hidden element's value, you could use the CSV-related actions in the vendor-provided Utility - Strings VBO to serialize this to a proper Collection for your use in your Objects/Processes.

Google Cloud Python Lib - Get Entity By ID or Key

I've been working on a python3 script that is given an Entity Id as a command line argument. I need to create a query or some other way to retrieve the entire entity based off this id.
Here are some things I've tried (self.entityId is the id provided on the commandline):
entityKey = self.datastore_client.key('Asdf', self.entityId, namespace='Asdf')
query = self.datastore_client.query(namespace='asdf', kind='Asdf')
query.key_filter(entityKey)
query_iter = query.fetch()
for entity in query_iter:
print(entity)
Instead of query.key_filter(), i have also tried:
query.add_filter('id', '=', self.entityId)
query.add_filter('__key__', '=', entityKey)
query.add_filter('key', '=', entityKey)
So far, none of these have worked. However, a generic non-filtered query does return all the Entities in the specified namespace. I have been consulting the documentation at: https://googleapis.dev/python/datastore/latest/queries.html and other similar pages of the same documentation.
A simpler answer is to simply fetch the entity. I.e. self.datastore_client.get(self.datastore_client.key('Asdf', self.entityId, namespace='asdf'))
However, given that you are casting both entity.key.id and self.entityId, you'll want to check your data to see if you are key names or ids. Alternatives to the above are:
You are using key ids, but self.entityid is a string self.datastore_client.get(self.datastore_client.key('Asdf', int(self.entityId), namespace='asdf'))
You are using key names, and entityId is an int self.datastore_client.get(self.datastore_client.key('Asdf', str(self.entityId), namespace='asdf'))
I've fixed this problem myself. Because I could not get any filter approach to work, I ended up doing a query for all Entities in the namespace, and then did a conditional check on entity.key.id, and comparing it to the id passed on the commandline.
query = self.datastore_client.query(namespace='asdf', kind='Asdf')
query_iter = query.fetch()
for entity in query_iter:
if (int(entity.key.id) == int(self.entityId)):
#do some stuff with the entity data
It is actually very easy to do, although not so clear from the docs.
Here's the working example:
>>> key = client.key('EntityKind', 1234)
>>> client.get(key)
<Entity('EntityKind', 1234) {'property': 'value'}>

Is there a way to pass a parameter to google bigquery to be used in their "IN" function

I'm currently writing an app that accesses google bigquery via their "#google-cloud/bigquery": "^2.0.6" library. In one of my queries I have a where clause where i need to pass a list of ids. If I use UNNEST like in their example and pass an array of strings, it works fine.
https://cloud.google.com/bigquery/docs/parameterized-queries
However, I have found that UNNEST can be really slow and just want to use IN on its own and pass in a string list of ids. No matter what format of string list I send, the query returns null results. I think this is because of the way they convert parameters in order to avoid sql injection. I have to use a parameter because I, myself want to avoid SQL injection attacks on my app. If i pass just one id it works fine, but if i pass a list it blows up so I figure it has something to do with formatting, but I know my format is correct in terms of what IN would normally expect i.e. IN ('', '')
Has anyone been able to just pass a param to IN and have it work? i.e. IN (#idParam)?
We declare params like this at the beginning of the script:
DECLARE var_country_ids ARRAY<INT64> DEFAULT [1,2,3];
and use like this:
WHERE if(var_country_ids is not null,p.country_id IN UNNEST(var_country_ids),true) AND ...
as you see we let NULL and array notation as well. We don't see issues with speed.

Best practice to pass query conditions in ajax request

I'm writing a REST api in node js that will execute a sql query and send the results;
in the request I need to send the WHERE conditions; ex:
GET 127.0.0.1:5007/users //gets the list of users
GET 127.0.0.1:5007/users
id = 1 //gets the user with id 1
Right now the conditions are passed from the client to the rest api in the request's headers.
In the API I'm using sequelize, an ORM that needs to receive WHERE conditions in a particular form (an object); ex: having the condition:
(x=1 AND (y=2 OR z=3)) OR (x=3 AND y=1)
this needs to be formatted as a nested object:
-- x=1
-- AND -| -- y=2
| -- OR ----|
| -- z=3
-- OR -|
|
| -- x=3
-- AND -|
-- y=1
so the object would be:
Sequelize.or (
Sequelize.and (
{x=1},
Sequelize.or(
{y=2},
{z=3}
)
),
Sequelize.and (
{x=3},
{y=1}
)
)
Now I'm trying to pass a simple string (like "(x=1 AND (y=2 OR z=3)) OR (x=3 AND y=1)"), but then I will need a function on the server that can convert the string in the needed object (this method in my opinion has the advantage that the developer writing the client, can pass the where conditions in a simple way, like using sql, and this method is also indipendent from the used ORM, with no need to change the client if we need to change the server or use a different ORM);
The function to read and convert the conditions' string into an object is giving me headache (I'm trying to write one without success, so if you have some examples about how to do something like this...)
What I would like to get is a route capable of executing almost any kind of sql query and give the results:
now I have a different route for everything:
127.0.0.1:5007/users //to get all users
127.0.0.1:5007/users/1 //to get a single user
127.0.0.1:5007/lastusers //to get user registered in the last month
and so on for the other tables i need to query (one route for every kind of request I need in the client);
instead I would like to have only one route, something like:
127.0.0.1:5007/request
(when calling this route I will pass the table name and the conditions' string)
Do you think this solution would be a good solution or you generally use other ways to handle this kind of things?
Do you have any idea on how to write a function to convert the conditions' string into the desired object?
Any suggestion would be appreciated ;)
I would strongly advise you not to expose any part of your database model to your clients. Doing so means you can't change anything you expose without the risk of breaking the clients. One suggestion as far as what you've supplied is that you can and should use query parameters to cut down on the number of endpoints you've got.
GET /users //to get all users
GET /users?registeredInPastDays=30 //to get user registered in the last month
GET /users/1 //to get a single user
Obviously "registeredInPastDays" should be renamed to something less clumsy .. it's just an example.
As far as the conditions string, there ought to be plenty of parsers available online. The grammar looks very straightforward.
IMHO the main disadvantage of your solution is that you are creating just another API for quering data. Why create sthm from scratch if it is already created? You should use existing mature query API and focus on your business logic rather then inventing sthm new.
For example, you can take query syntax from Odata. Many people have been developing that standard for a long time. They have already considered different use cases and obstacles for query API.
Resources are located with a URI. You can use or mix three ways to address them:
Hierarchically with a sequence of path segments:
/users/john/posts/4711
Non hierarchically with query parameters:
/users/john/posts?minVotes=10&minViews=1000&tags=java
With matrix parameters which affect only one path segment:
/users;country=ukraine/posts
This is normally sufficient enough but it has limitations like the maximum length. In your case a problem is that you can't easily describe and and or conjunctions with query parameters. But you can use a custom or standard query syntax. For instance if you want to find all cars or vehicles from Ford except the Capri with a price between $10000 and $20000 Google uses the search parameter
q=cars+OR+vehicles+%22ford%22+-capri+%2410000..%2420000
(the %22 is a escaped ", the %24 a escaped $).
If this does not work for your case and you want to pass data outside of the URI the format is just a matter of your taste. Adding a custom header like X-Filter may be a valid approach. I would tend to use a POST. Although you just want to query data this is still RESTful if you treat your request as the creation of a search result resource:
POST /search HTTP/1.1
your query-data
Your server should return the newly created resource in the Location header:
HTTP/1.1 201 Created
Location: /search/3
The result can still be cached and you can bookmark it or send the link. The downside is that you need an additional POST.

Retrieving all possible values for a field via a RESTlet

Is there an api call that will retrieve all possible values for a field via a RESTlet script for Netsuite?
For example, I want to return all of the possible class field values (Class 1, Class 2, ...) for an inventory item.
I have already tried nlapiGetFieldValues('class') but without success. I'm guessing that is a client side only call?
Similar to what Suite Resources said, but use some pre-existing records for the classes you want to evaluate:
switch(true){
case req.type == 'customer':
var x = nlapiLoadRecord('class',1000);
and either
return x; OR return x.getAllFields() OR return JSON.stringify(x);
case req.type == 'salesorder':
...... etc.
}
I'd personally just return the whole record to get subfields and prototype functions.
RESTLets are written in SuiteScript, so look at the supported records.
Class (classification).
You can write up a saved search within the UI, then use nlapiSearchRecord in your RESTLet. Loop through the results of the search and append to an array of objects representing the record. Then use JSON.stringify and return results. Pretty easy.
Try coding it up and post the code if you have issues.

Resources