帆集合搜索

时间:2015-10-18 02:15:12

标签: javascript model sails.js

Sails nested model collection相似,但从未得到回答

我有2个模型可以轻松获取数据。我得到了播放器,从那时起我必须列出排名的权限。 我目前正在寻找.native()和.query()的替代方案,因为它们迫使我成为特定于数据库的。

/**
* Player.js
*
* @description :: TODO: You might write a short summary of how this model works and what it represents here.
* @docs        :: http://sailsjs.org/#!documentation/models
*/

module.exports = {

  attributes: {

    // SteamId of the user used as primary key
    steam_id: {
      type: 'string',
      primaryKey: true,
      unique: true,
      required: true
    },

    // The steam name of the player
    steam_name: {
      type: 'string',
      required: true
    },

    // A friendly name (RPname etc)
    friendly_name: {
      type: 'string'
    },

    servers:{
      collection: 'Server',
      via: 'players'
    },

    // The rank of the user (Admin, user, etc)
    ranks:{
      collection:'Rank',
      via: 'players',
      required: true
    },

    // List of their punishments (This includes warns)
    punishments: {
      collection: 'Punishment',
      via: 'victim'
    },

    notes: {
      collection: 'Note',
      via: 'player'
    }

  }
};

等级

/**
* Rank.js
*
* @description :: TODO: You might write a short summary of how this model works and what it represents here.
* @docs        :: http://sailsjs.org/#!documentation/models
*/

module.exports = {

  attributes: {


    name:{
      type: 'string',
      required: true
    },

    players:{
      collection: 'Player',
      via: 'ranks',
    },

    permissions:{
      collection: 'Permission',
      via: 'rank'
    },

    server:{
      model: 'Server',
      required: true
    }

  }
};

我能想到的唯一方法就是这样。

Player.findOne(Player).populate('ranks').exec(function(err, player){
    player.ranks.forEach(function(err, rank){
        Rank.findOne(rank).populate('permissions', {Command:1}).exec(function(error, rank){
            // What I want
            rank.permissions;
        });
    });
});

哪个过于繁琐。您能否提供一些有关如何提高效率或效率的提示?

我想做点什么

Rank.findOne().where({server:1,players:{contains:player}}).populate('permissions').exec(function(err, rank){
    //What I want
    rank.permissions
});

0 个答案:

没有答案