Skip to content
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

Add google cloud datastore Lock implementation #135

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ It can be very useful as domain component if you work with (d)ddd, cqrs, eventde
},

// optional, default is in-memory
// currently supports: mongodb, redis, tingodb, couchdb, azuretable, dynamodb and inmemory
// currently supports: mongodb, redis, tingodb, couchdb, azuretable, dynamodb, cloud datastore and inmemory
// hint settings like: [eventstore](https://github.com/adrai/node-eventstore#provide-implementation-for-storage)
aggregateLock: {
type: 'redis',
Expand Down
130 changes: 130 additions & 0 deletions lib/lock/databases/datastore.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
var util = require('util'),
Lock = require('../base'),
_ = require('lodash'),
async = require('async'),
DS = Lock.use('@google-cloud/datastore');

function Datastore(options) {
options = options || {};

var dsConf = {
projectId: ""
};

this.options = _.defaults(options, dsConf);

var defaults = {
lockTableName: 'aggregatelock'
};

this.options = _.defaults(this.options, defaults);
}

util.inherits(Datastore, Lock);

_.extend(Datastore.prototype, {

AGGREGATE_KIND: "Aggregate",

connect: function (callback) {
var self = this;
self.client = new DS(self.options);
self.isConnected = true;

self.emit('connect');
if (callback) callback(null, self);
},

disconnect: function (callback) {
// do nothing on cloud datastore client
this.emit('disconnect');
if (callback) callback(null);
},

reserve: function(workerId, aggregateId, callback) {
var self = this;
var client = self.client;

var entity = {
key: client.key([self.AGGREGATE_KIND, aggregateId, self.options.lockTableName, workerId]),
data: {
aggregateId: aggregateId,
workerId: workerId,
timeStamp: new Date()
}
};

client.save(entity, function(err, apiResponse) {
if(callback) callback(err);
});
},

getAll: function(aggregateId, callback) {
var self = this;
var client = self.client;

if (callback) {
var q = client
.createQuery(self.options.lockTableName)
.hasAncestor(client.key([self.AGGREGATE_KIND, aggregateId]))
.order("timeStamp");

client.runQuery(q, function(err, entities, info) {
if (err) {
return callback(err);
}

var res = entities.map(function(r){ return r.workerId});
callback(null, res);
});
}
},

resolve: function(aggregateId, callback) {
var self = this;
var client = self.client;

var q = client
.createQuery(self.options.lockTableName)
.select('__key__')
.hasAncestor(client.key([self.AGGREGATE_KIND, aggregateId]));

client.runQuery(q, function(err, entities) {
if (err) {
if (callback) callback(err);
return;
}

var keys = entities.map(function(r){ return r[client.KEY] });

client.delete(keys, function(err) {
if (callback) callback(err);
});
});
},

clear: function(callback) {
var self = this;
var client = self.client;

var q = client
.createQuery(self.options.lockTableName)
.select('__key__');

client.runQuery(q, function(err, entities) {
if (err) {
if (callback) callback(err);
return;
}

var keys = entities.map(function(r){ return r[client.KEY] });

client.delete(keys, function(err) {
if (callback) callback(err);
});
});
}

});

module.exports = Datastore;
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"uuid": "3.1.0"
},
"devDependencies": {
"@google-cloud/datastore": "^2.0.0",
"aws-sdk": ">=2.96.0",
"azure-storage": ">=0.10.0",
"cradle": ">=0.6.7",
Expand Down
2 changes: 1 addition & 1 deletion test/unit/aggregateLockTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ describe('AggregateLock', function() {

describe('with options containing a type property with the value of', function() {

var types = ['inmemory', 'mongodb', 'tingodb', 'redis', 'dynamodb'/*, 'couchdb', 'azuretable'*/];
var types = ['inmemory', 'mongodb', 'tingodb', 'redis', 'dynamodb', 'datastore'/*, 'couchdb', 'azuretable'*/];

types.forEach(function(type) {

Expand Down
8 changes: 8 additions & 0 deletions test/unit/index.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# This is needed for testing google cloud datastore, since a lot of queries are using composite index
# please set up your gcloud-cli, and run "gcloud datastore create-indexes test/unit/index.yaml"

indexes:
- kind: aggregatelock
ancestor: yes
properties:
- name: timeStamp