mirror of
https://github.com/seejohnrun/haste-server.git
synced 2024-11-01 11:31:22 +00:00
92 lines
2.3 KiB
JavaScript
92 lines
2.3 KiB
JavaScript
/*global require,module,process*/
|
|
|
|
const datastore = require('@google-cloud/datastore');
|
|
const winston = require('winston');
|
|
|
|
class GoogleDatastoreDocumentStore {
|
|
|
|
// Create a new store with options
|
|
constructor(options) {
|
|
this.kind = "Haste";
|
|
this.expire = options.expire;
|
|
this.datastore = new Datastore();
|
|
}
|
|
|
|
// Save file in a key
|
|
set(key, data, callback, skipExpire) {
|
|
var now = new Date();
|
|
var expireTime = skipExpire ? null : new Date(now.getTime() + this.expire * 1000;
|
|
|
|
var taskKey = this.datastore.key([this.kind, key])
|
|
var task = {
|
|
key: taskKey,
|
|
data: [
|
|
{
|
|
name: 'value',
|
|
value: data,
|
|
excludeFromIndexes: true
|
|
},
|
|
{
|
|
name: 'expiration',
|
|
value: expireTime
|
|
}
|
|
]
|
|
};
|
|
|
|
this.datastore.insert(task).then(() => {
|
|
callback(true);
|
|
})
|
|
.catch(err => {
|
|
callback(false);
|
|
});
|
|
}
|
|
|
|
// Get a file from a key
|
|
get(key, callback, skipExpire) {
|
|
var taskKey = this.datastore.key([this.kind, key])
|
|
|
|
this.datastore.get(taskKey).then((entity) => {
|
|
|
|
if (skipExpire) {
|
|
callback(entity[0]["value"]);
|
|
}
|
|
else {
|
|
// check for expiry
|
|
if (entity[0]["expiration"] != null && entity[0]["expiration"] < new Date()) {
|
|
winston.info("document expired", {key: key});
|
|
callback(false);
|
|
}
|
|
else {
|
|
|
|
// update expiry
|
|
var now = new Date();
|
|
var task = {
|
|
key: taskKey,
|
|
data: [
|
|
{
|
|
name: 'value',
|
|
value: entity[0]["value"],
|
|
excludeFromIndexes: true
|
|
},
|
|
{
|
|
name: 'expiration',
|
|
value: new Date(now.getTime() + this.expire * 1000;
|
|
}
|
|
]
|
|
};
|
|
this.datastore.update(task).then(() => {
|
|
})
|
|
.catch(err => {
|
|
winston.error("failed to update expiration", {error: err});
|
|
});
|
|
callback(entity[0]["value"]);
|
|
}
|
|
}
|
|
})
|
|
.catch(err => {
|
|
winston.error("Error retrieving value from Google Datastore", {error: err});
|
|
callback(false);
|
|
});
|
|
}
|
|
|
|
module.exports = GoogleDatastoreDocumentStore;
|