From 17949393010b476d01e1c4e5e2d46288d93e3318 Mon Sep 17 00:00:00 2001 From: "m.broersen" Date: Wed, 3 Jan 2024 20:51:53 +0100 Subject: [PATCH] build: rebuild release --- dist/jeloquent.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dist/jeloquent.js b/dist/jeloquent.js index d6764ad..b815a61 100644 --- a/dist/jeloquent.js +++ b/dist/jeloquent.js @@ -1 +1 @@ -var z=Object.defineProperty;var s=(n,e)=>z(n,"name",{value:e,configurable:!0});var c=class{static{s(this,"Field")}constructor(e,t=!1){this._isPrimary=t,this.$fieldValue=null,this.$previousValue=void 0,this.$originalValue=void 0,this.$parent=null,this.$name=e}get isDirty(){return this.$fieldValue!=this.$previousValue}get isPrimary(){return this._isPrimary}get name(){return this.$name}get originalValue(){return this.$originalValue}get previousValue(){return this.$previousValue}get value(){return this.$fieldValue}set _value(e){this.$originalValue===void 0&&(this.$originalValue=JSON.parse(JSON.stringify(this.value??e))),this.$previousValue=JSON.parse(JSON.stringify(this.value)),this.$fieldValue=e}set value(e){this.$previousValue===void 0&&(this.$previousValue=JSON.parse(JSON.stringify(this.value??e))),this.$originalValue===void 0&&(this.$originalValue=JSON.parse(JSON.stringify(this.value??e))),this.$previousValue=JSON.parse(JSON.stringify(this.value)),this.$fieldValue=e}resetDirty(){this.$originalValue=JSON.parse(JSON.stringify(this.$fieldValue)),this.$previousValue=JSON.parse(JSON.stringify(this.$fieldValue))}setName(){return this}setup(e){return this.$parent=e,this.setName().setParentProperties()}tableSetup(e){console.info(e.name)}toJson(){let e={};return e[this.$name]=this.value,JSON.parse(JSON.stringify(e))}setParentProperties(){return this}};var g=class extends c{static{s(this,"ForeignKey")}constructor(e,t=null){super(e),this._foreignKey=e??t}get foreignKey(){return this._foreignKey}get value(){return this.$fieldValue}set value(e){this.$fieldValue=e}setRelation(e){return this.relation=e,this}tableSetup(e){e.registerIndex(this.foreignKey)}};var l=class extends c{static{s(this,"Relation")}constructor(e,t=null,r=null){let i=r??e.snakeCaseClassName;super(i),this.model=e,this.foreignKey=t}set _value(e){Array.isArray(e)||(e=[e]),e.forEach(t=>{this.model.insert(t)})}getRelationalFields(){return[new g(this.foreignKey).setRelation(this)]}tableSetup(e){e.registerIndex(this.foreignKey)}};var y=class extends l{static{s(this,"HasManyThrough")}constructor(e,t,r=null,i=null){super(e,r),this.model=e,this.throughModel=t,this.localKey=i??"id"}get indexName(){return`${this._lcThroughModelClassName}.${this._lcParentClassName}_id`}get originalValue(){return this.getValueByParentKey("originalPrimaryKey")}get value(){return this.getValueByParentKey("primaryKey")}getRelationalFields(){return[]}setName(){return this._lcThroughModelClassName=this.throughModel.snakeCaseClassName,this._lcModelClassName=this.model.snakeCaseClassName,this._lcParentClassName=this.$parent.snakeCaseClassName,this.foreignKey=`${this._lcThroughModelClassName}_id`,this.$name=`${this._lcModelClassName}s`,this}tableSetup(){this.model.registerIndex(this.indexName)}getValueByParentKey(e){let t=this.model.getIndexByKey(this.indexName);return globalThis.Store.database().find(this.model.className,[...t.get(`${this.$parent[e]}`)?.values()??[]])}};var N=class extends l{static{s(this,"HasMany")}constructor(e,t=null,r=null){super(e,t),this.localKey=r??"id"}get count(){return globalThis.Store.database().indexes(this.model.className).get(this.foreignKey).get(this.$parent.primaryKey)?.size??0}get originalValue(){return this.getValueByParentKey("originalPrimaryKey")}get value(){return this.getValueByParentKey("primaryKey")}getRelationalFields(){return[]}setName(){let e=this.$parent.snakeCaseClassName,t=this.model.snakeCaseClassName;return this.foreignKey=`${e}_id`,this.$name=`${t}s`,this}getValueByParentKey(e){let t=this.model.getIndexByKey(this.foreignKey);return globalThis.Store.database().find(this.model.className,[...t.get(`${this.$parent[e]}`)?.values()??[]])}setParentProperties(){return super.setParentProperties(),Object.defineProperty(this.$parent,`${this.name}Count`,{get:()=>this.count}),Object.defineProperty(this.$parent,`has${this.model.className}s`,{get:()=>this.count>0}),this}};var _=class extends l{static{s(this,"BelongsTo")}constructor(e,t=null,r=null){super(e,t??`${e.snakeCaseClassName}_id`,r)}get originalValue(){return this.model.find(this.$parent[`original_${this.foreignKey}`])}get value(){return this.model.find(this.$parent[this.foreignKey])}setName(){let e=this.model.snakeCaseClassName;return this.$name=this.$name??`${e}`,this}setParentProperties(){super.setParentProperties();let e="";for(let t of this.$name.split("_"))e+=t[0].toUpperCase()+t.slice(1);return Object.defineProperty(this.$parent,`has${e}`,{get:()=>this.value!==null}),this}};var C=class extends l{static{s(this,"MorphOne")}constructor(e){super(e)}get originalValue(){return this.getValueByParentKey("originalPrimaryKey")}get value(){return this.getValueByParentKey("primaryKey")}getRelationalFields(){return[]}getValueByParentKey(e){let t=this.$parent.className,r=this.$parent[e],i=`${this.name}_id`,a=`${this.name}_type`,u={};return u[i]=r,u[a]=t,this.model.find(u)}};var v=class extends l{static{s(this,"HasOne")}constructor(e){super(e)}get originalValue(){return this.getValueByParentKey("originalPrimaryKey")}get value(){return this.getValueByParentKey("primaryKey")}getRelationalFields(){return[]}setName(){return this.foreignKey=`${this.$parent.snakeCaseClassName}_id`,this}getValueByParentKey(e){let t=this.model.getIndexByKey(this.foreignKey);return globalThis.Store.database().find(this.model.className,[...t.get(`${this.$parent[e]}`)?.values()??[]]).first()}};var w=class extends l{static{s(this,"HasOneThrough")}constructor(e,t,r=null,i="id"){super(e,r),this.throughModel=t,this.localKey=i}get indexName(){return`${this._lcThroughModelClassName}.${this._lcParentClassName}_id`}get originalValue(){return this.$parent[`original_${this._lcThroughModelClassName}`][`original_${this._lcModelClassName}`]??null}get value(){return this.$parent[this._lcThroughModelClassName][this._lcModelClassName]??null}getRelationalFields(){return[]}setName(){return this._lcThroughModelClassName=this.throughModel.snakeCaseClassName,this._lcModelClassName=this.model.snakeCaseClassName,this._lcParentClassName=this.$parent.snakeCaseClassName,this.foreignKey=`${this._lcThroughModelClassName}_id`,this.$name=`${this._lcModelClassName}`,this}};var b=class extends c{static{s(this,"MorphTo")}constructor(e){super(e)}get value(){let e=this.$parent.snakeCaseClassName,t=this.$parent[`${e}_type`],r=this.$parent[`${e}_id`];return globalThis.Store.classInstances[t].constructor.find(r)}set _value(e){Array.isArray(e)||(e=[e]);let t=this.$parent.snakeCaseClassName,r=`${t}_type`,i=`${t}_id`;for(let a of e)a.id=a[i],globalThis.Store.classInstances[a[r]].constructor.insert(a)}};var h=class{static{s(this,"Index")}constructor(){this._indexes=new Map,this.indexedFields=new Set,this.splitIndexNames=new Map}get indexes(){return this._indexes}static add(e,t){globalThis.Store.database().addIndex(e.className,t.foreignKey,t.value,e.primaryKey)}static addIndex(e,t){this.add(e,t)}static register(e,t){globalThis.Store.database().registerIndex(e.className,t)}static registerIndex(e,t){this.register(e,t)}static remove(e,t){globalThis.Store.database().removeIndex(e.className,t.foreignKey,t.previousValue,e.primaryKey)}static removeIndex(e,t){this.remove(e,t)}static removeTmpIdFromIndex(e){let t=e.className;e.dirtyFields.filter(r=>r.constructor.name==="ForeignKey").forEach(r=>{globalThis.Store.database().removeIndex(t,r.name,r.originalValue,e._tmpId)})}addValue(e,t,r){if(!this._indexes.has(e)||r===null)return;if(!this.index(e).has(`${t}`)){this.registerLookUpKey(e,t,r);return}let a=this.indexLookUpKey(e,t);a.has(`${r}`)||a.add(`${r}`)}addValueByModel(e){for(let[t]of this._indexes)this.addValue(t,this.getLookUpValue(e,t),e.primaryKey)}getIndexByKey(e){return this.index(e)}getLookUpValue(e,t){let r=this.splitIndexNames.get(t),i=null,a=e;for(let u of r){if(a[`original_${u}`]===null)break;a=a[`original_${u}`],i=a}return`${i}`}register(e){this._indexes.has(e)||(this.indexedFields.add(e),this.splitIndexNames.set(e,e.split(".")),this._indexes.set(e,new Map))}registerIndex(e){this.register(e)}registerLookUpKey(e,t,r){this.index(e).set(`${t}`,new Set([`${r}`]))}removeValue(e,t,r){this.indexLookUpKey(e,t).delete(`${r}`)}removeValueByModel(e){for(let[t]of this._indexes)this.removeValue(t,this.getLookUpValue(e,t),e.primaryKey)}truncate(){for(let e in this._indexes)this.index(e).clear()}unregisterLookUpKey(e,t){this.index(e).delete(`${t}`)}index(e){return this._indexes.get(e)}indexLookUpKey(e,t){return this.index(e).get(`${t}`)}};function K(n,e){return n!=e}s(K,"default");function O(n,e){return n==e}s(O,"default");function T(n,e){return n>e}s(T,"default");function A(n,e){return n>=e}s(A,"default");function k(n,e){return n=":A,"<=":V,">":T,"<":k}}first(){return this[0]??null}last(){return this.slice(-1)[0]??null}merge(e){return this.push(...e),this}pluck(e,t=""){let r=e.split(".");if(t){let a=t.split("."),u={};for(let f in this)u[this._getRowFieldResult(this[f],a)]=this._getRowFieldResult(this[f],r);return u}let i=[];for(let a in this)i.push(this._getRowFieldResult(this[a],r));return i}random(){return this[Math.round((this.length-1)*Math.random())]}toJSON(){return this.toObject()}toObject(){return this.map(e=>e?.toObject?.()??e)}unique(e){let t={};for(let r in this)t[this[r][e]]=this[r];return new n(...Object.values(t))}where(e,t,r=null){if(r=r??t,t=t===r?"==":t,!Object.prototype.hasOwnProperty.call(this.operators,t))throw new Error("Invalid comparison operator used");return this.whereIfFunction(e,(i,a)=>this.operators[t](a[i],r))}whereBetween(e,t){return this.whereIfFunction(e,(r,i)=>{let a=i[r];return a>=t[0]&&a<=t[1]})}whereIfFunction(e,t){let r=new n;for(let i in this)t(e,this[i])&&r.push(this[i]);return r}whereIn(e,t){return this.whereIfFunction(e,(r,i)=>t.includes(i[r]))}whereInstanceOf(e){return this.whereIfFunction(null,(t,r)=>r instanceof e)}whereNotBetween(e,t){return this.whereIfFunction(e,(r,i)=>{let a=i[r];return!(a>=t[0]&&a<=t[1])})}whereNotIn(e,t){return this.whereIfFunction(e,(r,i)=>!t.includes(i[r]))}whereNotInstanceOf(e){return this.whereIfFunction(null,(t,r)=>!(r instanceof e))}whereNotNull(e){return this.whereIfFunction(e,(t,r)=>r[t]!==null)}whereNull(e){return this.whereIfFunction(e,(t,r)=>r[t]===null)}_getRowFieldResult(e,t){let r=e[t[0]]??null;for(let i=1;it?.toObject(!0)??t)??[]]}s(ee,"arrayToObjects");function J(n){let e=[...n];return n.forEach((t,r)=>{t instanceof l&&e.splice(r,0,...t.getRelationalFields())}),e}s(J,"addRelationFieldsToList");function U(n,e){e.forEach(t=>{t.setup(L(n)),n._originalFields.set(t.name,t)}),n.numberOfFields=n.originalFields.length}s(U,"setFields");function q(n,e){n.originalFields.forEach(t=>{t instanceof g&&t.tableSetup(e),t instanceof y&&t.tableSetup()})}s(q,"setupTable");function L(n){return new Proxy(n,{construct(e,t,r){return Reflect.construct(e,t,r)},get(e,t){return Reflect.has(e,t)?Reflect.get(e,t):typeof t!="string"?null:e._originalFields.has(t)?e._originalFields.get(t).value:t.startsWith("original_")&&!Reflect.has(e,t.replace("original_",""))&&e._originalFields.has(t.replace("original_",""))?e._originalFields.get(t.replace("original_","")).originalValue:null},set(e,t,r){if(Reflect.has(e,t))return Reflect.set(e,t,r);if(typeof t!="string")return!1;if(e._originalFields.has(t))return e._originalFields.get(t).value=r,!0;if(t.startsWith("_")&&e._originalFields.has(t.replace("_",""))){let i=e._originalFields.get(t.replace("_",""));return i._value=r,!0}return!0}})}s(L,"modelProxy");var M=class{static{s(this,"Model")}constructor(e=[]){return this.setFields(J(e)),this._tmpId=`_${++globalThis.Store.numberOfModelCreated}`,L(this)}static get className(){return this.name}static get kebabCaseClassName(){return this.kebabCaseName??=R(this.className)}static get snakeCaseClassName(){return this.snakeCaseName??=j(this.className)}get className(){return this.constructor.className}get dirtyFieldNames(){return this.dirtyFields.map(e=>e.name)}get dirtyFields(){return this.originalFields.filter(e=>e.isDirty)}get kebabCaseClassName(){return this.constructor.kebabCaseClassName}get originalFields(){return[...this._originalFields.values()]}get originalPrimaryKey(){return this.primaryFields.reduce((e,t,r)=>r>0?`${e}-${t.originalValue}`:t.originalValue,"")??this._tmpId??null}get originalValues(){return this.originalFields.reduce((e,t)=>(t.originalValue!==void 0&&(e[t.name]=t.originalValue),e),{})}get primaryFields(){return this._primaryFields??=this.originalFields.filter(e=>e.isPrimary)}get primaryKey(){return this.primaryFields.reduce((e,t,r)=>r>0?`${e}-${t.value}`:t.value===null?t.value:`${t.value}`,"")??this._tmpId??null}get primaryKeyName(){return this.originalFields.filter(e=>e.isPrimary).map(e=>e.name)}get snakeCaseClassName(){return this.constructor.snakeCaseClassName}static aSyncInsert(e){return new Promise(t=>{queueMicrotask(()=>{t(this.insert(e))})})}static aSyncUpdate(e){return new Promise(t=>{queueMicrotask(()=>{t(this.update(e))})})}static all(){return globalThis.Store.database().all(this.className)}static delete(e){globalThis.Store.database().delete(this.className,e)}static find(e){return globalThis.Store.database().find(this.className,e)}static getIndexByKey(e){return globalThis.Store.database().getIndexByKey(this.className,e)}static getInstance(){let e=globalThis.Store.classInstances[this.className]??(globalThis.Store.classInstances[this.className]=new this),t=e.originalFields.reduce((r,i)=>(r.push(Object.assign(Object.create(Object.getPrototypeOf(i)),i)),r),[]);return L(Object.create(Object.getPrototypeOf(e)).setFields(t))}static ids(){return globalThis.Store.database().ids(this.className)}static insert(e){let t=Array.isArray(e)?e:[e],r=t.length,i=new d;for(let a=0;a0}registerIndex(e){h.register(this,e)}resetDirty(){this.originalFields.filter(e=>!(e instanceof l)).forEach(e=>{e.resetDirty()})}save(){globalThis.Store.database().save(this.className,this)}setFields(e){return this._originalFields=new Map,U(this,e),this}tableSetup(e){q(this,e)}toJSON(){return this.toObject()}toObject(e=!1){return Q(this,e)}};var I=class n{static{s(this,"Table")}constructor(e){this.setup(e.getInstance())}get ids(){return[...this._models.keys()]}get indexes(){return this._index.indexes}get models(){return this._models}static make(e){return new n(e)}addIndex(e,t,r){this._index.addValue(e,t,`${r}`)}all(){let e=[...this._models.values()],t=e.length,r=new d;for(let i=0;i1;return Array.isArray(e)?t?this.findCollectionComposedPrimaryKey(e):this.findCollection(e):t?this.findOneComposedPrimaryKey(e):this.findOne(e)}getIndexByKey(e){return this._index.getIndexByKey(e)}insert(e){if(!(e instanceof M))throw new Error("Record should be instance of model");this._models.has(e.primaryKey)||(e.resetDirty(),e.primaryKey!=null&&this._models.set(e.primaryKey,e),this._index.addValueByModel(e))}registerIndex(e){this._index.register(e)}removeIndex(e,t,r){this._index.removeValue(e,t,r)}save(e){if(!e.primaryKey.startsWith("_")&&this.ids.includes(e._tmpId)&&(h.removeTmpIdFromIndex(e),this.delete(e._tmpId)),this.ids.includes(e.primaryKey)){this.update(e);return}this.insert(e)}setupIndexes(){this._model.tableSetup(this)}truncate(){this._models.clear(),this._index.truncate()}update(e){if(!this.models.has(e.primaryKey))throw new Error("Record doesn't exists");if(!(e instanceof M))throw new Error("Record should be instance of model");this._index.removeValueByModel(e),e.resetDirty(),this._index.addValueByModel(e),this._models.set(e.primaryKey,e)}findCollection(e){let t=[];for(let r=0;r{this.register(r)})}get name(){return this._name}addIndex(e,t,r,i){this.table(e).addIndex(t,r,i)}allModels(e){return this.table(e).allModels()}drop(e){this._tables.delete(e)}ids(e){return this.table(e).ids}indexes(e){return this.table(e).indexes}query(e){let t=e.match(/^((SELECT)|(INSERT)|(DELETE))\s+(.*)\s+FROM\s+([^\s]+)(\s+WHERE\s+([^\s]+)\s+(=)\s+([^\s+]))?((\s+)|;)?$/i);if(t.length===0)return null;let r=t[1],i=t[6],a=t[8],u=t[10];return a==="id"?this.table(i)[r.toLowerCase()](u):a===void 0&&r==="SELECT"?this.table(i).all():null}register(e){let t=new I(e);this._tables.set(t.name,t)}setIndexes(){this._tables.forEach(e=>{e.setupIndexes()})}showTables(){return[...this._tables.keys()]}table(e){return this._tables.get(e)}};var F=class{static{s(this,"Connection")}constructor(e){this.adapter=e,this._updateQueue=[],this.paused=!1}all(e){return new Promise(t=>{this.adapter.all(e).then(r=>{this.handleQueueMessage(r,t)})})}delete(e){return new Promise(t=>{this.adapter.delete(e).then(r=>{this.handleQueueMessage(r,t)})})}load(e){return this.all(e)}patch(e){return new Promise(t=>{this.adapter.patch(e).then(r=>{this.handleQueueMessage(r,t)})})}pause(){this.paused=!0}post(e){return new Promise(t=>{this.adapter.post(e).then(r=>{this.handleQueueMessage(r,t)})})}put(e){return new Promise(t=>{this.adapter.put(e).then(r=>{this.handleQueueMessage(r,t)})})}resume(){this.paused=!1}addToQueue(...e){this._updateQueue.push(...e)}handleQueueMessage(e,t){e.addCallback(t),this.addToQueue(e),queueMicrotask(()=>{this.processQueue()})}processQueue(){let e=(this._updateQueue??[]).shift();e&&(e.execute(),queueMicrotask(()=>{this.processQueue()}))}};var D=class{static{s(this,"Store")}constructor(){this.classInstances={},this.databases=new Map,this.connections=new Map,this.numberOfModelCreated=0,this.useDatabase="default",this.useConnectionName="default",globalThis.Store=this}add(e){this.databases.set(e.name,e)}addConnection(e,t="default"){this.connections.set(t,e)}connection(){return this.connections.get(this.useConnectionName)??null}database(){return this.databases.has(this.useDatabase)?new Proxy(this.databases.get(this.useDatabase),{construct(e,t,r){return Reflect.construct(e,t,r)},get(e,t){return e[t]?Reflect.get(e,t):(...r)=>{let i=[...r],a=i.shift();return e.table(a)[t](...i)}}}):null}use(e="default"){this.useDatabase=e,this.databases.get(this.useDatabase)?.setIndexes()}useConnection(e="default"){this.useConnectionName=e}useConnections(e){this.useConnection(e)}};var o=class{static{s(this,"QueueMessage")}constructor(e,t,r){this.model=e,this.action=t,this.data=r,this.callback=()=>null}addCallback(e){this.callback=e}execute(){this.model[this.action](this.data),this.callback()}};var m=class{static{s(this,"ConnectionRequest")}constructor(e){this.connectionRequestSettings=e}all(e){return fetch(`${this.modelApiLocation(e)}`,{method:"GET",...this.connectionRequestSettings.getSettings()})}delete(e){return fetch(`${this.modelApiLocation(e)}/${e.primaryKey}`,{method:"DELETE",...this.connectionRequestSettings.getSettings()})}get(e){return fetch(`${this.modelApiLocation(e)}/${e.primaryKey}`,{method:"GET",...this.connectionRequestSettings.getSettings()})}modelApiLocation(e){return this.connectionRequestSettings.modelEndPoint(e)}patch(e){return fetch(`${this.modelApiLocation(e)}/${e.primaryKey}`,{method:"PATCH",body:JSON.stringify(e.dirtyFields),...this.connectionRequestSettings.getSettings()})}post(e){return fetch(this.modelApiLocation(e),{method:"POST",body:e.jsonStringify(),...this.connectionRequestSettings.getSettings()})}put(e){return fetch(`${this.modelApiLocation(e)}/`,{method:"PUT",body:e.jsonStringify(),...this.connectionRequestSettings.getSettings()})}};var S=class{static{s(this,"JsonRequestAdapter")}constructor(e){this.connectionSettings=e}get isLocal(){return!1}get isRemote(){return!0}all(e){return new Promise(t=>{new m(this.connectionSettings).all(e).then(r=>this.responseJson(r)).then(r=>{let i=new o(e,"insert",r);t(i)})})}delete(e){return new Promise(t=>{new m(this.connectionSettings).delete(e).then(r=>this.responseJson(r)).then(r=>{let i=new o(e,"delete",r);t(i)})})}get(e){return new Promise(t=>{new m(this.connectionSettings).get(e).then(r=>this.responseJson(r)).then(r=>{let i=new o(e,"fill",r);t(i)})})}load(e){return this.all(e)}patch(e){return new Promise(t=>{new m(this.connectionSettings).patch(e).then(r=>this.responseJson(r)).then(r=>{let i=new o(e,"fill",r);t(i)})})}post(e){return new Promise(t=>{new m(this.connectionSettings).post(e).then(r=>this.responseJson(r)).then(r=>{let i=new o(e,"fill",r);t(i)})})}put(e){return new Promise(t=>{new m(this.connectionSettings).put(e).then(r=>this.responseJson(r)).then(r=>{let i=new o(e,"fill",r);t(i)})})}responseJson(e){return e.json()}};var P=class{static{s(this,"LocalStorageAdapter")}constructor(e){this.connectionSettings=e}all(e){return Promise.resolve(new o(e,"aSyncUpdate",this.getTableFromLocalStorage(e)))}delete(e){return Promise.resolve(new o(e,"aSyncUpdate",this.getTableFromLocalStorage(e)))}get(e){return Promise.resolve(new o(e,"aSyncUpdate",this.getTableFromLocalStorage(e)))}load(e){return Promise.resolve(new o(e,"aSyncInsert",this.getTableFromLocalStorage(e)))}patch(e){return Promise.resolve(new o(e,"aSyncUpdate",this.getTableFromLocalStorage(e)))}post(e){return Promise.resolve(new o(e,"aSyncInsert",this.getTableFromLocalStorage(e)))}put(e){return Promise.resolve(new o(e,"aSyncUpdate",this.getTableFromLocalStorage(e)))}getLocalStorageKey(e){return`jeloquent-${globalThis.Store.useDatabase}-${e.className}`}getTableFromLocalStorage(e){return JSON.parse(localStorage.getItem(this.getLocalStorageKey(e))??"[]")}};var $=class{static{s(this,"LocalArrayAdapter")}constructor(e){this.connectionSettings=e}all(e){return new Promise(t=>{t(new o(e,"insert",this.options[e.className]))})}delete(e){return new Promise(t=>{t(new o(e,"insert",this.options[e.className]))})}get(e){return new Promise(t=>{t(new o(e,"insert",this.options[e.className]))})}load(e){return new Promise(t=>{t(new o(e,"insert",this.options[e.className]))})}patch(e){return new Promise(t=>{t(new o(e,"insert",this.options[e.className]))})}post(e){return new Promise(t=>{t(new o(e,"insert",this.options[e.className]))})}put(e){return new Promise(t=>{t(new o(e,"insert",this.options[e.className]))})}};var B=class{static{s(this,"ConnectionAdapterFactory")}static getAdapter(e,t){return e==="jsonRequest"?new S(t):e==="localStorage"?new P(t):e==="localArray"?new $(t):new S(t)}};var H=(r=>(r.NO_CORS="no-cors",r.CORS="cors",r.SAME_ORIGIN="same-origin",r))(H||{}),G=(a=>(a.DEFAULT="default",a.NO_CACHE="no-cache",a.RELOAD="reload",a.FORCE_CACHE="force-cache",a.ONLY_IF_CACHED="only-if-cached",a))(G||{}),W=(r=>(r.MANUAL="manual",r.FOLLOW="follow",r.ERROR="error",r))(W||{}),Z=(p=>(p.NO_REFERRER="no-referrer",p.NO_REFERRER_WHEN_DOWNGRADE="*no-referrer-when-downgrade",p.ORIGIN="origin",p.ORIGIN_WHEN_CROSS_ORIGIN="origin-when-cross-origin",p.SAME_ORIGIN="same-origin",p.STRICT_ORIGIN="strict-origin",p.STRICT_ORIGIN_WHEN_CROSS_ORIGIN="strict-origin-when-cross-origin",p.UNSAFE_URL="unsafe-url",p))(Z||{}),E=class n{static{s(this,"ConnectionSettings")}static{this.OPTION_CACHES=G}static{this.OPTION_MODE=H}static{this.OPTION_REDIRECT=W}static{this.OPTION_REFERRER_POLICY=Z}constructor(e){this.contentType=e?.contentType??"application/json",this.mode=e?.mode??n.OPTION_MODE.CORS,this.cache=e?.cache??n.OPTION_CACHES.NO_CACHE,this.headers=e?.headers??{},this._baseUrl=e?.baseUrl??"http://localhost",this.modelPathMappings=e?.modelPathMappings??new Map}get baseUrl(){return this._baseUrl}getSettings(){return{mode:this.mode,cache:this.cache,headers:{Accept:this.contentType,"Content-Type":this.contentType,...this.headers},redirect:n.OPTION_REDIRECT.FOLLOW,referrerPolicy:n.OPTION_REFERRER_POLICY.NO_REFERRER}}modelEndPoint(e){return`${this.baseUrl}/${this.modelPathMappings.get(e.className)??e.kebabCaseClassName}`}};export{_ as BelongsTo,d as Collection,F as Connection,B as ConnectionAdapterFactory,E as ConnectionSettings,x as Database,c as Field,g as ForeignKey,N as HasMany,y as HasManyThrough,v as HasOne,w as HasOneThrough,M as Model,C as MorphOne,b as MorphTo,o as QueueMessage,l as Relation,D as Store,I as Table}; +var q=Object.defineProperty;var ee=(n,e,t)=>e in n?q(n,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):n[e]=t;var i=(n,e)=>q(n,"name",{value:e,configurable:!0});var I=(n,e,t)=>(ee(n,typeof e!="symbol"?e+"":e,t),t);var d=class{$fieldValue;$name;$originalValue;$parent;$previousValue;_isPrimary;constructor(e,t=!1){this._isPrimary=t,this.$fieldValue=null,this.$previousValue=void 0,this.$originalValue=void 0,this.$parent=null,this.$name=e}get isDirty(){return this.$fieldValue!=this.$previousValue}get isPrimary(){return this._isPrimary}get name(){return this.$name}get originalValue(){return this.$originalValue}get previousValue(){return this.$previousValue}get value(){return this.$fieldValue}set _value(e){this.$originalValue===void 0&&(this.$originalValue=JSON.parse(JSON.stringify(this.value??e))),this.$previousValue=JSON.parse(JSON.stringify(this.value)),this.$fieldValue=e}set value(e){this.$previousValue===void 0&&(this.$previousValue=JSON.parse(JSON.stringify(this.value??e))),this.$originalValue===void 0&&(this.$originalValue=JSON.parse(JSON.stringify(this.value??e))),this.$previousValue=JSON.parse(JSON.stringify(this.value)),this.$fieldValue=e}resetDirty(){this.$originalValue=JSON.parse(JSON.stringify(this.$fieldValue)),this.$previousValue=JSON.parse(JSON.stringify(this.$fieldValue))}setName(){return this}setup(e){return this.$parent=e,this.setName().setParentProperties()}tableSetup(e){console.info(e.name)}toJSON(){let e={};return e[this.$name]=this.value,e}setParentProperties(){return this}};i(d,"Field");var g=class extends d{_foreignKey;relation;constructor(e,t=null){super(e),this._foreignKey=e??t}get foreignKey(){return this._foreignKey}get value(){return this.$fieldValue}set value(e){this.$fieldValue=e}setRelation(e){return this.relation=e,this}tableSetup(e){e.registerIndex(this.foreignKey)}};i(g,"ForeignKey");var l=class extends d{foreignKey;model;constructor(e,t=null,r=null){let s=r??e.snakeCaseClassName;super(s),this.model=e,this.foreignKey=t}set _value(e){Array.isArray(e)||(e=[e]),e.forEach(t=>{this.model.insert(t)})}getRelationalFields(){return[new g(this.foreignKey).setRelation(this)]}tableSetup(e){e.registerIndex(this.foreignKey)}};i(l,"Relation");var M=class extends l{localKey;throughModel;_lcModelClassName;_lcParentClassName;_lcThroughModelClassName;constructor(e,t,r=null,s=null){super(e,r),this.model=e,this.throughModel=t,this.localKey=s??"id"}get indexName(){return`${this._lcThroughModelClassName}.${this._lcParentClassName}_id`}get originalValue(){return this.getValueByParentKey("originalPrimaryKey")}get value(){return this.getValueByParentKey("primaryKey")}getRelationalFields(){return[]}setName(){return this._lcThroughModelClassName=this.throughModel.snakeCaseClassName,this._lcModelClassName=this.model.snakeCaseClassName,this._lcParentClassName=this.$parent.snakeCaseClassName,this.foreignKey=`${this._lcThroughModelClassName}_id`,this.$name=`${this._lcModelClassName}s`,this}tableSetup(){this.model.registerIndex(this.indexName)}getValueByParentKey(e){let t=this.model.getIndexByKey(this.indexName);return globalThis.Store.database().find(this.model.className,[...t.get(`${this.$parent[e]}`)?.values()??[]])}};i(M,"HasManyThrough");var v=class extends l{localKey;constructor(e,t=null,r=null){super(e,t),this.localKey=r??"id"}get count(){return globalThis.Store.database().indexes(this.model.className).get(this.foreignKey).get(this.$parent.primaryKey)?.size??0}get originalValue(){return this.getValueByParentKey("originalPrimaryKey")}get value(){return this.getValueByParentKey("primaryKey")}getRelationalFields(){return[]}setName(){let e=this.$parent.snakeCaseClassName,t=this.model.snakeCaseClassName;return this.foreignKey=`${e}_id`,this.$name=`${t}s`,this}getValueByParentKey(e){let t=this.model.getIndexByKey(this.foreignKey);return globalThis.Store.database().find(this.model.className,[...t.get(`${this.$parent[e]}`)?.values()??[]])}setParentProperties(){return super.setParentProperties(),Object.defineProperty(this.$parent,`${this.name}Count`,{get:()=>this.count}),Object.defineProperty(this.$parent,`has${this.model.className}s`,{get:()=>this.count>0}),this}};i(v,"HasMany");var w=class extends l{constructor(e,t=null,r=null){super(e,t??`${e.snakeCaseClassName}_id`,r)}get originalValue(){return this.model.find(this.$parent[`original_${this.foreignKey}`])}get value(){return this.model.find(this.$parent[this.foreignKey])}setName(){let e=this.model.snakeCaseClassName;return this.$name=this.$name??`${e}`,this}setParentProperties(){super.setParentProperties();let e="";for(let t of this.$name.split("_"))e+=t[0].toUpperCase()+t.slice(1);return Object.defineProperty(this.$parent,`has${e}`,{get:()=>this.value!==null}),this}};i(w,"BelongsTo");var C=class extends l{constructor(e){super(e)}get originalValue(){return this.getValueByParentKey("originalPrimaryKey")}get value(){return this.getValueByParentKey("primaryKey")}getRelationalFields(){return[]}getValueByParentKey(e){let t=this.$parent.className,r=this.$parent[e],s=`${this.name}_id`,a=`${this.name}_type`,u={};return u[s]=r,u[a]=t,this.model.find(u)}};i(C,"MorphOne");var x=class extends l{constructor(e){super(e)}get originalValue(){return this.getValueByParentKey("originalPrimaryKey")}get value(){return this.getValueByParentKey("primaryKey")}getRelationalFields(){return[]}setName(){return this.foreignKey=`${this.$parent.snakeCaseClassName}_id`,this}getValueByParentKey(e){let t=this.model.getIndexByKey(this.foreignKey);return globalThis.Store.database().find(this.model.className,[...t.get(`${this.$parent[e]}`)?.values()??[]]).first()}};i(x,"HasOne");var F=class extends l{localKey;throughModel;_lcModelClassName;_lcParentClassName;_lcThroughModelClassName;constructor(e,t,r=null,s="id"){super(e,r),this.throughModel=t,this.localKey=s}get indexName(){return`${this._lcThroughModelClassName}.${this._lcParentClassName}_id`}get originalValue(){return this.$parent[`original_${this._lcThroughModelClassName}`][`original_${this._lcModelClassName}`]??null}get value(){return this.$parent[this._lcThroughModelClassName][this._lcModelClassName]??null}getRelationalFields(){return[]}setName(){return this._lcThroughModelClassName=this.throughModel.snakeCaseClassName,this._lcModelClassName=this.model.snakeCaseClassName,this._lcParentClassName=this.$parent.snakeCaseClassName,this.foreignKey=`${this._lcThroughModelClassName}_id`,this.$name=`${this._lcModelClassName}`,this}};i(F,"HasOneThrough");var S=class extends d{constructor(e){super(e)}get value(){let e=this.$parent.snakeCaseClassName,t=this.$parent[`${e}_type`],r=this.$parent[`${e}_id`];return globalThis.Store.classInstances[t].constructor.find(r)}set _value(e){Array.isArray(e)||(e=[e]);let t=this.$parent.snakeCaseClassName,r=`${t}_type`,s=`${t}_id`;for(let a of e)a.id=a[s],globalThis.Store.classInstances[a[r]].constructor.insert(a)}};i(S,"MorphTo");var h=class{_indexes;indexedFields;splitIndexNames;constructor(){this._indexes=new Map,this.indexedFields=new Set,this.splitIndexNames=new Map}get indexes(){return this._indexes}static add(e,t){globalThis.Store.database().addIndex(e.className,t.foreignKey,t.value,e.primaryKey)}static addIndex(e,t){this.add(e,t)}static register(e,t){globalThis.Store.database().registerIndex(e.className,t)}static registerIndex(e,t){this.register(e,t)}static remove(e,t){globalThis.Store.database().removeIndex(e.className,t.foreignKey,t.previousValue,e.primaryKey)}static removeIndex(e,t){this.remove(e,t)}static removeTmpIdFromIndex(e){let t=e.className;e.dirtyFields.filter(r=>r.constructor.name==="ForeignKey").forEach(r=>{globalThis.Store.database().removeIndex(t,r.name,r.originalValue,e._tmpId)})}addValue(e,t,r){if(!this._indexes.has(e)||r===null)return;if(!this.index(e).has(`${t}`)){this.registerLookUpKey(e,t,r);return}let a=this.indexLookUpKey(e,t);a.has(`${r}`)||a.add(`${r}`)}addValueByModel(e){for(let[t]of this._indexes)this.addValue(t,this.getLookUpValue(e,t),e.primaryKey)}getIndexByKey(e){return this.index(e)}getLookUpValue(e,t){let r=this.splitIndexNames.get(t),s=null,a=e;for(let u of r){if(a[`original_${u}`]===null)break;a=a[`original_${u}`],s=a}return`${s}`}register(e){this._indexes.has(e)||(this.indexedFields.add(e),this.splitIndexNames.set(e,e.split(".")),this._indexes.set(e,new Map))}registerIndex(e){this.register(e)}registerLookUpKey(e,t,r){this.index(e).set(`${t}`,new Set([`${r}`]))}removeValue(e,t,r){this.indexLookUpKey(e,t).delete(`${r}`)}removeValueByModel(e){for(let[t]of this._indexes)this.removeValue(t,this.getLookUpValue(e,t),e.primaryKey)}truncate(){for(let e in this._indexes)this.index(e).clear()}unregisterLookUpKey(e,t){this.index(e).delete(`${t}`)}index(e){return this._indexes.get(e)}indexLookUpKey(e,t){return this.index(e).get(`${t}`)}};i(h,"Index");function T(n,e){return n!=e}i(T,"default");function k(n,e){return n==e}i(k,"default");function V(n,e){return n>e}i(V,"default");function R(n,e){return n>=e}i(R,"default");function j(n,e){return n=":R,"<=":Q,">":V,"<":j}}first(){return this[0]??null}last(){return this.slice(-1)[0]??null}merge(e){return this.push(...e),this}pluck(e,t=""){let r=e.split(".");if(t){let a=t.split("."),u={};for(let N in this)u[this._getRowFieldResult(this[N],a)]=this._getRowFieldResult(this[N],r);return u}let s=[];for(let a in this)s.push(this._getRowFieldResult(this[a],r));return s}random(){return this[Math.round((this.length-1)*Math.random())]}toJSON(){return this.toObject()}toObject(){return this.map(e=>e?.toObject?.()??e)}unique(e){let t={};for(let r in this)t[this[r][e]]=this[r];return new c(...Object.values(t))}where(e,t,r=null){if(r=r??t,t=t===r?"==":t,!Object.prototype.hasOwnProperty.call(this.operators,t))throw new Error("Invalid comparison operator used");return this.whereIfFunction(e,(s,a)=>this.operators[t](a[s],r))}whereBetween(e,t){return this.whereIfFunction(e,(r,s)=>{let a=s[r];return a>=t[0]&&a<=t[1]})}whereIfFunction(e,t){let r=new c;for(let s in this)t(e,this[s])&&r.push(this[s]);return r}whereIn(e,t){return this.whereIfFunction(e,(r,s)=>t.includes(s[r]))}whereInstanceOf(e){return this.whereIfFunction(null,(t,r)=>r instanceof e)}whereNotBetween(e,t){return this.whereIfFunction(e,(r,s)=>{let a=s[r];return!(a>=t[0]&&a<=t[1])})}whereNotIn(e,t){return this.whereIfFunction(e,(r,s)=>!t.includes(s[r]))}whereNotInstanceOf(e){return this.whereIfFunction(null,(t,r)=>!(r instanceof e))}whereNotNull(e){return this.whereIfFunction(e,(t,r)=>r[t]!==null)}whereNull(e){return this.whereIfFunction(e,(t,r)=>r[t]===null)}_getRowFieldResult(e,t){let r=e[t[0]]??null;for(let s=1;st?.toObject(!0)??t)??[]]}i(se,"arrayToObjects");function G(n){let e=[...n];return n.forEach((t,r)=>{t instanceof l&&e.splice(r,0,...t.getRelationalFields())}),e}i(G,"addRelationFieldsToList");function H(n,e){e.forEach(t=>{t.setup(B(n)),n._originalFields.set(t.name,t)}),n.numberOfFields=n.originalFields.length}i(H,"setFields");function W(n,e){n.originalFields.forEach(t=>{t instanceof g&&t.tableSetup(e),t instanceof M&&t.tableSetup()})}i(W,"setupTable");function B(n){return new Proxy(n,{construct(e,t,r){return Reflect.construct(e,t,r)},get(e,t){return Reflect.has(e,t)?Reflect.get(e,t):typeof t!="string"?null:e._originalFields.has(t)?e._originalFields.get(t).value:t.startsWith("original_")&&!Reflect.has(e,t.replace("original_",""))&&e._originalFields.has(t.replace("original_",""))?e._originalFields.get(t.replace("original_","")).originalValue:null},set(e,t,r){if(Reflect.has(e,t))return Reflect.set(e,t,r);if(typeof t!="string")return!1;if(e._originalFields.has(t))return e._originalFields.get(t).value=r,!0;if(t.startsWith("_")&&e._originalFields.has(t.replace("_",""))){let s=e._originalFields.get(t.replace("_",""));return s._value=r,!0}return!0}})}i(B,"modelProxy");var f=class{_tmpId;["constructor"];_originalFields;_primaryFields;numberOfFields;constructor(e=[]){return this.setFields(G(e)),this._tmpId=`_${++globalThis.Store.numberOfModelCreated}`,B(this)}static get className(){return this.name}static get kebabCaseClassName(){return this.kebabCaseName??=L(this.className)}static get snakeCaseClassName(){return this.snakeCaseName??=E(this.className)}get className(){return this.constructor.className}get dirtyFieldNames(){return this.dirtyFields.map(e=>e.name)}get dirtyFields(){return this.originalFields.filter(e=>e.isDirty)}get kebabCaseClassName(){return this.constructor.kebabCaseClassName}get originalFields(){return[...this._originalFields.values()]}get originalPrimaryKey(){return this.primaryFields.reduce((e,t,r)=>r>0?`${e}-${t.originalValue}`:t.originalValue,"")??this._tmpId??null}get originalValues(){return this.originalFields.reduce((e,t)=>(t.originalValue!==void 0&&(e[t.name]=t.originalValue),e),{})}get primaryFields(){return this._primaryFields??=this.originalFields.filter(e=>e.isPrimary)}get primaryKey(){return this.primaryFields.reduce((e,t,r)=>r>0?`${e}-${t.value}`:t.value===null?t.value:`${t.value}`,"")??this._tmpId??null}get primaryKeyName(){return this.originalFields.filter(e=>e.isPrimary).map(e=>e.name)}get snakeCaseClassName(){return this.constructor.snakeCaseClassName}static aSyncInsert(e){return new Promise(t=>{queueMicrotask(()=>{t(this.insert(e))})})}static aSyncUpdate(e){return new Promise(t=>{queueMicrotask(()=>{t(this.update(e))})})}static all(){return globalThis.Store.database().all(this.className)}static delete(e){globalThis.Store.database().delete(this.className,e)}static find(e){return globalThis.Store.database().find(this.className,e)}static getIndexByKey(e){return globalThis.Store.database().getIndexByKey(this.className,e)}static getInstance(){let e=globalThis.Store.classInstances[this.className]??(globalThis.Store.classInstances[this.className]=new this),t=e.originalFields.reduce((r,s)=>(r.push(Object.assign(Object.create(Object.getPrototypeOf(s)),s)),r),[]);return B(Object.create(Object.getPrototypeOf(e)).setFields(t))}static ids(){return globalThis.Store.database().ids(this.className)}static insert(e){let t=Array.isArray(e)?e:[e],r=t.length,s=new c;for(let a=0;a0}registerIndex(e){h.register(this,e)}resetDirty(){this.originalFields.filter(e=>!(e instanceof l)).forEach(e=>{e.resetDirty()})}save(){globalThis.Store.database().save(this.className,this)}setFields(e){return this._originalFields=new Map,H(this,e),this}tableSetup(e){W(this,e)}toJSON(){return this.toObject()}toObject(e=!1){return D(this,e)}};i(f,"Model"),I(f,"kebabCaseName"),I(f,"snakeCaseName");var p=class{name;_index;_model;_models;_primaryKeyFieldNames;constructor(e){this.setup(e.getInstance())}get ids(){return[...this._models.keys()]}get indexes(){return this._index.indexes}get models(){return this._models}static make(e){return new p(e)}addIndex(e,t,r){this._index.addValue(e,t,`${r}`)}all(){let e=[...this._models.values()],t=e.length,r=new c;for(let s=0;s1;return Array.isArray(e)?t?this.findCollectionComposedPrimaryKey(e):this.findCollection(e):t?this.findOneComposedPrimaryKey(e):this.findOne(e)}getIndexByKey(e){return this._index.getIndexByKey(e)}insert(e){if(!(e instanceof f))throw new Error("Record should be instance of model");this._models.has(e.primaryKey)||(e.resetDirty(),e.primaryKey!=null&&this._models.set(e.primaryKey,e),this._index.addValueByModel(e))}registerIndex(e){this._index.register(e)}removeIndex(e,t,r){this._index.removeValue(e,t,r)}save(e){if(!e.primaryKey.startsWith("_")&&this.ids.includes(e._tmpId)&&(h.removeTmpIdFromIndex(e),this.delete(e._tmpId)),this.ids.includes(e.primaryKey)){this.update(e);return}this.insert(e)}setupIndexes(){this._model.tableSetup(this)}truncate(){this._models.clear(),this._index.truncate()}update(e){if(!this.models.has(e.primaryKey))throw new Error("Record doesn't exists");if(!(e instanceof f))throw new Error("Record should be instance of model");this._index.removeValueByModel(e),e.resetDirty(),this._index.addValueByModel(e),this._models.set(e.primaryKey,e)}findCollection(e){let t=[];for(let r=0;r{this.register(r)})}get name(){return this._name}addIndex(e,t,r,s){this.table(e).addIndex(t,r,s)}allModels(e){return this.table(e).allModels()}drop(e){this._tables.delete(e)}ids(e){return this.table(e).ids}indexes(e){return this.table(e).indexes}query(e){let t=e.match(/^((SELECT)|(INSERT)|(DELETE))\s+(.*)\s+FROM\s+([^\s]+)(\s+WHERE\s+([^\s]+)\s+(=)\s+([^\s+]))?((\s+)|;)?$/i);if(t.length===0)return null;let r=t[1],s=t[6],a=t[8],u=t[10];return a==="id"?this.table(s)[r.toLowerCase()](u):a===void 0&&r==="SELECT"?this.table(s).all():null}register(e){let t=new p(e);this._tables.set(t.name,t)}setIndexes(){this._tables.forEach(e=>{e.setupIndexes()})}showTables(){return[...this._tables.keys()]}table(e){return this._tables.get(e)}};i(P,"Database");var $=class{_updateQueue;adapter;paused;constructor(e){this.adapter=e,this._updateQueue=[],this.paused=!1}all(e){return new Promise(t=>{this.adapter.all(e).then(r=>{this.handleQueueMessage(r,t)})})}delete(e){return new Promise(t=>{this.adapter.delete(e).then(r=>{this.handleQueueMessage(r,t)})})}load(e){return this.all(e)}patch(e){return new Promise(t=>{this.adapter.patch(e).then(r=>{this.handleQueueMessage(r,t)})})}pause(){this.paused=!0}post(e){return new Promise(t=>{this.adapter.post(e).then(r=>{this.handleQueueMessage(r,t)})})}put(e){return new Promise(t=>{this.adapter.put(e).then(r=>{this.handleQueueMessage(r,t)})})}resume(){this.paused=!1}addToQueue(...e){this._updateQueue.push(...e)}handleQueueMessage(e,t){e.addCallback(t),this.addToQueue(e),queueMicrotask(()=>{this.processQueue()})}processQueue(){let e=(this._updateQueue??[]).shift();!e||(e.execute(),queueMicrotask(()=>{this.processQueue()}))}};i($,"Connection");var U=class{classInstances;numberOfModelCreated;connections;databases;useConnectionName;useDatabase;constructor(){this.classInstances={},this.databases=new Map,this.connections=new Map,this.numberOfModelCreated=0,this.useDatabase="default",this.useConnectionName="default",globalThis.Store=this}add(e){this.databases.set(e.name,e)}addConnection(e,t="default"){this.connections.set(t,e)}connection(){return this.connections.get(this.useConnectionName)??null}database(){return this.databases.has(this.useDatabase)?new Proxy(this.databases.get(this.useDatabase),{construct(e,t,r){return Reflect.construct(e,t,r)},get(e,t){return e[t]?Reflect.get(e,t):(...r)=>{let s=[...r],a=s.shift();return e.table(a)[t](...s)}}}):null}use(e="default"){this.useDatabase=e,this.databases.get(this.useDatabase)?.setIndexes()}useConnection(e="default"){this.useConnectionName=e}useConnections(e){this.useConnection(e)}};i(U,"Store");var o=class{action;callback;data;model;constructor(e,t,r){this.model=e,this.action=t,this.data=r,this.callback=()=>null}addCallback(e){this.callback=e}execute(){this.model[this.action](this.data),this.callback()}};i(o,"QueueMessage");var m=class{connectionRequestSettings;constructor(e){this.connectionRequestSettings=e}all(e){return fetch(`${this.modelApiLocation(e)}`,{method:"GET",...this.connectionRequestSettings.getSettings()})}delete(e){return fetch(`${this.modelApiLocation(e)}/${e.primaryKey}`,{method:"DELETE",...this.connectionRequestSettings.getSettings()})}get(e){return fetch(`${this.modelApiLocation(e)}/${e.primaryKey}`,{method:"GET",...this.connectionRequestSettings.getSettings()})}modelApiLocation(e){return this.connectionRequestSettings.modelEndPoint(e)}patch(e){return fetch(`${this.modelApiLocation(e)}/${e.primaryKey}`,{method:"PATCH",body:JSON.stringify(e.dirtyFields),...this.connectionRequestSettings.getSettings()})}post(e){return fetch(this.modelApiLocation(e),{method:"POST",body:e.jsonStringify(),...this.connectionRequestSettings.getSettings()})}put(e){return fetch(`${this.modelApiLocation(e)}/`,{method:"PUT",body:e.jsonStringify(),...this.connectionRequestSettings.getSettings()})}};i(m,"ConnectionRequest");var _=class{connectionSettings;constructor(e){this.connectionSettings=e}get isLocal(){return!1}get isRemote(){return!0}all(e){return new Promise(t=>{new m(this.connectionSettings).all(e).then(r=>this.responseJson(r)).then(r=>{let s=new o(e,"insert",r);t(s)})})}delete(e){return new Promise(t=>{new m(this.connectionSettings).delete(e).then(r=>this.responseJson(r)).then(r=>{let s=new o(e,"delete",r);t(s)})})}get(e){return new Promise(t=>{new m(this.connectionSettings).get(e).then(r=>this.responseJson(r)).then(r=>{let s=new o(e,"fill",r);t(s)})})}load(e){return this.all(e)}patch(e){return new Promise(t=>{new m(this.connectionSettings).patch(e).then(r=>this.responseJson(r)).then(r=>{let s=new o(e,"fill",r);t(s)})})}post(e){return new Promise(t=>{new m(this.connectionSettings).post(e).then(r=>this.responseJson(r)).then(r=>{let s=new o(e,"fill",r);t(s)})})}put(e){return new Promise(t=>{new m(this.connectionSettings).put(e).then(r=>this.responseJson(r)).then(r=>{let s=new o(e,"fill",r);t(s)})})}responseJson(e){return e.json()}};i(_,"JsonRequestAdapter");var K=class{connectionSettings;constructor(e){this.connectionSettings=e}all(e){return Promise.resolve(new o(e,"aSyncUpdate",this.getTableFromLocalStorage(e)))}delete(e){return Promise.resolve(new o(e,"aSyncUpdate",this.getTableFromLocalStorage(e)))}get(e){return Promise.resolve(new o(e,"aSyncUpdate",this.getTableFromLocalStorage(e)))}load(e){return Promise.resolve(new o(e,"aSyncInsert",this.getTableFromLocalStorage(e)))}patch(e){return Promise.resolve(new o(e,"aSyncUpdate",this.getTableFromLocalStorage(e)))}post(e){return Promise.resolve(new o(e,"aSyncInsert",this.getTableFromLocalStorage(e)))}put(e){return Promise.resolve(new o(e,"aSyncUpdate",this.getTableFromLocalStorage(e)))}getLocalStorageKey(e){return`jeloquent-${globalThis.Store.useDatabase}-${e.className}`}getTableFromLocalStorage(e){return JSON.parse(localStorage.getItem(this.getLocalStorageKey(e))??"[]")}};i(K,"LocalStorageAdapter");var O=class{connectionSettings;options;constructor(e){this.connectionSettings=e}all(e){return new Promise(t=>{t(new o(e,"insert",this.options[e.className]))})}delete(e){return new Promise(t=>{t(new o(e,"insert",this.options[e.className]))})}get(e){return new Promise(t=>{t(new o(e,"insert",this.options[e.className]))})}load(e){return new Promise(t=>{t(new o(e,"insert",this.options[e.className]))})}patch(e){return new Promise(t=>{t(new o(e,"insert",this.options[e.className]))})}post(e){return new Promise(t=>{t(new o(e,"insert",this.options[e.className]))})}put(e){return new Promise(t=>{t(new o(e,"insert",this.options[e.className]))})}};i(O,"LocalArrayAdapter");var J=class{static getAdapter(e,t){return e==="jsonRequest"?new _(t):e==="localStorage"?new K(t):e==="localArray"?new O(t):new _(t)}};i(J,"ConnectionAdapterFactory");var Z=(r=>(r.NO_CORS="no-cors",r.CORS="cors",r.SAME_ORIGIN="same-origin",r))(Z||{}),z=(a=>(a.DEFAULT="default",a.NO_CACHE="no-cache",a.RELOAD="reload",a.FORCE_CACHE="force-cache",a.ONLY_IF_CACHED="only-if-cached",a))(z||{}),X=(r=>(r.MANUAL="manual",r.FOLLOW="follow",r.ERROR="error",r))(X||{}),Y=(b=>(b.NO_REFERRER="no-referrer",b.NO_REFERRER_WHEN_DOWNGRADE="*no-referrer-when-downgrade",b.ORIGIN="origin",b.ORIGIN_WHEN_CROSS_ORIGIN="origin-when-cross-origin",b.SAME_ORIGIN="same-origin",b.STRICT_ORIGIN="strict-origin",b.STRICT_ORIGIN_WHEN_CROSS_ORIGIN="strict-origin-when-cross-origin",b.UNSAFE_URL="unsafe-url",b))(Y||{}),A=class{_baseUrl;cache;contentType;headers;mode;modelPathMappings;constructor(e){this.contentType=e?.contentType??"application/json",this.mode=e?.mode??A.OPTION_MODE.CORS,this.cache=e?.cache??A.OPTION_CACHES.NO_CACHE,this.headers=e?.headers??{},this._baseUrl=e?.baseUrl??"http://localhost",this.modelPathMappings=e?.modelPathMappings??new Map}get baseUrl(){return this._baseUrl}getSettings(){return{mode:this.mode,cache:this.cache,headers:{Accept:this.contentType,"Content-Type":this.contentType,...this.headers},redirect:A.OPTION_REDIRECT.FOLLOW,referrerPolicy:A.OPTION_REFERRER_POLICY.NO_REFERRER}}modelEndPoint(e){return`${this.baseUrl}/${this.modelPathMappings.get(e.className)??e.kebabCaseClassName}`}},y=A;i(y,"ConnectionSettings"),I(y,"OPTION_CACHES",z),I(y,"OPTION_MODE",Z),I(y,"OPTION_REDIRECT",X),I(y,"OPTION_REFERRER_POLICY",Y);export{w as BelongsTo,c as Collection,$ as Connection,J as ConnectionAdapterFactory,y as ConnectionSettings,P as Database,d as Field,g as ForeignKey,v as HasMany,M as HasManyThrough,x as HasOne,F as HasOneThrough,f as Model,C as MorphOne,S as MorphTo,o as QueueMessage,l as Relation,U as Store,p as Table};