Skip to content

Commit 5de0c24

Browse files
authored
feature: commit timestamp (#163)
1 parent ca1a88b commit 5de0c24

3 files changed

Lines changed: 38 additions & 4 deletions

File tree

handwritten/spanner/src/index.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,15 @@ function Spanner(options) {
213213

214214
util.inherits(Spanner, commonGrpc.Service);
215215

216+
/**
217+
* Placeholder used to auto populate a column with the commit timestamp.
218+
* This can only be used for timestamp columns that have set the option
219+
* "(allow_commit_timestamp=true)" in the schema.
220+
*
221+
* @type {string}
222+
*/
223+
Spanner.COMMIT_TIMESTAMP = 'spanner.commit_timestamp()';
224+
216225
/**
217226
* Helper function to get a Cloud Spanner Date object.
218227
*

handwritten/spanner/src/v1/spanner_client.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,7 @@ class SpannerClient {
205205
static get scopes() {
206206
return [
207207
'https://www.googleapis.com/auth/cloud-platform',
208+
'https://www.googleapis.com/auth/spanner.admin',
208209
'https://www.googleapis.com/auth/spanner.data',
209210
];
210211
}

handwritten/spanner/system-test/spanner.js

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ describe('Spanner', function() {
6666

6767
insertData.Key = id;
6868

69-
table.insert(insertData, function(err) {
69+
table.insert(insertData, function(err, insertResp) {
7070
if (err) {
7171
callback(err);
7272
return;
@@ -79,13 +79,13 @@ describe('Spanner', function() {
7979
id: id,
8080
},
8181
},
82-
function(err, rows) {
82+
function(err, rows, readResp) {
8383
if (err) {
8484
callback(err);
8585
return;
8686
}
8787

88-
callback(null, rows.shift());
88+
callback(null, rows.shift(), insertResp, readResp);
8989
}
9090
);
9191
});
@@ -110,7 +110,8 @@ describe('Spanner', function() {
110110
FloatArray ARRAY<FLOAT64>,
111111
IntArray ARRAY<INT64>,
112112
StringArray ARRAY<STRING(MAX)>,
113-
TimestampArray ARRAY<TIMESTAMP>
113+
TimestampArray ARRAY<TIMESTAMP>,
114+
CommitTimestamp TIMESTAMP OPTIONS (allow_commit_timestamp=true)
114115
) PRIMARY KEY (Key)
115116
`,
116117
},
@@ -600,6 +601,24 @@ describe('Spanner', function() {
600601
});
601602
});
602603

604+
describe('commit timestamp', function() {
605+
it('should accept the commit timestamp placeholder', function(done) {
606+
var data = {CommitTimestamp: Spanner.COMMIT_TIMESTAMP};
607+
608+
insert(data, function(err, row, commitResponse) {
609+
assert.ifError(err);
610+
611+
var timestampFromCommit = fromProtoToDate(
612+
commitResponse.commitTimestamp
613+
);
614+
var timestampFromRead = row.toJSON().CommitTimestamp;
615+
616+
assert.deepEqual(timestampFromCommit, timestampFromRead);
617+
done();
618+
});
619+
});
620+
});
621+
603622
it('should throw an error for incorrect value types', function(done) {
604623
table.insert({BoolValue: 'abc'}, function(err) {
605624
assert(err);
@@ -3584,3 +3603,8 @@ function wait(time) {
35843603
setTimeout(resolve, time);
35853604
});
35863605
}
3606+
3607+
function fromProtoToDate(obj) {
3608+
var milliseconds = parseInt(obj.nanos, 10) / 1e6;
3609+
return new Date(parseInt(obj.seconds, 10) * 1000 + milliseconds);
3610+
}

0 commit comments

Comments
 (0)