C2FO / patio

Idiomatic database toolkit

Home Page:http://c2fo.github.io/patio

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Remove only one row

opened this issue · comments

Hello, I ask myself a question. I do this to get an item from a table and delete it if I must. But I wonder if it removes the only row I select from the table, or rather all the rows in the table?

Thank you

KeywordsList.first({kliDvcToken: DEVICE_ID, kliKeyDesc: key}).then(function(keyword){
        if(keyword == null){
            if(data[key] == 'on'){
                KeywordsList.insert([key, DEVICE_ID]);

                util.log(key + ' has been added');
            }
        } else {
            if(data[key] == 'off'){
                KeywordsList.remove().then(function(numRowsDeleted){
                    if(numRowsDeleted > 0){
                        util.log(key + ' has been deleted');
                    }
                }, function(error){
                    console.log('ERROR : ' + error);
                });
            }
        }
    }, function(error){
        console.log('ERROR : ' + error);
    });

I believe that when you run KeywordsList.remove() you are removing all rows.

How can I remove only the selected row with this request :
KeywordsList.first({kliDvcToken: DEVICE_ID, kliKeyDesc: key})

You should be able to use the keyword instance that is passed to the callback you give to then().

KeywordsList.first({kliDvcToken: DEVICE_ID, kliKeyDesc: key}).then(function(keyword){ //keyword is an the instance you care about keyword.remove(); });

Ok, thanks, I'll try that now !!

I've got this error : TypeError: Object # has no method 'remove'

So what is the difference between Truncate() and Remove() (seems to have the same action) ? The .filter(args) and .first(args) returns a dataset ?

It's ok I found the solution :)

KeywordsList.filter({kliDvcToken: DEVICE_ID, kliKeyDesc: key}).remove().then(function(numRowsDeleted){
                    if(numRowsDeleted > 0){
                        util.log(key + ' has been deleted');
                    }
                }, function(error){
                    console.log('ERROR : ' + error);
                });

You end up doing two requests in this case. One to find the keyword and then you do another filter to get the same keyword to delete().

Not sure why keyword would not have the function .remove()

I just did a test with similar code and it works as expected ....

User.first({id: myUser.id}).then(function(aUser) { aUser.remove().then(disconnect, disconnectError); });

In this case, it finds the user and then uses the .remove() instance method to remove this instance.

I test it again to be sure but I ended with the same error ..

node.js:201

        throw e; // process.nextTick error, or 'error' event on first tick


^

TypeError: Object #<Object> has no method 'remove'
    at [object Object].<anonymous> (/Users/appconcept/Desktop/Push Manager/libs/notification.js:246:25)
    at [object Object].<anonymous> (/Users/appconcept/node_modules/comb/lib/promise.js:68:20)
    at Array.0 (/Users/appconcept/node_modules/comb/lib/base/functions.js:30:27)
    at EventEmitter._tickCallback (node.js:192:40)

6 Jun 09:45:35 - [nodemon] app crashed - waiting for file changes before starting...

Here my function code

function update_keyword_for_key(KeywordsList, DEVICE_ID, data, key) {
    KeywordsList.first({kliDvcToken: DEVICE_ID, kliKeyDesc: key}).then(function(keyword){
        if(keyword == null){
            if(data[key] == 'on'){
                KeywordsList.insert([key, DEVICE_ID]);

                util.log(key + ' has been added');
            }
        } else {
            if(data[key] == 'off'){
                /*KeywordsList.filter({kliDvcToken: DEVICE_ID, kliKeyDesc: key}).remove().then(function(numRowsDeleted){
                    if(numRowsDeleted > 0){
                        util.log(key + ' has been deleted');
                    }
                }, function(error){
                    console.log('ERROR : ' + error);
                });*/

                keyword.remove().then(function(numRowDeleted){
                    if(numRowDeleted > 0){
                        util.log(key + 'has been deleted');
                    }
                }, function(error){
                    console.log('ERROR : ' + error);
                });
            }
        }
    }, function(error){
        console.log('ERROR : ' + error);
    });
}

Hi the method remove does not exists because you are not using models, your just using the datasets. The way you did it before is the correct way to do it.

var errorHandler = function(error){
    console.log('ERROR : ' + error);
};

function update_keyword_for_key(KeywordsList, DEVICE_ID, data, key) {
    var ds = KeywordsList.filter({kliDvcToken: DEVICE_ID, kliKeyDesc: key});
    ds.first().then(function(keyword){
        if(keyword == null){
            if(data[key] == 'on'){
                KeywordsList.insert([key, DEVICE_ID]);
                util.log(key + ' has been added');
            }
        } else {
            if(data[key] == 'off'){
                ds.remove().then(function(numRowsDeleted){
                    if(numRowsDeleted > 0){
                        util.log(key + ' has been deleted');
                    }
                }, errorHandler);              
            }
        }
    },errorHandler);
}

If KeyWorldList were a model the previous method would work