@live-state/sync 0.0.4-beta.9 → 0.0.6-beta-1

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.
@@ -0,0 +1 @@
1
+ var b="0123456789ABCDEFGHJKMNPQRSTVWXYZ";var l;(function(t){t.Base32IncorrectEncoding="B32_ENC_INVALID",t.DecodeTimeInvalidCharacter="DEC_TIME_CHAR",t.DecodeTimeValueMalformed="DEC_TIME_MALFORMED",t.EncodeTimeNegative="ENC_TIME_NEG",t.EncodeTimeSizeExceeded="ENC_TIME_SIZE_EXCEED",t.EncodeTimeValueMalformed="ENC_TIME_MALFORMED",t.PRNGDetectFailure="PRNG_DETECT",t.ULIDInvalid="ULID_INVALID",t.Unexpected="UNEXPECTED",t.UUIDInvalid="UUID_INVALID";})(l||(l={}));var u=class extends Error{constructor(e,n){super(`${n} (${e})`),this.name="ULIDError",this.code=e;}};function R(t){let e=Math.floor(t()*32);return e===32&&(e=31),b.charAt(e)}function _(t){let e=w(),n=e&&(e.crypto||e.msCrypto)||null;if(typeof(n==null?void 0:n.getRandomValues)=="function")return ()=>{let i=new Uint8Array(1);return n.getRandomValues(i),i[0]/255};if(typeof(n==null?void 0:n.randomBytes)=="function")return ()=>n.randomBytes(1).readUInt8()/255;throw new u(l.PRNGDetectFailure,"Failed to find a reliable PRNG")}function w(){return M()?self:typeof window<"u"?window:typeof global<"u"?global:typeof globalThis<"u"?globalThis:null}function A(t,e){let n="";for(;t>0;t--)n=R(e)+n;return n}function E(t,e=10){if(isNaN(t))throw new u(l.EncodeTimeValueMalformed,`Time must be a number: ${t}`);if(t>0xffffffffffff)throw new u(l.EncodeTimeSizeExceeded,`Cannot encode a time larger than ${0xffffffffffff}: ${t}`);if(t<0)throw new u(l.EncodeTimeNegative,`Time must be positive: ${t}`);if(Number.isInteger(t)===false)throw new u(l.EncodeTimeValueMalformed,`Time must be an integer: ${t}`);let n,i="";for(let r=e;r>0;r--)n=t%32,i=b.charAt(n)+i,t=(t-n)/32;return i}function M(){return typeof WorkerGlobalScope<"u"&&self instanceof WorkerGlobalScope}function I(t,e){let n=_(),i=Date.now();return E(i,10)+A(16,n)}var N=()=>I().toLowerCase();var j=(t,e)=>typeof t=="function"?t(e):t;var L=class t{_collection;_client;_where;_include;_limit;_single;_sort;_shouldAwait;constructor(e,n,i,r,a,s,o,T){this._collection=e,this._client=n,this._where=i??{},this._include=r??{},this._limit=a,this._single=s,this._sort=o,this._shouldAwait=T,this.get=this.get.bind(this),this.subscribe=this.subscribe.bind(this);}where(e){return new t(this._collection,this._client,{...this._where,...e},this._include,this._limit,this._single,this._sort,this._shouldAwait)}include(e){return new t(this._collection,this._client,this._where,{...this._include,...e},this._limit,this._single,this._sort,this._shouldAwait)}limit(e){return new t(this._collection,this._client,this._where,this._include,e,this._single,this._sort,this._shouldAwait)}one(e){return this.first({id:e})}first(e){return new t(this._collection,this._client,e??this._where,this._include,1,true,this._sort,this._shouldAwait)}orderBy(e,n="asc"){let i=[...this._sort??[],{key:e,direction:n}];return new t(this._collection,this._client,this._where,this._include,this._limit,this._single,i,this._shouldAwait)}toJSON(){return {resource:this._collection.name,where:this._where,include:this._include,limit:this._limit,sort:this._sort}}buildQueryRequest(){return {resource:this._collection.name,where:this._where,include:this._include,limit:this._limit,sort:this._sort}}get(){let e=this._client.get(this.buildQueryRequest());return this._shouldAwait?Promise.resolve(e).then(n=>this._single?n[0]:n):this._single?e[0]:e}subscribe(e){return this._client.subscribe(this.buildQueryRequest(),n=>{if(this._single)return e(n[0]);e(n);})}static _init(e,n,i){return new t(e,n,void 0,void 0,void 0,void 0,void 0,i??false)}};var O=(t,e,n=[])=>new Proxy(t,{get:(i,r)=>{var T,y;if(r==="__isProxy__")return true;let a=(T=e.get)==null?void 0:T.call(e,i,[...n,r]);if(a!==void 0)return a;let s=i,o=r;return (y=s[o])!=null&&y.__isProxy__||(s[o]=O(typeof s[o]=="object"?s[o]:()=>{},e,[...n,r])),s[o]},apply:(i,r,a)=>{var s;return (s=e.apply)==null?void 0:s.call(e,i,n,a)}}),D=(t,e,n)=>{let i=[],r=0;for(let a=0;a<t.length&&(n===void 0||r<n);a++)e(t[a],a)&&(i.push(t[a]),r++);return i};var g=t=>t?Array.isArray(t.value)?t.value.map(n=>g(n)):typeof t.value!="object"||t.value===null||t.value instanceof Date?t.value:Object.fromEntries(Object.entries(t.value).map(([n,i])=>Array.isArray(i)?[n,i.map(r=>g(r))]:[n,g(i)])):void 0;export{N as a,j as b,L as c,O as d,D as e,g as f};
@@ -0,0 +1 @@
1
+ import {xxHash32}from'js-xxhash';var O=Object.create;var I=Object.defineProperty;var K=Object.getOwnPropertyDescriptor;var A=Object.getOwnPropertyNames;var j=Object.getPrototypeOf,M=Object.prototype.hasOwnProperty;var V=(t,e)=>()=>(e||t((e={exports:{}}).exports,e),e.exports);var S=(t,e,i,n)=>{if(e&&typeof e=="object"||typeof e=="function")for(let a of A(e))!M.call(t,a)&&a!==i&&I(t,a,{get:()=>e[a],enumerable:!(n=K(e,a))||n.enumerable});return t};var $=(t,e,i)=>(i=t!=null?O(j(t)):{},S(e||!t||!t.__esModule?I(i,"default",{value:t,enumerable:true}):i,t));var o=class{_value;_meta;_encodeInput;_decodeInput};var m=class extends o{inner;constructor(e){super(),this.inner=e;}encodeMutation(e,i,n){return this.inner.encodeMutation(e,i,n)}mergeMutation(e,i,n){return this.inner.mergeMutation(e,i,n)}getStorageFieldType(){return {...this.inner.getStorageFieldType(),nullable:true}}},y=class t extends o{storageType;convertFunc;isIndex;isUnique;defaultValue;foreignReference;isPrimary;constructor(e,i,n,a,s,l,r){super(),this.storageType=e,this.convertFunc=i,this.isIndex=n??false,this.isUnique=a??false,this.defaultValue=s,this.foreignReference=l,this.isPrimary=r??false;}encodeMutation(e,i,n){return {value:i,_meta:{timestamp:n}}}mergeMutation(e,i,n){return n&&n._meta.timestamp&&i._meta.timestamp&&n._meta.timestamp.localeCompare(i._meta.timestamp)>=0?[n,null]:[{value:this.convertFunc?this.convertFunc(i.value):i.value,_meta:i._meta},i]}getStorageFieldType(){return {type:this.storageType,nullable:false,index:this.isIndex,unique:this.isUnique,default:this.defaultValue,references:this.foreignReference,primary:this.isPrimary}}unique(){return new t(this.storageType,this.convertFunc,this.isIndex,true,this.defaultValue,this.foreignReference,this.isPrimary)}index(){return new t(this.storageType,this.convertFunc,true,this.isUnique,this.defaultValue,this.foreignReference,this.isPrimary)}default(e){return new t(this.storageType,this.convertFunc,this.isIndex,this.isUnique,e,this.foreignReference,this.isPrimary)}primary(){return new t(this.storageType,this.convertFunc,this.isIndex,this.isUnique,this.defaultValue,this.foreignReference,true)}nullable(){return new m(this)}},v=class t extends y{constructor(){super("integer",e=>Number(e));}static create(){return new t}},q=v.create,p=class t extends y{constructor(e){super("varchar",void 0,void 0,void 0,void 0,e);}static create(){return new t}static createId(){return new t().index().unique().primary()}static createReference(e){return new t(e)}},N=p.create,D=p.createId,W=p.createReference,L=class t extends y{constructor(){super("boolean",e=>typeof e=="string"?e.toLowerCase()==="true":!!e);}static create(){return new t}},P=L.create,x=class t extends y{constructor(){super("timestamp",e=>typeof e=="string"?new Date(e):e);}static create(){return new t}},k=x.create;var g=class t extends o{name;fields;relations;constructor(e,i,n){super(),this.name=e,this.fields=i,this.relations=n??{};}encodeMutation(e,i,n){return Object.fromEntries(Object.entries(i).map(([a,s])=>[a,(this.fields[a]??this.relations[a]).encodeMutation("set",s,n)]))}mergeMutation(e,i,n){let a={};return [{value:{...(n==null?void 0:n.value)??{},...Object.fromEntries(Object.entries(i).map(([s,l])=>{let r=this.fields[s]??this.relations[s];if(!r)return [s,l];let[u,d]=r.mergeMutation(e,l,n==null?void 0:n.value[s]);return d&&(a[s]=d),[s,u]}))}},a]}setRelations(e){return new t(this.name,this.fields,e)}getStorageFieldType(){throw new Error("Method not implemented.")}static create(e,i){return new t(e,i)}},H=g.create,f=class t extends o{entity;type;required;relationalColumn;foreignColumn;sourceEntity;constructor(e,i,n,a,s){super(),this.entity=e,this.type=i,this.required=s??false,this.relationalColumn=n,this.foreignColumn=a;}encodeMutation(e,i,n){if(e!=="set")throw new Error("Mutation type not implemented.");if(this.type==="many")throw new Error("Many not implemented.");return {value:i,_meta:{timestamp:n}}}mergeMutation(e,i,n){var a;return this.type==="many"?n?[n,null]:[i,i]:(a=n==null?void 0:n._meta)!=null&&a.timestamp&&i._meta.timestamp&&n._meta.timestamp.localeCompare(i._meta.timestamp)>=0?[n,null]:[i,i]}getStorageFieldType(){return {type:"varchar",nullable:!this.required,references:`${this.entity.name}.${String(this.foreignColumn??this.relationalColumn??"id")}`}}toJSON(){return {entityName:this.entity.name,type:this.type,required:this.required,relationalColumn:this.relationalColumn,foreignColumn:this.foreignColumn}}static createOneFactory(){return (e,i,n)=>new t(e,"one",i,void 0,n??false)}static createManyFactory(){return (e,i,n)=>new t(e,"many",void 0,i,n??false)}},z=(t,e)=>({$type:"relations",objectName:t.name,relations:e({one:f.createOneFactory(),many:f.createManyFactory()})}),h=t=>t?Array.isArray(t.value)?t.value.map(i=>h(i)):typeof t.value!="object"||t.value===null||t.value instanceof Date?t.value:Object.fromEntries(Object.entries(t.value).map(([i,n])=>Array.isArray(n)?[i,n.map(a=>h(a))]:[i,h(n)])):void 0,B=t=>Object.fromEntries(Object.entries(t).flatMap(([e,i])=>{if(i.$type==="relations")return [];let n=i,a=Object.values(t).find(s=>s.$type==="relations"&&s.objectName===i.name);return a&&(n=n.setRelations(a.relations)),[[n.name,n]]}));var Z=t=>xxHash32(JSON.stringify(t)).toString(32),C=(t,e,i)=>{let n={},a=i[e];if(!a)return n;let s=l=>{l.$and?l.$and.forEach(s):l.$or?l.$or.forEach(s):Object.entries(l).forEach(([r,u])=>{var d;if((d=a.relations)!=null&&d[r]&&(n[r]=true,typeof u=="object"&&u!==null&&!Array.isArray(u))){let b=C(u,a.relations[r].entity.name,i);Object.keys(b).length>0&&(n[r]=b);}});};return s(t),n},c=(t,e,i=false)=>Object.entries(e).every(([n,a])=>{if(n==="$and")return a.every(l=>c(t,l,i));if(n==="$or")return a.some(l=>c(t,l,i));let s=(a==null?void 0:a.$eq)!==void 0?a==null?void 0:a.$eq:a;if(typeof a=="object"&&a!==null&&(a==null?void 0:a.$eq)===void 0){if(a.$in!==void 0){let r=t[n];return r===void 0?false:i?!a.$in.includes(r):a.$in.includes(r)}if(a.$not!==void 0&&!i)return c(t,{[n]:a.$not},true);if(a.$gt!==void 0){let r=t[n];return typeof r!="number"?false:i?r<=a.$gt:r>a.$gt}if(a.$gte!==void 0){let r=t[n];return typeof r!="number"?false:i?r<a.$gte:r>=a.$gte}if(a.$lt!==void 0){let r=t[n];return typeof r!="number"?false:i?r>=a.$lt:r<a.$lt}if(a.$lte!==void 0){let r=t[n];return typeof r!="number"?false:i?r>a.$lte:r<=a.$lte}let l=t[n];return !l||typeof l!="object"&&!Array.isArray(l)?false:Array.isArray(l)?i?!l.some(r=>c(r,a,false)):l.some(r=>c(r,a,false)):c(l,a,i)}return i?t[n]!==s:t[n]===s}),T={CRITICAL:0,ERROR:1,WARN:2,INFO:3,DEBUG:4},R=class{level;prefix;constructor(e={}){this.level=e.level??T.INFO,this.prefix=e.prefix?`[${e.prefix}] `:"";}critical(...e){this.level>=T.CRITICAL&&console.error(`${this.prefix}[CRITICAL]`,...e);}error(...e){this.level>=T.ERROR&&console.error(`${this.prefix}[ERROR]`,...e);}warn(...e){this.level>=T.WARN&&console.warn(`${this.prefix}[WARN]`,...e);}info(...e){this.level>=T.INFO&&console.log(`${this.prefix}[INFO]`,...e);}debug(...e){this.level>=T.DEBUG&&console.log(`${this.prefix}[DEBUG]`,...e);}setLevel(e){this.level=e;}getLevel(){return this.level}},ee=t=>new R(t);export{V as a,$ as b,o as c,m as d,y as e,v as f,q as g,p as h,N as i,D as j,W as k,L as l,P as m,x as n,k as o,g as p,H as q,f as r,z as s,h as t,B as u,Z as v,C as w,c as x,T as y,ee as z};
package/dist/client.d.ts CHANGED
@@ -1,5 +1,2 @@
1
- export { d as Client, c as ClientEvents, C as ClientOptions, b as ConnectionStateChangeEvent, M as MessageReceivedEvent, S as SubscriptionProvider, e as createClient, u as useLiveQuery } from './index-BgowrRDR.js';
2
- import 'react/jsx-runtime';
1
+ export { n as Client, m as ClientEvents, C as ClientOptions, a as ClientRouterConstraint, e as ClientStorageLoadedEvent, d as ConnectionStateChangeEvent, f as DataLoadReplyEvent, D as DataLoadRequestedEvent, M as MessageReceivedEvent, h as MutationReceivedEvent, i as MutationRejectedEvent, g as MutationSentEvent, O as OptimisticMutationAppliedEvent, l as OptimisticMutationUndoneEvent, Q as QueryExecutedEvent, k as StoreStateUpdatedEvent, S as SubscriptionCreatedEvent, j as SubscriptionRemovedEvent, o as createClient, u as useLiveQuery, c as useLoadData } from './index-BFevRjd8.js';
3
2
  import 'zod';
4
- import 'zod/v3';
5
- import 'zod/v4/core';
package/dist/client.js CHANGED
@@ -1 +1 @@
1
- import {d,c,a,f,e,b}from'./chunk-WZ7JXHZL.js';import {useSyncExternalStore,useEffect}from'react';import {xxHash32}from'js-xxhash';import {jsx,Fragment}from'react/jsx-runtime';import {z}from'zod';import {stringify}from'qs';import ne from'fast-deep-equal';import {openDB}from'idb';var y=u=>xxHash32(JSON.stringify(u)).toString(32),g=(u,e,t=false)=>Object.entries(e).every(([i,s])=>{if(i==="$and")return s.every(n=>g(u,n,t));if(i==="$or")return s.some(n=>g(u,n,t));let r=(s==null?void 0:s.$eq)!==void 0?s==null?void 0:s.$eq:s;if(typeof s=="object"&&s!==null&&(s==null?void 0:s.$eq)===void 0){if(s.$in!==void 0){let n=u[i];return n===void 0?false:t?!s.$in.includes(n):s.$in.includes(n)}if(s.$not!==void 0&&!t)return g(u,{[i]:s.$not},true);if(s.$gt!==void 0){let n=u[i];return typeof n!="number"?false:t?n<=s.$gt:n>s.$gt}if(s.$gte!==void 0){let n=u[i];return typeof n!="number"?false:t?n<s.$gte:n>=s.$gte}if(s.$lt!==void 0){let n=u[i];return typeof n!="number"?false:t?n>=s.$lt:n<s.$lt}if(s.$lte!==void 0){let n=u[i];return typeof n!="number"?false:t?n>s.$lte:n<=s.$lte}return !u[i]||typeof u[i]!="object"?false:g(u[i],s,t)}return t?u[i]!==r:u[i]===r});var R=class{subscriptions=new Map;getOrStoreSubscription(e){let t=y(e);return this.subscriptions.has(t)?this.subscriptions.get(t).subscribe:(this.subscriptions.set(t,{subscribe:i=>{var r;(r=this.subscriptions.get(t))==null||r.callbacks.add(i);let s=e.subscribe(()=>{var n;(n=this.subscriptions.get(t))==null||n.callbacks.forEach(o=>{o();});});return ()=>{var n;(n=this.subscriptions.get(t))==null||n.callbacks.delete(i),setTimeout(()=>{var o;((o=this.subscriptions.get(t))==null?void 0:o.callbacks.size)===0&&(this.subscriptions.delete(t),s());},10);}},callbacks:new Set}),this.subscriptions.get(t).subscribe)}},N=new R,le=u=>useSyncExternalStore(N.getOrStoreSubscription(u),u.get),ue=({children:u,client:e})=>(useEffect(()=>{e.subscribe();},[e.subscribe]),jsx(Fragment,{children:u}));var $=z.object({resource:z.string(),where:z.record(z.string(),z.any()).optional(),include:z.record(z.string(),z.any()).optional(),lastSyncedAt:z.string().optional(),limit:z.coerce.number().optional(),sort:z.array(z.object({key:z.string(),direction:z.enum(["asc","desc"])})).optional()}),C=z.record(z.string(),z.object({value:z.string().or(z.number()).or(z.boolean()).or(z.date()).nullable(),_meta:z.object({timestamp:z.string().optional().nullable()}).optional()})),Y=C.superRefine((u,e)=>{u.id&&e.addIssue({code:z.ZodIssueCode.custom,message:"Payload cannot have an id"});}),G=z.object({id:z.string().optional(),type:z.literal("MUTATE"),resource:z.string(),resourceId:z.string().optional()}),j=G.extend({procedure:z.string(),payload:z.any().optional()}),k=G.extend({procedure:z.enum(["INSERT","UPDATE"]),payload:Y});z.union([k,j]);var v=z.string(),F=z.object({id:v,type:z.literal("SUBSCRIBE"),resource:z.string()}),Z=$.extend({id:v,type:z.literal("QUERY")}),I=k.extend({id:v}),X=j.extend({id:v}),ee=z.union([X,I]);z.union([F,Z,ee]);var te=z.object({id:v,type:z.literal("REJECT"),resource:z.string(),message:z.string().optional()}),ie=z.object({id:v,type:z.literal("REPLY"),data:z.any()}),H=z.union([te,ie,I]),Q=z.object({resource:z.string(),data:z.record(z.string(),C)});var M=class{ws=null;url;autoConnect;autoReconnect;reconnectTimeout;reconnectLimit;reconnectAttempts=0;eventListeners=new Map;reconnectTimer=null;intentionallyDisconnected=false;credentials;constructor(e){this.url=e.url,this.autoConnect=e.autoConnect??false,this.autoReconnect=e.autoReconnect??false,this.reconnectTimeout=e.reconnectTimeout??5e3,this.reconnectLimit=e.reconnectLimit,this.credentials=e.credentials,this.autoConnect&&this.connect();}connected(){var e;return ((e=this.ws)==null?void 0:e.readyState)===WebSocket.OPEN}async connect(){if(this.ws&&(this.ws.readyState===WebSocket.OPEN||this.ws.readyState===WebSocket.CONNECTING))return;this.intentionallyDisconnected=false;let e=await b(this.credentials);this.ws=new WebSocket(this.url+(e?`?${stringify(e)}`:"")),this.ws.addEventListener("open",this.handleOpen.bind(this)),this.ws.addEventListener("close",this.handleClose.bind(this)),this.ws.addEventListener("error",this.handleError.bind(this)),this.ws.addEventListener("message",this.handleMessage.bind(this));}disconnect(){this.reconnectTimer&&(clearTimeout(this.reconnectTimer),this.reconnectTimer=null),this.intentionallyDisconnected=true,this.ws&&(this.ws.close(),this.ws=null);}addEventListener(e,t){var i;this.eventListeners.has(e)||this.eventListeners.set(e,new Set),(i=this.eventListeners.get(e))==null||i.add(t);}removeEventListener(e,t){var i;this.eventListeners.has(e)&&((i=this.eventListeners.get(e))==null||i.delete(t));}send(e){if(this.ws&&this.ws.readyState===WebSocket.OPEN)this.ws.send(e);else throw new Error("WebSocket is not open")}handleOpen(e){this.reconnectAttempts=0,this.dispatchEvent("open",e),this.dispatchEvent("connectionChange",{open:true});}handleClose(e){this.dispatchEvent("close",e),this.dispatchEvent("connectionChange",{open:false}),this.autoReconnect&&!this.intentionallyDisconnected&&this.scheduleReconnect();}handleError(e){var t,i;this.dispatchEvent("error",e),this.dispatchEvent("connectionChange",{open:false}),(i=(t=e.error)==null?void 0:t.message)!=null&&i.includes("non-101")&&(this.ws&&(this.ws.close(),this.ws=null),this.autoReconnect&&!this.intentionallyDisconnected&&this.scheduleReconnect());}handleMessage(e){this.dispatchEvent("message",e);}scheduleReconnect(){this.reconnectLimit&&this.reconnectAttempts>=this.reconnectLimit||(this.reconnectAttempts++,this.reconnectTimer=setTimeout(()=>{this.connect();},this.reconnectTimeout));}dispatchEvent(e,t){var i;(i=this.eventListeners.get(e))==null||i.forEach(s=>{s(t);});}};var w=class{nodes;constructor(){this.nodes=new Map;}createNode(e,t,i){if(this.nodes.has(e))throw new Error(`Node with id ${e} already exists`);let s={id:e,type:t,referencedBy:new Map(i.map(r=>[r,new Set])),references:new Map,subscriptions:new Set};return this.nodes.set(e,s),s}getNode(e){return this.nodes.get(e)}hasNode(e){return this.nodes.has(e)}createLink(e,t){let i=this.nodes.get(e),s=this.nodes.get(t);if(!i)throw new Error(`Source node with id ${e} does not exist`);if(!s)throw new Error(`Target node with id ${t} does not exist`);i.references.set(s.type,t);let r=s.referencedBy.get(i.type);r&&r instanceof Set?r.add(e):s.referencedBy.set(i.type,e),this.notifySubscribers(t);}removeLink(e,t){let i=this.nodes.get(e);if(!i)throw new Error(`Node with id ${e} does not exist`);let s=i.references.get(t);if(!s)return;i.references.delete(t);let r=this.nodes.get(s);if(!r)return;let n=r.referencedBy.get(i.type);n&&(n instanceof Set?n.delete(e):r.referencedBy.delete(i.type),this.notifySubscribers(s)),this.notifySubscribers(e);}subscribe(e,t){let i=this.nodes.get(e);if(!i)throw new Error(`Node with id ${e} does not exist`);return i.subscriptions.add(t),()=>{i.subscriptions.delete(t);}}removeNode(e){let t=this.nodes.get(e);t&&(Array.from(t.referencedBy.entries()).forEach(([i,s])=>{(s instanceof Set?Array.from(s.values()):[s]).forEach(n=>{let o=this.nodes.get(n);!o||!o.references.get(i)||(o.references.delete(i),this.notifySubscribers(n));});}),this.nodes.delete(e));}updateNode(e,t){let i=this.nodes.get(e);if(!i)throw new Error(`Node with id ${e} does not exist`);t(i),this.notifySubscribers(e);}notifySubscribers(e){let t=this.nodes.get(e);t&&Array.from(t.subscriptions).forEach(i=>{try{i(e);}catch(s){console.error(`Error in node subscription for node ${e}:`,s);}});}getAllNodes(){return Array.from(this.nodes.values())}};var A="__meta",E="databases",O=class{db;async init(e,t){var p,h;if(typeof window>"u")return;let s=((p=(await window.indexedDB.databases()).find(c=>c.name===t))==null?void 0:p.version)??1,r=await y(e),n=Object.fromEntries(await Promise.all(Object.entries(e).map(async([c,d])=>[c,await y(d)]))),o=await openDB("live-state-databases",1,{upgrade(c){c.objectStoreNames.contains(E)||c.createObjectStore(E);}}),a=(h=await this.getAll(o,E))==null?void 0:h[t];(a==null?void 0:a.schemaHash)!==r&&s++,this.db=await openDB(t,s,{async upgrade(c){[...Object.keys(e),A].forEach(d=>{(a==null?void 0:a.objectHashes[d])!==n[d]&&c.objectStoreNames.contains(d)&&c.deleteObjectStore(d),c.objectStoreNames.contains(d)||c.createObjectStore(d);}),await o.put(E,{schemaHash:r,objectHashes:n},t);},blocking(){window.location.reload();},blocked(){window.location.reload();}});}async get(e){return await this.getAll(this.db,e)??{}}getOne(e,t){return this.db?this.db.get(e,t):new Promise(i=>i(void 0))}set(e,t,i){var s;return (s=this.db)==null?void 0:s.put(e,i,t)}delete(e,t){var i;return (i=this.db)==null?void 0:i.delete(e,t)}getMeta(e){return this.db?this.db.get(A,e):new Promise(t=>t(void 0))}setMeta(e,t){var i;return (i=this.db)==null?void 0:i.put(A,t,e)}async getAll(e,t){if(!e)return;if(e.getAllRecords)return e.getAllRecords(t);let[i,s]=await Promise.all([e.getAll(t),e.getAllKeys(t)]);return Object.fromEntries(i.map((r,n)=>[s[n],r]))}};var T=class{constructor(e,t,i){this.schema=e;this.kvStorage=new O,t!==false&&this.kvStorage.init(this.schema,t.name).then(()=>{this.kvStorage.getMeta("mutationStack").then(s=>{!s||Object.keys(s).length===0||(this.optimisticMutationStack=s,i==null||i(this.optimisticMutationStack));}).then(()=>{Object.entries(this.schema).forEach(([s,r])=>{this.kvStorage.get(s).then(n=>{!n||Object.keys(n).length===0||this.loadConsolidatedState(s,n);});});}).catch(s=>{console.error("Failed to load state from storage",s);});});}rawObjPool={};optimisticMutationStack={};optimisticObjGraph=new w;optimisticRawObjPool={};collectionSubscriptions=new Map;querySnapshots={};kvStorage;get(e$1,t,i=false){var n;let s=t??y(e$1);if(this.querySnapshots[s]&&!i){let o=this.querySnapshots[s];if(o)return o}let r=((n=e$1.where)!=null&&n.id?[e$1.where.id]:Object.keys(this.optimisticRawObjPool[e$1.resource]??{})).flatMap(o=>{let a=f(this.materializeOneWithInclude(o,e$1.include));return a?[a]:[]});if(e$1.sort&&e$1.sort.length>0){let o=(a,p)=>{for(let h of e$1.sort){let c=a[h.key],d=p[h.key];if(c<d)return h.direction==="asc"?-1:1;if(c>d)return h.direction==="asc"?1:-1}return 0};r.sort(o);}if(e$1.where||e$1.limit){let o=e$1.where?a=>g(a,e$1.where):()=>true;r=e(r,o,e$1.limit);}return i||(this.querySnapshots[s]=r),r}subscribe(e,t){var r;let i=y(e);return this.collectionSubscriptions.get(i)||this.collectionSubscriptions.set(i,{callbacks:new Set,query:e,flatInclude:e.include?this.flattenIncludes(e.include,e.resource):void 0}),(r=this.collectionSubscriptions.get(i))==null||r.callbacks.add(t),()=>{var n,o;(n=this.collectionSubscriptions.get(i))==null||n.callbacks.delete(t),((o=this.collectionSubscriptions.get(i))==null?void 0:o.callbacks.size)===0&&(this.collectionSubscriptions.delete(i),delete this.querySnapshots[i]);}}addMutation(e,t,i=false){var n,o,a;let s=this.schema[e];if(console.log("Adding mutation",t),!s)throw new Error("Schema not found");let r=(n=this.optimisticRawObjPool[e])==null?void 0:n[t.resourceId];if(i)this.optimisticMutationStack[e]??=[],this.optimisticMutationStack[e].push(t);else {this.optimisticMutationStack[e]=((a=(o=this.optimisticMutationStack)==null?void 0:o[e])==null?void 0:a.filter(c=>c.id!==t.id))??[],this.rawObjPool[e]??={};let p={value:{...this.schema[e].mergeMutation("set",t.payload,this.rawObjPool[e][t.resourceId])[0].value,id:{value:t.resourceId}}};this.rawObjPool[e][t.resourceId]=p;let h=p.value;delete h.id,this.kvStorage.set(e,t.resourceId,h);}this.kvStorage.setMeta("mutationStack",this.optimisticMutationStack),this.updateRawObjPool(e,t.resourceId,t.payload,r);}undoMutation(e,t){var n,o;if(!this.optimisticMutationStack[e])return;let i=(n=this.optimisticMutationStack[e])==null?void 0:n.findIndex(a=>a.id===t);if(i===-1)return;let s=this.optimisticMutationStack[e][i];console.log("Removing mutation",s);let r=(o=this.optimisticRawObjPool[e])==null?void 0:o[s.resourceId];this.optimisticMutationStack[e].splice(i,1),this.kvStorage.setMeta("mutationStack",this.optimisticMutationStack),this.updateRawObjPool(e,s.resourceId,Object.fromEntries(Object.entries(s.payload).map(([a])=>[a,{value:null,_meta:{}}])),r);}loadConsolidatedState(e,t){Object.entries(t).forEach(([i,s])=>{this.addMutation(e,{id:i,type:"MUTATE",resource:e,resourceId:i,procedure:"INSERT",payload:s});});}updateRawObjPool(e,t,i,s){var o;if(!this.schema[e])return;let r=(o=this.rawObjPool[e])==null?void 0:o[t],n=(this.optimisticMutationStack[e]??[]).reduce((a,p)=>p.resourceId!==t?a:this.schema[e].mergeMutation("set",p.payload,a)[0],r);if(this.optimisticRawObjPool[e]??={},n?this.optimisticRawObjPool[e][t]={value:{...n.value,id:{value:t}}}:delete this.optimisticRawObjPool[e][t],!(!this.optimisticObjGraph.hasNode(t)&&!n)){if(this.optimisticObjGraph.hasNode(t)||this.optimisticObjGraph.createNode(t,e,Object.values(this.schema[e].relations).flatMap(a=>a.type==="many"?[a.entity.name]:[])),Object.keys(this.schema[e].relations).length>0){let a=Object.fromEntries(Object.entries(this.schema[e].relations).flatMap(([p,h])=>h.type==="one"?[[h.relationalColumn,p]]:[]));Object.entries(i).forEach(([p,h])=>{let c=this.schema[e].relations[a[p]];if(!a[p])return;let d=s==null?void 0:s.value[p],[,m]=c.mergeMutation("set",h,d);if(m){if(!this.optimisticObjGraph.hasNode(m.value)){let S=c.entity.name;this.optimisticObjGraph.createNode(m.value,S,Object.values(this.schema[S].relations).flatMap(x=>x.type==="many"?[x.entity.name]:[]));}d!=null&&d.value&&this.optimisticObjGraph.removeLink(t,c.entity.name),this.optimisticObjGraph.createLink(t,m.value);}});}this.notifyCollectionSubscribers(e),this.optimisticObjGraph.notifySubscribers(t);}}materializeOneWithInclude(e,t={}){var a;if(!e)return;let i=this.optimisticObjGraph.getNode(e);if(!i)return;let s=i.type,r=(a=this.optimisticRawObjPool[s])==null?void 0:a[e];if(!r)return;let[n,o]=Object.entries(t).reduce((p,[h,c])=>{let d=this.schema[s].relations[h];return d&&(d.type==="one"?p[0].push([h,d.entity.name,c??true]):d.type==="many"&&p[1].push([h,d.entity.name,c??true])),p},[[],[]]);return {value:{...r.value,...Object.fromEntries(n.map(([p,h,c])=>[p,this.materializeOneWithInclude(i.references.get(h),typeof c=="object"&&c!==null?c:{})])),...Object.fromEntries(o.map(([p,h,c])=>{let d=i.referencedBy.get(h),m=d instanceof Set;return [p,m?{value:Array.from(d.values()).map(S=>this.materializeOneWithInclude(S,typeof c=="object"&&c!==null?c:{}))}:this.materializeOneWithInclude(d,typeof c=="object"&&c!==null?c:{})]}))}}}notifyCollectionSubscribers(e){this.collectionSubscriptions.forEach(t=>{if(t.query.resource===e||t.flatInclude&&t.flatInclude.includes(e)){let i=y(t.query),s=this.querySnapshots[i],r=this.get(t.query,void 0,true);if(ne(r,s))return;this.querySnapshots[i]=r,t.callbacks.forEach(n=>{n(r);});}});}flattenIncludes(e,t){let i=[];return Object.entries(e).forEach(([s,r])=>{var a;let n=(a=this.schema[t])==null?void 0:a.relations[s];if(!n)return;let o=n.entity.name;i.push(o),typeof r=="object"&&r!==null&&i.push(...this.flattenIncludes(r,o));}),Array.from(new Set(i))}};var L=class{url;ws;store;remoteSubCounters={};eventListeners=new Set;replyHandlers={};constructor(e){var t,i,s,r;this.url=e.url,this.store=new T(e.schema,e.storage,n=>{var o,a;(a=(o=Object.values(n))==null?void 0:o.flat())==null||a.forEach(p=>{this.sendWsMessage(p);});}),this.ws=new M({url:e.url,autoConnect:((t=e.connection)==null?void 0:t.autoConnect)??true,autoReconnect:((i=e.connection)==null?void 0:i.autoReconnect)??true,reconnectTimeout:((s=e.connection)==null?void 0:s.reconnectTimeout)??5e3,reconnectLimit:(r=e.connection)==null?void 0:r.maxReconnectAttempts,credentials:e.credentials}),this.ws.addEventListener("message",n=>{this.handleServerMessage(n.data);}),this.ws.addEventListener("connectionChange",n=>{this.emitEvent({type:"CONNECTION_STATE_CHANGE",open:n.open}),n.open&&(Object.keys(this.store.schema).forEach(o=>{this.sendWsMessage({id:a(),type:"QUERY",resource:o});}),Object.entries(this.remoteSubCounters).forEach(([o,a$1])=>{a$1>0&&this.sendWsMessage({id:a(),type:"SUBSCRIBE",resource:o});}),Object.values(this.store.optimisticMutationStack).forEach(o=>{o&&o.forEach(a=>{this.sendWsMessage(a);});}));});}get(e){return this.store.get(e)}handleServerMessage(e){try{console.log("Message received from the server:",e);let t=H.parse(JSON.parse(e));if(console.log("Parsed message:",t),this.emitEvent({type:"MESSAGE_RECEIVED",message:t}),t.type==="MUTATE"){let{resource:i}=t;try{this.store.addMutation(i,t);}catch(s){console.error("Error merging mutation from the server:",s);}}else if(t.type==="REJECT")this.store.undoMutation(t.resource,t.id);else if(t.type==="REPLY"){let{id:i,data:s}=t;if(this.replyHandlers[i]){clearTimeout(this.replyHandlers[i].timeoutHandle),this.replyHandlers[i].handler(s);return}let r=Q.parse(s);this.store.loadConsolidatedState(r.resource,r.data);}}catch(t){console.error("Error parsing message from the server:",t);}}subscribeToRemote(e){return this.remoteSubCounters[e]=(this.remoteSubCounters[e]??0)+1,this.sendWsMessage({id:a(),type:"SUBSCRIBE",resource:e}),()=>{this.remoteSubCounters[e]-=1,this.remoteSubCounters[e];}}subscribe(e,t){return this.store.subscribe(e,t)}mutate(e,t,i,s){var n;let r={id:a(),type:"MUTATE",resource:e,payload:this.store.schema[e].encodeMutation("set",s,new Date().toISOString()),resourceId:t,procedure:i};(n=this.store)==null||n.addMutation(e,r,true),this.sendWsMessage(r);}genericMutate(e,t,i){if(!this.ws||!this.ws.connected())throw new Error("WebSocket not connected");let s={id:a(),type:"MUTATE",resource:e,procedure:t,payload:i};return this.sendWsMessage(s),new Promise((r,n)=>{this.replyHandlers[s.id]={timeoutHandle:setTimeout(()=>{delete this.replyHandlers[s.id],n(new Error("Reply timeout"));},5e3),handler:o=>{delete this.replyHandlers[s.id],r(o);}};})}addEventListener(e){return this.eventListeners.add(e),()=>{this.eventListeners.delete(e);}}sendWsMessage(e){var t;(t=this.ws)!=null&&t.connected()&&this.ws.send(JSON.stringify(e));}emitEvent(e){this.eventListeners.forEach(t=>{t(e);});}},Ie=u=>{let e=new L(u);return {client:{ws:e.ws,subscribe:t=>{let i=[];for(let s of t??Object.keys(e.store.schema))i.push(e.subscribeToRemote(s));return ()=>{console.log("Removing listeners",i),i.forEach(s=>{s();});}},addEventListener:t=>e.addEventListener(t)},store:{query:Object.entries(u.schema).reduce((t,[i,s])=>(t[i]=c._init(s,e),t),{}),mutate:d(()=>{},{apply:(t,i,s)=>{if(i.length<2)return;if(i.length>2)throw new Error("Trying to access an invalid path");let[r,n]=i;if(n==="insert"){let{id:o,...a}=s[0];return e.mutate(r,o,"INSERT",a)}if(n==="update"){let[o,a]=s;return e.mutate(r,o,"UPDATE",a)}return e.genericMutate(r,n,s[0])}})}}};export{ue as SubscriptionProvider,Ie as createClient,le as useLiveQuery};
1
+ import {d,c,a,f,e,b as b$1}from'./chunk-AHF6GNMI.js';import {useSyncExternalStore,useEffect}from'react';import {xxHash32}from'js-xxhash';import {z as z$1}from'zod';import {stringify}from'qs';import ue from'fast-deep-equal';import {openDB}from'idb';var g=p=>xxHash32(JSON.stringify(p)).toString(32);var b=(p,e,t=false)=>Object.entries(e).every(([s,i])=>{if(s==="$and")return i.every(r=>b(p,r,t));if(s==="$or")return i.some(r=>b(p,r,t));let o=(i==null?void 0:i.$eq)!==void 0?i==null?void 0:i.$eq:i;if(typeof i=="object"&&i!==null&&(i==null?void 0:i.$eq)===void 0){if(i.$in!==void 0){let n=p[s];return n===void 0?false:t?!i.$in.includes(n):i.$in.includes(n)}if(i.$not!==void 0&&!t)return b(p,{[s]:i.$not},true);if(i.$gt!==void 0){let n=p[s];return typeof n!="number"?false:t?n<=i.$gt:n>i.$gt}if(i.$gte!==void 0){let n=p[s];return typeof n!="number"?false:t?n<i.$gte:n>=i.$gte}if(i.$lt!==void 0){let n=p[s];return typeof n!="number"?false:t?n>=i.$lt:n<i.$lt}if(i.$lte!==void 0){let n=p[s];return typeof n!="number"?false:t?n>i.$lte:n<=i.$lte}let r=p[s];return !r||typeof r!="object"&&!Array.isArray(r)?false:Array.isArray(r)?t?!r.some(n=>b(n,i,false)):r.some(n=>b(n,i,false)):b(r,i,t)}return t?p[s]!==o:p[s]===o}),v={CRITICAL:0,ERROR:1,WARN:2,INFO:3,DEBUG:4},L=class{level;prefix;constructor(e={}){this.level=e.level??v.INFO,this.prefix=e.prefix?`[${e.prefix}] `:"";}critical(...e){this.level>=v.CRITICAL&&console.error(`${this.prefix}[CRITICAL]`,...e);}error(...e){this.level>=v.ERROR&&console.error(`${this.prefix}[ERROR]`,...e);}warn(...e){this.level>=v.WARN&&console.warn(`${this.prefix}[WARN]`,...e);}info(...e){this.level>=v.INFO&&console.log(`${this.prefix}[INFO]`,...e);}debug(...e){this.level>=v.DEBUG&&console.log(`${this.prefix}[DEBUG]`,...e);}setLevel(e){this.level=e;}getLevel(){return this.level}},$=p=>new L(p);var x=class{subscriptions=new Map;getOrStoreSubscription(e){let t=g(e);return this.subscriptions.has(t)?this.subscriptions.get(t).subscribe:(this.subscriptions.set(t,{subscribe:s=>{var o;(o=this.subscriptions.get(t))==null||o.callbacks.add(s);let i=e.subscribe(()=>{var r;(r=this.subscriptions.get(t))==null||r.callbacks.forEach(n=>{n();});});return ()=>{var r;(r=this.subscriptions.get(t))==null||r.callbacks.delete(s),setTimeout(()=>{var n;((n=this.subscriptions.get(t))==null?void 0:n.callbacks.size)===0&&(this.subscriptions.delete(t),i());},10);}},callbacks:new Set}),this.subscriptions.get(t).subscribe)}},Z=new x,ye=p=>useSyncExternalStore(Z.getOrStoreSubscription(p),p.get),fe=(p,e)=>{useEffect(()=>{let t=p.load(e.buildQueryRequest());return ()=>{t();}},[e,p.load]);};var T=z$1.object({resource:z$1.string(),where:z$1.record(z$1.string(),z$1.any()).optional(),include:z$1.record(z$1.string(),z$1.any()).optional(),lastSyncedAt:z$1.string().optional(),limit:z$1.coerce.number().optional(),sort:z$1.array(z$1.object({key:z$1.string(),direction:z$1.enum(["asc","desc"])})).optional()}),I=z$1.record(z$1.string(),z$1.object({value:z$1.any().nullable(),_meta:z$1.object({timestamp:z$1.string().optional().nullable()}).optional()})),ee=I.superRefine((p,e)=>{p.id&&e.addIssue({code:z$1.ZodIssueCode.custom,message:"Payload cannot have an id"});}),Q=z$1.object({id:z$1.string().optional(),type:z$1.literal("MUTATE"),resource:z$1.string(),resourceId:z$1.string().optional()}),j=Q.extend({procedure:z$1.string(),payload:z$1.any().optional()}),k=Q.extend({procedure:z$1.enum(["INSERT","UPDATE"]),payload:ee});z$1.union([k,j]);var E=z$1.string(),te=T.extend({id:E,type:z$1.literal("SUBSCRIBE")}),ie=T.extend({id:E,type:z$1.literal("UNSUBSCRIBE")}),se=T.extend({id:E,type:z$1.literal("QUERY")}),G=k.extend({id:E}),ne=j.extend({id:E}),re=z$1.union([ne,G]);z$1.union([te,se,re,ie]);var oe=z$1.object({id:E,type:z$1.literal("REJECT"),resource:z$1.string(),message:z$1.string().optional()}),ae=z$1.object({id:E,type:z$1.literal("REPLY"),data:z$1.any()}),z=z$1.union([oe,ae,G]),q=z$1.object({resource:z$1.string(),data:z$1.array(I)});var R=class{ws=null;url;autoConnect;autoReconnect;reconnectTimeout;reconnectLimit;reconnectAttempts=0;eventListeners=new Map;reconnectTimer=null;intentionallyDisconnected=false;credentials;constructor(e){this.url=e.url,this.autoConnect=e.autoConnect??false,this.autoReconnect=e.autoReconnect??false,this.reconnectTimeout=e.reconnectTimeout??5e3,this.reconnectLimit=e.reconnectLimit,this.credentials=e.credentials,this.autoConnect&&this.connect();}connected(){var e;return ((e=this.ws)==null?void 0:e.readyState)===WebSocket.OPEN}async connect(){if(this.ws&&(this.ws.readyState===WebSocket.OPEN||this.ws.readyState===WebSocket.CONNECTING))return;this.intentionallyDisconnected=false;let e=await b$1(this.credentials);this.ws=new WebSocket(this.url+(e?`?${stringify(e)}`:"")),this.ws.addEventListener("open",this.handleOpen.bind(this)),this.ws.addEventListener("close",this.handleClose.bind(this)),this.ws.addEventListener("error",this.handleError.bind(this)),this.ws.addEventListener("message",this.handleMessage.bind(this));}disconnect(){this.reconnectTimer&&(clearTimeout(this.reconnectTimer),this.reconnectTimer=null),this.intentionallyDisconnected=true,this.ws&&(this.ws.close(),this.ws=null);}addEventListener(e,t){var s;this.eventListeners.has(e)||this.eventListeners.set(e,new Set),(s=this.eventListeners.get(e))==null||s.add(t);}removeEventListener(e,t){var s;this.eventListeners.has(e)&&((s=this.eventListeners.get(e))==null||s.delete(t));}send(e){if(this.ws&&this.ws.readyState===WebSocket.OPEN)this.ws.send(e);else throw new Error("WebSocket is not open")}handleOpen(e){this.reconnectAttempts=0,this.dispatchEvent("open",e),this.dispatchEvent("connectionChange",{open:true});}handleClose(e){this.dispatchEvent("close",e),this.dispatchEvent("connectionChange",{open:false}),this.autoReconnect&&!this.intentionallyDisconnected&&this.scheduleReconnect();}handleError(e){var t,s;this.dispatchEvent("error",e),this.dispatchEvent("connectionChange",{open:false}),(s=(t=e.error)==null?void 0:t.message)!=null&&s.includes("non-101")&&(this.ws&&(this.ws.close(),this.ws=null),this.autoReconnect&&!this.intentionallyDisconnected&&this.scheduleReconnect());}handleMessage(e){this.dispatchEvent("message",e);}scheduleReconnect(){this.reconnectLimit&&this.reconnectAttempts>=this.reconnectLimit||(this.reconnectAttempts++,this.reconnectTimer=setTimeout(()=>{this.connect();},this.reconnectTimeout));}dispatchEvent(e,t){var s;(s=this.eventListeners.get(e))==null||s.forEach(i=>{i(t);});}};var O=class{constructor(e){this.logger=e;this.nodes=new Map;}nodes;createNode(e,t,s){if(this.nodes.has(e))throw new Error(`Node with id ${e} already exists`);let i={id:e,type:t,referencedBy:new Map(s.map(o=>[o,new Set])),references:new Map,subscriptions:new Set};return this.nodes.set(e,i),i}getNode(e){return this.nodes.get(e)}hasNode(e){return this.nodes.has(e)}createLink(e,t){let s=this.nodes.get(e),i=this.nodes.get(t);if(!s)throw new Error(`Source node with id ${e} does not exist`);if(!i)throw new Error(`Target node with id ${t} does not exist`);s.references.set(i.type,t);let o=i.referencedBy.get(s.type);o&&o instanceof Set?o.add(e):i.referencedBy.set(s.type,e),this.notifySubscribers(t);}removeLink(e,t){let s=this.nodes.get(e);if(!s)throw new Error(`Node with id ${e} does not exist`);let i=s.references.get(t);if(!i)return;s.references.delete(t);let o=this.nodes.get(i);if(!o)return;let r=o.referencedBy.get(s.type);r&&(r instanceof Set?r.delete(e):o.referencedBy.delete(s.type),this.notifySubscribers(i)),this.notifySubscribers(e);}subscribe(e,t){let s=this.nodes.get(e);if(!s)throw new Error(`Node with id ${e} does not exist`);return s.subscriptions.add(t),()=>{s.subscriptions.delete(t);}}removeNode(e){let t=this.nodes.get(e);t&&(Array.from(t.referencedBy.entries()).forEach(([s,i])=>{(i instanceof Set?Array.from(i.values()):[i]).forEach(r=>{let n=this.nodes.get(r);!n||!n.references.get(s)||(n.references.delete(s),this.notifySubscribers(r));});}),this.nodes.delete(e));}updateNode(e,t){let s=this.nodes.get(e);if(!s)throw new Error(`Node with id ${e} does not exist`);t(s),this.notifySubscribers(e);}notifySubscribers(e){let t=this.nodes.get(e);t&&Array.from(t.subscriptions).forEach(s=>{var i;try{s(e);}catch(o){(i=this.logger)==null||i.error(`Error in node subscription for node ${e}:`,o);}});}getAllNodes(){return Array.from(this.nodes.values())}};var D="__meta",C="databases",w=class{db;async init(e,t){var l,d;if(typeof window>"u")return;let i=((l=(await window.indexedDB.databases()).find(c=>c.name===t))==null?void 0:l.version)??1,o=await g(e),r=Object.fromEntries(await Promise.all(Object.entries(e).map(async([c,u])=>[c,await g(u)]))),n=await openDB("live-state-databases",1,{upgrade(c){c.objectStoreNames.contains(C)||c.createObjectStore(C);}}),a=(d=await this.getAll(n,C))==null?void 0:d[t];(a==null?void 0:a.schemaHash)!==o&&i++,this.db=await openDB(t,i,{async upgrade(c){[...Object.keys(e),D].forEach(u=>{(a==null?void 0:a.objectHashes[u])!==r[u]&&c.objectStoreNames.contains(u)&&c.deleteObjectStore(u),c.objectStoreNames.contains(u)||c.createObjectStore(u);}),await n.put(C,{schemaHash:o,objectHashes:r},t);},blocking(){window.location.reload();},blocked(){window.location.reload();}});}async get(e){return await this.getAll(this.db,e)??{}}getOne(e,t){return this.db?this.db.get(e,t):new Promise(s=>s(void 0))}set(e,t,s){var i;return (i=this.db)==null?void 0:i.put(e,s,t)}delete(e,t){var s;return (s=this.db)==null?void 0:s.delete(e,t)}getMeta(e){return this.db?this.db.get(D,e):new Promise(t=>t(void 0))}setMeta(e,t){var s;return (s=this.db)==null?void 0:s.put(D,t,e)}async getAll(e,t){if(!e)return;if(e.getAllRecords)return e.getAllRecords(t);let[s,i]=await Promise.all([e.getAll(t),e.getAllKeys(t)]);return Object.fromEntries(s.map((o,r)=>[i[r],o]))}};var A=class{constructor(e,t,s,i,o){this.schema=e;this.logger=s,this.optimisticObjGraph=new O(s),this.kvStorage=new w,t!==false&&this.kvStorage.init(this.schema,t.name).then(()=>{this.kvStorage.getMeta("mutationStack").then(r=>{!r||Object.keys(r).length===0||(this.optimisticMutationStack=r,i==null||i(this.optimisticMutationStack));}).then(()=>{Object.entries(this.schema).forEach(([r])=>{this.kvStorage.get(r).then(n=>{if(!n||Object.keys(n).length===0){o==null||o(r,0);return}let a=Object.values(n);o==null||o(r,a.length),this.loadConsolidatedState(r,a);});});}).catch(r=>{s.debug("Storage initialization failed (may not be available in this environment):",r);});});}rawObjPool={};optimisticMutationStack={};optimisticObjGraph;optimisticRawObjPool={};logger;collectionSubscriptions=new Map;querySnapshots={};kvStorage;get(e$1,t,s=false){var r;let i=t??g(e$1);if(this.querySnapshots[i]&&!s){let n=this.querySnapshots[i];if(n)return n}let o=((r=e$1.where)!=null&&r.id?[e$1.where.id]:Object.keys(this.optimisticRawObjPool[e$1.resource]??{})).flatMap(n=>{let a=f(this.materializeOneWithInclude(n,e$1.include));return a?[a]:[]});if(e$1.sort&&e$1.sort.length>0){let n=(a,l)=>{for(let d of e$1.sort){let c=a[d.key],u=l[d.key];if(c<u)return d.direction==="asc"?-1:1;if(c>u)return d.direction==="asc"?1:-1}return 0};o.sort(n);}if(e$1.where||e$1.limit){let n=e$1.where?a=>b(a,e$1.where):()=>true;o=e(o,n,e$1.limit);}return s||(this.querySnapshots[i]=o),o}subscribe(e,t){var o;let s=g(e);return this.collectionSubscriptions.get(s)||this.collectionSubscriptions.set(s,{callbacks:new Set,query:e,flatInclude:e.include?this.flattenIncludes(e.include,e.resource):void 0}),(o=this.collectionSubscriptions.get(s))==null||o.callbacks.add(t),()=>{var r,n;(r=this.collectionSubscriptions.get(s))==null||r.callbacks.delete(t),((n=this.collectionSubscriptions.get(s))==null?void 0:n.callbacks.size)===0&&(this.collectionSubscriptions.delete(s),delete this.querySnapshots[s]);}}addMutation(e,t,s=false){var r,n,a;let i=this.schema[e];if(this.logger.debug("Adding mutation",t),!i)throw new Error("Schema not found");let o=(r=this.optimisticRawObjPool[e])==null?void 0:r[t.resourceId];if(s)this.optimisticMutationStack[e]??=[],this.optimisticMutationStack[e].push(t);else {this.optimisticMutationStack[e]=((a=(n=this.optimisticMutationStack)==null?void 0:n[e])==null?void 0:a.filter(c=>c.id!==t.id))??[],this.rawObjPool[e]??={};let l={value:{...this.schema[e].mergeMutation("set",t.payload,this.rawObjPool[e][t.resourceId])[0].value,id:{value:t.resourceId}}};this.rawObjPool[e][t.resourceId]=l;let d=l.value;delete d.id,this.kvStorage.set(e,t.resourceId,d);}this.kvStorage.setMeta("mutationStack",this.optimisticMutationStack),this.updateRawObjPool(e,t.resourceId,t.payload,o);}undoMutation(e,t){var r,n;if(!this.optimisticMutationStack[e])return;let s=(r=this.optimisticMutationStack[e])==null?void 0:r.findIndex(a=>a.id===t);if(s===-1)return;let i=this.optimisticMutationStack[e][s];this.logger.debug("Removing mutation",i);let o=(n=this.optimisticRawObjPool[e])==null?void 0:n[i.resourceId];this.optimisticMutationStack[e].splice(s,1),this.kvStorage.setMeta("mutationStack",this.optimisticMutationStack),this.updateRawObjPool(e,i.resourceId,Object.fromEntries(Object.entries(i.payload).map(([a])=>[a,{value:null,_meta:{}}])),o);}loadConsolidatedState(e,t){t.forEach(s=>{var n;let i=(n=s.id)==null?void 0:n.value;if(!i)return;let{cleanedPayload:o,nestedMutations:r}=this.extractNestedRelations(e,s);r.forEach(a=>{this.addMutation(a.resource,a);}),this.addMutation(e,{id:i,type:"MUTATE",resource:e,resourceId:i,procedure:"INSERT",payload:o});});}extractNestedRelations(e,t){let s=this.schema[e],i={...t},o=[];return s!=null&&s.relations?(Object.entries(t).forEach(([r,n])=>{var c;let a=s.relations[r];if(!a)return;let l=a.entity.name,d=n==null?void 0:n.value;if(a.type==="one"){if(d&&typeof d=="object"&&!Array.isArray(d)&&((c=d.id)!=null&&c.value)){let u=d.id.value,f={...d},{cleanedPayload:m,nestedMutations:S}=this.extractNestedRelations(l,f);o.push(...S),o.push({id:u,type:"MUTATE",resource:l,resourceId:u,procedure:"INSERT",payload:m}),delete i[r];}}else a.type==="many"&&Array.isArray(d)&&(d.forEach(u=>{var f,m;if(u&&typeof u=="object"&&!Array.isArray(u)&&((m=(f=u.value)==null?void 0:f.id)!=null&&m.value)){let S=u.value.id.value,H={...u.value},{cleanedPayload:V,nestedMutations:J}=this.extractNestedRelations(l,H);o.push(...J),o.push({id:S,type:"MUTATE",resource:l,resourceId:S,procedure:"INSERT",payload:V});}}),delete i[r]);}),{cleanedPayload:i,nestedMutations:o}):{cleanedPayload:i,nestedMutations:o}}updateRawObjPool(e,t,s,i){var n;if(!this.schema[e])return;let o=(n=this.rawObjPool[e])==null?void 0:n[t],r=(this.optimisticMutationStack[e]??[]).reduce((a,l)=>l.resourceId!==t?a:this.schema[e].mergeMutation("set",l.payload,a)[0],o);if(this.optimisticRawObjPool[e]??={},r?this.optimisticRawObjPool[e][t]={value:{...r.value,id:{value:t}}}:delete this.optimisticRawObjPool[e][t],!(!this.optimisticObjGraph.hasNode(t)&&!r)){if(this.optimisticObjGraph.hasNode(t)||this.optimisticObjGraph.createNode(t,e,Object.values(this.schema[e].relations).flatMap(a=>a.type==="many"?[a.entity.name]:[])),Object.keys(this.schema[e].relations).length>0){let a=Object.fromEntries(Object.entries(this.schema[e].relations).flatMap(([l,d])=>d.type==="one"?[[d.relationalColumn,l]]:[]));Object.entries(s).forEach(([l,d])=>{let c=this.schema[e].relations[a[l]];if(!a[l])return;let u=i==null?void 0:i.value[l],[,f]=c.mergeMutation("set",d,u);if(f){if(!this.optimisticObjGraph.hasNode(f.value)){let m=c.entity.name;this.optimisticObjGraph.createNode(f.value,m,Object.values(this.schema[m].relations).flatMap(S=>S.type==="many"?[S.entity.name]:[]));}u!=null&&u.value&&this.optimisticObjGraph.removeLink(t,c.entity.name),this.optimisticObjGraph.createLink(t,f.value);}});}this.notifyCollectionSubscribers(e),this.optimisticObjGraph.notifySubscribers(t);}}materializeOneWithInclude(e,t={}){var a;if(!e)return;let s=this.optimisticObjGraph.getNode(e);if(!s)return;let i=s.type,o=(a=this.optimisticRawObjPool[i])==null?void 0:a[e];if(!o)return;let[r,n]=Object.entries(t).reduce((l,[d,c])=>{let u=this.schema[i].relations[d];return u&&(u.type==="one"?l[0].push([d,u.entity.name,c??true]):u.type==="many"&&l[1].push([d,u.entity.name,c??true])),l},[[],[]]);return {value:{...o.value,...Object.fromEntries(r.map(([l,d,c])=>[l,this.materializeOneWithInclude(s.references.get(d),typeof c=="object"&&c!==null?c:{})])),...Object.fromEntries(n.map(([l,d,c])=>{let u=s.referencedBy.get(d),f=u instanceof Set;return [l,f?{value:Array.from(u.values()).map(m=>this.materializeOneWithInclude(m,typeof c=="object"&&c!==null?c:{}))}:this.materializeOneWithInclude(u,typeof c=="object"&&c!==null?c:{})]}))}}}notifyCollectionSubscribers(e){this.collectionSubscriptions.forEach(t=>{if(t.query.resource===e||t.flatInclude&&t.flatInclude.includes(e)){let s=g(t.query),i=this.querySnapshots[s],o=this.get(t.query,void 0,true);if(ue(o,i))return;this.querySnapshots[s]=o,t.callbacks.forEach(r=>{r(o);});}});}flattenIncludes(e,t){let s=[];return Object.entries(e).forEach(([i,o])=>{var a;let r=(a=this.schema[t])==null?void 0:a.relations[i];if(!r)return;let n=r.entity.name;s.push(n),typeof o=="object"&&o!==null&&s.push(...this.flattenIncludes(o,n));}),Array.from(new Set(s))}};var P=class{url;ws;store;logger;remoteSubscriptions=new Map;eventListeners=new Set;replyHandlers={};constructor(e){var t,s,i,o;this.url=e.url,this.logger=$({level:e.logLevel??v.INFO}),this.store=new A(e.schema,e.storage,this.logger,r=>{var n,a;(a=(n=Object.values(r))==null?void 0:n.flat())==null||a.forEach(l=>{this.sendWsMessage(l);});},(r,n)=>{this.emitEvent({type:"CLIENT_STORAGE_LOADED",resource:r,itemCount:n});}),this.ws=new R({url:e.url,autoConnect:((t=e.connection)==null?void 0:t.autoConnect)??true,autoReconnect:((s=e.connection)==null?void 0:s.autoReconnect)??true,reconnectTimeout:((i=e.connection)==null?void 0:i.reconnectTimeout)??5e3,reconnectLimit:(o=e.connection)==null?void 0:o.maxReconnectAttempts,credentials:e.credentials}),this.ws.addEventListener("message",r=>{this.handleServerMessage(r.data);}),this.ws.addEventListener("connectionChange",r=>{this.emitEvent({type:"CONNECTION_STATE_CHANGE",open:r.open}),r.open&&(Array.from(this.remoteSubscriptions.values()).forEach(({query:n})=>{this.sendWsMessage({id:a(),type:"SUBSCRIBE",...n});}),Object.values(this.store.optimisticMutationStack).forEach(n=>{n&&n.forEach(a=>{this.emitEvent({type:"MUTATION_SENT",mutationId:a.id,resource:a.resource,resourceId:a.resourceId,procedure:a.procedure??"UNKNOWN",optimistic:true}),this.sendWsMessage(a);});}));});}get(e){let t=this.store.get(e);return this.emitEvent({type:"QUERY_EXECUTED",query:e,resultCount:Array.isArray(t)?t.length:t?1:0}),t}handleServerMessage(e){var t,s;try{this.logger.debug("Message received from the server:",e);let i=z.parse(JSON.parse(e));if(this.logger.debug("Parsed message:",i),this.emitEvent({type:"MESSAGE_RECEIVED",message:i}),i.type==="MUTATE"){let{resource:o,id:r,resourceId:n,procedure:a}=i;this.emitEvent({type:"MUTATION_RECEIVED",mutationId:r,resource:o,resourceId:n,procedure:a??"UNKNOWN"});try{this.store.addMutation(o,i);}catch(l){this.logger.error("Error merging mutation from the server:",l);}}else if(i.type==="REJECT"){let o=((t=this.store.optimisticMutationStack[i.resource])==null?void 0:t.length)??0,r=(s=this.store.optimisticMutationStack[i.resource])==null?void 0:s.find(n=>n.id===i.id);this.store.undoMutation(i.resource,i.id),this.emitEvent({type:"MUTATION_REJECTED",mutationId:i.id,resource:i.resource}),r&&this.emitEvent({type:"OPTIMISTIC_MUTATION_UNDONE",mutationId:i.id,resource:i.resource,resourceId:r.resourceId,pendingMutations:o-1});}else if(i.type==="REPLY"){let{id:o,data:r}=i;if(this.replyHandlers[o]){clearTimeout(this.replyHandlers[o].timeoutHandle),this.replyHandlers[o].handler(r);return}let n=q.parse(r);this.emitEvent({type:"DATA_LOAD_REPLY",resource:n.resource,itemCount:n.data.length}),this.store.loadConsolidatedState(n.resource,n.data),this.emitEvent({type:"STORE_STATE_UPDATED",resource:n.resource,itemCount:n.data.length});}}catch(i){this.logger.error("Error parsing message from the server:",i);}}load(e){let t=a(),s=g(e);this.emitEvent({type:"DATA_LOAD_REQUESTED",query:e,subscriptionId:t}),this.sendWsMessage({id:t,type:"SUBSCRIBE",...e});let i=!this.remoteSubscriptions.has(s);return this.remoteSubscriptions.has(s)?this.remoteSubscriptions.get(s).subCounter+=1:this.remoteSubscriptions.set(s,{query:e,subCounter:1}),i&&this.emitEvent({type:"SUBSCRIPTION_CREATED",query:e,subscriptionKey:s,subscriberCount:1}),()=>{if(this.remoteSubscriptions.has(s)){let o=this.remoteSubscriptions.get(s);o.subCounter-=1,this.remoteSubscriptions.get(s).subCounter<=0&&(this.remoteSubscriptions.delete(s),this.sendWsMessage({id:a(),type:"UNSUBSCRIBE",...e}),this.emitEvent({type:"SUBSCRIPTION_REMOVED",query:e,subscriptionKey:s}));}}}subscribe(e,t){return this.store.subscribe(e,t)}mutate(e,t,s,i){var n,a$1;let o={id:a(),type:"MUTATE",resource:e,payload:this.store.schema[e].encodeMutation("set",i,new Date().toISOString()),resourceId:t,procedure:s},r=(((n=this.store.optimisticMutationStack[e])==null?void 0:n.length)??0)+1;(a$1=this.store)==null||a$1.addMutation(e,o,true),this.emitEvent({type:"OPTIMISTIC_MUTATION_APPLIED",mutationId:o.id,resource:e,resourceId:t,procedure:s,pendingMutations:r}),this.emitEvent({type:"MUTATION_SENT",mutationId:o.id,resource:e,resourceId:t,procedure:s,optimistic:true}),this.sendWsMessage(o);}genericMutate(e,t,s){if(!this.ws||!this.ws.connected())throw new Error("WebSocket not connected");let i={id:a(),type:"MUTATE",resource:e,procedure:t,payload:s};return this.emitEvent({type:"MUTATION_SENT",mutationId:i.id,resource:e,resourceId:"",procedure:t,optimistic:false}),this.sendWsMessage(i),new Promise((o,r)=>{this.replyHandlers[i.id]={timeoutHandle:setTimeout(()=>{delete this.replyHandlers[i.id],r(new Error("Reply timeout"));},5e3),handler:n=>{delete this.replyHandlers[i.id],o(n);}};})}addEventListener(e){return this.eventListeners.add(e),()=>{this.eventListeners.delete(e);}}sendWsMessage(e){var t;(t=this.ws)!=null&&t.connected()&&this.ws.send(JSON.stringify(e));}emitEvent(e){this.eventListeners.forEach(t=>{t(e);});}},qe=p=>{let e=new P(p);return {client:{ws:e.ws,load:t=>e.load(t),addEventListener:t=>e.addEventListener(t)},store:{query:Object.entries(p.schema).reduce((t,[s,i])=>(t[s]=c._init(i,e),t),{}),mutate:d(()=>{},{apply:(t,s,i)=>{if(s.length<2)return;if(s.length>2)throw new Error("Trying to access an invalid path");let[o,r]=s;if(r==="insert"){let{id:n,...a}=i[0];return e.mutate(o,n,"INSERT",a)}if(r==="update"){let[n,a]=i;return e.mutate(o,n,"UPDATE",a)}return e.genericMutate(o,r,i[0])}})}}};export{qe as createClient,ye as useLiveQuery,fe as useLoadData};
@@ -1,9 +1,9 @@
1
- import { A as AnyRouter, C as ClientOptions, a as Client } from './index-BgowrRDR.js';
2
- import 'react/jsx-runtime';
1
+ import { C as ClientOptions, a as ClientRouterConstraint, b as Client } from './index-BFevRjd8.js';
3
2
  import 'zod';
4
- import 'zod/v3';
5
- import 'zod/v4/core';
6
3
 
7
- declare const createClient: <TRouter extends AnyRouter>(opts: Omit<ClientOptions, "storage">) => Client<TRouter, true>;
4
+ type FetchClientOptions = Omit<ClientOptions, "storage"> & {
5
+ fetchOptions?: RequestInit;
6
+ };
7
+ declare const createClient: <TRouter extends ClientRouterConstraint>(opts: FetchClientOptions) => Client<TRouter, true>;
8
8
 
9
- export { createClient };
9
+ export { type FetchClientOptions, createClient };
@@ -1 +1 @@
1
- import {d,b,c,f}from'./chunk-WZ7JXHZL.js';import {stringify}from'qs';var u=async(...e)=>{let o=await fetch(...e),t;try{t=await o.json();}catch{t=await o.text().catch(()=>{});}if(!o.ok)throw new Error(`Failed to fetch: ${o.status} ${o.statusText}`,{cause:t});return t},S=e=>{let o={get:async t=>{let n=stringify(t),a=await b(e.credentials)??{},r=await u(`${e.url}/${t.resource}${n?`?${n}`:""}`,{headers:{...a,"Content-Type":"application/json"}});return !r||typeof r!="object"?[]:Object.entries(r).map(([i,s])=>({...f(s),id:i}))},subscribe:()=>{throw new Error("Fetch client does not support subscriptions")}};return {query:Object.entries(e.schema).reduce((t,[n,a])=>(t[n]=c._init(a,o,true),t),{}),mutate:d(()=>{},{apply:async(t,n,a)=>{if(n.length<2)return;if(n.length>2)throw new Error("Trying to access an invalid path");let[r,i]=n,s=await b(e.credentials)??{};if(i==="insert"){let{id:c,...y}=a[0];await u(`${e.url}/${r}/insert`,{method:"POST",headers:{...s,"Content-Type":"application/json"},body:JSON.stringify({resourceId:c,payload:e.schema[r].encodeMutation("set",y,new Date().toISOString())})});return}if(i==="update"){let[c,y]=a,{id:O,...m}=y;await u(`${e.url}/${r}/update`,{method:"POST",headers:{...s,"Content-Type":"application/json"},body:JSON.stringify({resourceId:c,payload:e.schema[r].encodeMutation("set",m,new Date().toISOString())})});return}await u(`${e.url}/${r}/${i}`,{method:"POST",headers:{...s,"Content-Type":"application/json"},body:JSON.stringify({payload:a[0]})});}})}};export{S as createClient};
1
+ import {d,b,c,f as f$1}from'./chunk-AHF6GNMI.js';import {stringify}from'qs';var f=async(e,c,r)=>{let n=o=>{if(!o)return {};if(o instanceof Headers){let d={};return o.forEach((y,b)=>{d[b]=y;}),d}return Array.isArray(o)?Object.fromEntries(o):o},s=n(r==null?void 0:r.headers),u=n(c==null?void 0:c.headers),a={...r,...c,headers:{...s,...u}},t=await fetch(e,a),i;try{i=await t.json();}catch{i=await t.text().catch(()=>{});}if(!t.ok)throw new Error(`Failed to fetch: ${t.status} ${t.statusText}`,{cause:i});return i},h=e=>{if(e===null)return "null";if(Array.isArray(e))return e.map(h);if(typeof e=="object"&&e!==null&&e.constructor===Object){let c={};for(let[r,n]of Object.entries(e))c[r]=h(n);return c}return e},$=e=>{let c$1={get:async r=>{let n=h(r),s=stringify(n),u=await b(e.credentials)??{},a=await f(`${e.url}/${r.resource}${s?`?${s}`:""}`,{headers:{...u,"Content-Type":"application/json"}},e.fetchOptions);return !a||typeof a!="object"?[]:Array.isArray(a)?a.map(t=>{var d,y;let i=f$1(t),o=((y=(d=t==null?void 0:t.value)==null?void 0:d.id)==null?void 0:y.value)??(t==null?void 0:t.id);return {...i,id:o}}):Object.entries(a).map(([t,i])=>({...f$1(i),id:t}))},subscribe:()=>{throw new Error("Fetch client does not support subscriptions")}};return {query:Object.entries(e.schema).reduce((r,[n,s])=>(r[n]=c._init(s,c$1,true),r),{}),mutate:d(()=>{},{apply:async(r,n,s)=>{if(n.length<2)return;if(n.length>2)throw new Error("Trying to access an invalid path");let[u,a]=n,t=await b(e.credentials)??{};if(a==="insert"){let{id:i,...o}=s[0];await f(`${e.url}/${u}/insert`,{method:"POST",headers:{...t,"Content-Type":"application/json"},body:JSON.stringify({resourceId:i,payload:e.schema[u].encodeMutation("set",o,new Date().toISOString())})},e.fetchOptions);return}if(a==="update"){let[i,o]=s,{id:d,...y}=o;await f(`${e.url}/${u}/update`,{method:"POST",headers:{...t,"Content-Type":"application/json"},body:JSON.stringify({resourceId:i,payload:e.schema[u].encodeMutation("set",y,new Date().toISOString())})},e.fetchOptions);return}return await f(`${e.url}/${u}/${a}`,{method:"POST",headers:{...t,"Content-Type":"application/json"},body:JSON.stringify({payload:s[0]})},e.fetchOptions)}})}};export{$ as createClient};
@@ -1,7 +1,4 @@
1
- import * as react_jsx_runtime from 'react/jsx-runtime';
2
1
  import { z } from 'zod';
3
- import * as z3 from 'zod/v3';
4
- import * as z4 from 'zod/v4/core';
5
2
 
6
3
  type LiveTypeMeta = {};
7
4
  type MutationType = "set";
@@ -104,7 +101,7 @@ type InferLiveObjectWithoutRelations<T extends LiveObjectAny> = {
104
101
  [K in keyof T["fields"]]: InferLiveType<T["fields"][K]>;
105
102
  };
106
103
  type InferLiveObject<T extends LiveObjectAny, Include extends IncludeClause<T> | undefined = undefined> = InferLiveObjectWithoutRelations<T> & (Include extends IncludeClause<T> ? {
107
- [K in keyof T["relations"] as Include[K] extends true ? K : never]: T["relations"][K]["type"] extends "one" ? T["fields"][Exclude<T["relations"][K]["relationalColumn"], undefined>] extends NullableLiveType<any> ? InferLiveObject<T["relations"][K]["entity"]> | null : InferLiveObject<T["relations"][K]["entity"]> : InferLiveObject<T["relations"][K]["entity"]>[];
104
+ [K in keyof T["relations"] as Include[K] extends true | IncludeClause<T["relations"][K]["entity"]> ? K : never]: Include[K] extends true ? T["relations"][K]["type"] extends "one" ? T["fields"][Exclude<T["relations"][K]["relationalColumn"], undefined>] extends NullableLiveType<any> ? InferLiveObject<T["relations"][K]["entity"]> | null : InferLiveObject<T["relations"][K]["entity"]> : InferLiveObject<T["relations"][K]["entity"]>[] : Include[K] extends IncludeClause<T["relations"][K]["entity"]> ? T["relations"][K]["type"] extends "one" ? T["fields"][Exclude<T["relations"][K]["relationalColumn"], undefined>] extends NullableLiveType<any> ? InferLiveObject<T["relations"][K]["entity"], Include[K]> | null : InferLiveObject<T["relations"][K]["entity"], Include[K]> : InferLiveObject<T["relations"][K]["entity"], Include[K]>[] : never;
108
105
  } : {});
109
106
  type InferRelationalColumns<T extends Record<string, RelationAny>> = {
110
107
  [K in keyof T as T[K]["type"] extends "many" ? never : T[K]["relationalColumn"]]: T[K]["required"] extends true ? InferIndex<T[K]["entity"]> : InferIndex<T[K]["entity"]> | null;
@@ -221,11 +218,23 @@ type InferInsert<T extends LiveObjectAny> = {
221
218
  };
222
219
  type InferUpdate<T extends LiveObjectAny> = Omit<LiveObjectMutationInput<T>, "id">;
223
220
 
224
- type Promisify<T> = T extends Promise<any> ? T : Promise<T>;
221
+ type Promisify<T> = T extends Promise<unknown> ? T : Promise<T>;
225
222
  type ConditionalPromise<T, P extends boolean> = P extends true ? Promise<T> : T;
226
- type Awaitable<T> = T | Promise<T>;
223
+ type PromiseOrSync<T> = T | Promise<T>;
227
224
  type Generatable<T, Arg = never> = T | ((arg: Arg) => T);
228
225
 
226
+ type Simplify<T> = T extends Record<string, unknown> ? {
227
+ [K in keyof T]: Simplify<T[K]>;
228
+ } : T extends Array<infer U> ? Array<Simplify<U>> : T;
229
+ declare const LogLevel: {
230
+ readonly CRITICAL: 0;
231
+ readonly ERROR: 1;
232
+ readonly WARN: 2;
233
+ readonly INFO: 3;
234
+ readonly DEBUG: 4;
235
+ };
236
+ type LogLevel = (typeof LogLevel)[keyof typeof LogLevel];
237
+
229
238
  declare const querySchema: z.ZodObject<{
230
239
  resource: z.ZodString;
231
240
  where: z.ZodOptional<z.ZodRecord<z.ZodString, z.ZodAny>>;
@@ -242,143 +251,6 @@ declare const querySchema: z.ZodObject<{
242
251
  }, z.core.$strip>;
243
252
  type RawQueryRequest = z.infer<typeof querySchema>;
244
253
 
245
- type Simplify<T> = T extends Record<string, unknown> ? {
246
- [K in keyof T]: Simplify<T[K]>;
247
- } : T extends Array<infer U> ? Array<Simplify<U>> : T;
248
-
249
- /** biome-ignore-all lint/suspicious/noExplicitAny: false positive */
250
- /** biome-ignore-all lint/style/noNonNullAssertion: false positive */
251
-
252
- type RouteRecord = Record<string, AnyRoute>;
253
- declare class Router<TRoutes extends RouteRecord> {
254
- readonly routes: TRoutes;
255
- private constructor();
256
- static create<TRoutes extends RouteRecord>(opts: {
257
- routes: TRoutes;
258
- }): Router<TRoutes>;
259
- }
260
- type AnyRouter = Router<any>;
261
- type Mutation<TInputValidator extends z3.ZodTypeAny | z4.$ZodType, // TODO use StandardSchema instead
262
- TOutput> = {
263
- inputValidator: TInputValidator;
264
- handler: (opts: {
265
- req: MutationRequest<z.infer<TInputValidator>>;
266
- db: Storage;
267
- }) => TOutput;
268
- };
269
- declare const mutationCreator: <TInputValidator extends z3.ZodTypeAny | z4.$ZodType>(validator?: TInputValidator) => {
270
- handler: <THandler extends Mutation<TInputValidator, any>["handler"]>(handler: THandler) => Mutation<TInputValidator, ReturnType<THandler>>;
271
- };
272
- type ReadAuthorizationHandler<TShape extends LiveObjectAny> = (opts: {
273
- ctx: BaseRequest["context"];
274
- }) => WhereClause<TShape> | boolean;
275
- type MutationAuthorizationHandler<TShape extends LiveObjectAny> = (opts: {
276
- ctx: BaseRequest["context"];
277
- value: Simplify<InferLiveObjectWithRelationalIds<TShape>>;
278
- }) => WhereClause<TShape> | boolean;
279
- type Authorization<TShape extends LiveObjectAny> = {
280
- read?: ReadAuthorizationHandler<TShape>;
281
- insert?: MutationAuthorizationHandler<TShape>;
282
- update?: {
283
- preMutation?: MutationAuthorizationHandler<TShape>;
284
- postMutation?: MutationAuthorizationHandler<TShape>;
285
- };
286
- };
287
- declare class Route<TResourceSchema extends LiveObjectAny, TMiddleware extends Middleware<any>, TCustomMutations extends Record<string, Mutation<any, any>>> {
288
- readonly resourceSchema: TResourceSchema;
289
- readonly middlewares: Set<TMiddleware>;
290
- readonly customMutations: TCustomMutations;
291
- readonly authorization?: Authorization<TResourceSchema>;
292
- constructor(resourceSchema: TResourceSchema, customMutations?: TCustomMutations, authorization?: Authorization<TResourceSchema>);
293
- use(...middlewares: TMiddleware[]): this;
294
- withMutations<T extends Record<string, Mutation<any, any>>>(mutationFactory: (opts: {
295
- mutation: typeof mutationCreator;
296
- }) => T): Route<TResourceSchema, TMiddleware, T>;
297
- private handleSet;
298
- private wrapInMiddlewares;
299
- }
300
- type AnyRoute = Route<LiveObjectAny, Middleware<any>, Record<string, any>>;
301
-
302
- /** biome-ignore-all lint/suspicious/noExplicitAny: false positive */
303
-
304
- declare abstract class Storage {
305
- abstract findOne<T extends LiveObjectAny>(resource: T, id: string, options?: {
306
- include?: IncludeClause<T>;
307
- }): Promise<InferLiveObject<T> | undefined>;
308
- abstract find<T extends LiveObjectAny>(resource: T, options?: {
309
- where?: WhereClause<T>;
310
- include?: IncludeClause<T>;
311
- }): Promise<Record<string, InferLiveObject<T>>>;
312
- insert<T extends LiveObjectAny>(resource: T, value: Simplify<InferInsert<T>>): Promise<InferLiveObject<T>>;
313
- update<T extends LiveObjectAny>(resource: T, resourceId: string, value: InferUpdate<T>): Promise<InferLiveObject<T>>;
314
- abstract transaction<T>(fn: (opts: {
315
- trx: Storage;
316
- commit: () => Promise<void>;
317
- rollback: () => Promise<void>;
318
- }) => Promise<T>): Promise<T>;
319
- }
320
-
321
- /** biome-ignore-all lint/suspicious/noExplicitAny: any's are actually used correctly */
322
-
323
- interface BaseRequest {
324
- headers: Record<string, string>;
325
- cookies: Record<string, string>;
326
- queryParams: Record<string, string>;
327
- context: Record<string, any>;
328
- }
329
- interface QueryRequest extends BaseRequest, RawQueryRequest {
330
- type: "QUERY";
331
- }
332
- interface MutationRequest<TInput = any> extends BaseRequest {
333
- type: "MUTATE";
334
- input: TInput;
335
- resource: string;
336
- resourceId?: string;
337
- procedure: string;
338
- }
339
- type Request = QueryRequest | MutationRequest;
340
- type NextFunction<O, R = Request> = (req: R) => Awaitable<O>;
341
- type Middleware<T = any> = (opts: {
342
- req: Request;
343
- next: NextFunction<T>;
344
- }) => ReturnType<NextFunction<T>>;
345
-
346
- declare const useLiveQuery: <T extends {
347
- get: () => U;
348
- subscribe: (cb: (v: U) => void) => () => void;
349
- }, U>(observable: T) => ReturnType<T["get"]>;
350
- declare const SubscriptionProvider: ({ children, client, }: {
351
- children: React.ReactNode;
352
- client: Client<AnyRouter>["client"];
353
- }) => react_jsx_runtime.JSX.Element;
354
-
355
- declare const serverMessageSchema: z.ZodUnion<readonly [z.ZodObject<{
356
- id: z.ZodString;
357
- type: z.ZodLiteral<"REJECT">;
358
- resource: z.ZodString;
359
- message: z.ZodOptional<z.ZodString>;
360
- }, z.core.$strip>, z.ZodObject<{
361
- id: z.ZodString;
362
- type: z.ZodLiteral<"REPLY">;
363
- data: z.ZodAny;
364
- }, z.core.$strip>, z.ZodObject<{
365
- type: z.ZodLiteral<"MUTATE">;
366
- resource: z.ZodString;
367
- resourceId: z.ZodOptional<z.ZodString>;
368
- procedure: z.ZodEnum<{
369
- INSERT: "INSERT";
370
- UPDATE: "UPDATE";
371
- }>;
372
- payload: z.ZodRecord<z.ZodString, z.ZodObject<{
373
- value: z.ZodNullable<z.ZodUnion<[z.ZodUnion<[z.ZodUnion<[z.ZodString, z.ZodNumber]>, z.ZodBoolean]>, z.ZodDate]>>;
374
- _meta: z.ZodOptional<z.ZodObject<{
375
- timestamp: z.ZodNullable<z.ZodOptional<z.ZodString>>;
376
- }, z.core.$strip>>;
377
- }, z.core.$strip>>;
378
- id: z.ZodString;
379
- }, z.core.$strip>]>;
380
- type ServerMessage = z.infer<typeof serverMessageSchema>;
381
-
382
254
  /** biome-ignore-all lint/complexity/noBannedTypes: <explanation> */
383
255
 
384
256
  type InferQueryResult<TCollection extends LiveObjectAny, TInclude extends IncludeClause<TCollection>, TSingle extends boolean = false> = TSingle extends true ? Simplify<InferLiveObject<TCollection, TInclude>> | undefined : Simplify<InferLiveObject<TCollection, TInclude>>[];
@@ -394,8 +266,6 @@ declare class QueryBuilder<TCollection extends LiveObjectAny, TInclude extends I
394
266
  private constructor();
395
267
  where(where: WhereClause<TCollection>): QueryBuilder<TCollection, TInclude, TSingle, TShouldAwait>;
396
268
  include<TNewInclude extends IncludeClause<TCollection>>(include: TNewInclude): QueryBuilder<TCollection, TInclude & TNewInclude, TSingle, TShouldAwait>;
397
- get(): ConditionalPromise<InferQueryResult<TCollection, TInclude, TSingle>, TShouldAwait>;
398
- subscribe(callback: (value: InferQueryResult<TCollection, TInclude, TSingle>) => void): () => void;
399
269
  limit(limit: number): QueryBuilder<TCollection, TInclude, TSingle, TShouldAwait>;
400
270
  one(id: string): QueryBuilder<TCollection, TInclude, true, TShouldAwait>;
401
271
  first(where?: WhereClause<TCollection>): QueryBuilder<TCollection, TInclude, true, TShouldAwait>;
@@ -410,9 +280,38 @@ declare class QueryBuilder<TCollection extends LiveObjectAny, TInclude extends I
410
280
  direction: "asc" | "desc";
411
281
  }[] | undefined;
412
282
  };
283
+ buildQueryRequest(): RawQueryRequest;
284
+ get(): ConditionalPromise<InferQueryResult<TCollection, TInclude, TSingle>, TShouldAwait>;
285
+ subscribe(callback: (value: InferQueryResult<TCollection, TInclude, TSingle>) => void): () => void;
413
286
  }
414
287
 
415
- type Client$1<TRouter extends AnyRouter, TShouldAwait extends boolean = false> = {
288
+ /**
289
+ * Extracts the output type from a zod-like schema (mirrors z.infer behavior).
290
+ * TODO: Use StandardSchema instead
291
+ */
292
+ type InferSchema<T> = T extends {
293
+ _output: infer U;
294
+ } ? U : never;
295
+ /**
296
+ * Helper type for custom mutation functions.
297
+ * When the input type is `never` or `undefined`, the function has no parameters.
298
+ */
299
+ type CustomMutationFunction<TInput, TOutput> = [TInput] extends [never] | [undefined] ? () => Promisify<TOutput> : (input: TInput) => Promisify<TOutput>;
300
+ /**
301
+ * Simplified router constraint for client-side usage.
302
+ * This avoids importing server-internal types like Storage and Hooks,
303
+ * which can cause type incompatibilities when the package is bundled.
304
+ */
305
+ type ClientRouterConstraint = {
306
+ routes: Record<string, {
307
+ resourceSchema: LiveObjectAny;
308
+ customMutations: Record<string, {
309
+ inputValidator: any;
310
+ handler: (...args: any[]) => any;
311
+ }>;
312
+ }>;
313
+ };
314
+ type Client$1<TRouter extends ClientRouterConstraint, TShouldAwait extends boolean = false> = {
416
315
  query: {
417
316
  [K in keyof TRouter["routes"]]: QueryBuilder<TRouter["routes"][K]["resourceSchema"], {}, false, TShouldAwait>;
418
317
  };
@@ -421,11 +320,44 @@ type Client$1<TRouter extends AnyRouter, TShouldAwait extends boolean = false> =
421
320
  insert: (input: Simplify<InferInsert<TRouter["routes"][K]["resourceSchema"]>>) => ConditionalPromise<void, TShouldAwait>;
422
321
  update: (id: string, value: Simplify<InferUpdate<TRouter["routes"][K]["resourceSchema"]>>) => ConditionalPromise<void, TShouldAwait>;
423
322
  } & {
424
- [K2 in keyof TRouter["routes"][K]["customMutations"]]: (input: z.infer<TRouter["routes"][K]["customMutations"][K2]["inputValidator"]>) => Promisify<ReturnType<TRouter["routes"][K]["customMutations"][K2]["handler"]>>;
323
+ [K2 in keyof TRouter["routes"][K]["customMutations"]]: CustomMutationFunction<InferSchema<TRouter["routes"][K]["customMutations"][K2]["inputValidator"]>, ReturnType<TRouter["routes"][K]["customMutations"][K2]["handler"]>>;
425
324
  };
426
325
  };
427
326
  };
428
327
 
328
+ declare const useLiveQuery: <T extends {
329
+ get: () => U;
330
+ subscribe: (cb: (v: U) => void) => () => void;
331
+ }, U>(observable: T) => ReturnType<T["get"]>;
332
+ declare const useLoadData: (client: Client<ClientRouterConstraint>["client"], query: QueryBuilder<any, any>) => void;
333
+
334
+ declare const serverMessageSchema: z.ZodUnion<readonly [z.ZodObject<{
335
+ id: z.ZodString;
336
+ type: z.ZodLiteral<"REJECT">;
337
+ resource: z.ZodString;
338
+ message: z.ZodOptional<z.ZodString>;
339
+ }, z.core.$strip>, z.ZodObject<{
340
+ id: z.ZodString;
341
+ type: z.ZodLiteral<"REPLY">;
342
+ data: z.ZodAny;
343
+ }, z.core.$strip>, z.ZodObject<{
344
+ type: z.ZodLiteral<"MUTATE">;
345
+ resource: z.ZodString;
346
+ resourceId: z.ZodOptional<z.ZodString>;
347
+ procedure: z.ZodEnum<{
348
+ INSERT: "INSERT";
349
+ UPDATE: "UPDATE";
350
+ }>;
351
+ payload: z.ZodRecord<z.ZodString, z.ZodObject<{
352
+ value: z.ZodNullable<z.ZodAny>;
353
+ _meta: z.ZodOptional<z.ZodObject<{
354
+ timestamp: z.ZodNullable<z.ZodOptional<z.ZodString>>;
355
+ }, z.core.$strip>>;
356
+ }, z.core.$strip>>;
357
+ id: z.ZodString;
358
+ }, z.core.$strip>]>;
359
+ type ServerMessage = z.infer<typeof serverMessageSchema>;
360
+
429
361
  type WebSocketClientEventMap = WebSocketEventMap & {
430
362
  connectionChange: {
431
363
  open: boolean;
@@ -481,24 +413,97 @@ type MessageReceivedEvent = {
481
413
  type: "MESSAGE_RECEIVED";
482
414
  message: ServerMessage;
483
415
  };
484
- type ClientEvents = ConnectionStateChangeEvent | MessageReceivedEvent;
485
- type Client<TRouter extends AnyRouter> = {
416
+ type ClientStorageLoadedEvent = {
417
+ type: "CLIENT_STORAGE_LOADED";
418
+ resource: string;
419
+ itemCount: number;
420
+ };
421
+ type DataLoadRequestedEvent = {
422
+ type: "DATA_LOAD_REQUESTED";
423
+ query: RawQueryRequest;
424
+ subscriptionId: string;
425
+ };
426
+ type DataLoadReplyEvent = {
427
+ type: "DATA_LOAD_REPLY";
428
+ resource: string;
429
+ itemCount: number;
430
+ subscriptionId?: string;
431
+ };
432
+ type MutationSentEvent = {
433
+ type: "MUTATION_SENT";
434
+ mutationId: string;
435
+ resource: string;
436
+ resourceId: string;
437
+ procedure: string;
438
+ optimistic: boolean;
439
+ };
440
+ type MutationReceivedEvent = {
441
+ type: "MUTATION_RECEIVED";
442
+ mutationId: string;
443
+ resource: string;
444
+ resourceId: string;
445
+ procedure: string;
446
+ };
447
+ type MutationRejectedEvent = {
448
+ type: "MUTATION_REJECTED";
449
+ mutationId: string;
450
+ resource: string;
451
+ };
452
+ type SubscriptionCreatedEvent = {
453
+ type: "SUBSCRIPTION_CREATED";
454
+ query: RawQueryRequest;
455
+ subscriptionKey: string;
456
+ subscriberCount: number;
457
+ };
458
+ type SubscriptionRemovedEvent = {
459
+ type: "SUBSCRIPTION_REMOVED";
460
+ query: RawQueryRequest;
461
+ subscriptionKey: string;
462
+ };
463
+ type QueryExecutedEvent = {
464
+ type: "QUERY_EXECUTED";
465
+ query: RawQueryRequest;
466
+ resultCount: number;
467
+ };
468
+ type StoreStateUpdatedEvent = {
469
+ type: "STORE_STATE_UPDATED";
470
+ resource: string;
471
+ itemCount: number;
472
+ };
473
+ type OptimisticMutationAppliedEvent = {
474
+ type: "OPTIMISTIC_MUTATION_APPLIED";
475
+ mutationId: string;
476
+ resource: string;
477
+ resourceId: string;
478
+ procedure: string;
479
+ pendingMutations: number;
480
+ };
481
+ type OptimisticMutationUndoneEvent = {
482
+ type: "OPTIMISTIC_MUTATION_UNDONE";
483
+ mutationId: string;
484
+ resource: string;
485
+ resourceId: string;
486
+ pendingMutations: number;
487
+ };
488
+ type ClientEvents = ConnectionStateChangeEvent | MessageReceivedEvent | ClientStorageLoadedEvent | DataLoadRequestedEvent | DataLoadReplyEvent | MutationSentEvent | MutationReceivedEvent | MutationRejectedEvent | SubscriptionCreatedEvent | SubscriptionRemovedEvent | QueryExecutedEvent | StoreStateUpdatedEvent | OptimisticMutationAppliedEvent | OptimisticMutationUndoneEvent;
489
+ type Client<TRouter extends ClientRouterConstraint> = {
486
490
  client: {
487
491
  ws: WebSocketClient;
488
- subscribe: (resourceType?: string[]) => () => void;
489
492
  addEventListener: (listener: (event: ClientEvents) => void) => () => void;
493
+ load: (query: RawQueryRequest) => () => void;
490
494
  };
491
495
  store: Client$1<TRouter>;
492
496
  };
493
- declare const createClient: <TRouter extends AnyRouter>(opts: WebSocketClientOptions) => Client<TRouter>;
497
+ declare const createClient: <TRouter extends ClientRouterConstraint>(opts: WebSocketClientOptions) => Client<TRouter>;
494
498
 
495
499
  type ClientOptions = {
496
500
  url: string;
497
501
  schema: Schema<any>;
498
- credentials?: Generatable<Awaitable<Record<string, string>>>;
502
+ credentials?: Generatable<PromiseOrSync<Record<string, string>>>;
499
503
  storage: {
500
504
  name: string;
501
505
  } | false;
506
+ logLevel?: LogLevel;
502
507
  };
503
508
 
504
- export { type AnyRouter as A, type ClientOptions as C, type MessageReceivedEvent as M, SubscriptionProvider as S, type Client$1 as a, type ConnectionStateChangeEvent as b, type ClientEvents as c, type Client as d, createClient as e, useLiveQuery as u };
509
+ export { type ClientOptions as C, type DataLoadRequestedEvent as D, type MessageReceivedEvent as M, type OptimisticMutationAppliedEvent as O, type QueryExecutedEvent as Q, type SubscriptionCreatedEvent as S, type ClientRouterConstraint as a, type Client$1 as b, useLoadData as c, type ConnectionStateChangeEvent as d, type ClientStorageLoadedEvent as e, type DataLoadReplyEvent as f, type MutationSentEvent as g, type MutationReceivedEvent as h, type MutationRejectedEvent as i, type SubscriptionRemovedEvent as j, type StoreStateUpdatedEvent as k, type OptimisticMutationUndoneEvent as l, type ClientEvents as m, type Client as n, createClient as o, useLiveQuery as u };