node-persist icon indicating copy to clipboard operation
node-persist copied to clipboard

onSave should be able to change the isNew state

Open danyg opened this issue 12 years ago • 1 comments

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 avatar Aug 29 '13 09:08 danyg

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

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

danyg avatar Aug 29 '13 09:08 danyg