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
I'm trying to use Waterline directly to simplify tests, but it seems like
beforeCreateis not working in through tables. Here's some sample code reproducing the issue:Output:
As you can see
DeviceUser.beforeCreateis replaced with a default stub and is never triggered.Waterline version 0.10.26