Is delete function is depreciated in HTML5 indexed database API - html

HI I am trying to delete a record in indexed database by passing its id, but my the function is not working properly and even Visual Studio intellisence is not showing any such function. Is objectstore.delete() function of the indexed database API has been depreciated or I am doing something wrong in calling it.
Following is the code spinet
var result = objectStore.delete(key);
result.onsuccess = function() {
alert('Success');
};

The delete by key function is working fine in all browsers Chrome, FF and IE10. Here is the sample code:
var connection = indexedDB.open(dbName);
connection.onsuccess = function(e) {
var database = e.target.result;
var transaction = database.transaction(storeName, 'readwrite');
var objectStore = transaction.objectStore(storeName);
var request = objectStore.delete(parseInt(key));
request.onsuccess = function (event)
{
database.close();
};
}

Almost everything in IndexedDB works the same way, and your question belies a misunderstanding of this model: everything happens in a transaction.
Almost nothing is syncronous in the IndexedDB API except opening the database. So you'll never see anything like database.delete() or database.set() when dealing with records.
To delete a record, as with getting or setting, you start by creating a new transaction on the database. You then use that transaction (like in Deni's example) to invoke the method for your change.
The transaction then "disappears" when it goes out of scope of all functions and your change is then committed to the database. It's on this transaction's reference to the database (not the database itself) that you hook event listeners such as success and error callbacks.

Related

Fullcalendar scheduler v5 auto refresh events

Is it possible to 'push' new events to fullcalendar scheduler without page refresh?
My source for events is JSON feed. So I would like a new event to appear in the calendar once another user enters it. There should be a listener/webhook to the json feed and if recognizes a change in the data, it should trigger calendar to refetch events.
Is something like this possible?
I found a way to do it:
document.addEventListener('DOMContentLoaded', function() {
var calendarEl = document.getElementById('calendar');
var source = new EventSource('sse.php')
source.addEventListener('message', function(e) {
//console.log(e.data);
if (e.data != 'doNotUpdate'){
calendar.refetchEvents();
}
}, false);
}
However, the sse.php, which is the script running on server and checks for new events in database table, is probably being triggered every second or every few seconds and returning doUpdate or doNotUpdate. I am not sure if this is ok? To many requests on MySQL? Should I set it up differently?

"ReferenceError: Jdbc is not defined" when run from function in a library [duplicate]

My code has been working daily for 6 months and it appears it doesn't work anymore. I know Google App Script Runtime did an update to V8 https://developers.google.com/apps-script/guides/v8-runtime (message displayed when I open "Script editor"). I suspect the problem comes from this version change because nothing else has changed.
I use Jdbc to access an SQL database at the beginning of the code:
var connectionName = '';
var user = '';
var userPwd = '';
var db = '';
var dbUrl = 'jdbc:mysql://'+connectionName+'/'+db;
var conn = Jdbc.getConnection(dbUrl, user, userPwd);
The error message I get is "ReferenceError: Jdbc is not defined", and I don't know how to fix it.
Could someone have a solution please?
This is a known issue with v8 and Google has acknowledged the bug. You may follow the issue tracker for updates.
The Google issuetracker now says that the issue has been fixed
Original answer (obsolete)
The other answer is correct, here is some additional information and a possible workaround. As noted in a comment on the issue on the Google issuetracker, you can pass Jdbc from the script using the library to a function in the library to make this work.
Example in the library Lib:
function getConnection(tjdbc) {
//dbUrl = ...
//user = ...
//password = ...
return tjdbc.getCloudSqlConnection(dbUrl, user, password);
}
in the script using the library :
function doStuff() {
var connection = Lib.getConnection(Jdbc);
//...
}
As the database can be queried using the resulting JdbcConnection, this might be the only place where you need to pass Jdbc in this way. It seems however that under V8 accessing Databases using Jdbc is also much slower than under Rhino (by a factor of 800 in my tests), so be warned.

couchbase synchronous retrieval get API

I have ran into a problem while dealing with bucket.get() API of the couchbase. I need to see, if some set of DocIDs are already stored in couchbase server or not, if not then I need to do some XML parsing.
var policy_bucket = cluster.openBucket('ss_policy_db');
function someFun(){
for (var i = 0; i < Policies.length; i++) {
var Profile = Policies[i];
var polID = Profile.get('id');
var ret = retrievePolicyNew(polID)
// do some action on the basis of ret.
}
}
function retrievePolicyNew(id) {
var result = policy_bucket.get(id.toString()); // TypeError: Second argument needs to be an object or callback.
console.log(result);
// return -1, on if we find the ID.
}
The problem with bucket.get() is that, it is a asynchronous (not properly know how to make synchronous call), I don't want to handle callback for every ID search. Is their any other way to search the list of ID in couchbase. It would be great if someone can help me getting synchronous call API set, that will solve my lot of other problems also. Because it not looks very good to make very small search also and handling it in callback.
I have stored very less data in DB, so performance is not a issue here.
You should be able to use this in a synchronous manner. I think either the code sample you provide above is incomplete and somewhere you're calling CouchbaseBucket.async() or something else. In any case, the docs are pretty clear that get() takes a string and returns a JsonDocument

Chrome WebSocket - onopen is not a function

I have a really simple websocket test on chrome, but it seems to be failing miserably:
var ws = new WebSocket('ws://localhost:8002/', 'a')
ws.onopen(function() {
console.log("ok")
})
It tells me: Uncaught TypeError: Property 'onopen' of object #<WebSocket> is not a function. I would assume that onopen should exist as a method whether or not there is a websocket server actually running, but I do have one running on that port.
I'm using chrome 32.0.1700. I see that all of the callback methods (onopen, onmessage, etc) are all null. What's going on here?
The function is not correctly assigned to the onopen event. Do it like this instead:
var ws = new WebSocket('ws://localhost:8002/', 'a')
ws.onopen = function() {
console.log("ok")
};
http://www.tutorialspoint.com/html5/html5_websocket.htm

Using Cache from Server Handler

I am trying to save some user input from a Google App Script form on a spreadsheet to the private cache.
Here is a test script:
var cache = CacheService.getPrivateCache();
function onLoad() {
var sheet = SpreadsheetApp.getActiveSpreadsheet(),
entries = [{
name : "Show form",
functionName : "showForm"
}];
sheet.addMenu("Test Menu", entries);
}
function showForm() {
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet(),
app = UiApp.createApplication(),
setButton = app.createButton("Set"),
setHandler = app.createServerClickHandler('setTest');
setButton.addClickHandler(setHandler);
app.add(setButton);
spreadsheet.show(app);
}
function setTest(event) {
cache.put("test", "test", 7200);
Browser.msgBox("Test was set: " + cache.get("test") + ". Use the getTest cell formula to test the cache.");
}
function getTest() {
var result = cache.get("test");
return result;
}
Once I click the menu button, a form appears and sets a cache value in a server handler. Then I try to get the value from the cache using =getTest() in a cell. I would expect the cache to return the value "test" but it seems to return null.
I started this issue here:
http://code.google.com/p/google-apps-script-issues/issues/detail?id=2039
And I also found another similar one:
http://code.google.com/p/google-apps-script-issues/issues/detail?id=1804
Trying to save some user input on a form into the cache and be able to access it from another function at a later time.
Any suggestions?
Custom functions have limited scope and then can only access a few select services. You read more about their limitations here.
With Cache its a bit tricky - caching is per "script scope". So when a Cache is accessed first from the Server handler script scope its running as a UiApp which has more privileges and that is different from the script scope that a custom function runs as. Therefore, the cache is not shared between those two scopes.
You can access cached items that you set in custom functions from other custom functions, but this cache can't cross these boundaries.
You could theoretically store this in ScripProperties but that can very clumsy and it would be misusing those capabilities and this is shared amongst all users.
ScriptProperties.setProperty("test", "test")
and
var result = ScriptProperties.getProperty("test");
If you can explain your usecase a bit more in depth, perhaps we can provide some alternative solutions.