@flumens/models 0.0.3 → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1 +1 @@
1
- "use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("tslib"),t=require("mobx"),r=require("lodash"),s=require("@flumens/utils/dist/errors"),n=require("../Model.js"),a=require("./Media.js"),i=require("./Occurrence.js"),o=require("./helpers.js");function u(e,t){var r,s,n;t&&t.values?(e.id=t.values.id,e.samples&&t.samples&&(null===(r=e.samples)||void 0===r||r.forEach((function(e,r){return u(e,t.samples[r])}))),e.occurrences&&t.occurrences&&(null===(s=e.occurrences)||void 0===s||s.forEach((function(e,r){return u(e,t.occurrences[r])}))),e.media&&t.media&&(null===(n=e.media)||void 0===n||n.forEach((function(e,r){return u(e,t.media[r])})))):console.warn("Model didn't receive an id from the server")}var c=function(t){return e.__awaiter(void 0,void 0,void 0,(function(){var r,s,n;return e.__generator(this,(function(e){switch(e.label){case 0:return r=[],t.media?(s=function(e){return e.getFormData()},[4,Promise.all(t.media.map(s))]):[3,2];case 1:n=e.sent(),r.push.apply(r,n),e.label=2;case 2:return t.occurrences?[4,Promise.all(t.occurrences.map(c))]:[3,4];case 3:n=e.sent(),r.push.apply(r,n.flat()),e.label=4;case 4:return t.samples?[4,Promise.all(t.samples.map(c))]:[3,6];case 5:n=e.sent(),r.push.apply(r,n.flat()),e.label=6;case 6:return[2,r]}}))}))};function d(e,t){var r={values:{id:t||-1}};return e.occurrences&&(r.occurrences=e.occurrences.map((function(e){return d(e)}))),e.samples&&(r.samples=e.samples.map((function(e){return d(e)}))),e.media&&(r.media=e.media.map((function(e){return d(e)}))),r}function l(e){try{console.warn(JSON.stringify(e).substring(0,1e5))}catch(e){}}var h=function(n){function h(r){var s=this;void 0===r&&(r={});var a=r.metadata,i=void 0===a?{}:a,u=r.samples,c=void 0===u?[]:u,d=r.occurrences,l=void 0===d?[]:d,m=r.media,p=void 0===m?[]:m,f=r.attrs,v=void 0===f?{}:f,_=e.__rest(r,["metadata","samples","occurrences","media","attrs"]);(s=n.call(this,e.__assign(e.__assign({},_),{attrs:e.__assign({date:(new Date).toISOString(),location_type:"latlon"},v)}))||this).remote=t.observable({synchronising:!1,url:null,headers:{},timeout:12e4}),s.validateRemote=o.validateRemoteModel,s.error=t.observable({message:""}),s.debouncedValue=300,s.keys=function(){return e.__assign(e.__assign({},h.keys),function(t){return Object.keys(t).reduce((function(r,s){var n;return e.__assign(e.__assign({},r),((n={})[s]=t[s].remote||t[s],n))}),{})}(s.getSurvey().attrs||{}))},s.requiresRemoteSync=function(){return!s.syncedAt||s.updatedAt>s.syncedAt},s.metadata=t.observable(i),s.samples=t.observable(c),s.occurrences=t.observable(l),s.media=t.observable(p);var y=function(e){return e.parent=s};s.samples.forEach(y),s.occurrences.forEach(y),s.media.forEach(y);var g=function(e){var t;return(null===(t=e.added)||void 0===t?void 0:t.length)?(e.added.forEach(y),s.setupdatedAtTimestamp(Date.now())):e.removedCount&&s.setupdatedAtTimestamp(Date.now()),e};return t.intercept(s.samples,g),t.intercept(s.occurrences,g),t.intercept(s.media,g),s}return e.__extends(h,n),h.fromJSON=function(t,r,s,n){var o,u,c,d,l,h,m;void 0===r&&(r=i.default),void 0===s&&(s=this),void 0===n&&(n=a.default);var p=(null===(o=null==t?void 0:t.attrs)||void 0===o?void 0:o.attrs)?null==t?void 0:t.attrs:t;return new this(e.__assign(e.__assign({},t),{attrs:p.attrs,metadata:p.metadata,createdAt:t.createdAt||(null===(u=t.metadata)||void 0===u?void 0:u.createdOn),updatedAt:t.updatedAt||(null===(c=t.metadata)||void 0===c?void 0:c.updatedOn),syncedAt:t.syncedAt||(null===(d=t.metadata)||void 0===d?void 0:d.syncedOn),samples:null===(l=p.samples)||void 0===l?void 0:l.map((function(e){return s.fromJSON(e,r,s,n)})),occurrences:null===(h=p.occurrences)||void 0===h?void 0:h.map((function(e){return r.fromJSON(e,n)})),media:null===(m=p.media)||void 0===m?void 0:m.map((function(e){return n.fromJSON(e)}))}))},h.prototype.setupdatedAtTimestamp=function(e){var t;n.prototype.setUpdatedAtTimestamp.call(this,e),null===(t=this.parent)||void 0===t||t.setupdatedAtTimestamp(e)},h.prototype.toJSON=function(){var r,s,a,i,o=n.prototype.toJSON.call(this);return this.parent?JSON.parse(JSON.stringify(e.__assign(e.__assign({},o),{metadata:t.toJS(this.metadata)||{},occurrences:this.occurrences.map((function(e){return e.toJSON()}))||[],samples:(null===(a=this.samples)||void 0===a?void 0:a.map((function(e){return e.toJSON()})))||[],media:(null===(i=this.media)||void 0===i?void 0:i.map((function(e){return e.toJSON()})))||[]}))):JSON.parse(JSON.stringify(e.__assign(e.__assign({},o),{attrs:{attrs:o.attrs,metadata:t.toJS(this.metadata)||{},occurrences:this.occurrences.map((function(e){return e.toJSON()}))||[],samples:(null===(r=this.samples)||void 0===r?void 0:r.map((function(e){return e.toJSON()})))||[],media:(null===(s=this.media)||void 0===s?void 0:s.map((function(e){return e.toJSON()})))||[]}})))},h.prototype.getSurvey=function(){return this.survey?this.parent?this.parent.getSurvey().smp||{}:this.survey:{}},h.prototype.save=function(){return e.__awaiter(this,void 0,void 0,(function(){return e.__generator(this,(function(e){switch(e.label){case 0:if(this.attrs.deleted)return[2];if(this.parent)return this.parent.save(),[2];if(!this._store)throw new Error("Trying to sync locally without a store");return[4,this._store.save(this.toJSON())];case 1:return e.sent(),[2]}}))}))},h.prototype.destroy=function(t){return e.__awaiter(this,void 0,void 0,(function(){var r,s=this;return e.__generator(this,(function(e){switch(e.label){case 0:return r=function(){return Promise.all([Promise.all(s.media.map((function(e){return e.destroy(!0)}))),Promise.all(s.occurrences.map((function(e){return e.destroy(!0)})))])},this.parent?(this.parent.samples.remove(this),[4,r()]):[3,2];case 1:return e.sent(),t?[2]:(this.parent.save(),[2]);case 2:if(!this._store)throw new Error("Trying to sync locally without a store");return[4,this._store.destroy(this.cid)];case 3:return e.sent(),this.collection&&this.collection.remove(this),[4,r()];case 4:return e.sent(),this.attrs.deleted=!0,[2]}}))}))},h.prototype.sync=function(){return e.__awaiter(this,void 0,void 0,(function(){return e.__generator(this,(function(e){return[2,this.parent?this.parent.sync():n.prototype.sync.call(this)]}))}))},h.prototype.saveRemote=function(){return e.__awaiter(this,void 0,void 0,(function(){var t,r,s,n,a,i;return e.__generator(this,(function(e){switch(e.label){case 0:if(e.trys.push([0,6,,7]),!this.remote.url)throw new Error('A "remote" property is not configured.');t=void 0,e.label=1;case 1:return e.trys.push([1,4,,5]),this.remote.synchronising=!0,[4,this._uploadMedia()];case 2:return r=e.sent(),t=this.getSubmission(r),[4,this._createRemote(t)];case 3:return s=e.sent(),this.remote.synchronising=!1,u(this,s),n=(new Date).getTime(),this.updatedAt=n,this.syncedAt=n,this.save(),[3,5];case 4:throw a=e.sent(),this.remote.synchronising=!1,a.payload=t,a;case 5:return[3,7];case 6:throw 400===(i=e.sent()).status&&i.payload&&l(i.payload),i;case 7:return[2]}}))}))},h.prototype.updateRemote=function(){return e.__awaiter(this,void 0,void 0,(function(){var t,r,s,n,a,i;return e.__generator(this,(function(e){switch(e.label){case 0:if(e.trys.push([0,5,,6]),!this.remote.url)throw new Error('A "remote" property is not configured.');t=void 0,e.label=1;case 1:return e.trys.push([1,3,,4]),this.remote.synchronising=!0,r={},t=this.getSubmission(r),[4,this._updateRemote(this.id,t)];case 2:return s=e.sent(),this.remote.synchronising=!1,u(this,s),n=(new Date).getTime(),this.updatedAt=n,this.syncedAt=n,this.save(),[3,4];case 3:throw a=e.sent(),this.remote.synchronising=!1,a.payload=t,a;case 4:return[3,6];case 5:throw 400===(i=e.sent()).status&&i.payload&&l(i.payload),i;case 6:return[2]}}))}))},h.prototype._createRemote=function(t){return e.__awaiter(this,void 0,void 0,(function(){var r,n,a,i;return e.__generator(this,(function(e){switch(e.label){case 0:return r=this.remote.url,"function"!=typeof this.remote.headers?[3,2]:[4,this.remote.headers()];case 1:return n=e.sent(),[3,3];case 2:n=this.remote.headers,e.label=3;case 3:a={method:"POST",headers:n,body:JSON.stringify(t)},e.label=4;case 4:return e.trys.push([4,6,,7]),[4,o.makeRequest("".concat(r,"/samples"),a)];case 5:return[2,e.sent()];case 6:if(409===(i=e.sent()).status&&i.res&&i.res.duplicate_of)return[2,d(this,i.res.duplicate_of.id)];if("timeout"===i.message)throw new s.HandledError("Request aborted because of a network issue (timeout or similar).");throw i;case 7:return[2]}}))}))},h.prototype._updateRemote=function(t,r){return e.__awaiter(this,void 0,void 0,(function(){var n,a,i,u;return e.__generator(this,(function(e){switch(e.label){case 0:return n=this.remote.url,"function"!=typeof this.remote.headers?[3,2]:[4,this.remote.headers()];case 1:return a=e.sent(),[3,3];case 2:a=this.remote.headers,e.label=3;case 3:i={method:"PUT",headers:a,body:JSON.stringify(r)},e.label=4;case 4:return e.trys.push([4,6,,7]),[4,o.makeRequest("".concat(n,"/samples/").concat(t),i)];case 5:return[2,e.sent()];case 6:if(409===(u=e.sent()).status&&u.res&&u.res.duplicate_of)return[2,d(this,u.res.duplicate_of.id)];if("timeout"===u.message)throw new s.HandledError("Request aborted because of a network issue (timeout or similar).");throw u;case 7:return[2]}}))}))},h.prototype._uploadMedia=function(){return e.__awaiter(this,void 0,void 0,(function(){var t,r,n,a,i,u,d=this;return e.__generator(this,(function(l){switch(l.label){case 0:return t={},[4,c(this)];case 1:if(r=l.sent(),!r.length)return[2,t];n=function(t){return e.__awaiter(d,void 0,void 0,(function(){var r,n,a;return e.__generator(this,(function(e){switch(e.label){case 0:return r=this.remote.url,"function"!=typeof this.remote.headers?[3,2]:[4,this.remote.headers()];case 1:return n=e.sent(),[3,3];case 2:n=this.remote.headers,e.label=3;case 3:a={method:"POST",headers:n,body:t};try{return[2,o.makeRequest("".concat(r,"/media-queue"),a)]}catch(e){if("timeout"===e.message)throw new s.HandledError("Request aborted because of a network issue (timeout or similar).");throw e}return[2]}}))}))},a=5,i=function(s){var i,o,u;return e.__generator(this,(function(c){switch(c.label){case 0:return i=r.slice(s,s+a),o=new FormData,i.forEach((function(e){return o.append.apply(o,e)})),[4,n(o)];case 1:return u=c.sent(),t=e.__assign(e.__assign({},t),u),[2]}}))},u=0,l.label=2;case 2:return u<r.length?[5,i(u)]:[3,5];case 3:l.sent(),l.label=4;case 4:return u+=a,[3,2];case 5:return[2,t]}}))}))},h.prototype.getSubmission=function(t){var s=this;void 0===t&&(t={});var n="function"==typeof this.keys?this.keys():this.keys,a=e.__assign(e.__assign({},h.keys),n),i={values:{external_key:this.cid},media:[],samples:[],occurrences:[]};this.id&&(i.values.id=this.id);var o=function(e,t){var r=a[e].values;if(!r)return t;if("function"==typeof r)return r(t,i,s);if(t instanceof Array)return t.map((function(t){return o(e,t)}));if(r instanceof Array){var n=r.find((function(e){return e.value===t}));if(!n||!n.id)throw new Error('A "'.concat(e,'" attribute "').concat(t,'" value could not be mapped to a remote database field.'));return n.id}return r[t]};Object.keys(this.attrs).forEach((function(e){var t=s.attrs[e],n=function(e){return null==e};if(!n(t)){if(!a[e])return e=e.includes("smpAttr:")?e:r.snakeCase(e),void(i.values[e]=t);if(!n(t=o(e,t))){var u=a[e].id||e,c=Number.isNaN(Number(u))?u:"smpAttr:".concat(u);c=c.includes("smpAttr:")?c:r.snakeCase(c),i.values[c]=t}}})),this.samples.forEach((function(e){s.attrs.training&&(e.attrs.training=s.attrs.training);var r=e.getSubmission(t);r&&i.samples.push(r)})),this.occurrences.forEach((function(e){s.attrs.training&&(e.attrs.training=s.attrs.training);var r=e.getSubmission(t);r&&i.occurrences.push(r)})),this.media.forEach((function(e){var r=e.getSubmission(t);r&&i.media.push(r)}));var u=this.getSurvey();return u.modifySubmission?u.modifySubmission(i,this):i},h.prototype.isUploaded=function(){return this.parent?this.parent.isUploaded():!!this.syncedAt},h.prototype.isDisabled=function(){return this.isUploaded()},h.keys={location:{id:"entered_sref"},location_type:{id:"entered_sref_system",values:{british:"OSGB",irish:"OSIE",channel:"utm30ed50",latlon:4326}},group:{id:"group_id"},training:{id:"training",values:o.boolToWarehouseValue},deleted:{id:"deleted",values:o.boolToWarehouseValue}},h}(n.default);exports._getMediaFormData=c,exports.default=h;
1
+ "use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("tslib"),t=require("mobx"),r=require("lodash"),s=require("@flumens/utils/dist/errors"),n=require("../Model.js"),a=require("./Media.js"),i=require("./Occurrence.js"),o=require("./helpers.js");function u(e,t){var r,s,n;t&&t.values?(e.id=t.values.id,e.samples&&t.samples&&(null===(r=e.samples)||void 0===r||r.forEach((function(e,r){return u(e,t.samples[r])}))),e.occurrences&&t.occurrences&&(null===(s=e.occurrences)||void 0===s||s.forEach((function(e,r){return u(e,t.occurrences[r])}))),e.media&&t.media&&(null===(n=e.media)||void 0===n||n.forEach((function(e,r){return u(e,t.media[r])})))):console.warn("Model didn't receive an id from the server")}var c=function(t){return e.__awaiter(void 0,void 0,void 0,(function(){var r,s,n;return e.__generator(this,(function(e){switch(e.label){case 0:return r=[],t.media?(s=function(e){return e.getFormData()},[4,Promise.all(t.media.map(s))]):[3,2];case 1:n=e.sent(),r.push.apply(r,n),e.label=2;case 2:return t.occurrences?[4,Promise.all(t.occurrences.map(c))]:[3,4];case 3:n=e.sent(),r.push.apply(r,n.flat()),e.label=4;case 4:return t.samples?[4,Promise.all(t.samples.map(c))]:[3,6];case 5:n=e.sent(),r.push.apply(r,n.flat()),e.label=6;case 6:return[2,r]}}))}))};function d(e,t){var r={values:{id:t||-1}};return e.occurrences&&(r.occurrences=e.occurrences.map((function(e){return d(e)}))),e.samples&&(r.samples=e.samples.map((function(e){return d(e)}))),e.media&&(r.media=e.media.map((function(e){return d(e)}))),r}function l(e){try{console.warn(JSON.stringify(e).substring(0,1e5))}catch(e){}}var h=function(n){function h(r){var s=this;void 0===r&&(r={});var a=r.metadata,i=void 0===a?{}:a,u=r.samples,c=void 0===u?[]:u,d=r.occurrences,l=void 0===d?[]:d,m=r.media,p=void 0===m?[]:m,f=r.attrs,v=void 0===f?{}:f,_=e.__rest(r,["metadata","samples","occurrences","media","attrs"]);(s=n.call(this,e.__assign(e.__assign({},_),{attrs:e.__assign({date:(new Date).toISOString(),location_type:"latlon"},v)}))||this).remote=t.observable({synchronising:!1,url:null,headers:{},timeout:12e4}),s.validateRemote=o.validateRemoteModel,s.error=t.observable({message:""}),s.debouncedValue=300,s.keys=function(){return e.__assign(e.__assign({},h.keys),function(t){return Object.keys(t).reduce((function(r,s){var n;return e.__assign(e.__assign({},r),((n={})[s]=t[s].remote||t[s],n))}),{})}(s.getSurvey().attrs||{}))},s.requiresRemoteSync=function(){return!s.syncedAt||s.updatedAt>s.syncedAt},s.metadata=t.observable(i),s.samples=t.observable(c),s.occurrences=t.observable(l),s.media=t.observable(p);var y=function(e){return e.parent=s};s.samples.forEach(y),s.occurrences.forEach(y),s.media.forEach(y);var g=function(e){var t;return(null===(t=e.added)||void 0===t?void 0:t.length)?(e.added.forEach(y),s.setupdatedAtTimestamp(Date.now())):e.removedCount&&s.setupdatedAtTimestamp(Date.now()),e};return t.intercept(s.samples,g),t.intercept(s.occurrences,g),t.intercept(s.media,g),s}return e.__extends(h,n),h.fromJSON=function(t,r,s,n){var o,u,c,d,l,h,m;void 0===r&&(r=i.default),void 0===s&&(s=this),void 0===n&&(n=a.default);var p=(null===(o=null==t?void 0:t.attrs)||void 0===o?void 0:o.attrs)?null==t?void 0:t.attrs:t;return new this(e.__assign(e.__assign({},t),{attrs:p.attrs,metadata:p.metadata,createdAt:t.createdAt||(null===(u=t.metadata)||void 0===u?void 0:u.createdOn),updatedAt:t.updatedAt||(null===(c=t.metadata)||void 0===c?void 0:c.updatedOn),syncedAt:t.syncedAt||(null===(d=t.metadata)||void 0===d?void 0:d.syncedOn),samples:null===(l=p.samples)||void 0===l?void 0:l.map((function(e){return s.fromJSON(e,r,s,n)})),occurrences:null===(h=p.occurrences)||void 0===h?void 0:h.map((function(e){return r.fromJSON(e,n)})),media:null===(m=p.media)||void 0===m?void 0:m.map((function(e){return n.fromJSON(e)}))}))},h.prototype.setupdatedAtTimestamp=function(e){var t;n.prototype.setUpdatedAtTimestamp.call(this,e),null===(t=this.parent)||void 0===t||t.setupdatedAtTimestamp(e)},h.prototype.toJSON=function(){var r,s,a,i,o=n.prototype.toJSON.call(this);return this.parent?JSON.parse(JSON.stringify(e.__assign(e.__assign({},o),{metadata:t.toJS(this.metadata)||{},occurrences:this.occurrences.map((function(e){return e.toJSON()}))||[],samples:(null===(a=this.samples)||void 0===a?void 0:a.map((function(e){return e.toJSON()})))||[],media:(null===(i=this.media)||void 0===i?void 0:i.map((function(e){return e.toJSON()})))||[]}))):JSON.parse(JSON.stringify(e.__assign(e.__assign({},o),{attrs:{attrs:o.attrs,metadata:t.toJS(this.metadata)||{},occurrences:this.occurrences.map((function(e){return e.toJSON()}))||[],samples:(null===(r=this.samples)||void 0===r?void 0:r.map((function(e){return e.toJSON()})))||[],media:(null===(s=this.media)||void 0===s?void 0:s.map((function(e){return e.toJSON()})))||[]}})))},h.prototype.getSurvey=function(){return this.survey?this.parent?this.parent.getSurvey().smp||{}:this.survey:{}},h.prototype.save=function(){return e.__awaiter(this,void 0,void 0,(function(){var t;return e.__generator(this,(function(r){switch(r.label){case 0:if(this.attrs.deleted)return[2];if(this.parent)return this.parent.save(),[2];if(!this._store)throw new Error("Trying to sync locally without a store");return t=this.toJSON(),[4,this._store.save(e.__assign(e.__assign({},t),{data:t.attrs}))];case 1:return r.sent(),[2]}}))}))},h.prototype.destroy=function(t){return e.__awaiter(this,void 0,void 0,(function(){var r,s=this;return e.__generator(this,(function(e){switch(e.label){case 0:return r=function(){return Promise.all([Promise.all(s.media.map((function(e){return e.destroy(!0)}))),Promise.all(s.occurrences.map((function(e){return e.destroy(!0)})))])},this.parent?(this.parent.samples.remove(this),[4,r()]):[3,2];case 1:return e.sent(),t?[2]:(this.parent.save(),[2]);case 2:if(!this._store)throw new Error("Trying to sync locally without a store");return[4,this._store.delete(this.cid)];case 3:return e.sent(),this.collection&&this.collection.remove(this),[4,r()];case 4:return e.sent(),this.attrs.deleted=!0,[2]}}))}))},h.prototype.sync=function(){return e.__awaiter(this,void 0,void 0,(function(){return e.__generator(this,(function(e){return[2,this.parent?this.parent.sync():n.prototype.sync.call(this)]}))}))},h.prototype.saveRemote=function(){return e.__awaiter(this,void 0,void 0,(function(){var t,r,s,n,a,i;return e.__generator(this,(function(e){switch(e.label){case 0:if(e.trys.push([0,6,,7]),!this.remote.url)throw new Error('A "remote" property is not configured.');t=void 0,e.label=1;case 1:return e.trys.push([1,4,,5]),this.remote.synchronising=!0,[4,this._uploadMedia()];case 2:return r=e.sent(),t=this.getSubmission(r),[4,this._createRemote(t)];case 3:return s=e.sent(),this.remote.synchronising=!1,u(this,s),n=(new Date).getTime(),this.updatedAt=n,this.syncedAt=n,this.save(),[3,5];case 4:throw a=e.sent(),this.remote.synchronising=!1,a.payload=t,a;case 5:return[3,7];case 6:throw 400===(i=e.sent()).status&&i.payload&&l(i.payload),i;case 7:return[2]}}))}))},h.prototype.updateRemote=function(){return e.__awaiter(this,void 0,void 0,(function(){var t,r,s,n,a,i;return e.__generator(this,(function(e){switch(e.label){case 0:if(e.trys.push([0,5,,6]),!this.remote.url)throw new Error('A "remote" property is not configured.');t=void 0,e.label=1;case 1:return e.trys.push([1,3,,4]),this.remote.synchronising=!0,r={},t=this.getSubmission(r),[4,this._updateRemote(this.id,t)];case 2:return s=e.sent(),this.remote.synchronising=!1,u(this,s),n=(new Date).getTime(),this.updatedAt=n,this.syncedAt=n,this.save(),[3,4];case 3:throw a=e.sent(),this.remote.synchronising=!1,a.payload=t,a;case 4:return[3,6];case 5:throw 400===(i=e.sent()).status&&i.payload&&l(i.payload),i;case 6:return[2]}}))}))},h.prototype._createRemote=function(t){return e.__awaiter(this,void 0,void 0,(function(){var r,n,a,i;return e.__generator(this,(function(e){switch(e.label){case 0:return r=this.remote.url,"function"!=typeof this.remote.headers?[3,2]:[4,this.remote.headers()];case 1:return n=e.sent(),[3,3];case 2:n=this.remote.headers,e.label=3;case 3:a={method:"POST",headers:n,body:JSON.stringify(t)},e.label=4;case 4:return e.trys.push([4,6,,7]),[4,o.makeRequest("".concat(r,"/samples"),a)];case 5:return[2,e.sent()];case 6:if(409===(i=e.sent()).status&&i.res&&i.res.duplicate_of)return[2,d(this,i.res.duplicate_of.id)];if("timeout"===i.message)throw new s.HandledError("Request aborted because of a network issue (timeout or similar).");throw i;case 7:return[2]}}))}))},h.prototype._updateRemote=function(t,r){return e.__awaiter(this,void 0,void 0,(function(){var n,a,i,u;return e.__generator(this,(function(e){switch(e.label){case 0:return n=this.remote.url,"function"!=typeof this.remote.headers?[3,2]:[4,this.remote.headers()];case 1:return a=e.sent(),[3,3];case 2:a=this.remote.headers,e.label=3;case 3:i={method:"PUT",headers:a,body:JSON.stringify(r)},e.label=4;case 4:return e.trys.push([4,6,,7]),[4,o.makeRequest("".concat(n,"/samples/").concat(t),i)];case 5:return[2,e.sent()];case 6:if(409===(u=e.sent()).status&&u.res&&u.res.duplicate_of)return[2,d(this,u.res.duplicate_of.id)];if("timeout"===u.message)throw new s.HandledError("Request aborted because of a network issue (timeout or similar).");throw u;case 7:return[2]}}))}))},h.prototype._uploadMedia=function(){return e.__awaiter(this,void 0,void 0,(function(){var t,r,n,a,i,u,d=this;return e.__generator(this,(function(l){switch(l.label){case 0:return t={},[4,c(this)];case 1:if(r=l.sent(),!r.length)return[2,t];n=function(t){return e.__awaiter(d,void 0,void 0,(function(){var r,n,a;return e.__generator(this,(function(e){switch(e.label){case 0:return r=this.remote.url,"function"!=typeof this.remote.headers?[3,2]:[4,this.remote.headers()];case 1:return n=e.sent(),[3,3];case 2:n=this.remote.headers,e.label=3;case 3:a={method:"POST",headers:n,body:t};try{return[2,o.makeRequest("".concat(r,"/media-queue"),a)]}catch(e){if("timeout"===e.message)throw new s.HandledError("Request aborted because of a network issue (timeout or similar).");throw e}return[2]}}))}))},a=5,i=function(s){var i,o,u;return e.__generator(this,(function(c){switch(c.label){case 0:return i=r.slice(s,s+a),o=new FormData,i.forEach((function(e){return o.append.apply(o,e)})),[4,n(o)];case 1:return u=c.sent(),t=e.__assign(e.__assign({},t),u),[2]}}))},u=0,l.label=2;case 2:return u<r.length?[5,i(u)]:[3,5];case 3:l.sent(),l.label=4;case 4:return u+=a,[3,2];case 5:return[2,t]}}))}))},h.prototype.getSubmission=function(t){var s=this;void 0===t&&(t={});var n="function"==typeof this.keys?this.keys():this.keys,a=e.__assign(e.__assign({},h.keys),n),i={values:{external_key:this.cid},media:[],samples:[],occurrences:[]};this.id&&(i.values.id=this.id);var o=function(e,t){var r=a[e].values;if(!r)return t;if("function"==typeof r)return r(t,i,s);if(t instanceof Array)return t.map((function(t){return o(e,t)}));if(r instanceof Array){var n=r.find((function(e){return e.value===t}));if(!n||!n.id)throw new Error('A "'.concat(e,'" attribute "').concat(t,'" value could not be mapped to a remote database field.'));return n.id}return r[t]};Object.keys(this.attrs).forEach((function(e){var t=s.attrs[e],n=function(e){return null==e};if(!n(t)){if(!a[e])return e=e.includes("smpAttr:")?e:r.snakeCase(e),void(i.values[e]=t);if(!n(t=o(e,t))){var u=a[e].id||e,c=Number.isNaN(Number(u))?u:"smpAttr:".concat(u);c=c.includes("smpAttr:")?c:r.snakeCase(c),i.values[c]=t}}})),this.samples.forEach((function(e){s.attrs.training&&(e.attrs.training=s.attrs.training);var r=e.getSubmission(t);r&&i.samples.push(r)})),this.occurrences.forEach((function(e){s.attrs.training&&(e.attrs.training=s.attrs.training);var r=e.getSubmission(t);r&&i.occurrences.push(r)})),this.media.forEach((function(e){var r=e.getSubmission(t);r&&i.media.push(r)}));var u=this.getSurvey();return u.modifySubmission?u.modifySubmission(i,this):i},h.prototype.isUploaded=function(){return this.parent?this.parent.isUploaded():!!this.syncedAt},h.prototype.isDisabled=function(){return this.isUploaded()},h.keys={location:{id:"entered_sref"},location_type:{id:"entered_sref_system",values:{british:"OSGB",irish:"OSIE",channel:"utm30ed50",latlon:4326}},group:{id:"group_id"},training:{id:"training",values:o.boolToWarehouseValue},deleted:{id:"deleted",values:o.boolToWarehouseValue}},h}(n.default);exports._getMediaFormData=c,exports.default=h;
package/dist/Model.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import Store, { ModelDTO } from './Stores/Store';
1
+ import Store from './Stores/Store';
2
2
  export interface Options<T = any> {
3
3
  id?: string;
4
4
  cid?: string;
@@ -38,7 +38,7 @@ export default class Model {
38
38
  /**
39
39
  * Remote updated time.
40
40
  */
41
- syncedAt?: number;
41
+ syncedAt?: number | null;
42
42
  /**
43
43
  * Model's persistent observable attributes.
44
44
  */
@@ -71,6 +71,13 @@ export default class Model {
71
71
  /**
72
72
  * Returns a clean (no observables) JSON representation of the model.
73
73
  */
74
- toJSON(): ModelDTO;
74
+ toJSON(): {
75
+ id: string;
76
+ cid: string;
77
+ createdAt: number;
78
+ updatedAt: number;
79
+ syncedAt: number | null | undefined;
80
+ attrs: Attrs;
81
+ };
75
82
  resetDefaults(defaultsToSet?: any): Promise<void>;
76
83
  }
package/dist/Model.js CHANGED
@@ -1 +1 @@
1
- "use strict";Object.defineProperty(exports,"__esModule",{value:!0});var t=require("tslib"),e=require("mobx"),r=require("mobx-utils");function i(t){return t&&"object"==typeof t&&"default"in t?t:{default:t}}var s=i(require("@flumens/utils/dist/uuid")),n={deleted:!1},o=function(){function i(i){var o=this,a=i.id,u=void 0===a?"":a,d=i.cid,c=void 0===d?"":d,h=i.createdAt,f=i.updatedAt,_=i.syncedAt,l=i.attrs,p=void 0===l?{}:l,v=i.store;this.debouncedValue=3e3,this.id=u,this.cid=c||u||s.default();var y,A,b=Date.now();this.createdAt=h||b,this.updatedAt=f||b,this.syncedAt=_,this.attrs=e.observable((y=t.__assign(t.__assign({},n),p),JSON.parse(JSON.stringify(y)))),this._store=v;r.deepObserve(this.attrs,(function(){o.updatedAt=Date.now(),function(){for(var t=[],e=0;e<arguments.length;e++)t[e]=arguments[e];clearTimeout(A),A=setTimeout((function(){return o.sync.apply(o,t)}),o.debouncedValue)}()}))}return i.prototype.setUpdatedAtTimestamp=function(t){this.updatedAt=t},i.prototype.fetch=function(){return t.__awaiter(this,void 0,void 0,(function(){return t.__generator(this,(function(t){return this.ready=this._fetch(),[2,this.ready]}))}))},i.prototype._fetch=function(){return t.__awaiter(this,void 0,void 0,(function(){var r;return t.__generator(this,(function(t){switch(t.label){case 0:return this._store?[4,this._store.find(this.cid)]:[2,!1];case 1:return(r=t.sent())?[3,3]:[4,this.save()];case 2:return t.sent(),[2,!0];case 3:return r.id&&(this.id=r.id),r.cid&&(this.cid=r.cid),e.set(this.attrs,r.attrs),this.createdAt=r.createdAt,this.updatedAt=r.updatedAt,this.syncedAt=r.syncedAt,[2,!0]}}))}))},i.prototype.save=function(){return t.__awaiter(this,void 0,void 0,(function(){return t.__generator(this,(function(t){switch(t.label){case 0:if(!this._store)throw new Error("Trying to save locally without a store");return[4,this._store.save(this.toJSON())];case 1:return t.sent(),[2]}}))}))},i.prototype.destroy=function(){return t.__awaiter(this,void 0,void 0,(function(){return t.__generator(this,(function(t){switch(t.label){case 0:if(!this._store)throw new Error("Trying to delete locally without a store");return[4,this._store.destroy(this.cid)];case 1:return t.sent(),[2]}}))}))},i.prototype.sync=function(){return t.__awaiter(this,void 0,void 0,(function(){return t.__generator(this,(function(t){switch(t.label){case 0:return this._store?[4,this.save()]:[3,2];case 1:t.sent(),t.label=2;case 2:return[2]}}))}))},i.prototype.toJSON=function(){return{id:this.id,cid:this.cid,createdAt:this.createdAt,updatedAt:this.updatedAt,syncedAt:this.syncedAt,attrs:e.toJS(this.attrs)}},i.prototype.resetDefaults=function(r){var i=this;this.id="";var s=JSON.parse(JSON.stringify(t.__assign(t.__assign({},n),r)));e.set(this.attrs,s);var o=Object.keys(s);return Object.keys(this.attrs).forEach((function(t){o.includes(t)||(i.attrs[t]=null,delete i.attrs[t])})),this.save()},i.extendAttrs=function(r,i){return e.set(r,t.__assign(t.__assign({},i),r)),r},i}();exports.default=o;
1
+ "use strict";Object.defineProperty(exports,"__esModule",{value:!0});var t=require("tslib"),e=require("mobx"),s=require("mobx-utils");function i(t){return t&&"object"==typeof t&&"default"in t?t:{default:t}}var r=i(require("@flumens/utils/dist/uuid")),n={deleted:!1},a=function(){function i(i){var a=this,o=i.id,u=void 0===o?"":o,d=i.cid,c=void 0===d?"":d,h=i.createdAt,_=i.updatedAt,f=i.syncedAt,l=i.attrs,p=void 0===l?{}:l,v=i.store;this.debouncedValue=3e3,this.id=u,this.cid=c||u||r.default();var y,A,g=Date.now();this.createdAt=h||g,this.updatedAt=_||g,this.syncedAt=f,this.attrs=e.observable((y=t.__assign(t.__assign({},n),p),JSON.parse(JSON.stringify(y)))),this._store=v;s.deepObserve(this.attrs,(function(){a.updatedAt=Date.now(),function(){for(var t=[],e=0;e<arguments.length;e++)t[e]=arguments[e];clearTimeout(A),A=setTimeout((function(){return a.sync.apply(a,t)}),a.debouncedValue)}()}))}return i.prototype.setUpdatedAtTimestamp=function(t){this.updatedAt=t},i.prototype.fetch=function(){return t.__awaiter(this,void 0,void 0,(function(){return t.__generator(this,(function(t){return this.ready=this._fetch(),[2,this.ready]}))}))},i.prototype._fetch=function(){return t.__awaiter(this,void 0,void 0,(function(){var s;return t.__generator(this,(function(t){switch(t.label){case 0:return this._store?[4,this._store.find(this.cid)]:[2,!1];case 1:return(s=t.sent())?[3,3]:[4,this.save()];case 2:return t.sent(),[2,!0];case 3:return s.id&&(this.id=s.id),s.cid&&(this.cid=s.cid),e.set(this.attrs,s.data),this.createdAt=s.createdAt,this.updatedAt=s.updatedAt,this.syncedAt=s.syncedAt,[2,!0]}}))}))},i.prototype.save=function(){return t.__awaiter(this,void 0,void 0,(function(){var e;return t.__generator(this,(function(s){switch(s.label){case 0:if(!this._store)throw new Error("Trying to save locally without a store");return e=this.toJSON(),[4,this._store.save(t.__assign(t.__assign({},e),{data:e.attrs}))];case 1:return s.sent(),[2]}}))}))},i.prototype.destroy=function(){return t.__awaiter(this,void 0,void 0,(function(){return t.__generator(this,(function(t){switch(t.label){case 0:if(!this._store)throw new Error("Trying to delete locally without a store");return[4,this._store.delete(this.cid)];case 1:return t.sent(),[2]}}))}))},i.prototype.sync=function(){return t.__awaiter(this,void 0,void 0,(function(){return t.__generator(this,(function(t){switch(t.label){case 0:return this._store?[4,this.save()]:[3,2];case 1:t.sent(),t.label=2;case 2:return[2]}}))}))},i.prototype.toJSON=function(){return{id:this.id,cid:this.cid,createdAt:this.createdAt,updatedAt:this.updatedAt,syncedAt:this.syncedAt,attrs:e.toJS(this.attrs)}},i.prototype.resetDefaults=function(s){var i=this;this.id="";var r=JSON.parse(JSON.stringify(t.__assign(t.__assign({},n),s)));e.set(this.attrs,r);var a=Object.keys(r);return Object.keys(this.attrs).forEach((function(t){a.includes(t)||(i.attrs[t]=null,delete i.attrs[t])})),this.save()},i.extendAttrs=function(s,i){return e.set(s,t.__assign(t.__assign({},i),s)),s},i}();exports.default=a;
@@ -1,4 +1,4 @@
1
- import Store, { ModelDTO } from './Store';
1
+ import Store from './Store';
2
2
  export default class LocalForageStore implements Store {
3
3
  debugging: any;
4
4
  localForage: any;
@@ -6,11 +6,11 @@ export default class LocalForageStore implements Store {
6
6
  storeName: any;
7
7
  constructor(options: any);
8
8
  static _getDriverOrder(driverOrder: any): any;
9
- save(val: ModelDTO): Promise<any>;
9
+ save(val: any): Promise<any>;
10
10
  find(key: any): Promise<any>;
11
11
  findAll(): Promise<any>;
12
- destroy(key: any): Promise<any>;
13
- destroyAll(): Promise<void>;
12
+ delete(key: any): Promise<any>;
13
+ deleteAll(): Promise<void>;
14
14
  /**
15
15
  * Exports all objects from the store.
16
16
  */
@@ -1 +1 @@
1
- "use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("tslib"),r=require("localforage"),t=require("localforage-cordovasqlitedriver"),i=require("@ionic/react"),n=require("./utils.js");function o(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}var s=o(r),a=o(t),u=["indexeddb","websql","localstorage"];if(i.isPlatform("hybrid")){if(!window.sqlitePlugin)throw new Error("cordova-sql-storage plugin is missing. Please install it.");u=e.__spreadArray([a.default],u,!0)}var c={driverOrder:u},d=function(){function r(t){var i=this,n=e.__assign(e.__assign({},c),t);this.debugging=n.debugging,this.localForage=null,this.ready=new Promise((function(e,t){var o=new Promise((function(e){n.driverOrder&&"object"==typeof n.driverOrder[0]?s.default.defineDriver(n.driverOrder[0]).then(e):e(void 0)}));if(!n.storeName)throw new Error("storeName prop is missing");i.storeName=n.storeName,o.then((function(){var o={name:n.name||"indicia",storeName:n.storeName};n.version&&(o.version=n.version);var a=r._getDriverOrder(n.driverOrder),u=n.LocalForage||s.default;i.localForage=u.createInstance(o),i.localForage.setDriver(a).then(e).catch(t)}))}))}return r._getDriverOrder=function(e){return e.map((function(e){switch(e){case"indexeddb":return s.default.INDEXEDDB;case"websql":return s.default.WEBSQL;case"localstorage":return s.default.LOCALSTORAGE;default:return"object"==typeof e&&e._driver?e._driver:console.error("No such db driver!")}}))},r.prototype.save=function(r){return e.__awaiter(this,void 0,void 0,(function(){var t;return e.__generator(this,(function(e){switch(e.label){case 0:return[4,this.ready];case 1:if(e.sent(),!r.cid)throw new Error("Invalid key passed to store");return this.debugging?[4,this.find(r.cid)]:[3,3];case 2:t=e.sent(),n.printDiff(t,r,r.cid,this.storeName),e.label=3;case 3:return[2,this.localForage.setItem(r.cid,r)]}}))}))},r.prototype.find=function(r){return e.__awaiter(this,void 0,void 0,(function(){return e.__generator(this,(function(e){switch(e.label){case 0:return[4,this.ready];case 1:if(e.sent(),!r)throw new Error("Invalid key passed to store");return[2,this.localForage.getItem(r)]}}))}))},r.prototype.findAll=function(){return e.__awaiter(this,void 0,void 0,(function(){var r;return e.__generator(this,(function(e){switch(e.label){case 0:return[4,this.ready];case 1:return e.sent(),r=[],[4,this.localForage.iterate((function(e){r.push(e)}))];case 2:return e.sent(),[2,r]}}))}))},r.prototype.destroy=function(r){return e.__awaiter(this,void 0,void 0,(function(){return e.__generator(this,(function(e){switch(e.label){case 0:return[4,this.ready];case 1:if(e.sent(),!r)throw new Error("Invalid key passed to store");return[2,this.localForage.removeItem(r)]}}))}))},r.prototype.destroyAll=function(){return e.__awaiter(this,void 0,void 0,(function(){var r,t=this;return e.__generator(this,(function(e){switch(e.label){case 0:return[4,this.ready];case 1:return e.sent(),[4,this.findAll()];case 2:return r=e.sent(),[4,Promise.all(r.map((function(e){return t.destroy(e)})))];case 3:return e.sent(),[2]}}))}))},r.prototype.export=function(){return e.__awaiter(this,void 0,void 0,(function(){var r;return e.__generator(this,(function(t){switch(t.label){case 0:return[4,this.ready];case 1:return t.sent(),r=function(r,t){var i;return e.__assign(e.__assign({},r),((i={})[t.cid]=t,i))},[4,this.findAll()];case 2:return[2,t.sent().reduce(r,{})]}}))}))},r.prototype.import=function(r){return e.__awaiter(this,void 0,void 0,(function(){var t,i=this;return e.__generator(this,(function(e){switch(e.label){case 0:return[4,this.ready];case 1:return e.sent(),[4,this.destroyAll()];case 2:if(e.sent(),"object"!=typeof r)throw new Error("Invalid obj passed to store");return t=Object.entries(r).map((function(e){var r=e[1];return i.save(r)})),[4,Promise.all(t)];case 3:return e.sent(),[2]}}))}))},r}();exports.default=d;
1
+ "use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("tslib"),r=require("localforage"),t=require("localforage-cordovasqlitedriver"),i=require("@ionic/react"),n=require("./utils.js");function o(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}var a=o(r),s=o(t),u=["indexeddb","websql","localstorage"];if(i.isPlatform("hybrid")){if(!window.sqlitePlugin)throw new Error("cordova-sql-storage plugin is missing. Please install it.");u=e.__spreadArray([s.default],u,!0)}var c={driverOrder:u},d=function(){function r(t){var i=this,n=e.__assign(e.__assign({},c),t);this.debugging=n.debugging,this.localForage=null,this.ready=new Promise((function(e,t){var o=new Promise((function(e){n.driverOrder&&"object"==typeof n.driverOrder[0]?a.default.defineDriver(n.driverOrder[0]).then(e):e(void 0)}));if(!n.storeName)throw new Error("storeName prop is missing");i.storeName=n.storeName,o.then((function(){var o={name:n.name||"indicia",storeName:n.storeName};n.version&&(o.version=n.version);var s=r._getDriverOrder(n.driverOrder),u=n.LocalForage||a.default;i.localForage=u.createInstance(o),i.localForage.setDriver(s).then(e).catch(t)}))}))}return r._getDriverOrder=function(e){return e.map((function(e){switch(e){case"indexeddb":return a.default.INDEXEDDB;case"websql":return a.default.WEBSQL;case"localstorage":return a.default.LOCALSTORAGE;default:return"object"==typeof e&&e._driver?e._driver:console.error("No such db driver!")}}))},r.prototype.save=function(r){return e.__awaiter(this,void 0,void 0,(function(){var t;return e.__generator(this,(function(e){switch(e.label){case 0:return[4,this.ready];case 1:if(e.sent(),!r.cid)throw new Error("Invalid key passed to store");return this.debugging?[4,this.find(r.cid)]:[3,3];case 2:t=e.sent(),n.printDiff(t,r,r.cid,this.storeName),e.label=3;case 3:return[2,this.localForage.setItem(r.cid,r)]}}))}))},r.prototype.find=function(r){return e.__awaiter(this,void 0,void 0,(function(){return e.__generator(this,(function(e){switch(e.label){case 0:return[4,this.ready];case 1:if(e.sent(),!r)throw new Error("Invalid key passed to store");return[2,this.localForage.getItem(r)]}}))}))},r.prototype.findAll=function(){return e.__awaiter(this,void 0,void 0,(function(){var r;return e.__generator(this,(function(e){switch(e.label){case 0:return[4,this.ready];case 1:return e.sent(),r=[],[4,this.localForage.iterate((function(e){r.push(e)}))];case 2:return e.sent(),[2,r]}}))}))},r.prototype.delete=function(r){return e.__awaiter(this,void 0,void 0,(function(){return e.__generator(this,(function(e){switch(e.label){case 0:return[4,this.ready];case 1:if(e.sent(),!r)throw new Error("Invalid key passed to store");return[2,this.localForage.removeItem(r)]}}))}))},r.prototype.deleteAll=function(){return e.__awaiter(this,void 0,void 0,(function(){var r,t=this;return e.__generator(this,(function(e){switch(e.label){case 0:return[4,this.ready];case 1:return e.sent(),[4,this.findAll()];case 2:return r=e.sent(),[4,Promise.all(r.map((function(e){return t.delete(e)})))];case 3:return e.sent(),[2]}}))}))},r.prototype.export=function(){return e.__awaiter(this,void 0,void 0,(function(){var r;return e.__generator(this,(function(t){switch(t.label){case 0:return[4,this.ready];case 1:return t.sent(),r=function(r,t){var i;return e.__assign(e.__assign({},r),((i={})[t.cid]=t,i))},[4,this.findAll()];case 2:return[2,t.sent().reduce(r,{})]}}))}))},r.prototype.import=function(r){return e.__awaiter(this,void 0,void 0,(function(){var t,i=this;return e.__generator(this,(function(e){switch(e.label){case 0:return[4,this.ready];case 1:return e.sent(),[4,this.deleteAll()];case 2:if(e.sent(),"object"!=typeof r)throw new Error("Invalid obj passed to store");return t=Object.entries(r).map((function(e){var r=e[1];return i.save(r)})),[4,Promise.all(t)];case 3:return e.sent(),[2]}}))}))},r}();exports.default=d;
@@ -1,12 +1,17 @@
1
+ import { SQLiteConnection } from '@capacitor-community/sqlite';
1
2
  import Deferred from '@flumens/utils/dist/deferred';
2
3
  type SQLiteDatabaseOptions = {
3
4
  name?: string;
4
5
  debug?: boolean;
5
6
  web?: boolean;
6
7
  };
8
+ interface Query {
9
+ sql: string;
10
+ params?: unknown[];
11
+ }
7
12
  export default class SQLiteDatabase {
8
13
  static migrateCordova(): Promise<void>;
9
- private sqliteConnection;
14
+ sqliteConnection: SQLiteConnection;
10
15
  private connection?;
11
16
  private get db();
12
17
  protected name: string;
@@ -15,7 +20,7 @@ export default class SQLiteDatabase {
15
20
  ready: Deferred<boolean>;
16
21
  constructor({ name, debug, web }: SQLiteDatabaseOptions);
17
22
  init(): Promise<void>;
18
- query<T>(query: string, values?: any[]): Promise<T[]>;
23
+ query<T>({ sql, params }: Query): Promise<T[]>;
19
24
  getUrl(): Promise<string | undefined>;
20
25
  }
21
26
  export {};
@@ -1 +1 @@
1
- "use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("tslib"),t=require("jeep-sqlite/loader"),n=require("@capacitor-community/sqlite");function i(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}var r=i(require("@flumens/utils/dist/deferred")),s=function(){function i(e){var t=e.name,i=e.debug,s=e.web;this.sqliteConnection=new n.SQLiteConnection(n.CapacitorSQLite),this.name="main",this.debug=!1,this.isWeb=!1,this.ready=new r.default,this.name=t||this.name,this.debug=i||this.debug,this.isWeb=s||this.isWeb}return i.migrateCordova=function(){var t;return e.__awaiter(this,void 0,void 0,(function(){var i,r;return e.__generator(this,(function(e){switch(e.label){case 0:return[4,(i=new n.SQLiteConnection(n.CapacitorSQLite)).getMigratableDbList()];case 1:return r=e.sent(),(null===(t=r.values)||void 0===t?void 0:t.length)?[4,i.addSQLiteSuffix()]:[2];case 2:return e.sent(),[4,i.deleteOldDatabases()];case 3:return e.sent(),[2]}}))}))},Object.defineProperty(i.prototype,"db",{get:function(){return this.connection},enumerable:!1,configurable:!0}),i.prototype.init=function(){return e.__awaiter(this,void 0,void 0,(function(){var n,i;return e.__generator(this,(function(e){switch(e.label){case 0:return this.isWeb?(t.defineCustomElements(window),(n=document.createElement("jeep-sqlite")).setAttribute("autoSave","true"),document.body.appendChild(n),[4,customElements.whenDefined("jeep-sqlite")]):[3,3];case 1:return e.sent(),[4,this.sqliteConnection.initWebStore()];case 2:e.sent(),e.label=3;case 3:return i=this,[4,this.sqliteConnection.createConnection("".concat(this.name,"."),!1,"no-encryption",1,!1)];case 4:return i.connection=e.sent(),[4,this.connection.open()];case 5:return e.sent(),this.ready.resolve(!0),[2]}}))}))},i.prototype.query=function(t,n){var i;return e.__awaiter(this,void 0,void 0,(function(){var r;return e.__generator(this,(function(e){switch(e.label){case 0:return[4,this.ready];case 1:return e.sent(),[4,this.db.query(t,n).then((function(e){return e.values||[]}))];case 2:return r=e.sent(),this.debug?(console.groupCollapsed("💾 SQL: ".concat(t.trimStart().replaceAll(/\n/g," ").replaceAll(/\s+/g," ").substring(0,30),"...")),console.debug(t,n),console.groupEnd(),[4,null===(i=this.sqliteConnection)||void 0===i?void 0:i.saveToStore("".concat(this.name,"."))]):[3,4];case 3:e.sent(),e.label=4;case 4:return[2,r]}}))}))},i.prototype.getUrl=function(){return e.__awaiter(this,void 0,void 0,(function(){return e.__generator(this,(function(e){switch(e.label){case 0:return this.isWeb?[2,""]:[4,this.db.getUrl()];case 1:return[2,e.sent().url]}}))}))},i}();exports.default=s;
1
+ "use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("tslib"),t=require("jeep-sqlite/loader"),n=require("@capacitor-community/sqlite");function i(e){return e&&"object"==typeof e&&"default"in e?e:{default:e}}var r=i(require("@flumens/utils/dist/deferred")),s=function(){function i(e){var t=e.name,i=e.debug,s=e.web;this.sqliteConnection=new n.SQLiteConnection(n.CapacitorSQLite),this.name="main",this.debug=!1,this.isWeb=!1,this.ready=new r.default,this.name=t||this.name,this.debug=i||this.debug,this.isWeb=s||this.isWeb}return i.migrateCordova=function(){var t;return e.__awaiter(this,void 0,void 0,(function(){var i,r,s;return e.__generator(this,(function(e){switch(e.label){case 0:i=new n.SQLiteConnection(n.CapacitorSQLite),e.label=1;case 1:return e.trys.push([1,5,,6]),[4,i.getMigratableDbList()];case 2:return r=e.sent(),(null===(t=r.values)||void 0===t?void 0:t.length)?(console.log(r.values),[4,i.addSQLiteSuffix(void 0,r.values)]):[2];case 3:return e.sent(),[4,i.deleteOldDatabases(void 0,r.values)];case 4:return e.sent(),[3,6];case 5:return s=e.sent(),console.error(s),[3,6];case 6:return[4,i.checkConnectionsConsistency()];case 7:return e.sent(),[4,i.closeAllConnections()];case 8:return e.sent(),[2]}}))}))},Object.defineProperty(i.prototype,"db",{get:function(){return this.connection},enumerable:!1,configurable:!0}),i.prototype.init=function(){return e.__awaiter(this,void 0,void 0,(function(){var n,i;return e.__generator(this,(function(e){switch(e.label){case 0:return this.isWeb?(t.defineCustomElements(window),(n=document.createElement("jeep-sqlite")).setAttribute("autoSave","true"),document.body.appendChild(n),[4,customElements.whenDefined("jeep-sqlite")]):[3,3];case 1:return e.sent(),[4,this.sqliteConnection.initWebStore()];case 2:e.sent(),e.label=3;case 3:return[4,this.sqliteConnection.checkConnectionsConsistency()];case 4:return e.sent(),[4,this.sqliteConnection.closeAllConnections()];case 5:return e.sent(),i=this,[4,this.sqliteConnection.createConnection(this.name,!1,"no-encryption",1,!1)];case 6:return i.connection=e.sent(),[4,this.connection.open()];case 7:return e.sent(),this.ready.resolve(!0),[2]}}))}))},i.prototype.query=function(t){var n,i=t.sql,r=t.params;return e.__awaiter(this,void 0,void 0,(function(){var t;return e.__generator(this,(function(e){switch(e.label){case 0:return[4,this.ready];case 1:return e.sent(),[4,this.db.query(i,r).then((function(e){return e.values||[]}))];case 2:return t=e.sent(),this.debug?(console.groupCollapsed("💾 SQL: ".concat(i.trimStart().replaceAll(/\n/g," ").replaceAll(/\s+/g," ").substring(0,30),"...")),console.debug(i,r,t),console.groupEnd(),[4,null===(n=this.sqliteConnection)||void 0===n?void 0:n.saveToStore(this.name)]):[3,4];case 3:e.sent(),e.label=4;case 4:return[2,t]}}))}))},i.prototype.getUrl=function(){return e.__awaiter(this,void 0,void 0,(function(){return e.__generator(this,(function(e){switch(e.label){case 0:return this.isWeb?[2,""]:[4,this.db.getUrl()];case 1:return[2,e.sent().url]}}))}))},i}();exports.default=s;
@@ -1,24 +1,56 @@
1
+ import { BuildColumns, ColumnBuilderBase, ColumnBuilderBaseConfig, ColumnDataType, InferModelFromColumns } from 'drizzle-orm';
2
+ import { SQLiteSelect, SQLiteDelete } from 'drizzle-orm/sqlite-core';
1
3
  import Deferred from '@flumens/utils/dist/deferred';
2
4
  import SQLiteDatabase from './SQLiteDatabase';
3
- import Store, { ModelDTO } from './Store';
5
+ import Store from './Store';
6
+ export declare const jsonb: <TData>(name: string) => import("drizzle-orm/sqlite-core").SQLiteCustomColumnBuilder<{
7
+ name: string;
8
+ dataType: "custom";
9
+ columnType: "SQLiteCustomColumn";
10
+ data: TData;
11
+ driverParam: string;
12
+ enumValues: undefined;
13
+ generated: undefined;
14
+ }>;
4
15
  type SQLiteStoreOptions = {
5
16
  db: SQLiteDatabase;
6
17
  name: string;
7
18
  debug?: boolean;
19
+ columns?: any;
8
20
  };
9
- export default class SQLiteStore implements Store {
10
- private debug;
11
- ready: Deferred<boolean>;
12
- private name;
13
- localForage: any;
14
- db: SQLiteDatabase;
15
- constructor({ db, name, debug }: SQLiteStoreOptions);
16
- init(): Promise<void>;
17
- save(val: ModelDTO): Promise<void>;
18
- find(cid: string | number): Promise<ModelDTO | undefined>;
19
- findAll(): Promise<ModelDTO[]>;
20
- destroy(cid: any): Promise<void>;
21
- destroyAll(): Promise<void>;
21
+ declare const defaultCols: () => {
22
+ id: import("drizzle-orm/sqlite-core").SQLiteTextBuilderInitial<"", [string, ...string[]]>;
23
+ cid: import("drizzle-orm").HasDefault<import("drizzle-orm").NotNull<import("drizzle-orm").IsPrimaryKey<import("drizzle-orm").NotNull<import("drizzle-orm/sqlite-core").SQLiteTextBuilderInitial<"", [string, ...string[]]>>>>>;
24
+ data: import("drizzle-orm").NotNull<import("drizzle-orm/sqlite-core").SQLiteCustomColumnBuilder<{
25
+ name: string;
26
+ dataType: "custom";
27
+ columnType: "SQLiteCustomColumn";
28
+ data: any;
29
+ driverParam: string;
30
+ enumValues: undefined;
31
+ generated: undefined;
32
+ }>>;
33
+ createdAt: import("drizzle-orm").NotNull<import("drizzle-orm/sqlite-core").SQLiteIntegerBuilderInitial<"created_at">>;
34
+ updatedAt: import("drizzle-orm").NotNull<import("drizzle-orm/sqlite-core").SQLiteIntegerBuilderInitial<"updated_at">>;
35
+ syncedAt: import("drizzle-orm/sqlite-core").SQLiteIntegerBuilderInitial<"synced_at">;
36
+ };
37
+ type SelectQueryFn = (q: SQLiteSelect) => SQLiteSelect;
38
+ type DeleteQueryFn = (q: SQLiteDelete) => SQLiteDelete;
39
+ export type ColumnMap<T extends Record<string, ColumnBuilderBase<ColumnBuilderBaseConfig<ColumnDataType, string>, object>>> = InferModelFromColumns<BuildColumns<'', T, 'sqlite'>>;
40
+ type DefaultCols = ColumnMap<ReturnType<typeof defaultCols>>;
41
+ export default class SQLiteStore<T = DefaultCols> implements Store {
42
+ private readonly name;
43
+ private readonly db;
44
+ private readonly debug;
45
+ private readonly table;
46
+ readonly ready: Deferred<boolean>;
47
+ constructor({ db, name, debug, columns, }: SQLiteStoreOptions);
48
+ private init;
49
+ save(values: T): Promise<void>;
50
+ find(q: string | Partial<T> | SelectQueryFn): Promise<T>;
51
+ findAll(q?: SelectQueryFn): Promise<T[]>;
52
+ delete(q: string | Partial<T> | DeleteQueryFn): Promise<void>;
53
+ deleteAll(q?: DeleteQueryFn): Promise<void>;
22
54
  /**
23
55
  * Exports all objects from the store.
24
56
  */
@@ -27,5 +59,6 @@ export default class SQLiteStore implements Store {
27
59
  * Imports objects to store.
28
60
  */
29
61
  import(obj: any): Promise<void>;
62
+ private _getPrimaryCol;
30
63
  }
31
64
  export {};
@@ -1 +1 @@
1
- "use strict";Object.defineProperty(exports,"__esModule",{value:!0});var t=require("tslib"),e=require("@flumens/utils/dist/deferred"),n=require("./utils.js");function r(t){return t&&"object"==typeof t&&"default"in t?t:{default:t}}var a=r(e),d=function(t){var e=t.id,n=t.cid,r=t.data;return{id:e,cid:n,createdAt:t.created_at,updatedAt:t.updated_at,syncedAt:t.synced_at,attrs:JSON.parse(r)}},i=function(){function e(t){var e=t.db,n=t.name,r=t.debug;this.debug=!1,this.ready=new a.default,this.db=e,this.name=n,this.debug=r||this.debug,this.init()}return e.prototype.init=function(){return t.__awaiter(this,void 0,void 0,(function(){var e;return t.__generator(this,(function(t){switch(t.label){case 0:return[4,this.db.ready];case 1:return t.sent(),e="\n CREATE TABLE IF NOT EXISTS ".concat(this.name,"\n (\n id VARCHAR(36),\n cid VARCHAR(36) PRIMARY KEY NOT NULL DEFAULT id,\n data JSONB NOT NULL,\n created_at INTEGER NOT NULL,\n updated_at INTEGER NOT NULL,\n synced_at INTEGER\n );\n "),[4,this.db.query(e)];case 2:return t.sent(),this.ready.resolve(!0),[2]}}))}))},e.prototype.save=function(e){return t.__awaiter(this,void 0,void 0,(function(){var r,a,d,i,s,c,u,o;return t.__generator(this,(function(t){switch(t.label){case 0:return[4,this.ready];case 1:if(t.sent(),!e.cid)throw new Error("Invalid cid passed to store");return r=e.cid,a=e.id,d=e.createdAt,i=e.updatedAt,s=e.syncedAt,c=e.attrs,this.debug?[4,this.find(r)]:[3,3];case 2:u=t.sent(),n.printDiff(u,e,r,this.name),t.label=3;case 3:return o="\n INSERT INTO ".concat(this.name,"(id, cid, data, created_at, updated_at, synced_at)\n VALUES (?, ?, jsonb(?), ?, ?, ?)\n ON CONFLICT(cid) DO UPDATE SET \n id = excluded.id, \n cid = excluded.cid, \n data = excluded.data, \n created_at = \n excluded.created_at, \n updated_at = \n excluded.updated_at, \n synced_at = \n excluded.synced_at;\n "),[4,this.db.query(o,[a||null,r,JSON.stringify(c),new Date(d).getTime(),new Date(i).getTime(),s?new Date(s).getTime():null])];case 4:return t.sent(),[2]}}))}))},e.prototype.find=function(e){return t.__awaiter(this,void 0,void 0,(function(){var n,r;return t.__generator(this,(function(t){switch(t.label){case 0:return[4,this.ready];case 1:if(t.sent(),!e)throw new Error("Invalid cid passed to store");return n="\n SELECT id, cid, json(data) as data, created_at, updated_at, synced_at\n FROM ".concat(this.name,"\n WHERE cid = ?;\n "),[4,this.db.query(n,[e])];case 2:return[2,(r=t.sent()[0])?d(r):void 0]}}))}))},e.prototype.findAll=function(){return t.__awaiter(this,void 0,void 0,(function(){var e;return t.__generator(this,(function(t){switch(t.label){case 0:return[4,this.ready];case 1:return t.sent(),e="\n SELECT id, cid, json(data) as data, created_at, updated_at, synced_at\n FROM ".concat(this.name,";\n "),[4,this.db.query(e)];case 2:return[2,t.sent().map(d)]}}))}))},e.prototype.destroy=function(e){return t.__awaiter(this,void 0,void 0,(function(){var n;return t.__generator(this,(function(t){switch(t.label){case 0:return[4,this.ready];case 1:if(t.sent(),!e)throw new Error("Invalid cid passed to store");return n="DELETE from ".concat(this.name,' where cid = "').concat(e,'"'),[4,this.db.query(n)];case 2:return t.sent(),[2]}}))}))},e.prototype.destroyAll=function(){return t.__awaiter(this,void 0,void 0,(function(){var e;return t.__generator(this,(function(t){switch(t.label){case 0:return[4,this.ready];case 1:return t.sent(),e="DELETE from ".concat(this.name),[4,this.db.query(e)];case 2:return t.sent(),[2]}}))}))},e.prototype.export=function(){return t.__awaiter(this,void 0,void 0,(function(){var e;return t.__generator(this,(function(n){switch(n.label){case 0:return[4,this.ready];case 1:return n.sent(),e=function(e,n){var r;return t.__assign(t.__assign({},e),((r={})[n.cid]=n,r))},[4,this.findAll()];case 2:return[2,n.sent().reduce(e,{})]}}))}))},e.prototype.import=function(e){return t.__awaiter(this,void 0,void 0,(function(){var n,r,a=this;return t.__generator(this,(function(d){switch(d.label){case 0:return[4,this.ready];case 1:return d.sent(),[4,this.destroyAll()];case 2:if(d.sent(),"object"!=typeof e)throw new Error("Invalid obj passed to store");return n=function(e){var n,r,a,d,i,s;if(!e.createdAt&&(null===(n=e.metadata)||void 0===n?void 0:n.createdOn)&&(e.createdAt=null===(r=e.metadata)||void 0===r?void 0:r.createdOn),!e.updatedAt&&(null===(a=e.metadata)||void 0===a?void 0:a.updatedOn)&&(e.updatedAt=null===(d=e.metadata)||void 0===d?void 0:d.updatedOn),!e.syncedAt&&(null===(i=e.metadata)||void 0===i?void 0:i.syncedOn)&&(e.syncedAt=null===(s=e.metadata)||void 0===s?void 0:s.syncedOn),e.samples){var c=e.id,u=e.cid,o=e.createdAt,l=e.updatedAt,h=e.syncedAt,_=t.__rest(e,["id","cid","createdAt","updatedAt","syncedAt"]);e={id:c,cid:u,createdAt:o,updatedAt:l,syncedAt:h,attrs:t.__assign({},_)}}return e},r=Object.entries(e).map((function(t){var e=t[1];return a.save(n(e))})),Promise.all(r),[2]}}))}))},e}();exports.default=i;
1
+ "use strict";Object.defineProperty(exports,"__esModule",{value:!0});var t=require("tslib"),e=require("drizzle-orm"),r=require("drizzle-orm/casing"),n=require("drizzle-orm/sqlite-core"),i=require("@flumens/utils/dist/deferred"),a=require("./utils.js");function s(t){return t&&"object"==typeof t&&"default"in t?t:{default:t}}var o=s(i),u=function(t){return n.customType({dataType:function(){return"jsonb"},toDriver:function(t){return JSON.stringify(t)}})(t)},d=new n.SQLiteSyncDialect,c=function(){function i(t){var e=t.db,r=t.name,i=t.debug,a=t.columns,s=void 0===a?{id:n.text(),cid:n.text().primaryKey().notNull().default("id"),data:u("data").notNull(),createdAt:n.integer("created_at").notNull(),updatedAt:n.integer("updated_at").notNull(),syncedAt:n.integer("synced_at")}:a;this.ready=new o.default,this.db=e,this.name=r,this.debug=i||!1,this.table=n.sqliteTable(this.name,s),this.init()}return i.prototype.init=function(){return t.__awaiter(this,void 0,void 0,(function(){var e,r;return t.__generator(this,(function(t){switch(t.label){case 0:return[4,this.db.ready];case 1:return t.sent(),e=function(t){return"".concat(t.name," ").concat(t.getSQLType()).concat(t.primary?" primary key":"").concat(t.notNull?" not null":"").concat(t.default?" default ".concat(t.default):"")},r=n.getTableConfig(this.table).columns.map(e).join(",\n"),[4,this.db.query({sql:"CREATE TABLE IF NOT EXISTS ".concat(this.name," (").concat(r,");")})];case 2:return t.sent(),this.ready.resolve(!0),[2]}}))}))},i.prototype.save=function(e){return t.__awaiter(this,void 0,void 0,(function(){var r,i,s;return t.__generator(this,(function(t){switch(t.label){case 0:return[4,this.ready];case 1:return t.sent(),this.debug&&e.cid?[4,this.find({cid:e.cid})]:[3,3];case 2:r=t.sent(),a.printDiff(r,e,e.cid,this.name),t.label=3;case 3:return i=new n.SQLiteInsertBuilder(this.table,{},d).values(e),(s=this._getPrimaryCol())&&(i=i.onConflictDoUpdate({target:s,set:e})),[4,this.db.query(i.toSQL())];case 4:return t.sent(),[2]}}))}))},i.prototype.find=function(r){return t.__awaiter(this,void 0,void 0,(function(){var n,i=this;return t.__generator(this,(function(t){switch(t.label){case 0:return[4,this.ready];case 1:return t.sent(),n=function(t){if("function"==typeof r)return r(t).limit(1);if("string"==typeof r){var n=i._getPrimaryCol();if(!n)throw new Error("Primary col is missing");return t.where(e.eq(n,r)).limit(1)}var a=Object.entries(r).map((function(t){var r=t[0],n=t[1];return e.eq(i.table[r],n)}));return t.where(e.and.apply(void 0,a)).limit(1)},[4,this.findAll(n)];case 2:return[2,t.sent()[0]]}}))}))},i.prototype.findAll=function(i){return t.__awaiter(this,void 0,void 0,(function(){var a,s,o,u=this;return t.__generator(this,(function(t){switch(t.label){case 0:return[4,this.ready];case 1:return t.sent(),a=(new n.QueryBuilder).select().from(this.table),i&&(a=i(a)),[4,this.db.query(a.toSQL())];case 2:return s=t.sent(),o=function(t){return Object.entries(t).reduce((function(t,n){var i,a=n[0],s=n[1],o="jsonb"===(null===(i=e.getTableColumns(u.table)[a])||void 0===i?void 0:i.sqlName);return t[r.toCamelCase(a)]=o?JSON.parse(s):s,t}),{})},[2,s.map(o)]}}))}))},i.prototype.delete=function(r){return t.__awaiter(this,void 0,void 0,(function(){var n,i=this;return t.__generator(this,(function(t){switch(t.label){case 0:return[4,this.ready];case 1:return t.sent(),n=function(t){if("function"==typeof r)return r(t);if("string"==typeof r){var n=i._getPrimaryCol();if(!n)throw new Error("Primary col is missing");return t.where(e.eq(n,r))}var a=Object.entries(r).map((function(t){var r=t[0],n=t[1];return e.eq(i.table[r],n)}));return t.where(e.and.apply(void 0,a))},[4,this.deleteAll(n)];case 2:return t.sent(),[2]}}))}))},i.prototype.deleteAll=function(e){return t.__awaiter(this,void 0,void 0,(function(){var r;return t.__generator(this,(function(t){switch(t.label){case 0:return[4,this.ready];case 1:return t.sent(),r=new n.SQLiteDeleteBase(this.table,{},d),e&&(r=e(r)),[4,this.db.query(r.toSQL())];case 2:return t.sent(),[2]}}))}))},i.prototype.export=function(){return t.__awaiter(this,void 0,void 0,(function(){var e;return t.__generator(this,(function(r){switch(r.label){case 0:return[4,this.ready];case 1:return r.sent(),e=function(e,r){var n;return t.__assign(t.__assign({},e),((n={})[r.cid]=r,n))},[4,this.findAll()];case 2:return[2,r.sent().reduce(e,{})]}}))}))},i.prototype.import=function(e){return t.__awaiter(this,void 0,void 0,(function(){var r,n,i=this;return t.__generator(this,(function(a){switch(a.label){case 0:return[4,this.ready];case 1:return a.sent(),[4,this.deleteAll()];case 2:if(a.sent(),"object"!=typeof e)throw new Error("Invalid obj passed to store");return r=function(e){var r,n,i,a,s,o;if(!e.createdAt&&(null===(r=e.metadata)||void 0===r?void 0:r.createdOn)&&(e.createdAt=null===(n=e.metadata)||void 0===n?void 0:n.createdOn),!e.updatedAt&&(null===(i=e.metadata)||void 0===i?void 0:i.updatedOn)&&(e.updatedAt=null===(a=e.metadata)||void 0===a?void 0:a.updatedOn),!e.syncedAt&&(null===(s=e.metadata)||void 0===s?void 0:s.syncedOn)&&(e.syncedAt=null===(o=e.metadata)||void 0===o?void 0:o.syncedOn),e.samples){var u=e.id,d=e.cid,c=e.createdAt,l=e.updatedAt,f=e.syncedAt,h=t.__rest(e,["id","cid","createdAt","updatedAt","syncedAt"]);e={id:u,cid:d,createdAt:c,updatedAt:l,syncedAt:f,data:t.__assign({},h)}}return e},n=Object.entries(e).map((function(t){var e=t[1];return i.save(r(e))})),Promise.all(n),[2]}}))}))},i.prototype._getPrimaryCol=function(){return Object.values(e.getTableColumns(this.table)).find((function(t){return!!t.primary}))},i}();exports.default=c,exports.jsonb=u;
@@ -1,17 +1,9 @@
1
- export type ModelDTO = {
2
- id: string;
3
- cid: string;
4
- createdAt: number;
5
- updatedAt: number;
6
- syncedAt?: number;
7
- attrs: any;
8
- };
9
- export default interface Store {
10
- save(val: ModelDTO): Promise<void>;
11
- find(cid: any): Promise<ModelDTO | undefined>;
12
- findAll(): Promise<ModelDTO[]>;
13
- destroy(cid: any): Promise<void>;
14
- destroyAll(): Promise<void>;
15
- export(): Promise<any>;
16
- import(obj: any): Promise<void>;
1
+ export default abstract class Store<T = any> {
2
+ abstract save(val: T): Promise<void>;
3
+ abstract find(cid: any): Promise<T | undefined>;
4
+ abstract findAll(q?: any): Promise<T[]>;
5
+ abstract delete(cid: any): Promise<void>;
6
+ abstract deleteAll(): Promise<void>;
7
+ abstract export(): Promise<any>;
8
+ abstract import(obj: any): Promise<void>;
17
9
  }
@@ -0,0 +1 @@
1
+ "use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=function(){};exports.default=e;
package/dist/index.js CHANGED
@@ -1 +1 @@
1
- "use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("./DrupalUserModel.js"),r=require("./initStoredSamples.js"),t=require("./Stores/SQLiteDatabase.js"),i=require("./Stores/LocalForageStore.js"),a=require("./Stores/SQLiteStore.js"),o=require("./Model.js"),s=require("./Collection/index.js"),l=require("./Indicia/Media.js"),d=require("./Indicia/Occurrence.js"),u=require("./Indicia/Sample.js");exports.DrupalUserModel=e.default,exports.initStoredSamples=r.default,exports.SQLiteDatabase=t.default,exports.LocalForageStore=i.default,exports.SQLiteStore=a.default,exports.Model=o.default,exports.Collection=s.default,exports.IndiciaMedia=l.default,exports.IndiciaOccurrence=d.default,exports.IndiciaSample=u.default;
1
+ "use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("./DrupalUserModel.js"),r=require("./initStoredSamples.js"),t=require("./Stores/SQLiteDatabase.js"),i=require("./Stores/LocalForageStore.js"),o=require("./Stores/SQLiteStore.js"),a=require("./Stores/Store.js"),s=require("./Model.js"),l=require("./Collection/index.js"),u=require("./Indicia/Media.js"),d=require("./Indicia/Occurrence.js"),S=require("./Indicia/Sample.js");exports.DrupalUserModel=e.default,exports.initStoredSamples=r.default,exports.SQLiteDatabase=t.default,exports.LocalForageStore=i.default,exports.SQLiteStore=o.default,exports.IStore=a.default,exports.Model=s.default,exports.Collection=l.default,exports.IndiciaMedia=u.default,exports.IndiciaOccurrence=d.default,exports.IndiciaSample=S.default;
@@ -1 +1,3 @@
1
- export default function initStoredSamples(store: any, Sample: any): any;
1
+ import IndiciaSample from './Indicia/Sample';
2
+ import Store from './Stores/Store';
3
+ export default function initStoredSamples(store: Store, Sample: typeof IndiciaSample<any, any>): any;
@@ -1 +1 @@
1
- "use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("mobx");exports.default=function(r,t){var n=e.observable([]);e.observe(n,(function(e){e.addedCount&&(e.added[0].collection=n)}));var o=function(e){var r=t.fromJSON(e);n.push(r)};return n.ready=r.findAll().then((function(e){return e.forEach(o)})),n.removeAllSynced=function(){var e=[];return n.forEach((function(r){r.syncedAt&&e.push(r.destroy())})),Promise.all(e)},n.comparator=function(e){return-new Date(e.createdAt).getTime()},n.resetDefaults=function(){var e=n.map((function(e){return e.destroy()}));return Promise.all(e)},n};
1
+ "use strict";Object.defineProperty(exports,"__esModule",{value:!0});var e=require("tslib"),r=require("mobx");exports.default=function(t,n){var o=r.observable([]);return r.observe(o,(function(e){e.addedCount&&(e.added[0].collection=o)})),o.ready=t.findAll().then((function(r){return r.forEach((function(r){var t=n.fromJSON(e.__assign(e.__assign({},r),{attrs:r.data}));o.push(t)}))})),o.removeAllSynced=function(){var e=[];return o.forEach((function(r){r.syncedAt&&e.push(r.destroy())})),Promise.all(e)},o.comparator=function(e){return-new Date(e.createdAt).getTime()},o.resetDefaults=function(){var e=o.map((function(e){return e.destroy()}));return Promise.all(e)},o};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@flumens/models",
3
- "version": "0.0.3",
3
+ "version": "0.1.0",
4
4
  "main": "dist/index.js",
5
5
  "types": "dist/src/index.d.ts",
6
6
  "scripts": {
@@ -18,7 +18,6 @@
18
18
  "@rollup/plugin-json": "6.0.0",
19
19
  "@rollup/plugin-node-resolve": "13.3.0",
20
20
  "@rollup/plugin-typescript": "8.3.2",
21
- "rollup-plugin-copy": "3.5.0",
22
21
  "@trivago/prettier-plugin-sort-imports": "4.3.0",
23
22
  "@types/react": "18.2.38",
24
23
  "@types/react-dom": "18.2.17",
@@ -26,6 +25,7 @@
26
25
  "react": "^18.2.0",
27
26
  "react-dom": "^18.2.0",
28
27
  "rollup-plugin-clear": "2.0.7",
28
+ "rollup-plugin-copy": "3.5.0",
29
29
  "rollup-plugin-terser": "7.0.2",
30
30
  "tslib": "2.6.2",
31
31
  "typescript": "4.9.4"
@@ -34,13 +34,14 @@
34
34
  "@flumens/utils": "*"
35
35
  },
36
36
  "optionalDependencies": {
37
- "localforage": "^1",
38
- "jwt-decode": "^4",
39
- "deep-diff": "^1",
40
37
  "@capacitor-community/sqlite": "^6.0.2",
38
+ "deep-diff": "^1",
39
+ "drizzle-orm": "^0.34.1",
40
+ "jeep-sqlite": "^2.8.0",
41
+ "jwt-decode": "^4",
42
+ "localforage": "^1",
41
43
  "localforage-cordovasqlitedriver": "^1",
42
44
  "mobx-utils": "^6",
43
- "jeep-sqlite": "^2.8.0",
44
45
  "yup": "^1"
45
46
  },
46
47
  "jest": {
@@ -53,6 +54,5 @@
53
54
  "node_modules",
54
55
  "dist"
55
56
  ]
56
- },
57
- "dependencies": {}
57
+ }
58
58
  }