简体   繁体   English

sails.js一对多关系-变量所有者集合属性

[英]sails.js one to many relationship - variable owner collection attribute

On sailsjs.org's documentation, a one to many relationship for the owning side is defined like this 在sailsjs.org的文档中,拥有方的一对多关系定义如下:

//user.js
module.exports = {
    attributes: {
        name:'STRING',
        age:'INTEGER',
        pets:{
            collection: 'pet',
            via: 'owner'
        }
    }
}

'pet' is a constant, with a consistent schema on SQL databases. “ pet”是一个常量,在SQL数据库上具有一致的架构。 What if I want to have a superclass pet and subclasses with unique attributes(different number of rows)? 如果我想拥有一个超类宠物和具有唯一属性(不同行数)的子类怎么办? Say I have an octopus and a dog. 说我有一个章鱼和一条狗。 Dogs have 4 legs and 2 ears. 狗有4条腿和2个耳朵。 Octopus have 8 tentacles. 章鱼有8个触手。 The only commonality will be abstracted into the pet class(color, name, age). 唯一的共性将抽象到宠物类别(颜色,名称,年龄)中。

If this is not possible, I would have to resort to something like this no? 如果无法做到这一点,我将不得不诉诸类似的办法吗?

//user.js
module.exports = {
    attributes: {
        name:'STRING',
        age:'INTEGER',
        dogs:{
            collection: 'dog',
            via: 'owner'
        },
        octopuses:{
            collection: 'octopus',
            via: 'owner'
        }
    }
}

However, this could get quite messy if I wanted to introduce more pets like eagle(can fly), parrot(can talk), and would result in many nulls if I was to use a SQL database. 但是,如果我想引入更多的宠物,如鹰(会飞),鹦鹉(会说话),这可能会变得很混乱,如果我要使用SQL数据库,则会导致很多null。 Perhaps mongoDB would be ideal for this? 也许mongoDB对此很理想?

In Waterline each model is treated as a table in a SQL database or a Collection in Mongo. 在Waterline中,每个模型都被视为SQL数据库中的表或Mongo中的Collection。 If a Dog will have completely different attributes from an Octopus then yes you would break those into separate tables and link them to the User. 如果“狗”与“章鱼”的属性完全不同,那么可以,将它们分成单独的表并将其链接到“用户”。 I think the easiest way would to just add a type attribute to the Pet model. 我认为最简单的方法是将type属性添加到Pet模型中。

// user.js
module.exports = {
    attributes: {
        name:'STRING',
        age:'INTEGER',
        pets:{
            collection: 'pet',
            via: 'owner'
        }
    }
}

// pet.js
module.exports = {
    attributes: {
        name:'STRING',
        type: 'STRING',
        owner:{
            model: 'user'
        }
    }
}

This would allow queries such as: 这将允许查询,例如:

User.find().populate('pets', { type: 'dog' });

Another option would be to store the pet attributes in a json object. 另一个选择是将pet属性存储在json对象中。 This isn't currently searchable but would allow you to store various things about the pets in a denormalized fashion. 目前尚无法搜索到此信息,但可以让您以非规范化方式存储有关宠物的各种信息。

// pet.js
module.exports = {
    attributes: {
        name:'STRING',
        type: 'STRING',
        characteristics: {
            type: 'json'
        },
        owner:{
            model: 'user'
        }
    }
}

Which would allow you to have pets that look like the following: 这将使您拥有如下宠物:

[{
    id: 1,
    name: 'fluffy',
    type: 'dog',
    characteristics: {
        food: 'mice',
        limbs: 4,
        fur: 'white'
    },
    owner: 1
},
{
    id: 2,
    name: 'inky',
    type: 'octopus',
    characteristics: {
        habitat: 'ocean'
        tentacles: 8,
        canChooseWorldCupWinners: true
    },
    owner: 1
}]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM