Permalink
Browse files

Added support for object removal (via `persistence.remove`).

  • Loading branch information...
1 parent b07a858 commit 4941cd3aaa38b7011af5543263660d664e09af4c Zef Hemel committed Feb 5, 2010
Showing with 89 additions and 43 deletions.
  1. +5 −1 README.md
  2. +56 −16 persistence.js
  3. +24 −23 persistence.min.js
  4. +3 −2 test/test.manytomany.js
  5. +1 −1 test/test.onetomany.js
View
@@ -108,7 +108,7 @@ through the `QueryCollection` API that will be discussed later:
tasks.tags.remove(tag)l
tasks.tags.list(tx, function(allTags) { console.log(allTags); });
-Persisting objects
+Persisting/removing objects
------------------
Similar to [hibernate](http://www.hibernate.org), `persistence.js`
@@ -127,6 +127,10 @@ be persisted using the `persistence.add` function:
persistence.add(t);
}
+Objects can also be removed from the database:
+
+ persistence.remove(c);
+
All changes made to tracked objects can be flushed to the database by
using `persistence.flush`, which takes a transaction object and
callback function as arguments. A new transaction can be started using
View
@@ -29,6 +29,7 @@ var persistence = window.persistence || {};
var conn = null;
var entityMeta = {};
var trackedObjects = {};
+ var objectsToRemove = {};
persistence.trackedObjects = trackedObjects;
@@ -99,16 +100,16 @@ var persistence = window.persistence || {};
*/
persistence.schemaSync = function (callback) {
var queries = [];
- for ( var entityName in entityMeta) {
+ for (var entityName in entityMeta) {
if (entityMeta.hasOwnProperty(entityName)) {
var meta = entityMeta[entityName];
var rowDef = '';
- for ( var prop in meta.fields) {
+ for (var prop in meta.fields) {
if (meta.fields.hasOwnProperty(prop)) {
rowDef += prop + " " + meta.fields[prop] + ", ";
}
}
- for ( var rel in meta.hasOne) {
+ for (var rel in meta.hasOne) {
if (meta.hasOne.hasOwnProperty(rel)) {
var otherMeta = meta.hasOne[rel].type.meta;
rowDef += rel + " VARCHAR(255), ";
@@ -117,7 +118,7 @@ var persistence = window.persistence || {};
+ "` ON `" + meta.name + "` (`" + rel + "`)", null ]);
}
}
- for ( var rel in meta.hasMany) {
+ for (var rel in meta.hasMany) {
if (meta.hasMany.hasOwnProperty(rel) && meta.hasMany[rel].manyToMany) {
var tableName = meta.hasMany[rel].tableName;
if (!generatedTables[tableName]) {
@@ -162,6 +163,16 @@ var persistence = window.persistence || {};
};
/**
+ * Marks the object to be removed (on next flush)
+ * @param obj object to be removed
+ */
+ persistence.remove = function(obj) {
+ if (!objectsToRemove[obj.id]) {
+ objectsToRemove[obj.id] = obj;
+ }
+ };
+
+ /**
* Persists all changes to the database
*
* @param tx
@@ -174,24 +185,45 @@ var persistence = window.persistence || {};
persistence.transaction(function(tx) { persistence.flush(tx, callback); });
return;
}
- var objArray = [];
- for ( var id in trackedObjects) {
+ var persistObjArray = [];
+ for (var id in trackedObjects) {
if (trackedObjects.hasOwnProperty(id)) {
- objArray.push(trackedObjects[id]);
+ persistObjArray.push(trackedObjects[id]);
+ }
+ }
+ var removeObjArray = [];
+ for (var id in objectsToRemove) {
+ if (objectsToRemove.hasOwnProperty(id)) {
+ removeObjArray.push(objectsToRemove[id]);
+ delete trackedObjects[id]; // Stop tracking
}
}
- function persistOneEntity () {
- var obj = objArray.pop();
+ function removeOneObject() {
+ var obj = removeObjArray.pop();
+ remove(obj, tx, function () {
+ if (removeObjArray.length > 0) {
+ removeOneObject();
+ } else if (callback) {
+ callback();
+ }
+ });
+ }
+ function persistOneObject () {
+ var obj = persistObjArray.pop();
save(obj, tx, function () {
- if (objArray.length > 0) {
- persistOneEntity();
+ if (persistObjArray.length > 0) {
+ persistOneObject();
+ } else if(removeObjArray.length > 0) {
+ removeOneObject();
} else if (callback) {
callback();
}
});
}
- if (objArray.length > 0) {
- persistOneEntity();
+ if (persistObjArray.length > 0) {
+ persistOneObject();
+ } else if(removeObjArray.length > 0) {
+ removeOneObject();
} else if(callback) {
callback();
}
@@ -528,8 +560,16 @@ var persistence = window.persistence || {};
}
function remove (obj, tx, callback) {
- var sql = "DELETE FROM `" + obj._type + "` WHERE id = '" + obj.id + "'";
- tx.executeSql(sql, null, callback);
+ var queries = [["DELETE FROM `" + obj._type + "` WHERE id = '" + obj.id + "'", null]];
+ var meta = persistence.getMeta(obj._type);
+ for (var rel in meta.hasMany) {
+ if (meta.hasMany.hasOwnProperty(rel) && meta.hasMany[rel].manyToMany) {
+ var tableName = meta.hasMany[rel].tableName;
+ //var inverseProperty = meta.hasMany[rel].inverseProperty;
+ queries.push(["DELETE FROM `" + tableName + "` WHERE `" + meta.name + '_' + rel + "` = '" + obj.id + "'", null]);
+ }
+ }
+ executeQueriesSeq(tx, queries, callback);
}
/**
@@ -552,7 +592,7 @@ var persistence = window.persistence || {};
} else if (callback) {
callback.apply(this, callbackArgs);
}
- });
+ }, function(_, err) { console.log(err); });
}
if (queries.length > 0) {
executeOne();
Oops, something went wrong.

0 comments on commit 4941cd3

Please sign in to comment.