Permalink
Browse files

A simple database dump function. Restore will come soon.

  • Loading branch information...
1 parent c76f098 commit ef3606a29fa20f20c57babd8f14da6fc309779de Zef Hemel committed Mar 22, 2010
Showing with 86 additions and 34 deletions.
  1. +20 −0 README.md
  2. +33 −1 persistence.js
  3. +33 −33 persistence.min.js
View
@@ -165,6 +165,26 @@ explicitly call `persistence.flush()`. The exception to this rule is
when using the `list(...)` method on a `QueryCollection`, which also
flushes first.
+Dumping (and restoring) a database
+--------------------------------
+
+`persistence.dump` can be used to create an object containing a full
+dump of a database. Naturally, it is adviced to only do this with
+smaller databases. Example:
+
+ persistence.dump(tx, [Task, Category], function(dump) {
+ console.log(dump);
+ });
+
+When `null` is provided as a first argument a new transaction will be started for the operation. If `null` is provided as second argument, `dump` defaults to dumping _all_ defined entities.
+
+The dump format is:
+
+ {"entity-name": [list of instances],
+ ...}
+
+There is no restore API yet, but this will be implemented soon.
+
Query collections
-----------------
View
@@ -117,7 +117,7 @@ var persistence = window.persistence || {};
rowDef = '';
for (var prop in meta.fields) {
if (meta.fields.hasOwnProperty(prop)) {
- rowDef += prop + " " + meta.fields[prop] + ", ";
+ rowDef += "`" + prop + "` " + meta.fields[prop] + ", ";
}
}
for (var rel in meta.hasOne) {
@@ -589,6 +589,38 @@ var persistence = window.persistence || {};
return Entity;
}
+
+ /**
+ * Dumps the entire database into an object (that can be serialized to JSON for instance)
+ * @param entities a list of entity constructor functions to serialize, defaults to all
+ * @param callback (object) the callback function called with the results.
+ */
+ persistence.dump = function(tx, entities, callback) {
+ if(!entities) { // Default: all entity types
+ entities = [];
+ for(e in entityClassCache) {
+ if(entityClassCache.hasOwnProperty(e)) {
+ entities.push(entityClassCache[e]);
+ }
+ }
+ }
+
+ var finishedCount = 0;
+ var result = {};
+ for(var i = 0; i < entities.length; i++) {
+ (function() {
+ var Entity = entities[i];
+ Entity.all().list(tx, function(all) {
+ result[Entity.meta.name] = all.map(function(e) { return e._data; });
+ finishedCount++;
+ if(finishedCount === entities.length) {
+ callback(result);
+ }
+ });
+ }());
+ }
+ };
+
/**
* Internal function to persist an object to the database
* this function is invoked by persistence.flush()
Oops, something went wrong.

0 comments on commit ef3606a

Please sign in to comment.