うれしく思います。pg-promiseユーザ1人当たりの接続
私はpg-promiseでウェブサイト/サーバを構築しています。 認証のためにpostgreロール/グループログインを使用します。
私は物事を正しくやっているのかどうかはわかりませんが、私は各ユーザが独自のpostgres接続を使ってデータベースに問い合わせることを望みます。
実際には、接続時に各ユーザーの接続を作成します(まだ存在していない場合)。 はそうするために、私は醜い「偽の約束」とPGUSERオブジェクトとプールオブジェクトを作成しました:
var pgPool = function(pg){
var _this=this;
var fakePromise = function(err){
var _this=this;
_this.err=err
_this.then=function(cb){if(!err){cb();return _this}else{return _this};};
_this.catch=function(cb){if(err){cb(_this.err)}else{return _this};};
return _this;
};
_this.check= function(user){
if (_this[user]){
return _this[user].check();
}else{
return new fakePromise({error:'Echec de connection à la base de
données'})
}
}
_this.add = function(user,password){
var c={};
c.host = 'localhost';
c.port = 5432;
c.database = 'pfe';
c.poolSize = 10;
c.poolIdleTimeout = 30000;
c.user=user;
c.password=password
if (!_this[user]){
_this[user] = new pgUser(c,pg);
return _this[user].check();
}else{
_this[user].config.password=password;
return _this[user].check();
};
};
return _this;
};
var pgUser = function(c,pg){
var _this=this
_this.config = c
_this.db = new pg(_this.config)
_this.check = function(){
return _this.db.connect();
};
return _this;
};
そして、ここで私は、ログインPOST処理中に「プール」にユーザーを追加する方法です
pool.add(req.body.user,req.body.password).then(function(obj){
obj.done();
req.session.user = req.body.user;
req.session.password = req.body.password;
res.redirect("/");
return;
}).catch(function(err){
options.error='incorect password/login';
res.render('login', options);
return;
});
私はそれがプロdeveloppersを刺激する可能性があり、あなたが私に最善の方法を説明することができれば、あなたが親切だろうと確信しています:
- は良いアイデアは、ユーザーごとにデータベースへの接続を1つ持っているということです (それは良いセキュリティを持っていることは正当だと思われます)?
- どうすればpg-promiseライブラリを使ってこの醜いカスタム 'プール'オブジェクトを避けることができますか?
ありがとうございます。
これはまったく表示されません。しかし、もし私がしたら、なぜあなたはこれを望んでいますか?そのようなソリューションは、スケーラビリティのために接続が共有する必要がある最も貴重なリソースであるため、拡張できません。あなたは目的に応じてあなたのサービスをハンディキャップしようとしています。 –
こんにちはvitaly - t。あなたのプロンプトの返信をありがとう。接続はスケーラビリティのために共有する必要がある最も貴重なリソースなので、「そのようなソリューションは拡張できません」という言い方を変えてください。私はセキュリティを強化するためにそれをしたい。それぞれのユーザーはpgへの独自の接続を持つ必要がありますか?私はポストグルで複数のユーザーを扱う方法をどこにも見つけることができません。私はサーバにpg(admin)との接続が1つしかありませんか?私はpostgresの行のセキュリティレベルの機能を使用したいと思います。 – alexnode
または、私はクエリを実行するたびに、ここであなたの例のグローバル接続変数を変更する必要がありますか? :https://stackoverflow.com/questions/8484404/what-is-the-properway-to-use-the-node-js-postgresql-module – alexnode