Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(upsert): return upserted record with options.returning=true #8924

Merged
merged 1 commit into from Jan 22, 2018

Conversation

sushantdhiman
Copy link
Contributor

Pull Request check-list

Please make sure to review and check all of these items:

  • Does npm run test or npm run test-DIALECT pass with this change (including linting)?
  • Does the description below contain a link to an existing issue (Closes #[issue]) or a description of the issue you are solving?
  • Have you added new tests to prevent regressions?
  • Is a documentation update included (if this change modifies existing APIs, or introduces new ones)?
  • Did you follow the commit message conventions explained in CONTRIBUTING.md?

Description of change

Close #3354

Sequelize still doesnot support ON CONFLICT, but with using RETURNING its now possible for return upserted record.

// current behaviour
const created = await Model.upsert(....values....);

// with options.returning = false
const created = await Model.upsert(....values...., { returning: false });

// with options.returning = true , new feature
const [record, created] = await Model.upsert(....values...., { returning: true });

@sushantdhiman sushantdhiman requested review from janmeier and a team January 20, 2018 14:39
@codecov
Copy link

codecov bot commented Jan 20, 2018

Codecov Report

Merging #8924 into master will increase coverage by 0.01%.
The diff coverage is 100%.

lib/model.js Outdated
* @param {Transaction} [options.transaction] Transaction to run query under
* @param {Function} [options.logging=false] A function that gets executed while running the query to log the sql.
* @param {Boolean} [options.benchmark=false] Pass query execution time in milliseconds as second argument to logging function (options.logging).
* @param {String} [options.searchPath=DEFAULT] An optional parameter to specify the schema search_path (Postgres only)
*
* @return {Promise<created>} Returns a boolean indicating whether the row was created or updated.
* @return {Promise<created>} Returns a boolean indicating whether the row was created or updated. For Postgres with (options.returning=true), it returns record and created boolean with signature `<Model, created>`.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In v5, we can set this Promise<record, created> by setting returning=true by default. One caveat is null is now first argument for other dialects. May be should set this as Promise<created, record>, but that doesn't match with other method's API. Any ideas?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have added support for MSSQL as well, so now may be current order make sense.

@sushantdhiman
Copy link
Contributor Author

In issue #3354 there is discussion about MySQL support, but I find that LAST_INSERTED_ID is still limited. It wont work for any non incrementing primary keys like STRING, UUID etc. So I haven't added any support for MySQL.

Currently we support MSSQL and Postgres for this feature

@abdabsi
Copy link

abdabsi commented Jul 16, 2019

In issue #3354 there is discussion about MySQL support, but I find that LAST_INSERTED_ID is still limited. It wont work for any non incrementing primary keys like STRING, UUID etc. So I haven't added any support for MySQL.

Currently we support MSSQL and Postgres for this feature

If they're using non-incrementing primary keys, they most probably won't need LAST_INSERTED_ID. Those who use auto-incremented primary keys (absolute majority) on MySql (#1 rdbms) have no use of the current upsert method. Please fix it! :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Sequelize Upsert() return value
3 participants