Skip to content

Commit 9040f4e

Browse files
authored
Merge pull request #311 from ali-ince/1.5-add-passing-bookmark-example
Add example for passing bookmarks
2 parents c5a17b6 + 7ad6e30 commit 9040f4e

File tree

2 files changed

+99
-3
lines changed

2 files changed

+99
-3
lines changed

src/v1/internal/bookmark.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,10 +90,13 @@ function asStringArray(value) {
9090
const result = [];
9191
for (let i = 0; i < value.length; i++) {
9292
const element = value[i];
93-
if (!util.isString(element)) {
94-
throw new TypeError(`Bookmark should be a string, given: '${element}'`);
93+
// if it is undefined or null, ignore it
94+
if (element !== undefined && element !== null) {
95+
if (!util.isString(element)) {
96+
throw new TypeError(`Bookmark should be a string, given: '${element}'`);
97+
}
98+
result.push(element);
9599
}
96-
result.push(element);
97100
}
98101
return result;
99102
}

test/v1/examples.test.js

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -508,6 +508,99 @@ describe('examples', () => {
508508
done();
509509
});
510510
});
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+
const 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+
() => session1.writeTransaction(tx => addPerson(tx, 'Alice'))).then(
565+
() => session1.writeTransaction(tx => addEmployee(tx, 'Alice', 'Wayne Enterprises'))).then(
566+
() => {
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+
() => session2.writeTransaction(tx => addPerson(tx, 'Bob'))).then(
576+
() => session2.writeTransaction(tx => addEmployee(tx, 'Bob', 'LexCorp'))).then(
577+
() => {
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+
() => session3.readTransaction(findFriendships).then(
589+
() => session3.close()
590+
)
591+
);
592+
});
593+
// end::pass-bookmarks[]
594+
595+
last.then(() => {
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+
511604
});
512605

513606
function removeLineBreaks(string) {

0 commit comments

Comments
 (0)