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

Is it possible to create a record without assigning a PK & saving? #5840

Closed
cha0s opened this issue Dec 18, 2014 · 18 comments
Closed

Is it possible to create a record without assigning a PK & saving? #5840

cha0s opened this issue Dec 18, 2014 · 18 comments

Comments

@cha0s
Copy link

cha0s commented Dec 18, 2014

I'd like to be able to create an instance of a model without actually saving it or generating a PK (ID would equal null). Is this possible? I'd like to get an instance with all defaults populated, etc. but not actually save it.

@kevinburke
Copy link
Contributor

I'd also like to do this; is this possible? if so could you document it?

@particlebanana
Copy link
Contributor

This should work: new Pet._model(values);

@cha0s
Copy link
Author

cha0s commented Jan 16, 2015

Using an underscored method is a code smell. Can we have this as a public API, please?

@particlebanana
Copy link
Contributor

Sure mind putting together a pull request?

@cha0s
Copy link
Author

cha0s commented Jan 16, 2015

Yup I'll get to this later tonight

Sent from my T-Mobile 4G LTE Device

-------- Original message --------
From: Cody Stoltman notifications@github.com
Date:01/16/2015 12:17 PM (GMT-06:00)
To: balderdashy/waterline waterline@noreply.github.com
Cc: Ruben Rodriguez cha0s@therealcha0s.net
Subject: Re: [waterline] Is it possible to create a record without assigning a PK & saving? (#763)
Sure mind putting together a pull request?


Reply to this email directly or view it on GitHub.

@cha0s
Copy link
Author

cha0s commented Jan 16, 2015

Do you have a preference about the syntax? In the project I desired this functionality in, I augmented all collections with .instantiate, so I could do var user = User.instantiate();.

Does that seem like a good name? Should we go with instance, or something else do you think?

Might as well talk about it a little before I bother with a PR :)

@andrewjmead
Copy link

+1 for this. I wouldn't mind helping out with a PR as waterline has saved me a ton of development and maintenance time.

My use case is creating multiple instances of a model, but only if they are all valid.

@rishabhmhjn
Copy link

Was any method to support this feature added?

@devinivy
Copy link

devinivy commented Nov 6, 2015

No, there wasn't, but I would love to see a PR for this! Using Collection._model({/* record */}) still works (and I expect it will work until there is a new, non-backwards-compatible version of waterline), though it doesn't feel great to use the private method.

@ejlangev
Copy link

Collection._model({ /* data */}) doesn't seem to work very well. I'm using waterline 0.10.26. The method initially works and returns an instance of the model but if I try to call save on that model I immediately get an error "Error: No Primary Key set to update the record with! Primary Key must have a value, it can't be an optional value."

Some example code:

var user = User._model({ email: 'foo@bar.com' });

user.save()
  .then(function() {
    console.log(user.id);
  })
  .catch(function(err) {
    console.log(err);
  });

Is there another workaround I can use for now? Otherwise are there any plans to make a method providing this functionality available through a public api? I'd be willing to submit a pull request that implements that if it's wanted. There's an existing package called sails-model-new that seems to do most of what I'm looking for. It's also supported by many other ORMs.

@devinivy
Copy link

Ah, correct– save() only really knows how to perform an update. I'm not sure. I never really use it this way. Maybe Collection.create(modelInstance) works for your purposes.

@elennaro
Copy link

I have a similar problem:

    /**
     * MongoDB native findAndUpdate
     * @name DbHelperService._findAndUpdate
     * @param {Object} criteria
     * @param {Object} model Waterline model
     * @param {Object} update values to update { key: value }
     * @param {Function} cb
     * @private
     */
    _findAndUpdate: function (criteria, model, update, cb) {
        console.log('0000', model);
        model.native(function (err, collection) {
            if (err)
                return cb(err);

            collection.findOneAndUpdate(criteria, update, { new: true }, function (err, result) {
                cb(err, result && result.value && new model._model(result.value));
            });
        });
    }

this method does not return a good model, for example it does not convert _id<BSONObject> to id<String>.

Help, please!

@devinivy
Copy link

sails-mongo has a utility method to normalize results based upon a schema: https://github.com/balderdashy/sails-mongo/blob/master/lib/utils.js#L78-L96

@ejlangev
Copy link

Maybe Collection.create(modelInstance) works for your purposes.

Unfortunately it doesn't in this case. I'm trying to build an adapter between waterline and factory girl and part of the interface is the ability to build a record without saving it and then subsequently save it later on. I hacked around it myself for now but would be nice to fix the underlying issue so I can build the adapter into a real node package and release it. I'll try to submit a pull request that fixes this since it seems like you're open to it.

@devinivy
Copy link

I am open to it– not sure where @particlebanana and others are with this. But at least in the next big waterline release, I would love to see a proper public API for creating model instances.

@elennaro
Copy link

@devinivy +10000547

@particlebanana
Copy link
Contributor

Yeah having .save do a create if there is no primary key is probably an ok solution or just allowing the .create method to accept a model instance would be the easiest. Just check if the data has a toObject method and if so call it.

@ejlangev
Copy link

I think I like the former solution of .save doing a create if there is no primary key. What would the create method do if it accepts a model instance that already has a primary key? Would it create a new record or try to update the existing one. Seems like an easy mistake to make.

@johnabrams7 johnabrams7 transferred this issue from balderdashy/waterline May 20, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

8 participants