NetTalk Central

Author Topic: Inserting a record in idb  (Read 4546 times)

de la Rosa

  • Full Member
  • ***
  • Posts: 128
    • View Profile
    • Email
Inserting a record in idb
« on: October 01, 2023, 05:17:20 PM »
Hi,

Can I request a sample javascript to properly insert a record in an idb table using database.js device table example as follows:

{ name: "device",
      syncproc: "syncdevice",
      objectStore:{},
      everythingafter:0,
      primarykeyfield: "guid",
      timestampfield: "ts",
      servertimestampfield: "sts",
      deletedtimestampfield: "dts",
      indexes: [
        {name:'timestampkey',unique: false, fields:["ts"]},
        {name:'servertimestampkey',unique: false, fields:["sts"]}
      ],
      relations: [
      ],
      record: {
        guid:"",
        ts:0,
        sts:0,
        dts:0,
        syncurl:"",
        mobileno:"",
        email:"",
        user_name:"",
        password:"",
        description:"",
        lastsyncdate:"",
        lastsynctime:"",
        registered:0
      },

     I was thinking of doing it as follows but not so sure of the proper usage:

    database.device."guid" = Math.random().toString(36).substr(3,8).toUpperCase() + Math.random().toString(36).substr(3,8).toUpperCase();
    database.device."ts" = 0;
    database.device."sts" = 0;
    database.device."user_name" = ' Sample Name";
    idbWrite(database,device,record,fromSync,oncomplete,onerror)

    Thanks,
    Vic

Bruce

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 11250
    • View Profile
Re: Inserting a record in idb
« Reply #1 on: October 01, 2023, 09:54:09 PM »
Does your code work?
It looks fine here, but I haven't run it.

It might be more complicated than it needs to be. Since you are clearly adding a new record here, I'd suggest calling idbAdd, not idbWrite. And that will also prime the guid for you. You also don't need to set the time stamps.

Since this is the device table though, I'm wondering if you are over-complicating things. idbOne generates the single record table (a call to this is done for you on program startup if you are making a PWA). So then it becomes more of an edit than an add. Plus there are possibly embed points for priming this.

I think maybe you should describe in more detail what you are doing.

cheers
Bruce


de la Rosa

  • Full Member
  • ***
  • Posts: 128
    • View Profile
    • Email
Re: Inserting a record in idb
« Reply #2 on: October 02, 2023, 03:23:07 PM »
I am trying to add a record to an idb table in a PWA.

1.  Using idbAdd on the device table resulted in a new record but with only the guid and lastsyncdate populated. 
2.  Using idbAdd on any other table did not create a new record. Dev tools report  for users table

    Uncaught ReferenceError: users is not defined
    at amHere (sq_custom.js:37:28)
    at HTMLButtonElement.onclick (MakeABooking?PressedButton=SelAMBUBtn:383:205)

 Btw,I'm still on 14.04 though, will 14.05 be fully ready for push notifications?
   


« Last Edit: October 02, 2023, 07:36:46 PM by de la Rosa »

Bruce

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 11250
    • View Profile
Re: Inserting a record in idb
« Reply #3 on: October 02, 2023, 10:48:14 PM »
>>    Uncaught ReferenceError: users is not defined
    at amHere (sq_custom.js:37:28)

what does your javascript code look like?

>>  Btw,I'm still on 14.04 though, will 14.05 be fully ready for push notifications?

Probably 14.06. I need to do a 14.05 maintenance build soon, but I haven't had a chance to dig into the notifications stuff yet.

Bruce

de la Rosa

  • Full Member
  • ***
  • Posts: 128
    • View Profile
    • Email
Re: Inserting a record in idb
« Reply #4 on: October 03, 2023, 07:36:21 AM »
 here's my script

function amHere() {
     //database.users.guid = Math.random().toString(36).substr(3,8).toUpperCase() + Math.random().toString(36).substr(3,8).toUpperCase();
     database.users.name = "Ellen";
     console.log('Am Here Save!'  + database.users.guid + database.users.name + Organisation);
     idbAdd(database,users,record,fromSync,oncomplete,onerror);
     
}

- btw, Now, adding a record to device table also returns the same "device is not defined" ReferenceError

- is there a need to prime oncomplete and onerror?

- I notice priming such as database.users.name = "Ellen"; and database.users.record.name = "Ellen"; are both accepted, are there internal issues in the usage?

- In the OnClick of a button of a NetWebform, is there a way that both the javascript and the URL or procedure gets executed? I wanted to execute the script and jump to another page. Or is there an equivalent javascript embed point upon opening of the target page?

- Since the script is available on the client side, are there any safeguards we can use to avoid malicious code to be introduced?




   
« Last Edit: October 03, 2023, 11:12:22 AM by de la Rosa »

Bruce

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 11250
    • View Profile
Re: Inserting a record in idb
« Reply #5 on: October 03, 2023, 10:25:47 PM »
the nice thing about JavaScript is that you can put lines, or just variables, into the browser console and see if they are recognized.

>>    idbAdd(database,users,record,fromSync,oncomplete,onerror);

database is an object. (defined in database.js)
users is an object (also defined in database.js, and points back to database.users)
record is not an object. I think you meant to use users.record here

oncomplete and onerror are functions that you define - I don't know if you've defined them. They are optional parameters.
They contain the code you want to run after the add has succeeded or failed. You can use inline functions f you like, or write javascript functions.

fromsync is a parameter. In your case this is not a sync, so set it to false.

    idbAdd(database,users,users.record,false,function(){console.log('success')},function(err){console.log('fail')});

Cheers
Bruce

de la Rosa

  • Full Member
  • ***
  • Posts: 128
    • View Profile
    • Email
Re: Inserting a record in idb
« Reply #6 on: October 04, 2023, 10:40:52 AM »
>>  idbAdd(database,users,users.record,false,function(){console.log('success')},function(err){console.log('fail')});

   It turned out it wanted the db parameters to be fully specified as follows:

       idbAdd(database,database.users,database.users.record,false,function(){console.log('success')},function(err){console.log('fail')});

      Thanks Bruce!

   Now that I can add a record. I need to collect db.table.record values from several pages before adding a record. However, the db.table.record buffer is cleared on every page. Where should I
   declare a buffer variable that will be persistent in different pages until I get to complete priming the db.table record? I can't declare  global variables in custom.js as it continously resets. 
« Last Edit: October 04, 2023, 03:54:27 PM by de la Rosa »

Bruce

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 11250
    • View Profile
Re: Inserting a record in idb
« Reply #7 on: October 04, 2023, 10:07:55 PM »
JavaScript "resets" as you say, on each page.
If you want to persist information between pages then you'll need to write code to use local storage.

But of course, the better answer is "do what you need to do on one page". Popups for example are on the same page, so there's no reset in that situation.

Cheers
Bruce

de la Rosa

  • Full Member
  • ***
  • Posts: 128
    • View Profile
    • Email
Re: Inserting a record in idb
« Reply #8 on: October 05, 2023, 04:46:52 PM »
My problem with popups is say for example a button on a form

 - the Onclick javascript wont run if there is a destination URL or procedure defined
 - client side onclick javacript does not run either
 - is it possible to call the popup from javascript?

 yes, am using local storage in the meantime 

Bruce

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 11250
    • View Profile
Re: Inserting a record in idb
« Reply #9 on: October 05, 2023, 10:13:28 PM »
>> - the Onclick javascript wont run if there is a destination URL or procedure defined

correct. If you are going to a new page, then the javascript may or may not (usually not) be completed by the time the current page unloads.

>>  - client side onclick javacript does not run either

same reason. If you are navigating between pages then you're navigating between pages. You can't really do other things at the same time.

>>  - is it possible to call the popup from javascript?

to open it yes. As long as it's constructed on the page (ie the page "knows" it can be opened.)

But it sounds to me like you've gotten into a common trap. You've decided not to use the framework "as provided" but rather try and hook in other things to make the framework fit into your pattern. This of course leads to much frustration.

Perhaps you should step back, and define what you are really wanting to do that has lead you down this road in the first place...

Cheers
Bruce

de la Rosa

  • Full Member
  • ***
  • Posts: 128
    • View Profile
    • Email
Re: Inserting a record in idb
« Reply #10 on: October 07, 2023, 07:48:24 AM »
Ok, thanks Bruce!