Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I'm programming a Javascript application which saves "documents" in localStorage. As the app evolves, naturally there are changes in the document format.

I've tried searching but not found anything – probably (at least partly) because of search term ambiguities.

What is a good practice in managing document format versions/upgrades with local storage implementation approach?

share|improve this question

1 Answer 1

up vote 5 down vote accepted

One answer I see used in various languages is to version each stable format. Later, when you change to a new format you write a translator that reads the older format and converts it to the new when loaded.

The translator is not only responsible for repackaging the data into the new format but also providing sensible defaults for new properties or merging extinct properties into existing ones if their meaning isn't meant to be lost.

You can keep creating new versions as long as you keep writing matching translators, the conversions will then happen serially from the first match up to your most recent (eg. v1 -> v2 -> v3 -> v4).

When the new data is saved out it automatically is set to the latest version. In this way writing a utility to bulk-translate all old data (for major releases) is as simple as a loop:

while files
    read file
    write file
end while
share|improve this answer
    
Sounds sensible, and was along the lines I were thinking. I guess what I wasn't quite sure about was how to deal with upgrading the format – but serial conversion through all versions sounds like the easiest way to go. –  prikkprikkprikk Sep 23 '12 at 20:48

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.