UNPKG

dexie-relationships

Version:
3 lines (2 loc) 2.96 kB
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t(require("dexie")):"function"==typeof define&&define.amd?define(["dexie"],t):e.dexieRelationships=t(e.Dexie)}(this,function(e){"use strict";function t(e){return null!=e&&("string"==typeof e||"number"==typeof e||e instanceof Date||Array.isArray(e)&&e.every(t))}e="default"in e?e.default:e;var n=function(e){this.schema=e};n.prototype.getForeignKeys=function(){var e=this,t={};return Object.keys(this.schema).forEach(function(n){var r=e.schema[n].split(",");t[n]=r.filter(function(e){return-1!==e.indexOf("->")}).map(function(e){var t=e.split("->").map(function(e){return e.trim()}),n=t[0],r=t[1];return{index:n,targetTable:r.split(".")[0],targetIndex:r.split(".")[1]}})}),t},n.prototype.getCleanedSchema=function(){var e=this,t={};return Object.keys(this.schema).forEach(function(n){var r=e.schema[n].split(",");t[n]=r.map(function(e){return e.split("->")[0].trim()}).join(",")}),t};var r=function(r){var i=e.Promise;r.Table.prototype.with=function(e){return this.toCollection().with(e)},r.Collection.prototype.with=function(e){var n=this,o=this._ctx.table.name,a=r._allTables,f=[];return Object.keys(e).forEach(function(t){var r=e[t],i=n._ctx.table.schema.idxByName[r];if(i&&i.hasOwnProperty("foreignKey")){var u=i;f.push({column:t,index:u.foreignKey.targetIndex,tableName:u.foreignKey.targetTable,targetIndex:u.foreignKey.index,oneToOne:!0})}else{var c=r;if(!a.hasOwnProperty(c))throw new Error("Relationship table "+c+" doesn't exist.");if(!a[c].schema.hasOwnProperty("foreignKeys"))throw new Error("Relationship table "+c+" doesn't have foreign keys set.");var s=a[c].schema.foreignKeys.filter(function(e){return e.targetTable===o});s.length>0&&f.push({column:t,index:s[0].index,tableName:c,targetIndex:s[0].targetIndex})}}),this.toArray().then(function(e){var n=f.map(function(n){var r=n.tableName,i=e.map(function(e){return e[n.targetIndex]}).filter(t);return a[r].where(n.index).anyOf(i)}),r=n.map(function(e){return e.toArray()});return i.all(r).then(function(t){f.forEach(function(n,r){var i=n.tableName,a=t[r],f=n.targetIndex,u=n.index,c=n.column,s={};a.forEach(function(e){var t=e[u];n.oneToOne?s[t]=e:(s[t]=s[t]||[]).push(e)}),e.forEach(function(e){var t=e[f],n=s[t]||[];if(null!==t&&void 0!==t&&!n)throw new Error("Could not lookup foreign key where "+i+"."+u+" == "+o+"."+c+". The content of the failing key was: "+JSON.stringify(t)+".");Object.defineProperty(e,c,{value:n,enumerable:!1,configurable:!0,writable:!0})})})}).then(function(){return e})})},r.Version.prototype._parseStoresSpec=e.override(r.Version.prototype._parseStoresSpec,function(e){return function(t,r){var i=new n(t),o=i.getForeignKeys(),a=e.call(this,i.getCleanedSchema(),r);return Object.keys(r).forEach(function(e){o.hasOwnProperty(e)&&(r[e].foreignKeys=o[e],o[e].forEach(function(t){r[e].idxByName[t.index].foreignKey=t}))}),a}})};return r.default=r,r}); //# sourceMappingURL=index.min.js.map