Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

persistence.sync.js now keeps track of changes to all entities in

_Change table.
  • Loading branch information...
commit d61084e64c29d32beae6ef31f7a1e7c2dd45bc66 1 parent fced2de
Zef Hemel authored
View
7 README.md
@@ -68,7 +68,7 @@ lacks `__defineGetter__` and `__defineSetter__` support, which
Connecting to a database
-------------------------
-Currently there is one global database connection, which is
+There is one global database connection, which is
initialized with a `persistence.connect` call. Its first argument is
the database name, the second a database description and third the
maximum database size (in bytes):
@@ -220,7 +220,7 @@ explicitly call `persistence.flush()`. The exception to this rule is
using the `list(...)` method on a database `QueryCollection`, which also
flushes first, although this behavior may change in the future.
-Dumping (and restoring) a database
+Dumping and restoring data
--------------------------------
`persistence.dump` can be used to create an object containing a full
@@ -252,6 +252,9 @@ transaction. Note that `persistence.load` does not empty the database
first, it simply attempts to add all objects to the database. If
objects with, e.g. the same ID already exist, this will fail.
+Similarly, `persistence.loadFromJson` and `persistence.dumpToJson`
+respectively load and dump all the database's data as JSON strings.
+
Query collections
-----------------
View
3  persistence.js
@@ -33,6 +33,9 @@ var persistence = window.persistence || {};
persistence.trackedObjects = trackedObjects;
+ persistence.getObjectsToRemove = function() { return objectsToRemove; }
+ persistence.getTrackedObjects = function() { return trackedObjects; }
+
/**
* Retrieves metadata about entity, mostly for internal use
*/
View
34 persistence.min.js
@@ -43,23 +43,23 @@ persistence.remove=function(a){M[a.id]||(M[a.id]=a)};persistence.flush=function(
0;d<f.length;d++)L(f[d],a)}}else persistence.transaction(function(j){persistence.flush(j,c)})};persistence.clean=function(){z={}};persistence.reset=function(a){function c(){var d=b.pop();a.executeSql("DROP TABLE "+d,null,function(){b.length>0&&c()})}if(a){var b=[];for(p in N)N.hasOwnProperty(p)&&b.push(p);c();persistence.clean();N={}}else persistence.transaction(function(d){persistence.reset(d)})};persistence.rowToEntity=function(a,c,b){b=b||"";if(z[c[b+"id"]])return z[c[b+"id"]];var d=u[a];a=v(a);
if(!c[b+"id"])return null;a=new a;a.id=c[b+"id"];a._new=false;for(var f in c)if(c.hasOwnProperty(f))if(f.substring(0,b.length)===b){var h=f.substring(b.length);if(h!="id")a._data[h]=persistence.dbValToEntityVal(c[f],d.fields[h])}return a};persistence.dbValToEntityVal=function(a,c){switch(c){case "BOOL":return a==1;case "JSON":return a?JSON.parse(a):a;default:return a}};persistence.entityValToDbVal=function(a,c){return a===undefined||a===null?null:c==="JSON"&&a?JSON.stringify(a):a.id?a.id:c==="BOOL"?
a?1:0:a};var K={};persistence.dump=function(a,c,b){if(!c){c=[];for(e in K)K.hasOwnProperty(e)&&c.push(K[e])}for(var d=0,f={},h=0;h<c.length;h++)(function(){var i=c[h];i.all().list(a,function(j){f[i.meta.name]=j.map(function(g){var k={},t=i.meta.fields;for(var E in t)if(t.hasOwnProperty(E))k[E]=g._data[E];t=i.meta.hasOne;for(var G in t)if(t.hasOwnProperty(G))k[G]=g._data[G];k.id=g.id;return k});d++;d===c.length&&b(f)})})()};persistence.load=function(a,c,b){for(var d in c)if(c.hasOwnProperty(d))for(var f=
-v(d),h=c[d],i=0;i<h.length;i++){var j=h[i],g=new f;for(var k in j)if(j.hasOwnProperty(k))g[k]=j[k];persistence.add(g)}persistence.flush(a,b)};D.prototype.addEventListener=function(a,c){if(typeof a=="object")for(var b=a,d=0;d<b.length;d++){a=b[d];this.subscribers[a]||(this.subscribers[a]=[]);this.subscribers[a].push(c)}else{this.subscribers[a]||(this.subscribers[a]=[]);this.subscribers[a].push(c)}};D.prototype.removeEventListener=function(a,c){for(var b=this.subscribers[a],d=0;d<b.length;d++)if(b[d]==
-c){this.subscribers[a].splice(d,1);return true}return false};D.prototype.triggerEvent=function(a){if(this.subscribers[a])for(var c in this.subscribers[a])this.subscribers[a].hasOwnProperty(c)&&this.subscribers[a][c].apply(null,arguments)};m.prototype=new D;m.prototype.persistQueries=function(){return[]};m.prototype.init=function(a,c){this._filter=new C;this._orderColumns=[];this._prefetchFields=[];this._additionalJoinSqls=[];this._additionalWhereSqls=[];this._entityName=a;this._constructor=c;this._limit=
--1;this._skip=0;this.subscribers={}};m.prototype.clone=function(){var a=new this._constructor(this._entityName);a._filter=this._filter;a._prefetchFields=this._prefetchFields.slice(0);a._orderColumns=this._orderColumns.slice(0);a._additionalJoinSqls=this._additionalJoinSqls.slice(0);a._additionalWhereSqls=this._additionalWhereSqls.slice(0);a._limit=this._limit;a._skip=this._skip;a.subscribers=this.subscribers;return a};m.prototype.filter=function(a,c,b){var d=this.clone();d._filter=new l(this._filter,
-new r(a,c,b));return d};m.prototype.order=function(a,c){c=c===undefined?true:c;var b=this.clone();b._orderColumns.push([a,c]);return b};m.prototype.limit=function(a){var c=this.clone();c._limit=a;return c};m.prototype.skip=function(a){var c=this.clone();c._skip=a;return c};m.prototype.prefetch=function(a){var c=this.clone();c._prefetchFields.push(a);return c};m.prototype.add=function(a){if(!a.id||!a._type)throw"Cannot add object of non-entity type onto collection.";persistence.add(a);this._filter.makeFit(a);
-this.triggerEvent("add",this,a);this.triggerEvent("change",this,a)};m.prototype.remove=function(a){if(!a.id||!a._type)throw"Cannot remove object of non-entity type from collection.";this._filter.makeNotFit(a);this.triggerEvent("remove",this,a);this.triggerEvent("change",this,a)};q.prototype=new m;q.prototype.each=function(a,c){if(a&&!a.executeSql){c=a;a=null}this.list(a,function(b){for(var d=0;d<b.length;d++)c(b[d])})};q.prototype.one=function(a,c){if(a&&!a.executeSql){c=a;a=null}this.limit(1).list(a,
-function(b){b.length===0?c(null):c(b[0])})};q.prototype.list=function(a,c){function b(A,H,I){var O=["`"+H+"`.id AS "+I+"id"];for(var B in A.fields)A.fields.hasOwnProperty(B)&&O.push("`"+H+"`.`"+B+"` AS `"+I+B+"`");for(B in A.hasOne)A.hasOne.hasOwnProperty(B)&&O.push("`"+H+"`.`"+B+"` AS `"+I+B+"`");return O}var d=this;if(a){for(var f=this._entityName,h=persistence.getMeta(f),i=[],j=f+"_",g=b(h,h.name,j),k=this._additionalJoinSqls.join(" "),t=0;t<this._prefetchFields.length;t++){var E=this._prefetchFields[t],
-G=h.hasOne[E].type.meta,Q=G.name+"_"+E+"_tbl";g=g.concat(b(G,Q,E+"_"));k+="LEFT JOIN `"+G.name+"` AS `"+Q+"` ON `"+Q+"`.`id` = `"+j+E+"` "}t="WHERE "+[this._filter.sql(j,i)].concat(this._additionalWhereSqls).join(" AND ");var P="SELECT "+g.join(", ")+" FROM `"+f+"` "+k+" "+t;if(this._orderColumns.length>0)P+=" ORDER BY "+this._orderColumns.map(function(A){return"`"+j+A[0]+"` "+(A[1]?"ASC":"DESC")}).join(", ");if(this._limit>=0)P+=" LIMIT "+this._limit;if(this._skip>0)P+=" OFFSET "+this._skip;persistence.flush(a,
-function(){a.executeSql(P,i,function(A){for(var H=[],I=0;I<A.length;I++){for(var O=A[I],B=persistence.rowToEntity(f,O,j),R=0;R<d._prefetchFields.length;R++){var S=d._prefetchFields[R];B[S]=persistence.rowToEntity(h.hasOne[S].type.meta.name,O,S+"_")}H.push(B);persistence.add(B)}c(H);d.triggerEvent("list",d,H)})})}else persistence.transaction(function(A){d.list(A,c)})};y.prototype=new q;y.prototype.add=function(a){persistence.add(a);this.triggerEvent("add",this,a);this.triggerEvent("change",this,a)};
-y.prototype.remove=function(a){persistence.remove(a);this.triggerEvent("remove",this,a);this.triggerEvent("change",this,a)};o.prototype=new q;o.prototype.initManyToMany=function(a,c){this._obj=a;this._coll=c};o.prototype.add=function(a){if(!this._localAdded.contains(a)){persistence.add(a);this._localAdded.push(a)}};o.prototype.clone=function(){var a=q.prototype.clone.call(this);a._localAdded=this._localAdded;a._localRemoved=this._localRemoved;a._obj=this._obj;a._coll=this._coll;return a};o.prototype.remove=
-function(a){if(this._localAdded.contains(a))this._localAdded.remove(a);else this._localRemoved.contains(a)||this._localRemoved.push(a)};o.prototype.persistQueries=function(){for(var a=[],c=persistence.getMeta(this._obj._type),b=c.hasMany[this._coll].type.meta,d=0;d<this._localAdded.length;d++)a.push(["INSERT INTO "+c.hasMany[this._coll].tableName+" (`"+c.name+"_"+this._coll+"`, `"+b.name+"_"+c.hasMany[this._coll].inverseProperty+"`) VALUES (?, ?)",[this._obj.id,this._localAdded[d].id]]);this._localAdded=
-[];for(d=0;d<this._localRemoved.length;d++)a.push(["DELETE FROM "+c.hasMany[this._coll].tableName+" WHERE `"+c.name+"_"+this._coll+"` = ? AND `"+b.name+"_"+c.hasMany[this._coll].inverseProperty+"` = ?",[this._obj.id,this._localRemoved[d].id]]);this._localRemoved=[];return a};n.prototype=new m;n.prototype.clone=function(){var a=q.prototype.clone.call(this);a._items=this._items;return a};n.prototype.add=function(a){this._items.push(a);this.triggerEvent("add",this,a);this.triggerEvent("change",this,
-a)};n.prototype.remove=function(a){for(var c=this._items,b=0;b<c.length;b++)if(c[b]===a){this._items.splice(b,1);this.triggerEvent("remove",this,a);this.triggerEvent("change",this,a)}};n.prototype.list=function(a,c){if(!a||a.executeSql)a=c;c=this._items.slice(0);for(var b=this,d=[],f=0;f<c.length;f++)this._filter.match(c[f])&&d.push(c[f]);d.sort(function(h,i){for(var j=0;j<b._orderColumns.length;j++){var g=b._orderColumns[j][0],k=b._orderColumns[j][1];if(h[g]<i[g])return k?-1:1;else if(h[g]>i[g])return k?
-1:-1}return 0});this._skip&&d.splice(0,this._skip);if(this._limit>-1)d=d.slice(0,this._limit);if(a)a(d);else return d};persistence.LocalQueryCollection=n;persistence.Observable=D;persistence.db=persistence.db||{};persistence.db.implementation="unsupported";persistence.db.conn=null;persistence.db.log=true;if(window.openDatabase)persistence.db.implementation="html5";else if(window.google&&google.gears)persistence.db.implementation="gears";persistence.db.html5={};persistence.db.html5.connect=function(a,
-c,b,d){var f={},h=openDatabase(a,d,c,b);f.transaction=function(i){return h.transaction(function(j){return i(persistence.db.html5.transaction(j))})};return f};persistence.db.html5.transaction=function(a){var c={};c.executeSql=function(b,d,f,h){persistence.db.log&&console.log(b);a.executeSql(b,d,function(i,j){if(f){i=[];for(var g=0;g<j.rows.length;g++)i.push(j.rows.item(g));f(i)}},h)};return c};persistence.db.gears={};persistence.db.gears.connect=function(a){var c={},b=google.gears.factory.create("beta.database");
-b.open(a);c.transaction=function(d){d(persistence.db.gears.transaction(b))};return c};persistence.db.gears.transaction=function(a){var c={};c.executeSql=function(b,d,f){persistence.db.log&&console.log(b);b=a.execute(b,d);if(f){for(d=[];b.isValidRow();){for(var h={},i=0;i<b.fieldCount();i++)h[b.fieldName(i)]=b.field(i);d.push(h);b.next()}f(d)}};return c};persistence.db.connect=function(a,c,b,d){d=d||"1.0";if(persistence.db.implementation=="html5")return persistence.db.html5.connect(a,c,b,d);else if(persistence.db.implementation==
-"gears")return persistence.db.gears.connect(a)}})();Number.prototype.equals=function(s){return this==s};Boolean.prototype.equals=function(s){return this==s};String.prototype.equals=function(s){return this==s};Array.prototype.equals=function(s){if(this.length!==s.length)return false;for(var v=0;v<this.length;v++)if(!this[v].equals(s[v]))return false;return true};Array.prototype.contains=function(s){for(var v=this.length,w=0;w<v;w++)if(this[w].equals(s))return true;return false};
-Array.prototype.remove=function(s){for(var v=this.length,w=0;w<v;w++)this[w].equals(s)&&this.splice(w,1)};
+v(d),h=c[d],i=0;i<h.length;i++){var j=h[i],g=new f;for(var k in j)if(j.hasOwnProperty(k))g[k]=j[k];persistence.add(g)}persistence.flush(a,b)};persistence.dumpToJson=function(a,c,b){persistence.dump(a,c,function(d){b(JSON.stringify(d))})};persistence.loadFromJson=function(a,c,b){persistence.load(a,JSON.parse(json),b)};D.prototype.addEventListener=function(a,c){if(typeof a=="object")for(var b=a,d=0;d<b.length;d++){a=b[d];this.subscribers[a]||(this.subscribers[a]=[]);this.subscribers[a].push(c)}else{this.subscribers[a]||
+(this.subscribers[a]=[]);this.subscribers[a].push(c)}};D.prototype.removeEventListener=function(a,c){for(var b=this.subscribers[a],d=0;d<b.length;d++)if(b[d]==c){this.subscribers[a].splice(d,1);return true}return false};D.prototype.triggerEvent=function(a){if(this.subscribers[a])for(var c in this.subscribers[a])this.subscribers[a].hasOwnProperty(c)&&this.subscribers[a][c].apply(null,arguments)};m.prototype=new D;m.prototype.persistQueries=function(){return[]};m.prototype.init=function(a,c){this._filter=
+new C;this._orderColumns=[];this._prefetchFields=[];this._additionalJoinSqls=[];this._additionalWhereSqls=[];this._entityName=a;this._constructor=c;this._limit=-1;this._skip=0;this.subscribers={}};m.prototype.clone=function(){var a=new this._constructor(this._entityName);a._filter=this._filter;a._prefetchFields=this._prefetchFields.slice(0);a._orderColumns=this._orderColumns.slice(0);a._additionalJoinSqls=this._additionalJoinSqls.slice(0);a._additionalWhereSqls=this._additionalWhereSqls.slice(0);
+a._limit=this._limit;a._skip=this._skip;a.subscribers=this.subscribers;return a};m.prototype.filter=function(a,c,b){var d=this.clone();d._filter=new l(this._filter,new r(a,c,b));return d};m.prototype.order=function(a,c){c=c===undefined?true:c;var b=this.clone();b._orderColumns.push([a,c]);return b};m.prototype.limit=function(a){var c=this.clone();c._limit=a;return c};m.prototype.skip=function(a){var c=this.clone();c._skip=a;return c};m.prototype.prefetch=function(a){var c=this.clone();c._prefetchFields.push(a);
+return c};m.prototype.add=function(a){if(!a.id||!a._type)throw"Cannot add object of non-entity type onto collection.";persistence.add(a);this._filter.makeFit(a);this.triggerEvent("add",this,a);this.triggerEvent("change",this,a)};m.prototype.remove=function(a){if(!a.id||!a._type)throw"Cannot remove object of non-entity type from collection.";this._filter.makeNotFit(a);this.triggerEvent("remove",this,a);this.triggerEvent("change",this,a)};q.prototype=new m;q.prototype.each=function(a,c){if(a&&!a.executeSql){c=
+a;a=null}this.list(a,function(b){for(var d=0;d<b.length;d++)c(b[d])})};q.prototype.one=function(a,c){if(a&&!a.executeSql){c=a;a=null}this.limit(1).list(a,function(b){b.length===0?c(null):c(b[0])})};q.prototype.list=function(a,c){function b(A,H,I){var O=["`"+H+"`.id AS "+I+"id"];for(var B in A.fields)A.fields.hasOwnProperty(B)&&O.push("`"+H+"`.`"+B+"` AS `"+I+B+"`");for(B in A.hasOne)A.hasOne.hasOwnProperty(B)&&O.push("`"+H+"`.`"+B+"` AS `"+I+B+"`");return O}var d=this;if(a){for(var f=this._entityName,
+h=persistence.getMeta(f),i=[],j=f+"_",g=b(h,h.name,j),k=this._additionalJoinSqls.join(" "),t=0;t<this._prefetchFields.length;t++){var E=this._prefetchFields[t],G=h.hasOne[E].type.meta,Q=G.name+"_"+E+"_tbl";g=g.concat(b(G,Q,E+"_"));k+="LEFT JOIN `"+G.name+"` AS `"+Q+"` ON `"+Q+"`.`id` = `"+j+E+"` "}t="WHERE "+[this._filter.sql(j,i)].concat(this._additionalWhereSqls).join(" AND ");var P="SELECT "+g.join(", ")+" FROM `"+f+"` "+k+" "+t;if(this._orderColumns.length>0)P+=" ORDER BY "+this._orderColumns.map(function(A){return"`"+
+j+A[0]+"` "+(A[1]?"ASC":"DESC")}).join(", ");if(this._limit>=0)P+=" LIMIT "+this._limit;if(this._skip>0)P+=" OFFSET "+this._skip;persistence.flush(a,function(){a.executeSql(P,i,function(A){for(var H=[],I=0;I<A.length;I++){for(var O=A[I],B=persistence.rowToEntity(f,O,j),R=0;R<d._prefetchFields.length;R++){var S=d._prefetchFields[R];B[S]=persistence.rowToEntity(h.hasOne[S].type.meta.name,O,S+"_")}H.push(B);persistence.add(B)}c(H);d.triggerEvent("list",d,H)})})}else persistence.transaction(function(A){d.list(A,
+c)})};y.prototype=new q;y.prototype.add=function(a){persistence.add(a);this.triggerEvent("add",this,a);this.triggerEvent("change",this,a)};y.prototype.remove=function(a){persistence.remove(a);this.triggerEvent("remove",this,a);this.triggerEvent("change",this,a)};o.prototype=new q;o.prototype.initManyToMany=function(a,c){this._obj=a;this._coll=c};o.prototype.add=function(a){if(!this._localAdded.contains(a)){persistence.add(a);this._localAdded.push(a)}};o.prototype.clone=function(){var a=q.prototype.clone.call(this);
+a._localAdded=this._localAdded;a._localRemoved=this._localRemoved;a._obj=this._obj;a._coll=this._coll;return a};o.prototype.remove=function(a){if(this._localAdded.contains(a))this._localAdded.remove(a);else this._localRemoved.contains(a)||this._localRemoved.push(a)};o.prototype.persistQueries=function(){for(var a=[],c=persistence.getMeta(this._obj._type),b=c.hasMany[this._coll].type.meta,d=0;d<this._localAdded.length;d++)a.push(["INSERT INTO "+c.hasMany[this._coll].tableName+" (`"+c.name+"_"+this._coll+
+"`, `"+b.name+"_"+c.hasMany[this._coll].inverseProperty+"`) VALUES (?, ?)",[this._obj.id,this._localAdded[d].id]]);this._localAdded=[];for(d=0;d<this._localRemoved.length;d++)a.push(["DELETE FROM "+c.hasMany[this._coll].tableName+" WHERE `"+c.name+"_"+this._coll+"` = ? AND `"+b.name+"_"+c.hasMany[this._coll].inverseProperty+"` = ?",[this._obj.id,this._localRemoved[d].id]]);this._localRemoved=[];return a};n.prototype=new m;n.prototype.clone=function(){var a=q.prototype.clone.call(this);a._items=this._items;
+return a};n.prototype.add=function(a){this._items.push(a);this.triggerEvent("add",this,a);this.triggerEvent("change",this,a)};n.prototype.remove=function(a){for(var c=this._items,b=0;b<c.length;b++)if(c[b]===a){this._items.splice(b,1);this.triggerEvent("remove",this,a);this.triggerEvent("change",this,a)}};n.prototype.list=function(a,c){if(!a||a.executeSql)a=c;c=this._items.slice(0);for(var b=this,d=[],f=0;f<c.length;f++)this._filter.match(c[f])&&d.push(c[f]);d.sort(function(h,i){for(var j=0;j<b._orderColumns.length;j++){var g=
+b._orderColumns[j][0],k=b._orderColumns[j][1];if(h[g]<i[g])return k?-1:1;else if(h[g]>i[g])return k?1:-1}return 0});this._skip&&d.splice(0,this._skip);if(this._limit>-1)d=d.slice(0,this._limit);if(a)a(d);else return d};persistence.LocalQueryCollection=n;persistence.Observable=D;persistence.db=persistence.db||{};persistence.db.implementation="unsupported";persistence.db.conn=null;persistence.db.log=true;if(window.openDatabase)persistence.db.implementation="html5";else if(window.google&&google.gears)persistence.db.implementation=
+"gears";persistence.db.html5={};persistence.db.html5.connect=function(a,c,b,d){var f={},h=openDatabase(a,d,c,b);f.transaction=function(i){return h.transaction(function(j){return i(persistence.db.html5.transaction(j))})};return f};persistence.db.html5.transaction=function(a){var c={};c.executeSql=function(b,d,f,h){persistence.db.log&&console.log(b);a.executeSql(b,d,function(i,j){if(f){i=[];for(var g=0;g<j.rows.length;g++)i.push(j.rows.item(g));f(i)}},h)};return c};persistence.db.gears={};persistence.db.gears.connect=
+function(a){var c={},b=google.gears.factory.create("beta.database");b.open(a);c.transaction=function(d){d(persistence.db.gears.transaction(b))};return c};persistence.db.gears.transaction=function(a){var c={};c.executeSql=function(b,d,f){persistence.db.log&&console.log(b);b=a.execute(b,d);if(f){for(d=[];b.isValidRow();){for(var h={},i=0;i<b.fieldCount();i++)h[b.fieldName(i)]=b.field(i);d.push(h);b.next()}f(d)}};return c};persistence.db.connect=function(a,c,b,d){d=d||"1.0";if(persistence.db.implementation==
+"html5")return persistence.db.html5.connect(a,c,b,d);else if(persistence.db.implementation=="gears")return persistence.db.gears.connect(a)}})();Number.prototype.equals=function(s){return this==s};Boolean.prototype.equals=function(s){return this==s};String.prototype.equals=function(s){return this==s};Array.prototype.equals=function(s){if(this.length!==s.length)return false;for(var v=0;v<this.length;v++)if(!this[v].equals(s[v]))return false;return true};
+Array.prototype.contains=function(s){for(var v=this.length,w=0;w<v;w++)if(this[w].equals(s))return true;return false};Array.prototype.remove=function(s){for(var v=this.length,w=0;w<v;w++)this[w].equals(s)&&this.splice(w,1)};
if(!this.JSON){this.JSON={};(function(){function s(l){return l<10?"0"+l:l}function v(l){F.lastIndex=0;return F.test(l)?'"'+l.replace(F,function(r){var m=D[r];return typeof m==="string"?m:"\\u"+("0000"+r.charCodeAt(0).toString(16)).slice(-4)})+'"':'"'+l+'"'}function w(l,r){var m,q,y=x,o,n=r[l];if(n&&typeof n==="object"&&typeof n.toJSON==="function")n=n.toJSON(l);if(typeof C==="function")n=C.call(r,l,n);switch(typeof n){case "string":return v(n);case "number":return isFinite(n)?String(n):"null";case "boolean":case "null":return String(n);
case "object":if(!n)return"null";x+=J;o=[];if(Object.prototype.toString.apply(n)==="[object Array]"){q=n.length;for(l=0;l<q;l+=1)o[l]=w(l,n)||"null";r=o.length===0?"[]":x?"[\n"+x+o.join(",\n"+x)+"\n"+y+"]":"["+o.join(",")+"]";x=y;return r}if(C&&typeof C==="object"){q=C.length;for(l=0;l<q;l+=1){m=C[l];if(typeof m==="string")if(r=w(m,n))o.push(v(m)+(x?": ":":")+r)}}else for(m in n)if(Object.hasOwnProperty.call(n,m))if(r=w(m,n))o.push(v(m)+(x?": ":":")+r);r=o.length===0?"{}":x?"{\n"+x+o.join(",\n"+x)+
"\n"+y+"}":"{"+o.join(",")+"}";x=y;return r}}if(typeof Date.prototype.toJSON!=="function"){Date.prototype.toJSON=function(){return isFinite(this.valueOf())?this.getUTCFullYear()+"-"+s(this.getUTCMonth()+1)+"-"+s(this.getUTCDate())+"T"+s(this.getUTCHours())+":"+s(this.getUTCMinutes())+":"+s(this.getUTCSeconds())+"Z":null};String.prototype.toJSON=Number.prototype.toJSON=Boolean.prototype.toJSON=function(){return this.valueOf()}}var L=/[\u0000\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g,
View
61 persistence.sync.js
@@ -31,6 +31,13 @@ if(!window.persistence) { // persistence.js not loaded!
persistence.sync = {};
persistence.sync.Change = persistence.define('_Change', {
+ action: "VARCHAR(10)",
+ date: "INT",
+ data: "JSON"
+});
+
+persistence.sync.Sync = persistence.define('_Sync', {
+ date: "INT"
});
(function() {
@@ -46,21 +53,57 @@ persistence.sync.Change = persistence.define('_Change', {
persistence.transaction(function(tx) { persistence.flush(tx, callback); });
return;
}
- var persistObjArray = [];
- for (var id in trackedObjects) {
- if (trackedObjects.hasOwnProperty(id)) {
- persistObjArray.push(trackedObjects[id]);
+ for (var id in persistence.getTrackedObjects()) {
+ if (persistence.getTrackedObjects().hasOwnProperty(id)) {
+ var obj = persistence.trackedObjects[id];
+ if(obj._new) {
+ var change = new persistence.sync.Change();
+ change.date = new Date().getTime();
+ change.action = 'new';
+ var rec = {};
+ var fields = persistence.define(obj._type).meta.fields;
+ console.log(fields);
+ for(var f in fields) {
+ if(fields.hasOwnProperty(f)) {
+ rec[f] = obj._data[f];
+ }
+ }
+ console.log("New: " + id);
+ var refs = persistence.define(obj._type).meta.hasOne;
+ for(var r in refs) {
+ if(refs.hasOwnProperty(r)) {
+ rec[r] = obj._data[r];
+ }
+ }
+ console.log("New 8: " + id);
+ rec.id = obj.id;
+ rec._type = obj._type;
+ change.data = rec;
+ persistence.add(change);
+ } else {
+ for ( var p in obj._dirtyProperties) {
+ if (obj._dirtyProperties.hasOwnProperty(p)) {
+ var change = new persistence.sync.Change();
+ change.date = new Date().getTime();
+ change.action = 'set-prop';
+ change.data = {type: obj._type, id: obj.id, prop: p, value: obj[p]};
+ persistence.add(change);
+ }
+ }
+ }
}
}
- var removeObjArray = [];
- for (var id in objectsToRemove) {
- if (objectsToRemove.hasOwnProperty(id)) {
- removeObjArray.push(objectsToRemove[id]);
+ for (var id in persistence.getObjectsToRemove()) {
+ if (persistence.getObjectsToRemove().hasOwnProperty(id)) {
+ var change = new persistence.sync.Change();
+ change.date = new Date().getTime();
+ change.action = 'delete';
+ change.data = id;
+ persistence.add(change);
}
}
persistence.oldFlush(tx, callback);
}
-
}());
Please sign in to comment.
Something went wrong with that request. Please try again.