2016-11-28 13 views
0

私のFeathersJSアプリケーションでデータベースにログインする方法がわかりません。 サービスでデータベース情報とログイン情報を指定したいと思っていますが、動作させるだけです。FeathersJSのデータベースにログインするための情報を追加する

"postgres": "postgres://postgress:@localhost:5432/feathers_db", 

で: て、myApp /設定/ default.jsonで次の行がある

"postgres": "postgres://username:[email protected]:5432/feathers_db", 

http://docs.sequelizejs.com/en/latest/docs/getting-started/

これは、上記のような文字列がなければならないことを言います

しかし、これは動作しません。また、私はすべての私の前回の取引のために1つの後発DBにロックされているので、それはあまりFeathersのようなものではありません。

const sequelize = new Sequelize(app.get('postgres'), { 
    dialect: 'postgres', 
    logging: false 
}); 

私はSequelizeは彼らのガイドで行うと、引数としてユーザー名とパスワードを持って言うことを上記の行をカスタマイズしたが、その後可能性:サービスで

は/ index.js次の行がありますなぜこのようにレイアウトされていないのですか? この行もあります:

app.set('sequelize', sequelize); 

私はいくつかはpostgressデータベースを持っている場合、私は何をしますか?

app.set('sequelize_db1', sequelize_db1); 
app.set('sequelize_db2', sequelize_db2); 

または私は、サービスのモデルでは、ユーザ情報を含むDB情報を、指定しない:私は新しいSequelizeオブジェクトを作成し、ような何かを行うのですか? 続行するのではなく、汎用のdb言語を使用している場合、Postgressのログイン処理はどのようになりますか?

+0

あなたが提案した内容を正確に行う必要があります。それが発電機にないからといって、そうするのが正しい方法ではないというわけではありません。 – Daff

+0

私は、生成されたファイルを変更して、組み込みの機能を中断することに心配しています。彼らがそのようにして生成されるファイルを選んだ理由はわかりますが、その理由はわかりません。私はそれを試み、何が起こるか見る。ありがとう! –

+0

ジェネレータが使用しているすべての機能も文書化されています。私たちがこのように選んだのは、それが唯一の方法であるからではなく、維持するのが最も簡単なためです。 – Daff

答えて

2

単語「はい」です。私の質問で尋ねたものはすべて「はい」でした。 sequelizeのドキュメントにあるようなデータベースに接続できます。 config.jsonファイルを設定して、自分のユーザ名とdb名で "postgres"設定を行うこともできます。新しいsequelizeオブジェクトを作成するときに、services/index.jsファイルにフルパスを配置することもできます。

1はまた、いくつかのsequelizeオブジェクトを定義し、それらを設定することができます。

new_sequelize_object 
    .authenticate() 
    .then(function(err) { 
     console.log('Connection to the DB has been established successfully.'); 
    }) 
    .catch(function (err) { 
     console.log('Unable to connect to the database:', err); 
    }); 

http://docs.sequelizejs.com/en/latest/docs/getting-started/から撮影:)接続があることを確認する最良の方法は、新しいsequelizeオブジェクトを作成した後、次のコードを持つことですアプリで次に、特定のサービスのindex.jsファイルでモデルを定義するときは、新しいバインド名をapp.get( 'new_sequelize_object')に配置します。

'use strict'; 
const service1 = require('./service1'); 
const authentication = require('./authentication'); 
const user = require('./user'); 
const Sequelize = require('sequelize'); 
module.exports = function() { 
    const app = this; 

    const sequelize = new Sequelize('feathers_db1', 'u1', 'upw', { 
host: 'localhost', 
port: 5432, 
    dialect: 'postgres', 
    logging: false 
    }); 

    const sequelize2 = new Sequelize('postgres://u1:[email protected]:5432/feathers_db2', { 
    dialect: 'postgres', 
    logging: false 
    }); 

    app.set('sequelize', sequelize); 
    app.set('sequelize2', sequelize2); 

sequelize 
    .authenticate() 
    .then(function(err) { 
    console.log('Connection to sequelize has been established successfully.'); 
    }) 
    .catch(function (err) { 
    console.log('Unable to connect to the database:', err); 
    }); 

sequelize2 
    .authenticate() 
    .then(function(err) { 
    console.log('Connection has been established to sequelize2 successfully.'); 
    }) 
    .catch(function (err) { 
    console.log('Unable to connect to the database:', err); 
    }); 


    app.configure(authentication); 
    app.configure(user); 
    app.configure(service1); 
}; 

そして、ここでサービスsequelize2を使用してサービス1/index.jsファイルである:ここで

は、定義された2つのデータベースを持つ/ index.jsファイルサービスです 「厳格な使用」;

const service = require('feathers-sequelize'); 
const service1 = require('./service1-model'); 
const hooks = require('./hooks'); 

module.exports = function(){ 
    const app = this; 

    const options = { 
    //Here is where one sets the name of the differeng sequelize objects 
    Model: service1(app.get('sequelize2')), 
    paginate: { 
     default: 5, 
     max: 25 
    } 
    }; 

    // Initialize our service with any options it requires 
    app.use('/service1', service(options)); 

    // Get our initialize service to that we can bind hooks 
    const service1Service = app.service('/service1'); 


    // Set up our before hooks 
    service1Service.before(hooks.before); 
    // Set up our after hooks 
    service1Service.after(hooks.after); 
}; 
関連する問題