mirror of
https://github.com/seejohnrun/haste-server.git
synced 2024-11-01 03:21:21 +00:00
Added postgres adapter
This commit is contained in:
parent
447d0aae76
commit
ab029eae2f
3 changed files with 66 additions and 61 deletions
|
@ -56,7 +56,6 @@ DocumentHandler.prototype.handlePost = function (request, response) {
|
||||||
// What to do when done
|
// What to do when done
|
||||||
var onSuccess = function () {
|
var onSuccess = function () {
|
||||||
// Check length
|
// Check length
|
||||||
console.log(buffer);
|
|
||||||
if (_this.maxLength && buffer.length > _this.maxLength) {
|
if (_this.maxLength && buffer.length > _this.maxLength) {
|
||||||
cancelled = true;
|
cancelled = true;
|
||||||
winston.warn('document >maxLength', { maxLength: _this.maxLength });
|
winston.warn('document >maxLength', { maxLength: _this.maxLength });
|
||||||
|
|
|
@ -3,72 +3,78 @@
|
||||||
var postgres = require('pg');
|
var postgres = require('pg');
|
||||||
var winston = require('winston');
|
var winston = require('winston');
|
||||||
|
|
||||||
(function () {
|
// create table entries (id SERIAL primary key, key varchar(255) not null, value text not null, expiration int);
|
||||||
|
// CREATE UNIQUE INDEX unique_key ON entries (key);
|
||||||
|
|
||||||
'use strict';
|
// A postgres document store
|
||||||
|
var PostgresDocumentStore = function (options) {
|
||||||
|
this.expireJS = options.expire;
|
||||||
|
this.connectionUrl = process.env.DATABASE_URL || options.connectionUrl;
|
||||||
|
};
|
||||||
|
|
||||||
// A postgres document store
|
PostgresDocumentStore.prototype = {
|
||||||
var PostgresDocumentStore = function (options) {
|
|
||||||
this.expireJS = options.expire * 1000;
|
|
||||||
this.connectionString = process.env.DATABASE_URL;
|
|
||||||
};
|
|
||||||
|
|
||||||
PostgresDocumentStore.prototype = {
|
// Set a given key
|
||||||
|
set: function (key, data, callback, skipExpire) {
|
||||||
// Set a given key
|
var now = Math.floor(new Date().getTime() / 1000);
|
||||||
set: function (key, data, callback, skipExpire) {
|
var that = this;
|
||||||
var now = new Date().getTime() / 1000;
|
this.safeConnect(function (err, client, done) {
|
||||||
var that = this;
|
if (err) { return callback(false); }
|
||||||
this.safeConnect(function (err, client, done) {
|
client.query('INSERT INTO entries (key, value, expiration) VALUES ($1, $2, $3)', [
|
||||||
if (err) { return callback(false); }
|
key,
|
||||||
client.query('INSERT INTO entries (key, value, expiration) VALUES ($1, $2, $3)', [
|
data,
|
||||||
key,
|
that.expireJS && !skipExpire ? that.expireJS + now : null
|
||||||
data,
|
], function (err, result) {
|
||||||
that.expireJS && !skipExpire ? now + that.expireJS : null
|
|
||||||
], function (err, result) {
|
|
||||||
if (err) {
|
|
||||||
winston.error('error persisting value to postgres', { error: err });
|
|
||||||
return callback(false);
|
|
||||||
}
|
|
||||||
callback(true);
|
|
||||||
done();
|
|
||||||
});
|
|
||||||
});
|
|
||||||
},
|
|
||||||
|
|
||||||
// Get a given key's data
|
|
||||||
get: function (key, callback, skipExpire) {
|
|
||||||
var now = new Date().getTime() / 1000;
|
|
||||||
var that = this;
|
|
||||||
this.safeConnect(function (err, client, done) {
|
|
||||||
if (err) { return callback(false); }
|
|
||||||
client.query('SELECT value from entries where KEY = $1 AND (expiration IS NULL or expiration < $2)', [
|
|
||||||
key,
|
|
||||||
that.expireJS ? now - that.expireJS : 0
|
|
||||||
], function (err, result) {
|
|
||||||
if (err) {
|
|
||||||
winston.error('error retrieving value from postgres', { error: err });
|
|
||||||
return callback(false);
|
|
||||||
}
|
|
||||||
callback(result.rows.length ? result.rows[0].value : false);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
},
|
|
||||||
|
|
||||||
// A connection wrapper
|
|
||||||
safeConnect: function (callback) {
|
|
||||||
postgres.connect(this.connectionString, function (err, client, done) {
|
|
||||||
if (err) {
|
if (err) {
|
||||||
winston.error('error connecting to postgres', { error: err });
|
winston.error('error persisting value to postgres', { error: err });
|
||||||
callback(err);
|
return callback(false);
|
||||||
|
}
|
||||||
|
callback(true);
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
},
|
||||||
|
|
||||||
|
// Get a given key's data
|
||||||
|
get: function (key, callback, skipExpire) {
|
||||||
|
var now = Math.floor(new Date().getTime() / 1000);
|
||||||
|
var that = this;
|
||||||
|
this.safeConnect(function (err, client, done) {
|
||||||
|
if (err) { return callback(false); }
|
||||||
|
client.query('SELECT id,value,expiration from entries where KEY = $1 and (expiration IS NULL or expiration > $2)', [key, now], function (err, result) {
|
||||||
|
if (err) {
|
||||||
|
winston.error('error retrieving value from postgres', { error: err });
|
||||||
|
return callback(false);
|
||||||
|
}
|
||||||
|
callback(result.rows.length ? result.rows[0].value : false);
|
||||||
|
if (result.rows.length && that.expireJS && !skipExpire) {
|
||||||
|
client.query('UPDATE entries SET expiration = $1 WHERE ID = $2', [
|
||||||
|
that.expireJS + now,
|
||||||
|
result.rows[0].id
|
||||||
|
], function (err, result) {
|
||||||
|
if (!err) {
|
||||||
|
done();
|
||||||
|
}
|
||||||
|
});
|
||||||
} else {
|
} else {
|
||||||
callback(undefined, client, done);
|
done();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
});
|
||||||
|
},
|
||||||
|
|
||||||
};
|
// A connection wrapper
|
||||||
|
safeConnect: function (callback) {
|
||||||
|
postgres.connect(this.connectionUrl, function (err, client, done) {
|
||||||
|
if (err) {
|
||||||
|
winston.error('error connecting to postgres', { error: err });
|
||||||
|
callback(err);
|
||||||
|
} else {
|
||||||
|
callback(undefined, client, done);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
module.export = PostgresDocumentStore;
|
};
|
||||||
|
|
||||||
}());
|
module.exports = PostgresDocumentStore;
|
||||||
|
|
|
@ -37,7 +37,7 @@ if (!config.storage.type) {
|
||||||
|
|
||||||
var Store, preferredStore;
|
var Store, preferredStore;
|
||||||
|
|
||||||
if (process.env.REDISTOGO_URL) {
|
if (process.env.REDISTOGO_URL && config.storage.type === 'redis') {
|
||||||
var redisClient = require('redis-url').connect(process.env.REDISTOGO_URL);
|
var redisClient = require('redis-url').connect(process.env.REDISTOGO_URL);
|
||||||
Store = require('./lib/document_stores/redis');
|
Store = require('./lib/document_stores/redis');
|
||||||
preferredStore = new Store(config.storage, redisClient);
|
preferredStore = new Store(config.storage, redisClient);
|
||||||
|
|
Loading…
Reference in a new issue