typicaljoe / taffydb

TaffyDB - an open source JavaScript Database for your browser
http://taffydb.com
MIT License
2.21k stars 285 forks source link

Data Persistence #146

Open acefxlabs opened 7 years ago

acefxlabs commented 7 years ago

Is there a way to make TaffyDB data persistent so it can stay even onreload of page

mmikowski commented 7 years ago

You can serialize and store Taffy data using JSON and localStorage as shown below:

var db_fn, record_list, json_str;
db_fn       = TAFFY( [ { name : 'fred' } ] );
record_list = db_fn().get();
json_str    =  JSON.stringify( record_list );
localStorage.setItem( 'my_taffy_key', json_str ) );

And then to retrieve:

var json_str, record_list, db_fn;
json_str    = localStorage.getItem( 'my_taffy_key' );
record_list = JSON.parse( json_str );
db_fn       = TAFFY( record_list );

Please confirm if this works for you. I propose to add this to the README and then close.

mmikowski commented 7 years ago

@acefxlabs is this answer working for you? Please confirm and we can resolve the issue.

pmrivas commented 7 years ago

Hi!.. i'm not acefxlabs... but it worked for me!.. i'm using 3000 rows and works like a charm! with 4 tables..

tremendus commented 7 years ago

Related to this, I need to write an adapter to write persistent data to filesystem. Are there any examples of this type of persistence I can use as a model or am I starting from scratch here? Any thoughts on whether this is practical ... and wise? ;)

Use case: I am building an embedded server that must handle a modest number of records with persistence but is light on resources (ie. not enough for sqlite).

I plan to persist records as JSON (actually YAML) files the filesystem. So I need to initially populate the records into a taffy db on server start up, and then dump records to YAML @filesystem on store().

I also need to store one-to-many child data records under the parent (as streams) because there may be thousands of records in some cases.

Any hints, tips, thoughts, gotchas?

brittonk commented 5 years ago

You can serialize and store Taffy data using JSON and localStorage as shown below:

var db_fn, record_list, json_str;
db_fn       = TAFFY( [ { name : 'fred' } ] );
record_list = db_fn().get();
json_str    =  JSON.stringify( record_list );
localStorage.setItem( 'my_taffy_key', json_str ) );

To anyone else trying this code, make sure you remove that last closing parenthesis on the setItem line. That typo wasted 10 mins of my life 💃