Skip to content
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
9 changes: 9 additions & 0 deletions lib/gclouddatastore/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module.exports = {
insert : require('./insert'),
update : require('./update'),
upsert : function() {
var update = require('./update').apply(this,arguments);
update.options.upsert = true;
return update;
}
};
32 changes: 32 additions & 0 deletions lib/gclouddatastore/insert.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
var Streamz = require('streamz'),
Promise = require('bluebird'),
util = require('util');

function Insert(client,options) {
if (!(this instanceof Streamz))
return new Insert(_c,client);

if (!client)
throw 'CLIENT_MISSING';

Streamz.call(this,options);
this.client = client;
this.options = options || {};
}

util.inherits(Insert,Streamz);

Insert.prototype._fn = function(d) {
var self = this;
var query = {
key: client.key([this.options.prefix]),
data: d
};
return client.insert(query)
.then(d => {
if (self.options.pushResult)
return d;
});
};

module.exports = Insert;
40 changes: 40 additions & 0 deletions lib/gclouddatastore/update.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
var Streamz = require('streamz'),
Promise = require('bluebird'),
util = require('util');

function Update(client,keys,options) {
if (!(this instanceof Streamz))
return new Update(_c,client);

if (!client)
throw 'CLIENT_MISSING';

if (keys === undefined)
throw new Error('Missing Keys');

Streamz.call(this,options);
this.client = client;
this.options = options || {};
this.keys = [].concat(keys);
}

util.inherits(Update,Streamz);

Update.prototype._fn = function(d) {
var self = this;
var query = this.keys.reduce((o,key) => {
if (d[key] === undefined)
throw new Error('Key not found in data');
return o.push({
key: self.client.key([self.options.prefix,key]),
data: d[key]
});
},[]);
return (this.options.upsert ? client.upsert(query) : client.update(query))
.then(d => {
if (self.options.pushResult)
return d;
});
};

module.exports = Update;