Coder Social home page Coder Social logo

wsiegenthaler / sequelize-embed Goto Github PK

View Code? Open in Web Editor NEW
15.0 15.0 2.0 61 KB

Easily insert and update sequelize models with deeply nested associations

License: BSD 3-Clause "New" or "Revised" License

JavaScript 100.00%
database epilogue javascript nested-associations orm rest sequelize sequelize-models

sequelize-embed's Introduction

sequelize-embed's People

Contributors

fossabot avatar wsiegenthaler avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

sequelize-embed's Issues

Update to Sequelize 4

Hello, this is a wonderful project. Was wondering if you were planning on upgrading to Sequelize 4, specifically for the embedding feature.

Add Sequelize options to base queries

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!

TypeError: Cannot read property 'associationAccessor' of undefined

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)

const a = inc.association, as = a.associationAccessor, value = instance[as];

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,
          );

Update BelongsToMany associations (many-to-many through join table)

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.

Update with more than one records having the same primary key succeeds

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:

  1. diff in util.js skips duplicates. However, even after I changed the code to consider also duplicates as new records:
  2. updateOrInsert in embed.js firstly tries to find data in db (findOne) and in case of success it does just update

Are you going to fix it or is it by design?

Another question, do you plan to upgrade to Sequelize 5?

thanks

Primary key match fails upon type discrepancy

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.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.