X Tutup
The Wayback Machine - https://web.archive.org/web/20201206194854/https://github.com/redux-orm/redux-orm/issues/81
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

Enforcing Unique IDs #81

Open
Aryk opened this issue Jan 11, 2017 · 6 comments
Open

Enforcing Unique IDs #81

Aryk opened this issue Jan 11, 2017 · 6 comments

Comments

@Aryk
Copy link

@Aryk Aryk commented Jan 11, 2017

Is this possible to do? Seems like redux-orm would be confused if there were multiple records with the same ID since it wouldn't know which one to fetch. Do you plan to add a check against this?

In many cases, you will be getting records from an API which is the "source of truth". You then want to add it to the orm, but it's possible to manually specify the ID and keep essentially creating the same one. Seems like it should be the responsibility of the ORM to prevent this behavior since it then cannot operate correctly.

@tommikaikkonen
Copy link
Collaborator

@tommikaikkonen tommikaikkonen commented Jan 12, 2017

Currently a creating a record with an id that equals another existing record would just overwrite it - that's not expected functionality. I agree that this should be added.

Ideally this will be added to the database layer Table.prototype.insert. Currently that returns a new state and the created record. It should be extended to be able to return an error value as well, if it wasn't able to create a new record. Ability to return error values from the database layer isn't used by the ORM layer yet but when the new database API introduced in 0.9.0, it accommodates use of that pattern.

For now, you can patch this easily at the ORM layer before a full feature is added (might differ slightly for TS):

class MyBaseModel extends Model {
  static create(props) {
    if (props.hasOwnProperty(this.idAttribute) && this.hasId(props[this.idAttribute])) {
      throw new Error(`Id with ${props[this.idAttribute]} already exists`);
    }
    return super.create(props);
  }
}
@Aryk
Copy link
Author

@Aryk Aryk commented Jan 12, 2017

yup, makes sense, just checking on this. That's what I added in my basemodel.

@carlesnunez
Copy link

@carlesnunez carlesnunez commented Mar 2, 2017

I created the same object that u can extend to make and update if we currently have the record on our store or create if not:

import { Model } from 'redux-orm';

export default class DopeModel extends Model {
  static create(props) {
    if (props.hasOwnProperty(this.idAttribute) && this.hasId(props[this.idAttribute])) {
      const model = this.withId(props.id);
      return model.update(props);
    }
    return super.create(props);
  }
}
@haveyaseen haveyaseen added this to the v1.0 milestone Jun 13, 2018
@plandem plandem mentioned this issue Jun 13, 2018
3 of 11 tasks complete
@udayreddy
Copy link

@udayreddy udayreddy commented Mar 7, 2019

can we have idAttribute with 2 columns or two props of the object ?

@haveyaseen
Copy link
Member

@haveyaseen haveyaseen commented Mar 22, 2019

@udayreddy This is currently not possible. And I'm not planning this myself because there's already more than enough on the roadmap. But would happily merge a PR if tested, consistent with current memoization and non-breaking. I guess a reasonable API would be Book.idAttribute = ["title", "edition"].

The obvious easy workaround would be to store them in the same field instead (book.id = `${book.title}-${book.edition}`). Note that that's likely to cause bugs as you need to do this manually each time you perform a write operation (book.id = someId, book.set("id", someId), book.update({ id: someId }), Book.create). So if you can, I would suggest to add a surrogate key to your data source for this purpose.

And for anyone passing by, the Model.upsert solution by @carlesnunez has been added in v0.9.1.

@carlesnunez
Copy link

@carlesnunez carlesnunez commented Mar 23, 2019

Thank you for the kudos @haveyaseen 🤟🏻😊

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Version 1.0
  
To do
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
5 participants
You can’t perform that action at this time.
X Tutup