wsiegenthaler / sequelize-embed Goto Github PK
View Code? Open in Web Editor NEWEasily insert and update sequelize models with deeply nested associations
License: BSD 3-Clause "New" or "Revised" License
Easily insert and update sequelize models with deeply nested associations
License: BSD 3-Clause "New" or "Revised" License
Hello, this is a wonderful project. Was wondering if you were planning on upgrading to Sequelize 4, specifically for the embedding feature.
It would be great if the options parameter would include sequelize options like fields
and where
to be used on the base root query.
So, for instance, adding embed.update(User, user, Association, { where: <some filter> })
would update only users matching the filter, and any matching associations for them.
Other than that, great package! been very helpful!
Does anyone know how to use transaction with this library? Or is there any example that uses transaction?
Hi I just stumbled upon this package and think it's exactly what I'm looking for however I came across this error when trying to save an object that only includes data for some of the provided associations:
TypeError: Cannot read property 'associationAccessor' of undefined
at include.map.inc (.../node_modules/sequelize-embed/src/util.js:54:41)
at Array.map (<anonymous>)
at prune (.../node_modules/sequelize-embed/src/util.js:53:13)
at include.map.inc (.../node_modules/sequelize-embed/src/util.js:60:9)
at Array.map (<anonymous>)
at prune (.../node_modules/sequelize-embed/src/util.js:53:13)
at include.map.inc (.../node_modules/sequelize-embed/src/util.js:60:9)
at Array.map (<anonymous>)
at prune (.../node_modules/sequelize-embed/src/util.js:53:13)
at include.map.inc (.../node_modules/sequelize-embed/src/util.js:60:9)
at Array.map (<anonymous>)
at prune (.../node_modules/sequelize-embed/src/util.js:53:13)
at instance.reload.then.inst (.../node_modules/sequelize-embed/src/embed.js:147:26)
From previous event:
at reload (.../node_modules/sequelize-embed/src/embed.js:146:58)
at action.tap.catch.then.inst (.../node_modules/sequelize-embed/src/embed.js:26:23)
at runCallback (timers.js:693:18)
at tryOnImmediate (timers.js:664:5)
at processImmediate (timers.js:646:5)
Line 54 in b0c930e
I can get the error to go away when I don't include the unnecessary embed associations however I'd prefer to include them as I'm not sure which data is going to be updated.
Here are examples, not: location and profiles are associations on basics which is an association on basics which is an association on resume:
import {
Resume,
JsonResume,
JsonResumeBasics
} from 'data/models';
const resumeEmbedAssociations = [
mkInclude(
Resume.JsonResume,
mkInclude(
JsonResume.JsonResumeBasics,
mkInclude(
JsonResumeBasics.JsonResumeBasicsLocation,
JsonResumeBasics.JsonResumeBasicsProfile,
),
),
),
];
const updateResume = {
id: '67fc7ce0-9e51-11e8-ae55-1dd8aec4856d',
userId: 'fe07daa0-9e50-11e8-ae55-1dd8aec4856d',
jsonResume: {
basics: {
name: 'name',
label: '123',
picture: 'picture',
email: '[email protected]',
phone: 'phone',
website: 'ddddd',
location: null,
profiles: [],
},
},
};
embed.update(
Resume,
updateResume,
resumeEmbedAssociations,
);
Hello,
Am I correct in assumption that update of association defined using belongsToMany method is not implemented yet? I have checked the code of embed.js file and I can see function "updateBelongsTos" but not "updateBelongsToManys". This lead me to the assumption updating the many-to-many associations (through join table) are not possible yet. Please confirm. If so, is it planned?
Thanks for the information in advance.
Hello,
calling update with two or more records having same primary key silently succeeds if such a primary key is already in database. Of course, no other record with same primary key is in reality added to database. It is confusing for user however, he thinks he succeeded adding another record with same primary key.
From the source code I see this behavior is caused by:
Are you going to fix it or is it by design?
Another question, do you plan to upgrade to Sequelize 5?
thanks
Primary keys are compared to determine whether hasOne and hasMany associations should be inserted, updated, or deleted. If the primary key of the element being modified does not match the type of the key as defined by the model, elements are not matched correctly. This results in the deletion and re-insertion of the element.
Example:
const User = sequelize.define('user', { name: DataType.STRING })
const Contact = sequelize.define('contact', { email: DataType.STRING })
User.Contact = User.hasOne(Contact, { as: 'contact', foreignKey: 'userId' })
const include = [ { model: Contact, association: User.Contact } ]
// Match: Contact.id is Number
embed.update(User, { id: 1, name: 'Jon', contact: { id: 2, email: '[email protected]' })
// Mismatch: Contact.id is String, record will be deleted and re-inserted
embed.update(User, { id: 1, name: 'Jon', contact: { id: '2', email: '[email protected]' })
Solution: Use abstract equality (==) instead of strict equality (===) when matching primary keys.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.