【问题标题】:How to do dynamic queries on MySQL from Meteor?如何从 Meteor 对 MySQL 进行动态查询?
【发布时间】:2016-11-08 09:23:34
【问题描述】:

我一直在尝试使用 numtel:mysql 包从 Meteor 对 MySQL 进行动态查询。到目前为止,它还没有成功。也许我需要知道如何将动态参数传递给订阅,或者需要知道如何将 liveDb.select 的结果作为数组或对象而不是游标来获取(liveDb 通过调用 new LiveMysql(... ))。我已经尝试在服务器端的方法中进行查询(如 Meteor.methods(...) 中声明的那样,并且该方法不返回结果。如果有人有这方面的代码示例,那将非常感激!

【问题讨论】:

    标签: javascript mysql node.js meteor


    【解决方案1】:

    这里是使用 Meteor 和 MySQL 进行选择查询、执行存储过程、用户 mongo 喜欢发布订阅方法以及在发布中加入查询的有用链接。

    1. MySql:numtel package
    2. Leaderboard MySql example

      // 下面的代码参考来自Leaderboard MySql example

       Meteor.publish('allPlayers', function() {
      return liveDb.select(
      'SELECT * FROM players ORDER BY score DESC',
      [ { table: 'players' } ]
      );
      });
      Meteor.publish('playerScore', function(name) {
      return liveDb.select(
      'SELECT id, score FROM players WHERE name = ' + liveDb.db.escape(name),
      [
      {
      table: 'players',
      condition: function(row, newRow, rowDeleted) {
      // newRow provided on UPDATE query events
      return row.name === name || (newRow && newRow.name === name);
      }
      }
      ]
      );
      
      Meteor.methods({
      'incScore': function(id, amount) {
      check(id, Number);
      check(amount, Number);
      liveDb.db.query(
      'UPDATE players SET score = score + ? WHERE id = ?', [ amount, id ]);
      }
      });
      

    【讨论】:

      【解决方案2】:

      您需要使用与 Meteor.publish 中定义的参数相同的参数调用 MysqlSubscription 上的更改。

      就我而言:

          export const mySqlSubscription = new MysqlSubscription('tableName');
      
          Meteor.publish('tableName', function(filters){
              var filterString = buildFilterString(filters);
      
              var qString = 'SELECT ..... FROM tableName '+filterString;
      
              //console.log(qString);
      
              return liveDb.select( qString, [ { table: 'tableName' } ] );            
              }
          );
      

      然后,每当我想为不同的过滤器更新订阅时,我都会调用:

          mySqlSubscription.change(this.getReactively('filterRecord'));
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-04-02
        • 1970-01-01
        • 1970-01-01
        • 2020-06-01
        • 1970-01-01
        • 2019-12-17
        • 2021-03-07
        相关资源
        最近更新 更多