0

I need to write old queries from Gremlin 2.6 to 3.4 syntax in JS/TS, because in 2.6 i've done everything with strings and then i execute that string but now i want to use 3.4 syntax where i can use chaining methods.

First i need to convert this query, but i don't know what to do with this nested queries out().simplePath() and label().is('Recording').

g.V().repeat(out().simplePath()).until(label().is('Recording'))

I'm thinking something like this to do but i'm not sure that is correct.

g.V().repeat(g.V().out().simplePath()).until(g.V().label().is('Recording'));

Also there is no more between function in gremlin so how can i get same result for this old function and also to write that in js/ts? (This is just part of a query)

.has('name', between('${partialPropertyName}', '${partialPropertyName}a'))

Thank you guys in front.

Hazaaa
  • 144
  • 2
  • 14

1 Answers1

2

A couple of quick answers.

There is still a between() predicate in Gremlin. I don't know if the database you are using supports it or not.

For your repeat until you can just do

g.V().repeat(out().simplePath()).until(hasLabel('Recording'))

The TinkerPop docs have good examples of all these steps. I would recommend giving the docs a quick read. Also feel free to do a search on "Practical Gremlin" I have several examples in there.

Edited to add example JavaScript imports

const gremlin = require('gremlin');
const Graph = gremlin.structure.Graph;
const __ = gremlin.process.statics;
const { t: { id },order,cardinality } = gremlin.process;

Edited again to point out that there is an index.js file in the root directory of the gremlin package that npm install gremlin creates. You can also find this file in the TinkerPop GitHub repo.

Cheers Kelvin

Kelvin Lawrence
  • 14,674
  • 2
  • 16
  • 38
  • I can't just type .repeat(out().simplePath()) because JS doesn't see out() function. When i hover gremlin import i get "Could not find a declaration file for module 'gremlin'". That's why i had to create my own typings for gremlin in Typescript. – Hazaaa Mar 15 '19 at 15:22
  • P.S. Your book is really good will definitely check it. One downside is that i need JavaScript code but as i could see you did it in Java and Groovy. – Hazaaa Mar 15 '19 at 15:29
  • From JavaScript just import the necessary objects and use __.out() – Kelvin Lawrence Mar 15 '19 at 16:04
  • I added some sample imports to my answer above – Kelvin Lawrence Mar 15 '19 at 16:05
  • When i import or like you write require('gremlin') i still can't see all methods and types. Is there anything i can do to get all methods and types for gremlin in JS? – Hazaaa Mar 15 '19 at 17:17
  • It might be worth doing a git clone of the TinkerPop source tree. That way you will have a local copy of the file with all the definitions in it. When I get a spare moment I will update the answer with a link to the specific file. – Kelvin Lawrence Mar 15 '19 at 22:28
  • As an aside if you just do a console.log(gremlin) you will see everything you are looking for. You may also want to look at this file. https://github.com/apache/tinkerpop/blob/master/gremlin-javascript/src/main/javascript/gremlin-javascript/lib/process/traversal.js – Kelvin Lawrence Mar 16 '19 at 00:04
  • I edited the example again to show how you can import all of order and cardinality also. You would then reference using syntax such as `order.decr` – Kelvin Lawrence Mar 16 '19 at 14:27
  • I had an issue open against myself to add sample code and a discussion of using Node.js to Practical Gremlin. I added a sample which may be helpful. https://github.com/krlawrence/graph/blob/master/sample-code/glv-client.js – Kelvin Lawrence Mar 16 '19 at 16:17
  • When i console.log(__) i can't find between method and of course get error __.between is not a function... – Hazaaa Mar 18 '19 at 08:49
  • In their documentation you also can't find between method, so i think they removed it. Any other solution that can help archive between method? – Hazaaa Mar 18 '19 at 09:05
  • Between is a predicate not a step. It’s part of P. Import P and use P.between() – Kelvin Lawrence Mar 18 '19 at 13:11
  • Sir you are my savior! Can you tell me just one more thing. I can't connect to my cosmos db with gremlin i always get server timeout respond. I've tried in Javascript and C# but still got same error. So now i am working on localhost. Do you maybe know why this occurs. I used same connection methods as in documentation. – Hazaaa Mar 18 '19 at 13:24
  • Unfortunately I am not familiar with how Cosmos DB works. Hopefully someone else can help you with that. – Kelvin Lawrence Mar 18 '19 at 14:34