Sails嵌套模型集合

时间:2014-12-07 11:58:30

标签: mysql orm coffeescript sails.js

我有3个型号。

用户模型:

module.exports =
  schema: true
  attributes:
    login:
      type: 'string'
      required: true
    hosts:
      collection: 'host'
      via: 'owners'
    accounts:
      collection: 'account'
      via: 'users'

主机型号:

module.exports =

  attributes:
    url:
      type: 'string'
      required: true
      unique: true
    owners:
      collection: 'user'
      via: 'hosts'
    accounts:
      collection: 'account'
      via: 'host'

帐户模型:

module.exports =

  attributes:
    provider:
      type: 'string'
      required: true
    password:
      type: 'string'
    users:
      collection: 'user'
      via: 'accounts'
    host:
      model: 'host'

我发现所有用户都拥有帐号。怎么做?

这个选项有效,但我不确定他是不是很好,如果你告诉我怎么做的话,我会非常感激。或者也许在我的版本中更改一些内容。

User
.findOne(id)
.populate('hosts')
.then (hosts) ->
  hostsObj = []
  for host in hosts.hosts
    accounts = Host
    .find(host.id)
    .populate('accounts')
    .then (account) ->
      hostsObj.push(account[0])
      return account
  return [hostsObj,accounts]
.spread (hostsObj, accounts) ->
  done(null, hostsObj)
.catch (err) ->
  return done('Not hosts') if err

0 个答案:

没有答案