If you call .create() on a model with a field you are not allowed to set, the field will be set and not returned to you (even if you have the role's get set to true).
Example:
const RSVP = sequelize.define('rsvp', {
id: {
type: Sequelize.CHAR(26),
field: 'rsvp_id',
defaultValue: () => ulid().toLowerCase(),
primaryKey: true,
validate: {
isLowercase: true,
},
roles: {
self: {
get: true,
set: false,
},
},
},
});
Doing something like this:
RSVP.create({ id: '1234' }, { role: 'self' });
Results in saving a row with id set to 1234, but then returning an empty object.
This will also fail:
RSVP.build({ id: '1234' }, { role: 'self' }).save()
And this will fail:
(new RSVP({ id: '1234' }, { role: 'self' })).save()
Workaround is to call build, set, and save explicitly.
RSVP.build().set({ id: '1234' }, { role: 'self' }).save()
That will prevent setting the id, but still give you an empty object (since I guess the default doesn't come back through?) so to get around that you have to do this:
RSVP.build().set({ id: '1234' }, { role: 'self' }).save()
.then(rsvp => rsvp.get({ role: 'self' }))
If you call
.create()on a model with a field you are not allowed to set, the field will be set and not returned to you (even if you have the role'sgetset to true).Example:
Doing something like this:
Results in saving a row with id set to 1234, but then returning an empty object.
This will also fail:
And this will fail:
Workaround is to call build, set, and save explicitly.
That will prevent setting the id, but still give you an empty object (since I guess the default doesn't come back through?) so to get around that you have to do this: