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

Understanding upsert update #2

Closed
swampthang opened this issue Mar 5, 2017 · 3 comments
Closed

Understanding upsert update #2

swampthang opened this issue Mar 5, 2017 · 3 comments
Labels

Comments

@swampthang
Copy link

swampthang commented Mar 5, 2017

I'm able to successfully create records but not able to update. Inspecting the query args I can see that the where data is correct and can also confirm that the record I'm trying to update matches the query.

I have a feeling it's the way I have this set up. Here's what I have. I've removed some of the obj properties to keep it pithy:

SomeSQL('aniblocks')
.model([
  {key:'id',type:'uuid',props:['pk']},
  {key:'blockID', type: 'string'},
  {key:'title',type:'string'},
  {key:'timelineData',type:'map'}
])
.actions([
  {
    name: 'add_new_block',
    args: ['aniblock:map'],
    call: function(args,db) {
      return db.query('upsert',args.aniblock).exec();
    }
  },
  {
    name: 'update_block',
    args: ['aniblock:map'],
    call: function(args,db) {
      var D = args.aniblock;
      return db.query('upsert',{
        title:D.title,
        timelineData:D.timelineData
      }).where(['blockID','=',D.blockID]).exec();
    }
  }
])
.views([
  {
    name: 'get_block_by_blockID',
    args: ['blockID:string'],
    call: function(args,db) {
      return db.query('select').where(['blockID','=', args.blockID]).exec();
    }
  },
  {
    name: 'list_all_blocks',
    args: ['blockID:string'],
    call: function(args,db) {
      return db.query('select').exec();
    }
  }
])
.on('error', function(eventData){
  console.log(eventData);
});

function addNewBlock(obj) {
  SomeSQL()
  .connect()
  .then(function(result, db) {
      db.doAction('add_new_block',{aniblock:{
        id: null,
        blockID: obj.blockID,
        title: obj.title,
        timelineData: obj.timelineData
      }}).then(function(result, db) {
          console.log(result)
          return db.getView('get_block_by_blockID',obj);
      }).then(function(result, db) {
          console.log(result)
      }).on('error', function(eventData){
         console.log(eventData);
      });
  });
}

function updateBlock(obj) {
  SomeSQL()
  .connect()
  .then(function(result, db) {
      db.doAction('update_block',{aniblock:{
        blockID: obj.blockID,
        title: obj.title,
        timelineData: obj.timelineData
      }}).then(function(result, db) {
          return db.getView('get_block_by_blockID',obj);
      }).then(function(result, db) {
          logAll();
      }).on('error', function(eventData){
         console.log(eventData);
      });
  })
}

function logAll() {
  SomeSQL().connect()
  .then(function(result,db) {
    db.getView('list_all_blocks');
  })
  .then(function(result, db){
    console.log(result);
  });
}

I'm adding screenshots of a session so you can see what's happening. Successfully ran addNewBlock - note the blockID value:

screen shot 2017-03-05 at 12 34 30 pm

You can see that the result was success.
Then ran updateBlock. You can see here that the blockID is a match:

screen shot 2017-03-05 at 12 35 27 pm

From the same update instance here's showing the args object:

screen shot 2017-03-05 at 12 36 20 pm

Same instance showing the matching value for 'where':

screen shot 2017-03-05 at 12 36 31 pm

Back in the updateBlock method showing the result that nothing was modified:

screen shot 2017-03-05 at 12 37 00 pm

So, I'm sure I have everything out of place. Hoping you can tell me what I am I doing wrong.

@swampthang
Copy link
Author

When I run addNewBlock a row is added to the table because I can see it there. When I run updateBlock it seems to delete the row because when i run list_all_blocks it returns an empty array.

@only-cliches
Copy link
Owner

Hi swampthang, When you call 'connect()' the database is initialized, table objects are created, etc. The way you have it set up right now each of the bottom functions is emptying the database before querying it! You should call 'connect()' only once after you e setup the models and views.

I'm on my phone right now so I can't test this but that should fix things. 🙂

@swampthang
Copy link
Author

Ahhhh. That fixed it, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants