SELECT with almost dynamic WHERE clause - node.js

I am building an API with node/express and using PG and Postgres along with them.
At some point, I am trying to build a query that looks like this :
SELECT * FROM user_dog_view
WHERE dog_name = X AND dog_gender = Y AND dog_race = Z;
(The X, Y, Z represent info coming from the request.body or the query string)
Thing is: I would like my query to ignore one of the CONDITIONS in the WHERE clause lwhere the X, Y or Z is undefined or null (meaning the request.body or query string is incomplete).
E.g, if Y is undefined, I would like the query be like :
SELECT * FROM user_dog_view
WHERE dog_name = X AND dog_race = Z;
I tried looking into the coalesce ffunction but it does not seem to answer my needs.
Now I am wondering if I should try to put some complicated conditions to handle that thanks to JavaScript, but I am not sure that's the best idea?
Any Psql (or JS) insight on this is appreciated. Thanks !

Related

How to embed a ranking function into the AllPrevious Function for a calculated column in Spotfire

I am trying to make one equation from the two below, but either I am missing something completely simple, or there is a trick to embedding a ranking function into the "AllPrevious" function in Spotfire
VAR_X = (Rank([D] / [A],"desc",[B],"ties.method=first"))
VAR_Y = Sum([A] * [C]) OVER (intersect([B],AllPrevious([VAR_X])))
I am trying to essentially do (bypassing the creation of VAR_X:
VAR_Y = Sum([A] * [C]) OVER (intersect([B],AllPrevious(((Rank([D] / [A],"desc",[B],"ties.method=first"))))))
I keep getting an error message anytime I put something after "AllPrevious(" and I cannot figure it out. Any help would be appreciated!

Many to many AQL query

I have 2 collections and one edge collection. USERS, FILES and FILES_USERS.
Im trying to get all FILES documents, that has the field "what" set to "video", for a specific user, but also embed another document, also from the collection FILES, but where the "what" is set to "trailer" and belongs to the "video" into the results.
I have tried the below code but its not working correctly, im getting a lot of duplicate results...its a mess. Im definitely doing it wrong.
FOR f IN files
FILTER f.what=="video"
LET trailer = (
FOR f2 IN files
FILTER f2.parent_key==f._key
AND f2.what=="trailer"
RETURN f2
)
FOR x IN files_users
FILTER x._from=="users/18418062"
AND x.owner==true
RETURN DISTINCT {f,trailer}
There may be a better way to do this with graph query syntax, but try this. Adjust the UNIQUE functions based on your data-model.
LET user_files = UNIQUE(FOR u IN FILES_USERS
FILTER u._from == "users/18418062" AND u.owner
RETURN u._to)
FOR uf IN user_files
FOR f IN files
FILTER f._key == uf AND f.what == "video"
LET trailers = UNIQUE(FOR t IN files
FILTER t.parent_key == f._key AND t.what == "trailer"
RETURN t)
RETURN {"video": f, "trailers": trailers}
Well, check to see If you have duplicate data as suggested by TMan, however check your query syntax too. It appears that you have no link between your f subquery and the x in the main query. That would cause the query to potentially return a lot of dups if there are multiple records in collection files_users for user users/18418062
Try adding a join in the main query. Something like:
FOR x IN files_users
FILTER x._from=="users/18418062"
AND x.owner==true
AND x._to == f._id
RETURN DISTINCT {f,trailer}
On a related note, if you run into performance issues doing a subquery for trailers , you could instead try just doing a join and array expansion and see if that works for your case

DocumentHelper.GetDocuments().InCategories() API Method not working as intended

I am trying to get documents of given type with assigned category. This is my code:
var inCategoryDocuments = DocumentHelper.GetDocuments("XYZ.MyType").InCategories("CategoryCodeName");
It's result with this query:
SELECT * FROM View_CMS_Tree_Joined AS V WITH (NOLOCK, NOEXPAND) INNER JOIN xyz_MyType AS C WITH (NOLOCK) ON [V].[DocumentForeignKeyValue] = [C].[MyTypeID] AND V.ClassName = N'xyz.MyType' LEFT OUTER JOIN COM_SKU AS S WITH (NOLOCK) ON [V].[NodeSKUID] = [S].[SKUID] WHERE ([DocumentCulture] = N'en-EN' AND 0 = 1)
It looks like this API method (.InCategories()) doing nothing or I am missing something?
Kentico v11.0.26
Is this category assigned to specific site? If so, it wouldn't work, because in your query you didn't specify the site from which you want get documents.
You can simply add
.OnCurrentSite()
to your DataQuery, it will look like this
var inCategoryDocuments = DocumentHelper.GetDocuments("XYZ.MyType").OnCurrentSite().InCategories("CategoryCodeName");
It will retrieve the documents from the current website based on domain.
IMO method .InCategory shouldn't take care about site or it should be parametrized.
You need to include the using statement for it to work because those methods are extensions.
using CMS.DocumentEngine;

How to extract raw values for comparison or manipulation from Gremlin (Tinkerpop)

I know I'm missing something obvious here. I'm trying to extract values from TitanDB using Gremlin in order to compare them within Groovy.
graph = TinkerFactory.createModern()
g = graph.traversal(standard())
markoCount = g.V().has('name','marko').outE('knows').count()
lopCount = g.V().has('name','lop').outE('knows').count()
if(markoCount > lopCount){
// Do something
}
But apparently what I'm actually (incorrectly) doing here is comparing traversal steps which obviously won't work:
Cannot compare org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.DefaultGraphTraversal with value '[TinkerGraphStep(vertex,[name.eq(marko)]), VertexStep(OUT,[knows],edge), CountGlobalStep]' and org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.DefaultGraphTraversal with value '[TinkerGraphStep(vertex,[name.eq(lop)]), VertexStep(OUT,[knows],edge), CountGlobalStep]'
I'm having the same issue extracting values from properties for use in Groovy as well. I didn't see anything in the docs indicating how to set raw values like this.
What is needed to return actual values from Gremlin that I can use later in my Groovy code?
Figured it out, I needed next():
graph = TinkerFactory.createModern()
g = graph.traversal(standard())
markoCount = g.V().has('name','marko').outE('knows').count().next()
lopCount = g.V().has('name','lop').outE('knows').count().next()
if(markoCount > lopCount){
// Do something
}

neo4j nodejs strength or properties's relationship modified : how to do it?

I meet a problem with count request of neo4j with nodejs.
Here my problem :
When I insert a data, it will present like this:
start a = node(0)
create unique a-[:HAS_ID]->(b{id:'xx'})
create unique b-[:HAS_INFO]->(c{info:'xx'})
return a,b,c;
because it's unique node, so that it will not insert a new node if there are a same node exist. But, I wanna count how many request to call this query.
Ex :
request: -domain/id01/info
--return a node[0], b node[1] and c node[2]
add another data:
request: -domain/id02/info
-- return : a node[0], b node[3], c node[4]
call it again :
request: -domain/id01/info
--return a node[0], b node[1] and c node[2] //but here is any attribute or properties count to 2.
I've read any solution about strength. It told me create an properties of relationship as example :
[:HAS_INFO{strength:num}]
and let it increase but I still don't get it.
Anyone please give me solution and tell me how to do it.
Thank you.
more info : Representing (and incrementing) relationship strength in Neo4j
You can use the CASE statement, see http://gist.neo4j.org/?6052414 for an example. Feel free to for the underlying gist and improve it!
MATCH path=(a)-[rel:HAS_INFO]->(b)
WHERE a.name?='A' AND b.name?='Info'
SET rel.weight = (
CASE
WHEN not(has(rel.weight))
THEN 0
ELSE rel.weight + 1
END)
RETURN path, rel.weight;

Resources