@flumens/models 0.5.0 → 0.5.2

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.
@@ -63,7 +63,7 @@ declare class Media<T extends Attrs = Attrs, S extends Metadata = Metadata> exte
63
63
  setupdatedAtTimestamp(newUpdatedAt: number): void;
64
64
  getSubmission(warehouseMediaNames?: any): any;
65
65
  sync(): Promise<void>;
66
- uploadFile(): Promise<void>;
66
+ uploadFile(force?: boolean): Promise<void>;
67
67
  getFormData(): Promise<unknown[]>;
68
68
  getRemoteURL(): string;
69
69
  /**
@@ -1 +1 @@
1
- "use strict";Object.defineProperty(exports,"__esModule",{value:!0});var t=require("tslib"),e=require("mobx"),r=require("axios"),i=require("@flumens/utils/dist/image"),a=require("../Model.js"),n=require("./helpers.js");function o(t){return t&&"object"==typeof t&&"default"in t?t:{default:t}}var s,d=o(r),u=!!window.cordova||!!(null===(s=null===window||void 0===window?void 0:window.Capacitor)||void 0===s?void 0:s.isNative),h=function(r){function a(i){var a=this;void 0===i&&(i={});var n=i.metadata,o=void 0===n?{}:n,s=t.__rest(i,["metadata"]);return(a=r.call(this,s)||this).remote={synchronising:!1,url:null,headers:{},timeout:12e4},a.debouncedValue=300,a.parent=a.parent,a.metadata=e.observable(o),a}return t.__extends(a,r),a.fromJSON=function(e){var r,i,a;return new this(t.__assign(t.__assign({},e),{createdAt:e.createdAt||(null===(r=e.metadata)||void 0===r?void 0:r.createdOn),updatedAt:e.updatedAt||(null===(i=e.metadata)||void 0===i?void 0:i.updatedOn),syncedAt:e.syncedAt||(null===(a=e.metadata)||void 0===a?void 0:a.syncedOn)}))},a.getDataURI=function(t,e){void 0===e&&(e={});var r=new Promise((function(r,i){if("string"==typeof t){var n=t.replace(/.*\.([a-z]+)$/i,"$1");return"jpg"===n&&(n="jpeg"),void a.resize(t,n,e.width,e.height).then((function(t){var e=t[0],i=t[1];r([i,n,e.width,e.height])}))}if(window.FileReader){var o=new FileReader;o.onload=function(i){var n,o;if(e.width||e.height)a.resize(null===(n=i.target)||void 0===n?void 0:n.result,t.type,e.width,e.height).then((function(e){var i=e[0],a=e[1];r([a,t.type,i.width,i.height])}));else{var s=new window.Image;s.onload=function(){var e,a=t.type.replace(/.*\/([a-z]+)$/i,"$1");r([null===(e=i.target)||void 0===e?void 0:e.result,a,s.width,s.height])},s.src=null===(o=i.target)||void 0===o?void 0:o.result}},o.readAsDataURL(t)}else i(new Error("No File Reader"))}));return r},a.resize=function(t,e,r,i){var a=new Promise((function(a){var n=new window.Image;n.onload=function(){var t,o=n.width,s=n.height,d=null;o/=d=o>s?o/(!r||r>o?o:r):s/(!i||i>s?s:i),s/=d;var u=document.createElement("canvas");u.width=o,u.height=s,null===(t=u.getContext("2d"))||void 0===t||t.drawImage(n,0,0,o,s),a([n,u.toDataURL(e)])},n.src=t}));return a},a.getImageModel=function(e,r,a){return t.__awaiter(this,void 0,void 0,(function(){var n,o,s,d,h,c,l;return t.__generator(this,(function(t){switch(t.label){case 0:if(n=this,!e)throw new Error("File not found while creating image model.");return u?[4,i.createImage(e)]:[3,2];case 1:return h=t.sent(),o=h.width,s=h.height,d=e.split("/").pop(),[3,4];case 2:return[4,n.getDataURI(e,{width:2e3,height:2e3})];case 3:l=t.sent(),d=l[0],o=l[2],s=l[3],t.label=4;case 4:return c=new n({attrs:{data:d,type:"jpeg",width:o,height:s,path:r}}),a?[3,6]:[4,c.addThumbnail()];case 5:t.sent(),t.label=6;case 6:return[2,c]}}))}))},a.prototype.toJSON=function(){var i=r.prototype.toJSON.call(this);return JSON.parse(JSON.stringify(t.__assign(t.__assign({},i),{metadata:e.toJS(this.metadata)||{}})))},a.prototype.setupdatedAtTimestamp=function(t){var e;r.prototype.setUpdatedAtTimestamp.call(this,t),null===(e=this.parent)||void 0===e||e.setUpdatedAtTimestamp(t)},a.prototype.getSubmission=function(t){void 0===t&&(t={});var e=t[this.cid];if(!this.id&&!e)throw new Error("Image ID or queued ID is missing.");var r={values:{caption:this.attrs.caption}};return this.id?r.values.id=this.id:r.values.queued=e.name,r},a.prototype.sync=function(){return t.__awaiter(this,void 0,void 0,(function(){return t.__generator(this,(function(t){return[2,this.parent?this.parent.sync():r.prototype.sync.call(this)]}))}))},a.prototype.uploadFile=function(){var e;return t.__awaiter(this,void 0,void 0,(function(){var r,i,a,n,o,s;return t.__generator(this,(function(t){switch(t.label){case 0:if(this.id)throw new Error("A file of a media on the remote cannot be uploaded again.");return[4,this.getFormData()];case 1:return r=t.sent(),i=new FormData,(s=i).append.apply(s,r),"function"!=typeof this.remote.headers?[3,3]:[4,this.remote.headers()];case 2:return n=t.sent(),[3,4];case 3:n=this.remote.headers,t.label=4;case 4:return a=n,[4,d.default.post("".concat(this.remote.url,"/media-queue"),i,{headers:a,timeout:12e4})];case 5:if(o=t.sent(),!(null===(e=o.data[this.cid])||void 0===e?void 0:e.name))throw new Error("New remote media name was not be found.");return this.attrs.queued=o.data[this.cid].name,this.syncedAt=(new Date).getTime(),[2]}}))}))},a.prototype.getFormData=function(){return t.__awaiter(this,void 0,void 0,(function(){var e,r,i,a,o,s,d;return t.__generator(this,(function(t){switch(t.label){case 0:return e=this.attrs.type,r=e,i=e,(null==e?void 0:e.match(/image.*/))?(d=e.split("/"),r=d[1]):i="image/".concat(i),a=this.getURL(),[4,n.getBlobFromURL(a,i)];case 1:return o=t.sent(),[2,[s=this.cid,o,"".concat(s,".").concat(r)]]}}))}))},a.prototype.getRemoteURL=function(){if(!this.remote.url)throw new Error("No remote url was set.");if(!this.attrs.queued&&!this.attrs.path)throw new Error("No media queued or path attribute.");var t=this.remote.url.replace("/index.php/services/rest","");return this.attrs.queued?"".concat(t,"/upload-queue/").concat(this.attrs.queued):"".concat(t,"/upload/").concat(this.attrs.path)},a.prototype.getURL=function(){return this.attrs.data},a.prototype.resize=function(t,e){var r=this,i=this,n=new Promise((function(n,o){a.resize(r.getURL(),r.attrs.type,t,e).then((function(t){var e=t[0],r=t[1];i.attrs.data=r,n([e,r])})).catch(o)}));return n},a.prototype.addThumbnail=function(t){var e=this,r=this;return new Promise((function(t,i){/^data:/i.test(e.getURL())?a.resize(e.getURL(),e.attrs.type,100,100).then((function(e){var i=e[1];r.attrs.thumbnail=i,t()})).catch(i):a.getDataURI(e.getURL(),{width:100,height:100}).then((function(e){r.attrs.thumbnail=e[0],t()})).catch(i)}))},a.prototype.isUploaded=function(){if(!this.parent)throw new Error("No media parent to return disabled status.");return this.parent.isUploaded()},a.prototype.isDisabled=function(){return this.isUploaded()},a}(a.default);exports.default=h;
1
+ "use strict";Object.defineProperty(exports,"__esModule",{value:!0});var t=require("tslib"),e=require("mobx"),r=require("axios"),i=require("@flumens/utils/dist/image"),a=require("../Model.js"),n=require("./helpers.js");function o(t){return t&&"object"==typeof t&&"default"in t?t:{default:t}}var s,d=o(r),u=!!window.cordova||!!(null===(s=null===window||void 0===window?void 0:window.Capacitor)||void 0===s?void 0:s.isNative),h=function(r){function a(i){var a=this;void 0===i&&(i={});var n=i.metadata,o=void 0===n?{}:n,s=t.__rest(i,["metadata"]);return(a=r.call(this,s)||this).remote={synchronising:!1,url:null,headers:{},timeout:12e4},a.debouncedValue=300,a.parent=a.parent,a.metadata=e.observable(o),a}return t.__extends(a,r),a.fromJSON=function(e){var r,i,a;return new this(t.__assign(t.__assign({},e),{createdAt:e.createdAt||(null===(r=e.metadata)||void 0===r?void 0:r.createdOn),updatedAt:e.updatedAt||(null===(i=e.metadata)||void 0===i?void 0:i.updatedOn),syncedAt:e.syncedAt||(null===(a=e.metadata)||void 0===a?void 0:a.syncedOn)}))},a.getDataURI=function(t,e){void 0===e&&(e={});var r=new Promise((function(r,i){if("string"==typeof t){var n=t.replace(/.*\.([a-z]+)$/i,"$1");return"jpg"===n&&(n="jpeg"),void a.resize(t,n,e.width,e.height).then((function(t){var e=t[0],i=t[1];r([i,n,e.width,e.height])}))}if(window.FileReader){var o=new FileReader;o.onload=function(i){var n,o;if(e.width||e.height)a.resize(null===(n=i.target)||void 0===n?void 0:n.result,t.type,e.width,e.height).then((function(e){var i=e[0],a=e[1];r([a,t.type,i.width,i.height])}));else{var s=new window.Image;s.onload=function(){var e,a=t.type.replace(/.*\/([a-z]+)$/i,"$1");r([null===(e=i.target)||void 0===e?void 0:e.result,a,s.width,s.height])},s.src=null===(o=i.target)||void 0===o?void 0:o.result}},o.readAsDataURL(t)}else i(new Error("No File Reader"))}));return r},a.resize=function(t,e,r,i){var a=new Promise((function(a){var n=new window.Image;n.onload=function(){var t,o=n.width,s=n.height,d=null;o/=d=o>s?o/(!r||r>o?o:r):s/(!i||i>s?s:i),s/=d;var u=document.createElement("canvas");u.width=o,u.height=s,null===(t=u.getContext("2d"))||void 0===t||t.drawImage(n,0,0,o,s),a([n,u.toDataURL(e)])},n.src=t}));return a},a.getImageModel=function(e,r,a){return t.__awaiter(this,void 0,void 0,(function(){var n,o,s,d,h,c,l;return t.__generator(this,(function(t){switch(t.label){case 0:if(n=this,!e)throw new Error("File not found while creating image model.");return u?[4,i.createImage(e)]:[3,2];case 1:return h=t.sent(),o=h.width,s=h.height,d=e.split("/").pop(),[3,4];case 2:return[4,n.getDataURI(e,{width:2e3,height:2e3})];case 3:l=t.sent(),d=l[0],o=l[2],s=l[3],t.label=4;case 4:return c=new n({attrs:{data:d,type:"jpeg",width:o,height:s,path:r}}),a?[3,6]:[4,c.addThumbnail()];case 5:t.sent(),t.label=6;case 6:return[2,c]}}))}))},a.prototype.toJSON=function(){var i=r.prototype.toJSON.call(this);return JSON.parse(JSON.stringify(t.__assign(t.__assign({},i),{metadata:e.toJS(this.metadata)||{}})))},a.prototype.setupdatedAtTimestamp=function(t){var e;r.prototype.setUpdatedAtTimestamp.call(this,t),null===(e=this.parent)||void 0===e||e.setUpdatedAtTimestamp(t)},a.prototype.getSubmission=function(t){void 0===t&&(t={});var e=t[this.cid];if(!this.id&&!e)throw new Error("Image ID or queued ID is missing.");var r={values:{caption:this.attrs.caption}};return this.id?r.values.id=this.id:r.values.queued=e.name,r},a.prototype.sync=function(){return t.__awaiter(this,void 0,void 0,(function(){return t.__generator(this,(function(t){return[2,this.parent?this.parent.sync():r.prototype.sync.call(this)]}))}))},a.prototype.uploadFile=function(e){var r;return void 0===e&&(e=!1),t.__awaiter(this,void 0,void 0,(function(){var i,a,n,o,s,u,h,c;return t.__generator(this,(function(t){switch(t.label){case 0:if(this.id)throw new Error("A file of a media on the remote cannot be uploaded again.");return i=Date.now()-432e5,a=this.syncedAt>i&&this.attrs.queued,!e&&a?[2]:[4,this.getFormData()];case 1:return n=t.sent(),o=new FormData,(c=o).append.apply(c,n),"function"!=typeof this.remote.headers?[3,3]:[4,this.remote.headers()];case 2:return u=t.sent(),[3,4];case 3:u=this.remote.headers,t.label=4;case 4:return s=u,[4,d.default.post("".concat(this.remote.url,"/media-queue"),o,{headers:s,timeout:12e4})];case 5:if(h=t.sent(),!(null===(r=h.data[this.cid])||void 0===r?void 0:r.name))throw new Error("New remote media name was not be found.");return this.attrs.queued=h.data[this.cid].name,this.syncedAt=(new Date).getTime(),[2]}}))}))},a.prototype.getFormData=function(){return t.__awaiter(this,void 0,void 0,(function(){var e,r,i,a,o,s,d;return t.__generator(this,(function(t){switch(t.label){case 0:return e=this.attrs.type,r=e,i=e,(null==e?void 0:e.match(/image.*/))?(d=e.split("/"),r=d[1]):i="image/".concat(i),a=this.getURL(),[4,n.getBlobFromURL(a,i)];case 1:return o=t.sent(),[2,[s=this.cid,o,"".concat(s,".").concat(r)]]}}))}))},a.prototype.getRemoteURL=function(){if(!this.remote.url)throw new Error("No remote url was set.");if(!this.attrs.queued&&!this.attrs.path)throw new Error("No media queued or path attribute.");var t=this.remote.url.replace("/index.php/services/rest","");return this.attrs.queued?"".concat(t,"/upload-queue/").concat(this.attrs.queued):"".concat(t,"/upload/").concat(this.attrs.path)},a.prototype.getURL=function(){return this.attrs.data},a.prototype.resize=function(t,e){var r=this,i=this,n=new Promise((function(n,o){a.resize(r.getURL(),r.attrs.type,t,e).then((function(t){var e=t[0],r=t[1];i.attrs.data=r,n([e,r])})).catch(o)}));return n},a.prototype.addThumbnail=function(t){var e=this,r=this;return new Promise((function(t,i){/^data:/i.test(e.getURL())?a.resize(e.getURL(),e.attrs.type,100,100).then((function(e){var i=e[1];r.attrs.thumbnail=i,t()})).catch(i):a.getDataURI(e.getURL(),{width:100,height:100}).then((function(e){r.attrs.thumbnail=e[0],t()})).catch(i)}))},a.prototype.isUploaded=function(){if(!this.parent)throw new Error("No media parent to return disabled status.");return this.parent.isUploaded()},a.prototype.isDisabled=function(){return this.isUploaded()},a}(a.default);exports.default=h;
@@ -19,7 +19,7 @@ type SQLiteStoreOptions = {
19
19
  debug?: boolean;
20
20
  columns?: any;
21
21
  };
22
- declare const defaultCols: () => {
22
+ export declare const defaultCols: () => {
23
23
  id: import("drizzle-orm/sqlite-core").SQLiteTextBuilderInitial<"", [string, ...string[]]>;
24
24
  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[]]>>>>>;
25
25
  data: import("drizzle-orm").NotNull<import("drizzle-orm/sqlite-core").SQLiteCustomColumnBuilder<{
@@ -1 +1 @@
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);function u(r,n){var i=e.getTableColumns(r);return Object.entries(i).filter((function(t){var e=t[0];return!n.includes(e)})).reduce((function(r,n){var i,a=n[0],s=n[1];return t.__assign(t.__assign({},r),((i={})[a]=e.sql.raw("excluded.".concat(s.name)),i))}),{})}var c=function(t){return n.customType({dataType:function(){return"jsonb"},toDriver:function(t){return JSON.stringify(t)}})(t)},d=new n.SQLiteSyncDialect,l=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:c("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(),(Array.isArray(e)?e.length:e)?this.debug&&e.cid?[4,this.find({cid:e.cid})]:[3,3]:[2];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:u(this.table,[])})),[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,c=e.cid,d=e.createdAt,l=e.updatedAt,f=e.syncedAt,h=t.__rest(e,["id","cid","createdAt","updatedAt","syncedAt"]);e={id:u,cid:c,createdAt:d,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.conflictUpdateAllExcept=u,exports.default=l,exports.jsonb=c;
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);function u(r,n){var i=e.getTableColumns(r);return Object.entries(i).filter((function(t){var e=t[0];return!n.includes(e)})).reduce((function(r,n){var i,a=n[0],s=n[1];return t.__assign(t.__assign({},r),((i={})[a]=e.sql.raw("excluded.".concat(s.name)),i))}),{})}var c=function(t){return n.customType({dataType:function(){return"jsonb"},toDriver:function(t){return JSON.stringify(t)}})(t)},d=new n.SQLiteSyncDialect,l=function(){return{id:n.text(),cid:n.text().primaryKey().notNull().default("id"),data:c("data").notNull(),createdAt:n.integer("created_at").notNull(),updatedAt:n.integer("updated_at").notNull(),syncedAt:n.integer("synced_at")}},f=function(){function i(t){var e=t.db,r=t.name,i=t.debug,a=t.columns,s=void 0===a?l():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(),(Array.isArray(e)?e.length:e)?this.debug&&e.cid?[4,this.find({cid:e.cid})]:[3,3]:[2];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:u(this.table,[])})),[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,c=e.cid,d=e.createdAt,l=e.updatedAt,f=e.syncedAt,h=t.__rest(e,["id","cid","createdAt","updatedAt","syncedAt"]);e={id:u,cid:c,createdAt:d,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.conflictUpdateAllExcept=u,exports.default=f,exports.defaultCols=l,exports.jsonb=c;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@flumens/models",
3
- "version": "0.5.0",
3
+ "version": "0.5.2",
4
4
  "main": "dist/index.js",
5
5
  "types": "dist/src/index.d.ts",
6
6
  "scripts": {