Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

beforeCreate in a through table doesn't seem to work #5289

Closed
GeorgeSapkin opened this issue Nov 13, 2015 · 5 comments
Closed

beforeCreate in a through table doesn't seem to work #5289

GeorgeSapkin opened this issue Nov 13, 2015 · 5 comments

Comments

@GeorgeSapkin
Copy link

I'm trying to use Waterline directly to simplify tests, but it seems like beforeCreate is not working in through tables. Here's some sample code reproducing the issue:

'use strict';

const memoryAdapter = require('sails-memory');
const Waterline     = require('waterline');

const User = {
    identity:   'user',
    connection: 'memory',

    attributes: {
        name: 'string',
        generated: 'integer',

        devices: {
            collection: 'device',
            through:    'deviceuser'
        }
    },

    beforeCreate(user, next) {
        console.log('user.beforeCreate');
        user.generated = 100;
        next();
    },
};

const Device = {
    identity:   'device',
    connection: 'memory',

    attributes: {
        name: 'string',
        generated: 'integer',

        users: {
            collection: 'user',
            through:    'deviceuser'
        }
    },

    beforeCreate(device, next) {
        console.log('device.beforeCreate');
        device.generated = 200;
        next();
    },
};

const DeviceUser = {
    identity:   'deviceuser',
    connection: 'memory',

    attributes: {
        device: {
            model: 'device',
            via:   'users',
        },

        user: {
            model: 'user',
            via:   'devices'
        },

        generated: 'integer'
    },

    beforeCreate(device, next) {
        console.log('deviceuser.beforeCreate');
        device.generated = 300;
        next();
    },
};

const waterline = new Waterline();
waterline.loadCollection(Waterline.Collection.extend(User));
waterline.loadCollection(Waterline.Collection.extend(Device));
waterline.loadCollection(Waterline.Collection.extend(DeviceUser));

const config = {
    adapters: {
        'default': memoryAdapter,
        memory:    memoryAdapter
    },
    connections: {
        memory: { adapter: 'memory' }
    },
};

waterline.initialize(config, (err, models) => {
    for (const cb of models.collections.user._callbacks.beforeCreate)
        console.log(cb.toString());
    for (const cb of models.collections.device._callbacks.beforeCreate)
        console.log(cb.toString());
    // DeviceUser.beforeCreate already gone
    for (const cb of models.collections.deviceuser._callbacks.beforeCreate)
        console.log(cb.toString());

    models.collections.user.create({ name: 'John Doe' })
        .then(console.log)
        .then(_ => models.collections.device.create({ name: 'Thing 3000' }))
        .then(console.log)
        .then(_ => models.collections.deviceuser.create({ device: 1, user: 1 }))
        .then(console.log);
});

Output:

beforeCreate(user, next) {
        console.log('user.beforeCreate');
        user.generated = 100;
        next();
    }
beforeCreate(device, next) {
        console.log('device.beforeCreate');
        device.generated = 200;
        next();
    }
function (values, next) { return next(); }
user.beforeCreate
{ name: 'John Doe',
  generated: 100,
  createdAt: '2015-11-13T22:16:06.105Z',
  updatedAt: '2015-11-13T22:16:06.105Z',
  id: 1 }
device.beforeCreate
{ name: 'Thing 3000',
  generated: 200,
  createdAt: '2015-11-13T22:16:06.119Z',
  updatedAt: '2015-11-13T22:16:06.119Z',
  id: 1 }
{ device: 1, user: 1, id: 1 }

As you can see DeviceUser.beforeCreate is replaced with a default stub and is never triggered.

Waterline version 0.10.26

@devinivy
Copy link

@particlebanana
Copy link
Contributor

@devinivy I don't think the above addresses this issue. It would need to be tested if it happens to be working. I think this would need to setup to run manually though.

@devinivy
Copy link

When collections are initialized, junction/through tables go through a particular "migration" process that nixes their lifecycle callbacks. I figured your changes would allow one to not mark the through table as a waterline-created junction table. It is possible that there's more work that would need to be done. The relevant area to look is here: https://github.com/balderdashy/waterline/blob/f8b499ef549f864a813b1eca5bb591f0ec210ac6/lib/waterline.js#L127-L138 .

@sailsbot
Copy link

Thanks for posting, @GeorgeSapkin. I'm a repo bot-- nice to meet you!

It has been 30 days since there have been any updates or new comments on this page. If this issue has been resolved, feel free to disregard the rest of this message. On the other hand, if you are still waiting on a patch, please:

  • review our contribution guide to make sure this submission meets our criteria (only verified bugs with documented features, please; no questions, commentary, or bug reports about undocumented features or unofficial plugins)
  • create a new issue with the latest information, including updated version details with error messages, failing tests, and a link back to the original issue. This allows GitHub to automatically create a back-reference for future visitors arriving from search engines.

Thanks so much for your help!

@devinivy devinivy reopened this Dec 22, 2015
@particlebanana
Copy link
Contributor

Updated roadmap with this feature

@raqem raqem transferred this issue from balderdashy/waterline May 2, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

4 participants