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: Add ability to specify the records id when calling model.create() #679

Merged
merged 2 commits into from
Feb 14, 2017

Conversation

jamemackson
Copy link
Contributor

@zacharygolba here's the first pass, just pushing to start the discussion on the implementation of this.

refs: #678

@codecov-io
Copy link

codecov-io commented Feb 8, 2017

Codecov Report

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

@@            Coverage Diff            @@
##           master    #679      +/-   ##
=========================================
+ Coverage   92.28%   92.3%   +0.01%     
=========================================
  Files         180     180              
  Lines        1996    2000       +4     
=========================================
+ Hits         1842    1846       +4     
  Misses        154     154
Impacted Files Coverage Δ
src/packages/database/model/utils/persistence.js 100% <100%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 33a0ef3...6daa7c8. Read the comment docs.

@jamemackson jamemackson force-pushed the master branch 2 times, most recently from 0b5c016 to fa50ba4 Compare February 10, 2017 17:27
@jamemackson
Copy link
Contributor Author

@zacharygolba I updated this to use a property on the model that enables the alternate behavior to allow, on a model specific basis, inserting with a specified id. I'm sure there are still a lot of ways this could be better... I'm all ears for any suggestions you have.

* @default 'id'
* @public
*/
allowPrimaryKeyInsert: boolean = false;
Copy link
Contributor

Choose a reason for hiding this comment

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

I think that this flag is unnecessary. The ORM should be smart enough to know if a record has an id in it's change set and it has not been persisted to the database yet.

@@ -23,6 +23,24 @@ export function create(record: Model, trx: Object): Array<Object> {
updatedAt: timestamp
});

if (record.allowPrimaryKeyInsert) {
const columns = getColumns(record);
// check that we have a valid id here?
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think that we have to check if the id is valid. If this fails due to an error that is thrown by the database driver that is ok. No need for us to add an additional error message and spend more time in node than what is necessary.

@jamemackson jamemackson changed the title WIP: Add ability to specify the records id when calling model.create() feat: Add ability to specify the records id when calling model.create() Feb 13, 2017
Copy link
Contributor

@zacharygolba zacharygolba left a comment

Choose a reason for hiding this comment

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

This is great!

I think we can merge this once we clean up the test case to not include the extra assertions.

👏 Awesome work seeing this feature implementation through!

isPublic: true
});

expect(result).to.be.an.instanceof(Subject);
Copy link
Contributor

Choose a reason for hiding this comment

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

I feel like this assertion and expect(result).to.have.property('id', id); from L403 are all that is required to ensure that you can manually insert ids. Maybe w could remove all of the other expect calls and the unnecessary constants for this test case.

@jamemackson
Copy link
Contributor Author

@zacharygolba thanks for the feedback, all great suggestions. I hope the modifications to the test was inline with what you were thinking. Let me know... :)

@zacharygolba
Copy link
Contributor

Looks good @jamemackson. Awesome work!

@jamemackson
Copy link
Contributor Author

thanks @zacharygolba!!

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.

3 participants