@@ -508,6 +508,99 @@ describe('examples', () => {
508
508
done ( ) ;
509
509
} ) ;
510
510
} ) ;
511
+
512
+ it ( 'pass bookmarks example' , done => {
513
+ const driver = driverGlobal ;
514
+
515
+ // tag::pass-bookmarks[]
516
+ // Create a company node
517
+ function addCompany ( tx , name ) {
518
+ return tx . run ( 'CREATE (a:Company {name: $name})' , { 'name' : name } ) ;
519
+ }
520
+
521
+ // Create a person node
522
+ function addPerson ( tx , name ) {
523
+ return tx . run ( 'CREATE (a:Person {name: $name})' , { 'name' : name } ) ;
524
+ }
525
+
526
+ // Create an employment relationship to a pre-existing company node.
527
+ // This relies on the person first having been created.
528
+ function addEmployee ( tx , personName , companyName ) {
529
+ return tx . run ( 'MATCH (person:Person {name: $personName}) ' +
530
+ 'MATCH (company:Company {name: $companyName}) ' +
531
+ 'CREATE (person)-[:WORKS_FOR]->(company)' , { 'personName' : personName , 'companyName' : companyName } ) ;
532
+ }
533
+
534
+ // Create a friendship between two people.
535
+ function makeFriends ( tx , name1 , name2 ) {
536
+ return tx . run ( 'MATCH (a:Person {name: $name1}) ' +
537
+ 'MATCH (b:Person {name: $name2}) ' +
538
+ 'MERGE (a)-[:KNOWS]->(b)' , { 'name1' : name1 , 'name2' : name2 } ) ;
539
+ }
540
+
541
+ // To collect friend relationships
542
+ const friends = [ ] ;
543
+
544
+ // Match and display all friendships.
545
+ function findFriendships ( tx ) {
546
+ const result = tx . run ( 'MATCH (a)-[:KNOWS]->(b) RETURN a.name, b.name' ) ;
547
+
548
+ result . subscribe ( {
549
+ onNext : record => {
550
+ const name1 = record . get ( 0 ) ;
551
+ const name2 = record . get ( 1 ) ;
552
+
553
+ friends . push ( { 'name1' : name1 , 'name2' : name2 } ) ;
554
+ }
555
+ } ) ;
556
+ }
557
+
558
+ // To collect the session bookmarks
559
+ let savedBookmarks = [ ] ;
560
+
561
+ // Create the first person and employment relationship.
562
+ const session1 = driver . session ( neo4j . WRITE ) ;
563
+ const first = session1 . writeTransaction ( tx => addCompany ( tx , 'Wayne Enterprises' ) ) . then (
564
+ ignore => session1 . writeTransaction ( tx => addPerson ( tx , 'Alice' ) ) ) . then (
565
+ ignore => session1 . writeTransaction ( tx => addEmployee ( tx , 'Alice' , 'Wayne Enterprises' ) ) ) . then (
566
+ ignore => {
567
+ savedBookmarks . push ( session1 . lastBookmark ( ) ) ;
568
+
569
+ return session1 . close ( ) ;
570
+ } ) ;
571
+
572
+ // Create the second person and employment relationship.
573
+ const session2 = driver . session ( neo4j . WRITE ) ;
574
+ const second = session2 . writeTransaction ( tx => addCompany ( tx , 'LexCorp' ) ) . then (
575
+ ignore => session2 . writeTransaction ( tx => addPerson ( tx , 'Bob' ) ) ) . then (
576
+ ignore => session2 . writeTransaction ( tx => addEmployee ( tx , 'Bob' , 'LexCorp' ) ) ) . then (
577
+ ignore => {
578
+ savedBookmarks . push ( session2 . lastBookmark ( ) ) ;
579
+
580
+ return session2 . close ( ) ;
581
+ } ) ;
582
+
583
+ // Create a friendship between the two people created above.
584
+ const last = Promise . all ( [ first , second ] ) . then ( ignore => {
585
+ const session3 = driver . session ( neo4j . WRITE , savedBookmarks ) ;
586
+
587
+ return session3 . writeTransaction ( tx => makeFriends ( tx , 'Alice' , 'Bob' ) ) . then (
588
+ ignore => session3 . readTransaction ( findFriendships ) . then (
589
+ ignore => session3 . close ( )
590
+ )
591
+ ) ;
592
+ } ) ;
593
+ // end::pass-bookmarks[]
594
+
595
+ last . then ( ignore => {
596
+ expect ( friends . length ) . toBe ( 1 ) ;
597
+ expect ( friends [ 0 ] . name1 ) . toBe ( 'Alice' ) ;
598
+ expect ( friends [ 0 ] . name2 ) . toBe ( 'Bob' ) ;
599
+
600
+ done ( ) ;
601
+ } ) ;
602
+ } ) ;
603
+
511
604
} ) ;
512
605
513
606
function removeLineBreaks ( string ) {
0 commit comments