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

Allow passing a single object to Model.create. #164

Merged
merged 1 commit into from
May 16, 2013
Merged

Allow passing a single object to Model.create. #164

merged 1 commit into from
May 16, 2013

Conversation

dxg
Copy link
Collaborator

@dxg dxg commented May 15, 2013

Addresses #159

Enables:

Model.create({name: 'jane'}, function(err, item) {
  // item is not an array, but a single object
});
Model.create([{name: 'jane'}], function(err, items) {
  // items is an array
});

It seems that you can pass the same parameters to Model.create in many different ways.
Eg:

Model.create([{name: 'jane'}, {name: 'john'}], function (err, items) { ... } );
Model.create([{name: 'jane'}], [{name: 'john'}], function (err, items) { ... } );
Model.create([{name: 'jane'}], function (err, items) { ... }, [{name: 'john'}] );
Model.create([{name: 'jane'}], function (err, items) { ... }, {someOption: false}, [{name: 'john'}] );

are all equivalent.

This PR limits this somewhat, as the first argument which is an object but not an array is assumed to be a single item to be created.

I think this is resonable.
I also think the way parameters are accepted is a little too flexible. Requiring that the first parameter is the item(s) to be created, and last parameter is the callback (with an optional middle options param) would make the code a bit easier to read.

switch (typeof arguments[i]) {
case "object":
options = arguments[i];
if ( !single && Array.isArray(arguments[i]) ) {

Choose a reason for hiding this comment

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

I don't understand how you space

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

haha.. normally i would write: if (!single && Array.isArray(arguments[i])) { but there was a lot of brackets happening towards the end so I spaced it out for readability. I don't really mind it either way though.

@fjenett
Copy link

fjenett commented May 15, 2013

+1

I think less options of how to pass arguments are better to remember and will on the long run help the project as you can read other people's code, share and so on ...

dxg added a commit that referenced this pull request May 16, 2013
Allow passing a single object to Model.create.
@dxg dxg merged commit 7e8478f into master May 16, 2013
@dxg dxg deleted the create-single branch May 16, 2013 09:45
@dxg
Copy link
Collaborator Author

dxg commented May 16, 2013

Will revisit the way arguments are passed once @dresende is back

@dresende
Copy link
Owner

This is ok for now, maybe we can improve later if someone is not confortable with 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.

4 participants