Skip to content

Commit 1178d1a

Browse files
committed
Support for bookmarking
1 parent dc6ed54 commit 1178d1a

File tree

3 files changed

+37
-4
lines changed

3 files changed

+37
-4
lines changed

src/v1/session.js

+8-2
Original file line numberDiff line numberDiff line change
@@ -79,15 +79,21 @@ class Session {
7979
*
8080
* @returns {Transaction} - New Transaction
8181
*/
82-
beginTransaction() {
82+
beginTransaction(bookmark) {
8383
if (this._hasTx) {
8484
throw newError("You cannot begin a transaction on a session with an "
8585
+ "open transaction; either run from within the transaction or use a "
8686
+ "different session.")
8787
}
8888

8989
this._hasTx = true;
90-
return new Transaction(this._connectionPromise, () => {this._hasTx = false}, this._onRunFailure());
90+
return new Transaction(this._connectionPromise, () => {
91+
this._hasTx = false},
92+
this._onRunFailure(), bookmark, (bookmark) => {this._lastBookmark = bookmark});
93+
}
94+
95+
lastBookmark() {
96+
return this._lastBookmark;
9197
}
9298

9399
/**

src/v1/transaction.js

+16-2
Original file line numberDiff line numberDiff line change
@@ -30,19 +30,25 @@ class Transaction {
3030
* @param {Promise} connectionPromise - A connection to use
3131
* @param {function()} onClose - Function to be called when transaction is committed or rolled back.
3232
* @param errorTransformer callback use to transform error
33+
* @param bookmark optional bookmark
3334
*/
34-
constructor(connectionPromise, onClose, errorTransformer) {
35+
constructor(connectionPromise, onClose, errorTransformer, bookmark, onBookmark) {
3536
this._connectionPromise = connectionPromise;
3637
let streamObserver = new _TransactionStreamObserver(this);
38+
let params = {};
39+
if (bookmark) {
40+
params = {bookmark: bookmark};
41+
}
3742
this._connectionPromise.then((conn) => {
3843
streamObserver.resolveConnection(conn);
39-
conn.run("BEGIN", {}, streamObserver);
44+
conn.run("BEGIN", params, streamObserver);
4045
conn.discardAll(streamObserver);
4146
}).catch(streamObserver.onError);
4247

4348
this._state = _states.ACTIVE;
4449
this._onClose = onClose;
4550
this._errorTransformer = errorTransformer;
51+
this._onBookmark = onBookmark || (() => {});
4652
}
4753

4854
/**
@@ -114,6 +120,14 @@ class _TransactionStreamObserver extends StreamObserver {
114120
this._hasFailed = true;
115121
}
116122
}
123+
124+
onCompleted(meta) {
125+
super.onCompleted(meta);
126+
let bookmark = meta.bookmark;
127+
if (bookmark) {
128+
this._tx._onBookmark(bookmark);
129+
}
130+
}
117131
}
118132

119133
/** internal state machine of the transaction*/

test/v1/transaction.test.js

+13
Original file line numberDiff line numberDiff line change
@@ -214,4 +214,17 @@ describe('transaction', function() {
214214
done();
215215
});
216216
});
217+
218+
it('should provide bookmark on commit', function (done) {
219+
// When
220+
var tx = session.beginTransaction();
221+
expect(session.lastBookmark()).not.toBeDefined();
222+
tx.run("CREATE (:TXNode1)");
223+
tx.run("CREATE (:TXNode2)");
224+
tx.commit()
225+
.then(function () {
226+
expect(session.lastBookmark()).toBeDefined();
227+
done();
228+
});
229+
});
217230
});

0 commit comments

Comments
 (0)