Async issue with Persistence.js & Node.js

139 views
Skip to first unread message

Sudhakar Fomra

unread,
Apr 11, 2012, 7:12:24 AM4/11/12
to persis...@googlegroups.com
Hey Zef,

So in my project am using persistence.js to sync databases on mobile devices with dbs on a node.js server and my server is required to host multiple DBs. 

The mobile app I am developing might call the server for synching with a particular DB. So at any point of time AppA running on a phone could call server to synch with DB1 and at the same time AppB could make a call (from another phone) to sync with DB2.

My question here is will persistence.js perform well in such a scenario, as we know Persistence.js is essentially a singleton. How will it handle 2 concurrent requests to different DBs??? And should I alter the code for such a scenario... Hope my question is clear...

Cheers,
SF.

Mark M. Young

unread,
Jul 9, 2012, 4:57:16 PM7/9/12
to persis...@googlegroups.com
Sudhakar and Mason,

If you don't mind using jQuery, what about something like this?
<code>
var PersistenceJS = function(dbname, description, size)
{
jQuery.extend(true, this, persistence);
this.store.websql.config(this, dbname, description, size);
};
PersistenceJS.prototype = new Object();
PersistenceJS.prototype.constructor = PersistenceJS;
</code>
<code>
$(document).ready( function()
{
var dbA = new PersistenceJS('dba', 'DBA', 1024 * 1024);
dbA.debug = true;
var EntityA = dbA.define('EntityA', {});
EntityA.enableSync();

var dbB = new PersistenceJS('dbb', 'DBB', 1024 * 1024);
dbB.debug = true;
var EntityB = dbB.define('EntityB', {});
EntityB.enableSync();

dbA.schemaSync( function()
{
console.log('DBA schema synced');
dbA.add( new EntityA());
dbA.flush('DBA flushed');
dbB.schemaSync( function()
{
console.log('DBB schema synced');
dbB.add( new EntityB());
dbB.flush('DBB flushed');
});
});
});
</code>
Regards,
Mark M. Young

On 6/30/12, Mason <alad...@gmail.com> wrote:
> Hi Sudhakar,
>
> What's going on in your project? I have the exactly same requirement to
> sync DBs between devices. I tested persisntence serer sync, and also found
> some issues. Are you going to contribute to github or create a new branch?
>
> Thanks,
> Mason
--
"When pride comes, then comes disgrace, but with humility comes wisdom."
—Proverbs 11:2

Mark M. Young

unread,
Jul 9, 2012, 5:03:57 PM7/9/12
to persis...@googlegroups.com
If you want to use different stores across DBs, you can pass it as a parameter.
<code>
var PersistenceJS = function(dbname, description, size, store)
{
store = store || 'websql';
if( store in this.store )
{
jQuery.extend(true, this, persistence);
this.store[store].config(this, dbname, description, size);
}
else
{
console.error( "Unknown PersistenceJS store '" + store "'." );
}
};
</code>
Regards,
Mark M. Young

Mark M. Young

unread,
Jul 9, 2012, 5:09:14 PM7/9/12
to persis...@googlegroups.com
I posted prematurely--I found a logic error and a syntax error.
var PersistenceJS = function(dbname, description, size, store)
{
store = store || 'websql';
jQuery.extend(true, this, persistence);
if( store in this.store )
{
this.store[store].config(this, dbname, description, size);
}
else
{
console.error( "Unknown PersistenceJS store '" + store + "'." );
}
};
Regards,
Mark M. Young
Reply all
Reply to author
Forward
0 new messages