Skip to content

Add example for passing bookmarks #311

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Dec 14, 2017
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 93 additions & 0 deletions test/v1/examples.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -508,6 +508,99 @@ describe('examples', () => {
done();
});
});

it('pass bookmarks example', done => {
const driver = driverGlobal;

// tag::pass-bookmarks[]
// Create a company node
function addCompany(tx, name) {
return tx.run('CREATE (a:Company {name: $name})', {'name': name});
}

// Create a person node
function addPerson(tx, name) {
return tx.run('CREATE (a:Person {name: $name})', {'name': name});
}

// Create an employment relationship to a pre-existing company node.
// This relies on the person first having been created.
function addEmployee(tx, personName, companyName) {
return tx.run('MATCH (person:Person {name: $personName}) ' +
'MATCH (company:Company {name: $companyName}) ' +
'CREATE (person)-[:WORKS_FOR]->(company)', {'personName': personName, 'companyName': companyName});
}

// Create a friendship between two people.
function makeFriends(tx, name1, name2) {
return tx.run('MATCH (a:Person {name: $name1}) ' +
'MATCH (b:Person {name: $name2}) ' +
'MERGE (a)-[:KNOWS]->(b)', {'name1': name1, 'name2': name2});
}

// To collect friend relationships
const friends = [];

// Match and display all friendships.
function findFriendships(tx) {
const result = tx.run('MATCH (a)-[:KNOWS]->(b) RETURN a.name, b.name');

result.subscribe({
onNext: record => {
const name1 = record.get(0);
const name2 = record.get(1);

friends.push({'name1': name1, 'name2': name2});
}
});
}

// To collect the session bookmarks
let savedBookmarks = [];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe const


// Create the first person and employment relationship.
const session1 = driver.session(neo4j.WRITE);
const first = session1.writeTransaction(tx => addCompany(tx, 'Wayne Enterprises')).then(
ignore => session1.writeTransaction(tx => addPerson(tx, 'Alice'))).then(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

() instead of ignore will probably look better

ignore => session1.writeTransaction(tx => addEmployee(tx, 'Alice', 'Wayne Enterprises'))).then(
ignore => {
savedBookmarks.push(session1.lastBookmark());

return session1.close();
});

// Create the second person and employment relationship.
const session2 = driver.session(neo4j.WRITE);
const second = session2.writeTransaction(tx => addCompany(tx, 'LexCorp')).then(
ignore => session2.writeTransaction(tx => addPerson(tx, 'Bob'))).then(
ignore => session2.writeTransaction(tx => addEmployee(tx, 'Bob', 'LexCorp'))).then(
ignore => {
savedBookmarks.push(session2.lastBookmark());

return session2.close();
});

// Create a friendship between the two people created above.
const last = Promise.all([first, second]).then(ignore => {
const session3 = driver.session(neo4j.WRITE, savedBookmarks);

return session3.writeTransaction(tx => makeFriends(tx, 'Alice', 'Bob')).then(
ignore => session3.readTransaction(findFriendships).then(
ignore => session3.close()
)
);
});
// end::pass-bookmarks[]

last.then(ignore => {
expect(friends.length).toBe(1);
expect(friends[0].name1).toBe('Alice');
expect(friends[0].name2).toBe('Bob');

done();
});
});

});

function removeLineBreaks(string) {
Expand Down