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

onSave should be able to change the isNew state #88

Open
danyg opened this issue Aug 29, 2013 · 1 comment
Open

onSave should be able to change the isNew state #88

danyg opened this issue Aug 29, 2013 · 1 comment

Comments

@danyg
Copy link

danyg commented Aug 29, 2013

Hello, I don't know how to resolve with your ORM this MySql sentence (I'm using your ORM to connect to SQLITE and MySQL)

UPDATE ON KEY EXISTS

to do this, I use the onSave hook, and there I check if are a record with the Keys,

the code is something like this:

TypesDressStores.onSave = function(obj, connection, callback){
    if(!obj.id && !!obj.idStore && !!obj.idDressStore){
        TypesDressStores
            .using(connection)
            .where({
                idStore: obj._idStore,
                idDressStore: obj.idDressStore
            })
            .all(function(err, items){
                if(err) throw err;

                if(items.length === 0){
                    callback();
                }else if(items.length === 1){
                    obj.id = items[0];
                    callback(); // THE ISSUE IS HERE!
                }else{
                    throw 'To many matches!';
                }
            }

    }else{
        callback();
    }
};

I see that callback is defined in lib/connection.js:92 and check for a isNew variable received from save or update method
I think that callback must receive a parameter to force the update for this cases.

There is another way to do this?

@danyg
Copy link
Author

danyg commented Aug 29, 2013

You can change the code addin this (in lib/connection.js:93)

  doOnSave(function(forceUpdate) {
      if(isNew && !!forceUpdate){
          isNew = false;
      }

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

No branches or pull requests

1 participant