Question

Having some difficulty with the Sequelize QueryChainer. Normally I wouldn't need to do this, but I decided early on to handle my own associations instead of using the modules built in association management (which has turned out to be quite punishing).

I'm writing out my REST routines and I'm trying to delete some records based on other records already fetched or needing fetched. Instead of having 4-5 nested callbacks, I decided the QueryChainer and a serial run would be appropriate.

In this example, I'm taking a user and if one was found I would like to stitch together a line of queries to handle to deletion of specific associations. The problem I'm having mainly is understanding the syntax for the chainer outside of just using model EventEmitting methods.

        // Find user, destroy it and associations
    db.Models.user.find({ where: {STUID: id} })
        .success(function (user) {
            if (!user) {
                res.locals.err = {
                    status: 'FAILED',
                    message: 'Could not delete user',
                    reason: 'No user resource found'
                }
            } else {
                chain = new db._sqlize.Utils.QueryChainer();
                chain.add(user.destroy())

                // Check and destroy author association
                if (user.AUTHOR) {
                    // Would like to fetch the specific author model
                    // db.models.author.find({})
                    // ... and destroy it too,
                    // author.destroy()
                    // without having to nest
                }


            }
        });

The sequelize querychain syntax specifies to do something like the following:

.add(Model, 'function', [param1, param2])

But I'm not too sure on what exactly that means. Can I define functions inline here? Is function just a string looking for a one already defined elsewhere, or is it a function of the model that I'm 'adding'? The docs feel a little rushed here.

Was it helpful?

Solution

The syntax you need for queryChain is:

.add(sequelize, 'sync', [ {force: true} ])
.add(Partner, 'create', [ {name: 'foo'} ])

I made the the same mistake, as you can see here: https://github.com/sdepold/sequelize/issues/385#issuecomment-11690183

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top