elemental2 indexeddb example?

109 views
Skip to first unread message

rhodebump

unread,
Jan 26, 2019, 11:57:44 PM1/26/19
to GWT Users

I am interested in using indexeddb from within GWT.  I found the following library that indicates that it provides indexeddb support https://github.com/google/elemental2


Does anyone have an example of opening a database? 


Thanks,

Phillip



Thomas Broyer

unread,
Jan 27, 2019, 5:48:37 AM1/27/19
to GWT Users
Elemental2 API map directly to the native browser APIs, so you'll start with IndexedDBGlobal.indexedDB and walk your way almost exactly like you'd do in JS.

rhodebump

unread,
Jan 27, 2019, 6:49:41 PM1/27/19
to GWT Users
Thanks, I did make some progress.   I dug around a little in the javadoc and found the IndexedDbGlobal, which I was able to use to create a new database.


elemental2.indexeddb.IDBFactory indexedDB = elemental2.indexeddb.IndexedDbGlobal.indexedDB;

elemental2.indexeddb.IDBOpenDBRequest openDBRequest = indexedDB.open("mydbtest", 1.0);


So far so good.


With a open database call, I get a returned object of elemental2.indexeddb.IDBOpenDBRequest (https://static.javadoc.io/com.google.elemental2/elemental2-indexeddb/1.0.0-RC1/elemental2/indexeddb/IDBOpenDBRequest.html)



In the google documentation that I have found that describes the indexedDB open operation, https://developers.google.com/web/ilt/pwa/working-with-indexeddb#opening_a_database


They provide the following code:

  var dbPromise = idb.open('test-db2', 1, function(upgradeDb) {
    console
.log('making a new object store');
   
if (!upgradeDb.objectStoreNames.contains('firstOS')) {
      upgradeDb
.createObjectStore('firstOS');
   
}
 
});

I want to register a function to execute when the database is upgraded.  I see there is a openDBRequest.onupgradeneeded(), but I just can't the syntax right.


elemental2.indexeddb.IDBFactory indexedDB = elemental2.indexeddb.IndexedDbGlobal.indexedDB;

elemental2.indexeddb.IDBOpenDBRequest openDBRequest = indexedDB.open("mydbtest", 1.0);


//how to i call a function for upgrade?  I want to create some object stores.

openDBRequest. onupgradeneeded = doUpgrade();

Thanks for your help.

Thomas Broyer

unread,
Jan 28, 2019, 10:17:33 AM1/28/19
to GWT Users


On Monday, January 28, 2019 at 12:49:41 AM UTC+1, rhodebump wrote:
Thanks, I did make some progress.   I dug around a little in the javadoc and found the IndexedDbGlobal, which I was able to use to create a new database.


elemental2.indexeddb.IDBFactory indexedDB = elemental2.indexeddb.IndexedDbGlobal.indexedDB;

elemental2.indexeddb.IDBOpenDBRequest openDBRequest = indexedDB.open("mydbtest", 1.0);


So far so good.


With a open database call, I get a returned object of elemental2.indexeddb.IDBOpenDBRequest (https://static.javadoc.io/com.google.elemental2/elemental2-indexeddb/1.0.0-RC1/elemental2/indexeddb/IDBOpenDBRequest.html)



In the google documentation that I have found that describes the indexedDB open operation, https://developers.google.com/web/ilt/pwa/working-with-indexeddb#opening_a_database


They provide the following code:

  var dbPromise = idb.open('test-db2', 1, function(upgradeDb) {
    console
.log('making a new object store');
   
if (!upgradeDb.objectStoreNames.contains('firstOS')) {
      upgradeDb
.createObjectStore('firstOS');
   
}
 
});


 
I want to register a function to execute when the database is upgraded.  I see there is a openDBRequest.onupgradeneeded(), but I just can't the syntax right.


elemental2.indexeddb.IDBFactory indexedDB = elemental2.indexeddb.IndexedDbGlobal.indexedDB;

elemental2.indexeddb.IDBOpenDBRequest openDBRequest = indexedDB.open("mydbtest", 1.0);


//how to i call a function for upgrade?  I want to create some object stores.

openDBRequest. onupgradeneeded = doUpgrade();

That would be something like:

openDBRequest.onupgradeneeded = new OnupgradeneededFn() {
 
@Override
 
public Object onInvoke(IDBVersionChangeEvent event) {
    doUpgrade
();
   
return Js.undefined(); // that value will likely be ignored anyway
 
}
};


or, using a lambda:

openDBRequest.onupgradeneeded = event -> {
  doUpgrade
();
 
return Js.undefined();
};

Reply all
Reply to author
Forward
0 new messages