How to implement many to many association using through in Sails?

戏子无情 提交于 2019-12-12 02:43:29

问题


I'm using Sails v0.11.2 and MongoDB 3.2 on Mac OS X El Capitan and I'm trying to implement Many-To-Many association using Through option which isn't supported yet.

However, googling I found this Waterline Github Issue and elennaro, a github user, gave me a couple of links with some examples:

  1. First one
  2. Second one

I have tried to adapt them to my own Sails app but I can't make it works. I got no errors on the console but the record or document on the intermediary table is not created only the Form document in it's table.

These are my models:

User.js

module.exports = {
    schema: true,
    tableName: 'users',
    autoCreatedAt: false,
    autoUpdatedAt: false,

    attributes:
    {
        email               : { type: 'email', required: true, unique: true },
        encrypted_password  : { type: 'string' },
        reset_password_token: { type: 'string', defaultsTo: null},
        permission_level    : { type: 'integer', required: true, min: 1, max: 3, defaultsTo: 0 },
        belongs_to          : { type: 'string', required: true, defaultsTo: 0 },
        signin_count        : { type: 'integer', required: true, defaultsTo: 1 },
        status_active       : { type: 'boolean', required: true, defaultsTo: false },
        last_signin_at      : { type: 'datetime', defaultsTo: function (){ return new Date(); } },
        last_signin_ip      : { type: 'string', defaultsTo: '0.0.0.0' },

        // Add a reference to Person
        person_id:
        {
            model: 'person'
        },
        // Add a reference to Forms collection
        forms:
        {
            collection: 'form',
            via: 'user_id',
            through: 'userhasform'
        },
        has:
        {
            collection: 'userhasform',
            via: 'form_id'
        }
    }

};

Form.js

module.exports = {
    schema: true,
    tableName: 'forms',

    attributes:
    {
        name    : { type: 'string', required: true, unique: true },
        creator : { type: 'string', unique: false },
        sequence: { type: 'integer', autoIncrement: true },

        // Add a reference to Questions collection
        questions:
        {
            collection: 'question',
            via: 'form_id'
        },
        // Add a reference to the owners Users
        owners: {
            collection: 'user',
            via: 'form_id',
            through: 'userhasform'
        }
    }

};

UserHasForm.js

module.exports = {
    schema: true,
    tableName: 'users_have_forms',

    attributes:
    {
        to_edit     : { type: 'boolean' },
        to_delete   : { type: 'boolean' },
        user_id     : { model: 'user' },
        form_id     : { model: 'form' }
    }

};

The controller in which I create a form and it is supposed the intermediary document is been created at the join table is:

FormController.js

module.exports = {
    create: function (req, res)
    {
        var ownerJson = {},
            tmpFolio;

        // Get the logged user to make the Folio and then create the form
        SessionService.getUser(req, createForm);
        // Callback function
        function createForm (err, session)
        {
            // If there's no logged user or any error
            if (err || !session)
            {
                console.log(err);
                return res.json(err.status, {error: err});
            }

            console.log('User to create Folio: ', session.id);

            ownerJson.owner_a = session.first_name;
            ownerJson.owner_b = session.second_name;
            ownerJson.owner_c = session.last_name;
            // Construct the Folio creator part like AVC
            tmpFolio = FolioService.generateFolio(ownerJson);

            Form.create({
                name: req.body.name,
                creator: tmpFolio
            })
            .then(function (form){
                if (err)
                {
                    console.log(err);
                    return res.json(err.status, {error: err});
                }

                // Create the jointable record
                var createdRecord = UserHasForm.create({
                        to_edit: true,
                        to_delete: true,
                        user_id: session.id,
                        form_id: form.id
                    })
                    .then(function (createdRecord){
                        if (err)
                        {
                            console.log(err);
                            return res.json(err.status, {error: err});
                        }

                        return createdRecord;
                    });

                return [form, createdRecord];
            })
            .spread(function (form, createdRecord){
                return res.json(200,
                {
                    message: 'The form was created successfuly!',
                    data: form,
                    sharing: createdRecord
                });
            })
            .fail(function (err){
                if (err)
                {
                    console.log(err);
                    res.json(err.status, {error: err});
                }
            });
        }
    },

};

When I run this code I got the next error:

[ReferenceError: UserHasForm is not defined]
Unhandled rejection TypeError: Cannot read property 'toString' of undefined

So I suppose it can't find the model so I add the next line to the model at the beginning:

var UserHasForm = require('../models/UserHasForm');

And now I get the next error:

[TypeError: UserHasForm.create is not a function]

All this is following the the first example on the list.

Any idea why I'm getting this error?

Any kind of help will be welcomed!


回答1:


Well after trying to many examples finally I found the solution thanks to @elennaro for all his support. The whole conversation could be found in the link to the chat we both started under the main question's comments.

Also I can tell you that the examples in the links provided by him (which are in the question above) works fine, the problem was that the version I was using didn't support the features that those examples show.

Basically what I had to do is to install the most recent version for NodeJS, SailsJS and Waterline.

In my case I actually have the next ones:

  • Node v5.3.0
  • Sails v0.11.3
  • Waterline v0.10.30

After that I have to make some changes to my models and at the end they look like this:

User.js

module.exports = {

    schema: true,
    tableName: 'users',
    autoCreatedAt: false,
    autoUpdatedAt: false,

    attributes:
    {
        // username         : { type: 'string', unique: true, minLength: 5, maxLength: 15 },
        email               : { type: 'email', required: true, unique: true },
        encrypted_password  : { type: 'string' },
        reset_password_token: { type: 'string', defaultsTo: null},
        permission_level    : { type: 'integer', required: true, min: 1, max: 3, defaultsTo: 0 },
        belongs_to          : { type: 'string', required: true, defaultsTo: 0 },
        signin_count        : { type: 'integer', required: true, defaultsTo: 1 },
        status_active       : { type: 'boolean', required: true, defaultsTo: false },
        last_signin_at      : { type: 'datetime', defaultsTo: function (){ return new Date(); } },
        last_signin_ip      : { type: 'string', defaultsTo: '0.0.0.0' },

        // Add a reference to Forms collection
        forms:
        {
            collection: 'form',
            via: 'user',
            through: 'userhasform'

            // dominant: true
        }
    }

};

Form.js

module.exports = {

    schema: true,
    tableName: 'forms',

    attributes:
    {
        name    : { type: 'string', required: true, unique: true },
        creator : { type: 'string', unique: false },
        sequence: { type: 'integer', autoIncrement: true },

        // Add a reference to the owners Users
        owners: {
            collection: 'user',
            via: 'form',
            through: 'userhasform'
        }
    }

};

UserHasForm.js

module.exports = {

    schema: true,
    tableName: 'users_have_forms',

    attributes:
    {
        to_edit     : { type: 'boolean' },
        to_delete   : { type: 'boolean' },
        user        : { model: 'User', foreignKey: true, columnName: 'user_id' },
        form        : { model: 'Form',  foreignKey: true, columnName: 'form_id' }
    }

};

FormController.js

Still the same as in the question

I hope it could be useful for anybody. And once again thanks to @ Alexander Arutinyants for your support!

Any question, please leave a comment!



来源:https://stackoverflow.com/questions/34570740/how-to-implement-many-to-many-association-using-through-in-sails

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!