Coder Social home page Coder Social logo

sequelize-to-json's People

Contributors

andrewhillcode avatar doublesharp avatar hauru avatar

Stargazers

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

Watchers

 avatar  avatar

sequelize-to-json's Issues

Not working with Sequelize v4.1.0

class extends Model {} is not a valid Sequelize model
Error: class extends Model {} is not a valid Sequelize model
    at new Serializer ([...]/node_modules/sequelize-to-json/index.js:91:13)
let serializer = new Serializer(Item, scheme);
let res = serializer.serialize(Item);
// item model

module.exports = (sequelize, DataTypes)=> {
  const Position = sequelize.define('Item', {
    name: DataTypes.STRING,
  });
  return Item;
};

@hauru this seems to be the issue:

https://github.com/hauru/sequelize-to-json/blob/master/index.js#L90-L92

Not working with Sequelize v5.1.0

The Model seems to have changed in Sequelize 5 is being detected as invalid.

Error: class extends Model {} is not a valid Sequelize model
     at new Serializer (/node_modules/sequelize-to-json/index.js:124:13)
     at Function.serializeMany (/node_modules/sequelize-to-json/index.js:352:22)

Error during model instance comparison

Hi

I ran into an error when serializing with two different model class object. Check the code below and take note of "Object 1" and "Object 2".

function getSegmentProfilesFor(tenant, segmentId) {
  // Object 1 of SegmentProfile
  const SegmentProfile = models(authorizerContext.tenant).SegmentProfile;
  const segmentProfiles = SegmentProfile.findAll({
    where: {
      segmentId: segmentId
    },
    order: [['createdAt', 'DESC']]
  })
  return segmentProfiles;
}

async function main() {
  const segmentProfiles = await getSegmentProfilesFor(authorizerContext.tenant, segmentId, SegmentProfile);

  // Object 2 of SegmentProfile
  const SegmentProfile = models(authorizerContext.tenant).SegmentProfile; 
  let segmentProfilesAsJSON = Serializer.serializeMany(segmentProfiles, SegmentProfile, serializeProfileScheme);
}

main()

Error Trace

(node:37071) UnhandledPromiseRejectionWarning: Error: Not an instance of SegmentProfile
    at Serializer.serialize (node_modules/sequelize-to-json/index.js:303:13)

Looks like the below piece of code is comparing for the exact model instance. As long as you are passing around same model instance it works. Passing different instance of the same model breaks everything.

function _isSpecificModelInstance(obj, model, seqVer) {
  if(seqVer >= 4) {
    return obj instanceof model;
  } else {
    return obj instanceof model.Instance;
  }
}

Line 111

Please help me understand the need for comparing via instanceof instead of model class name. I think the comparison can be limited to strings and instead of doing

return obj instanceof model;

we can do

return obj.constructor.name == model.name;

can't serialize nested associations

does this lib work with nested associations? i'm working with 2 level deep nested associations. for example models Order -> User -> UserLocation

json { order: { user: {userLocation:{ // i want data from here to be included in the json }}}

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.