2013-12-16 76 views
1

有沒有辦法在Meteor啓動時從MySQL數據庫導入數據?我基本上只需要一個來自MySQL的初始數據將其導出到Mongo集合以供使用。如何從流星訪問MySQL數據庫

+0

http://stackoverflow.com/questions/19269075/can-we-use-meteor-framework-with- MySQL的數據庫 – bredikhin

回答

4

你最好的辦法可能就是使用一個mysql節點包(記得使用Meteor.npmRequire(..)而不是require(..))。這其中似乎不錯:

https://github.com/felixge/node-mysql

像這樣的東西應該工作:

if (Meteor.isServer) { 
    var mysql = Meteor.npmRequire('mysql'); 
    Meteor.startup(function() { 
     var connection = mysql.createConnection({ 
      host  : 'localhost', 
      user  : 'me', 
      password : 'secret' 
     }); 

     connection.connect(); 

     connection.query('SELECT * FROM table', function(err, rows, fields) { 
      if (err) throw err; 
      // create documents from rows[i] and add to your collection 
     }); 

     connection.end(); 

    }); 
}