neumino / thinky

JavaScript ORM for RethinkDB
http://justonepixel.com/thinky/
Other
1.12k stars 128 forks source link

about relations #552

Closed xy02 closed 8 years ago

xy02 commented 8 years ago

Question

Now I have a data about Android Views:

//class View
var viewData = {
    //class LinearLayout
    linearLayout:{
        vertical: true,
        items: [
            //has many Views
            //class View
            {
                //class ImageView
                imageView:{}
            },
            //class View
            {
                //class LinearLayout
                linearLayout: {
                    vertical: false,
                    items:[
                        //has many Views
                        //class View
                        {
                            //class ImageView
                            imageView: {}
                        },
                        //class View
                        {
                            //class ImageView
                            imageView: {}
                        }
                    ]
                }
            }
        ]
    }
}

one LinearLayout or ImageView instance only belongsTo one instance of View (is relation 1-1 ?) one LinearLayout instance contains many items of View (1-n?) how to define their schema and relations exactly? thanks a lot

vmarcel commented 8 years ago

A model can't have two different relation types to the same model

For belongsTo

LinearLayout.belongsTo(View, 'name of field', 'FK to LinearLayout', 'PK of View');
ImageView.belongsTo(View, 'name of field', 'FK to ImageView', 'PK of View');

For hasMany

LinearLayout.hasMany(View, 'name of field', 'PK of LinearLayout', 'FK to View');
xy02 commented 8 years ago

@vmarcel thanks! then how to design the schema of polymorphic type? (we know LinearLayout and ImageView extend View, and LinearLayout contains many Views)

neumino commented 8 years ago

There's no polymorphic type in thinky. As for the questions about relations I think @vmarcel answered it.