question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Make create to return with scope

See original GitHub issue

I have this model

module.exports = (sequelize, DataTypes) => {
  const Partner = sequelize.define('Partner', {
    id: {
      type: DataTypes.INTEGER,
      primaryKey: true,
      autoIncrement: true
    },
    name: {
      type: DataTypes.STRING,
      allowNull: false
    }
  }, {
    tableName: 'partners',
    paranoid: true,
    underscored: true,
    freezeTableName: false,
    scopes: {
      clean: {
        attributes: { exclude: [
          'createdAt',
          'updatedAt',
          'deletedAt'
        ] }
      }
    }
  })

  return Partner
}

When doing a Partner.scope('clean').find(), the query runs

SELECT "id", "name", "logo_id" AS "logoId" FROM "partners" AS "Partner" [...]

Whereas doing a Partner.scope('clean').create() runs

INSERT INTO "partners" ("id","name","created_at","updated_at") VALUES (DEFAULT,$1,$2,$3) RETURNING *

So it doesn’t respect the scope.

I think it would be very more consistent (and actually, relatively easy) to tweak the RETURNING * part automatically to respect scopes ?

Furthermore, I can’t even do this manually, becasecreate() returning option is only boolean (whereas bulkCreate can take an array of columns)

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:4
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
cyrilchaponcommented, Oct 21, 2019

Hey @papb

You mean Partner.scope(‘clean’).create({ returning: true }), right?

Yes about the original issue; but actually yes and no. Yes for the insert / returning of Postgres. But I’m thinking more broadly about UPDATE / .reload() now. What do you think ?

One should produce failing tests maybe.

Thanks for the report!! I agree with you that the current behavior is a potential leak, and your suggested behavior is better 😃

Hum… I didn’t find anything in the docs mentionning any of this… (I had to actually test it)

So… It’s a “breaking” change in the mean it should break stuff; but SEMVER-speaking, no…

Ninja commiting it would break stuff for sure, and I think it can be added as a breaking change.

0reactions
github-actions[bot]commented, Nov 6, 2021

This issue has been automatically marked as stale because it has been open for 7 days without activity. It will be closed if no further activity occurs. If this is still an issue, just leave a comment or remove the “stale” label. 🙂

Read more comments on GitHub >

github_iconTop Results From Across the Web

Scope and return values in C++
The scope of the local variable ends, but a copy is made, and returned to the calling function. Example: int funcB() { int...
Read more >
JavaScript Functions — Understanding Scope & the Return ...
In this post, we will focus on 2 additional concepts, which are really important for understanding functions: scope and the return statement.
Read more >
Make query scope only return one object
Hello! I created a query scope to return the current promotion. It looks like this: public function scopeCurrent($query, array $fields = null) {...
Read more >
Scope and Closures in JavaScript – Explained with ...
First, we can access word in the function returned from sayWord. ... Thus, creating a variable in the global scope makes it accessible...
Read more >
Scope functions
Although the scope functions are a way of making the code more ... the return value and use a scope function to create...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found