@live-state/sync 0.0.5 → 0.0.6-beta-2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -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,3 @@
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-D8jFoiy6.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-z1KvGSvY.js';
3
2
  import 'zod';
4
- import 'zod/v3';
5
- import 'zod/v4/core';
3
+ import '@standard-schema/spec';
package/dist/client.js CHANGED
@@ -1 +1 @@
1
- import {d,c,a,f,e,b}from'./chunk-RCXJM33Z.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 ae from'fast-deep-equal';import {openDB}from'idb';var g=l=>xxHash32(JSON.stringify(l)).toString(32);var m=(l,e,t=false)=>Object.entries(e).every(([i,s])=>{if(i==="$and")return s.every(o=>m(l,o,t));if(i==="$or")return s.some(o=>m(l,o,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=l[i];return n===void 0?false:t?!s.$in.includes(n):s.$in.includes(n)}if(s.$not!==void 0&&!t)return m(l,{[i]:s.$not},true);if(s.$gt!==void 0){let n=l[i];return typeof n!="number"?false:t?n<=s.$gt:n>s.$gt}if(s.$gte!==void 0){let n=l[i];return typeof n!="number"?false:t?n<s.$gte:n>=s.$gte}if(s.$lt!==void 0){let n=l[i];return typeof n!="number"?false:t?n>=s.$lt:n<s.$lt}if(s.$lte!==void 0){let n=l[i];return typeof n!="number"?false:t?n>s.$lte:n<=s.$lte}let o=l[i];return !o||typeof o!="object"&&!Array.isArray(o)?false:Array.isArray(o)?t?!o.some(n=>m(n,s,false)):o.some(n=>m(n,s,false)):m(o,s,t)}return t?l[i]!==r:l[i]===r}),y={CRITICAL:0,ERROR:1,WARN:2,INFO:3,DEBUG:4},C=class{level;prefix;constructor(e={}){this.level=e.level??y.INFO,this.prefix=e.prefix?`[${e.prefix}] `:"";}critical(...e){this.level>=y.CRITICAL&&console.error(`${this.prefix}[CRITICAL]`,...e);}error(...e){this.level>=y.ERROR&&console.error(`${this.prefix}[ERROR]`,...e);}warn(...e){this.level>=y.WARN&&console.warn(`${this.prefix}[WARN]`,...e);}info(...e){this.level>=y.INFO&&console.log(`${this.prefix}[INFO]`,...e);}debug(...e){this.level>=y.DEBUG&&console.log(`${this.prefix}[DEBUG]`,...e);}setLevel(e){this.level=e;}getLevel(){return this.level}},T=l=>new C(l);var j=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:i=>{var r;(r=this.subscriptions.get(t))==null||r.callbacks.add(i);let s=e.subscribe(()=>{var o;(o=this.subscriptions.get(t))==null||o.callbacks.forEach(n=>{n();});});return ()=>{var o;(o=this.subscriptions.get(t))==null||o.callbacks.delete(i),setTimeout(()=>{var n;((n=this.subscriptions.get(t))==null?void 0:n.callbacks.size)===0&&(this.subscriptions.delete(t),s());},10);}},callbacks:new Set}),this.subscriptions.get(t).subscribe)}},J=new j,de=l=>useSyncExternalStore(J.getOrStoreSubscription(l),l.get),he=({children:l,client:e})=>(useEffect(()=>{e.subscribe();},[e.subscribe]),jsx(Fragment,{children:l}));var N=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()}),x=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()})),X=x.superRefine((l,e)=>{l.id&&e.addIssue({code:z.ZodIssueCode.custom,message:"Payload cannot have an id"});}),U=z.object({id:z.string().optional(),type:z.literal("MUTATE"),resource:z.string(),resourceId:z.string().optional()}),k=U.extend({procedure:z.string(),payload:z.any().optional()}),A=U.extend({procedure:z.enum(["INSERT","UPDATE"]),payload:X});z.union([A,k]);var S=z.string(),ee=z.object({id:S,type:z.literal("SUBSCRIBE"),resource:z.string()}),te=N.extend({id:S,type:z.literal("QUERY")}),H=A.extend({id:S}),ie=k.extend({id:S}),se=z.union([ie,H]);z.union([ee,te,se]);var ne=z.object({id:S,type:z.literal("REJECT"),resource:z.string(),message:z.string().optional()}),re=z.object({id:S,type:z.literal("REPLY"),data:z.any()}),Q=z.union([ne,re,H]),K=z.object({resource:z.string(),data:z.record(z.string(),x)});var E=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{constructor(e){this.logger=e;this.nodes=new Map;}nodes;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 o=r.referencedBy.get(i.type);o&&(o instanceof Set?o.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(o=>{let n=this.nodes.get(o);!n||!n.references.get(i)||(n.references.delete(i),this.notifySubscribers(o));});}),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=>{var s;try{i(e);}catch(r){(s=this.logger)==null||s.error(`Error in node subscription for node ${e}:`,r);}});}getAllNodes(){return Array.from(this.nodes.values())}};var W="__meta",O="databases",R=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 g(e),o=Object.fromEntries(await Promise.all(Object.entries(e).map(async([c,d])=>[c,await g(d)]))),n=await openDB("live-state-databases",1,{upgrade(c){c.objectStoreNames.contains(O)||c.createObjectStore(O);}}),a=(h=await this.getAll(n,O))==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),W].forEach(d=>{(a==null?void 0:a.objectHashes[d])!==o[d]&&c.objectStoreNames.contains(d)&&c.deleteObjectStore(d),c.objectStoreNames.contains(d)||c.createObjectStore(d);}),await n.put(O,{schemaHash:r,objectHashes:o},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(W,e):new Promise(t=>t(void 0))}setMeta(e,t){var i;return (i=this.db)==null?void 0:i.put(W,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,o)=>[s[o],r]))}};var L=class{constructor(e,t,i,s){this.schema=e;this.logger=i,this.optimisticObjGraph=new w(i),this.kvStorage=new R,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,s==null||s(this.optimisticMutationStack));}).then(()=>{Object.entries(this.schema).forEach(([r,o])=>{this.kvStorage.get(r).then(n=>{!n||Object.keys(n).length===0||this.loadConsolidatedState(r,n);});});}).catch(r=>{i.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,i=false){var o;let s=t??g(e$1);if(this.querySnapshots[s]&&!i){let n=this.querySnapshots[s];if(n)return n}let r=((o=e$1.where)!=null&&o.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,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(n);}if(e$1.where||e$1.limit){let n=e$1.where?a=>m(a,e$1.where):()=>true;r=e(r,n,e$1.limit);}return i||(this.querySnapshots[s]=r),r}subscribe(e,t){var r;let i=g(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 o,n;(o=this.collectionSubscriptions.get(i))==null||o.callbacks.delete(t),((n=this.collectionSubscriptions.get(i))==null?void 0:n.callbacks.size)===0&&(this.collectionSubscriptions.delete(i),delete this.querySnapshots[i]);}}addMutation(e,t,i=false){var o,n,a;let s=this.schema[e];if(this.logger.debug("Adding mutation",t),!s)throw new Error("Schema not found");let r=(o=this.optimisticRawObjPool[e])==null?void 0:o[t.resourceId];if(i)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 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 o,n;if(!this.optimisticMutationStack[e])return;let i=(o=this.optimisticMutationStack[e])==null?void 0:o.findIndex(a=>a.id===t);if(i===-1)return;let s=this.optimisticMutationStack[e][i];this.logger.debug("Removing mutation",s);let r=(n=this.optimisticRawObjPool[e])==null?void 0:n[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 n;if(!this.schema[e])return;let r=(n=this.rawObjPool[e])==null?void 0:n[t],o=(this.optimisticMutationStack[e]??[]).reduce((a,p)=>p.resourceId!==t?a:this.schema[e].mergeMutation("set",p.payload,a)[0],r);if(this.optimisticRawObjPool[e]??={},o?this.optimisticRawObjPool[e][t]={value:{...o.value,id:{value:t}}}:delete this.optimisticRawObjPool[e][t],!(!this.optimisticObjGraph.hasNode(t)&&!o)){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],[,b]=c.mergeMutation("set",h,d);if(b){if(!this.optimisticObjGraph.hasNode(b.value)){let M=c.entity.name;this.optimisticObjGraph.createNode(b.value,M,Object.values(this.schema[M].relations).flatMap(I=>I.type==="many"?[I.entity.name]:[]));}d!=null&&d.value&&this.optimisticObjGraph.removeLink(t,c.entity.name),this.optimisticObjGraph.createLink(t,b.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[o,n]=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(o.map(([p,h,c])=>[p,this.materializeOneWithInclude(i.references.get(h),typeof c=="object"&&c!==null?c:{})])),...Object.fromEntries(n.map(([p,h,c])=>{let d=i.referencedBy.get(h),b=d instanceof Set;return [p,b?{value:Array.from(d.values()).map(M=>this.materializeOneWithInclude(M,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=g(t.query),s=this.querySnapshots[i],r=this.get(t.query,void 0,true);if(ae(r,s))return;this.querySnapshots[i]=r,t.callbacks.forEach(o=>{o(r);});}});}flattenIncludes(e,t){let i=[];return Object.entries(e).forEach(([s,r])=>{var a;let o=(a=this.schema[t])==null?void 0:a.relations[s];if(!o)return;let n=o.entity.name;i.push(n),typeof r=="object"&&r!==null&&i.push(...this.flattenIncludes(r,n));}),Array.from(new Set(i))}};var P=class{url;ws;store;logger;remoteSubCounters={};eventListeners=new Set;replyHandlers={};constructor(e){var t,i,s,r;this.url=e.url,this.logger=T({level:e.logLevel??y.INFO}),this.store=new L(e.schema,e.storage,this.logger,o=>{var n,a;(a=(n=Object.values(o))==null?void 0:n.flat())==null||a.forEach(p=>{this.sendWsMessage(p);});}),this.ws=new E({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",o=>{this.handleServerMessage(o.data);}),this.ws.addEventListener("connectionChange",o=>{this.emitEvent({type:"CONNECTION_STATE_CHANGE",open:o.open}),o.open&&(Object.keys(this.store.schema).forEach(n=>{this.sendWsMessage({id:a(),type:"QUERY",resource:n});}),Object.entries(this.remoteSubCounters).forEach(([n,a$1])=>{a$1>0&&this.sendWsMessage({id:a(),type:"SUBSCRIBE",resource:n});}),Object.values(this.store.optimisticMutationStack).forEach(n=>{n&&n.forEach(a=>{this.sendWsMessage(a);});}));});}get(e){return this.store.get(e)}handleServerMessage(e){try{this.logger.debug("Message received from the server:",e);let t=Q.parse(JSON.parse(e));if(this.logger.debug("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){this.logger.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=K.parse(s);this.store.loadConsolidatedState(r.resource,r.data);}}catch(t){this.logger.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 o;let r={id:a(),type:"MUTATE",resource:e,payload:this.store.schema[e].encodeMutation("set",s,new Date().toISOString()),resourceId:t,procedure:i};(o=this.store)==null||o.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,o)=>{this.replyHandlers[s.id]={timeoutHandle:setTimeout(()=>{delete this.replyHandlers[s.id],o(new Error("Reply timeout"));},5e3),handler:n=>{delete this.replyHandlers[s.id],r(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=l=>{let e=new P(l),t=T({level:l.logLevel??y.INFO,prefix:"Client"});return {client:{ws:e.ws,subscribe:i=>{let s=[];for(let r of i??Object.keys(e.store.schema))s.push(e.subscribeToRemote(r));return ()=>{t.debug("Removing listeners",s),s.forEach(r=>{r();});}},addEventListener:i=>e.addEventListener(i)},store:{query:Object.entries(l.schema).reduce((i,[s,r])=>(i[s]=c._init(r,e),i),{}),mutate:d(()=>{},{apply:(i,s,r)=>{if(s.length<2)return;if(s.length>2)throw new Error("Trying to access an invalid path");let[o,n]=s;if(n==="insert"){let{id:a,...p}=r[0];return e.mutate(o,a,"INSERT",p)}if(n==="update"){let[a,p]=r;return e.mutate(o,a,"UPDATE",p)}return e.genericMutate(o,n,r[0])}})}}};export{he as SubscriptionProvider,Qe as createClient,de 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,10 @@
1
- import { A as AnyRouter, C as ClientOptions, a as Client } from './index-D8jFoiy6.js';
2
- import 'react/jsx-runtime';
1
+ import { C as ClientOptions, a as ClientRouterConstraint, b as Client } from './index-z1KvGSvY.js';
3
2
  import 'zod';
4
- import 'zod/v3';
5
- import 'zod/v4/core';
3
+ import '@standard-schema/spec';
6
4
 
7
- declare const createClient: <TRouter extends AnyRouter>(opts: Omit<ClientOptions, "storage">) => Client<TRouter, true>;
5
+ type FetchClientOptions = Omit<ClientOptions, "storage"> & {
6
+ fetchOptions?: RequestInit;
7
+ };
8
+ declare const createClient: <TRouter extends ClientRouterConstraint>(opts: FetchClientOptions) => Client<TRouter, true>;
8
9
 
9
- export { createClient };
10
+ export { type FetchClientOptions, createClient };
@@ -1 +1 @@
1
- import {d,b,c,f}from'./chunk-RCXJM33Z.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,5 @@
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';
2
+ import { StandardSchemaV1 } from '@standard-schema/spec';
5
3
 
6
4
  type LiveTypeMeta = {};
7
5
  type MutationType = "set";
@@ -221,9 +219,9 @@ type InferInsert<T extends LiveObjectAny> = {
221
219
  };
222
220
  type InferUpdate<T extends LiveObjectAny> = Omit<LiveObjectMutationInput<T>, "id">;
223
221
 
224
- type Promisify<T> = T extends Promise<any> ? T : Promise<T>;
222
+ type Promisify<T> = T extends Promise<unknown> ? T : Promise<T>;
225
223
  type ConditionalPromise<T, P extends boolean> = P extends true ? Promise<T> : T;
226
- type Awaitable<T> = T | Promise<T>;
224
+ type PromiseOrSync<T> = T | Promise<T>;
227
225
  type Generatable<T, Arg = never> = T | ((arg: Arg) => T);
228
226
 
229
227
  type Simplify<T> = T extends Record<string, unknown> ? {
@@ -254,139 +252,6 @@ declare const querySchema: z.ZodObject<{
254
252
  }, z.core.$strip>;
255
253
  type RawQueryRequest = z.infer<typeof querySchema>;
256
254
 
257
- /** biome-ignore-all lint/suspicious/noExplicitAny: false positive */
258
- /** biome-ignore-all lint/style/noNonNullAssertion: false positive */
259
-
260
- type RouteRecord = Record<string, AnyRoute>;
261
- declare class Router<TRoutes extends RouteRecord> {
262
- readonly routes: TRoutes;
263
- private constructor();
264
- static create<TRoutes extends RouteRecord>(opts: {
265
- routes: TRoutes;
266
- }): Router<TRoutes>;
267
- }
268
- type AnyRouter = Router<any>;
269
- type Mutation<TInputValidator extends z3.ZodTypeAny | z4.$ZodType, // TODO use StandardSchema instead
270
- TOutput> = {
271
- inputValidator: TInputValidator;
272
- handler: (opts: {
273
- req: MutationRequest<z.infer<TInputValidator>>;
274
- db: Storage;
275
- }) => TOutput;
276
- };
277
- declare const mutationCreator: <TInputValidator extends z3.ZodTypeAny | z4.$ZodType>(validator?: TInputValidator) => {
278
- handler: <THandler extends Mutation<TInputValidator, any>["handler"]>(handler: THandler) => Mutation<TInputValidator, ReturnType<THandler>>;
279
- };
280
- type ReadAuthorizationHandler<TShape extends LiveObjectAny> = (opts: {
281
- ctx: BaseRequest["context"];
282
- }) => WhereClause<TShape> | boolean;
283
- type MutationAuthorizationHandler<TShape extends LiveObjectAny> = (opts: {
284
- ctx: BaseRequest["context"];
285
- value: Simplify<InferLiveObjectWithRelationalIds<TShape>>;
286
- }) => WhereClause<TShape> | boolean;
287
- type Authorization<TShape extends LiveObjectAny> = {
288
- read?: ReadAuthorizationHandler<TShape>;
289
- insert?: MutationAuthorizationHandler<TShape>;
290
- update?: {
291
- preMutation?: MutationAuthorizationHandler<TShape>;
292
- postMutation?: MutationAuthorizationHandler<TShape>;
293
- };
294
- };
295
- declare class Route<TResourceSchema extends LiveObjectAny, TMiddleware extends Middleware<any>, TCustomMutations extends Record<string, Mutation<any, any>>> {
296
- readonly resourceSchema: TResourceSchema;
297
- readonly middlewares: Set<TMiddleware>;
298
- readonly customMutations: TCustomMutations;
299
- readonly authorization?: Authorization<TResourceSchema>;
300
- constructor(resourceSchema: TResourceSchema, customMutations?: TCustomMutations, authorization?: Authorization<TResourceSchema>);
301
- use(...middlewares: TMiddleware[]): this;
302
- withMutations<T extends Record<string, Mutation<any, any>>>(mutationFactory: (opts: {
303
- mutation: typeof mutationCreator;
304
- }) => T): Route<TResourceSchema, TMiddleware, T>;
305
- private handleSet;
306
- private wrapInMiddlewares;
307
- }
308
- type AnyRoute = Route<LiveObjectAny, Middleware<any>, Record<string, any>>;
309
-
310
- /** biome-ignore-all lint/suspicious/noExplicitAny: false positive */
311
-
312
- declare abstract class Storage {
313
- abstract findOne<T extends LiveObjectAny>(resource: T, id: string, options?: {
314
- include?: IncludeClause<T>;
315
- }): Promise<InferLiveObject<T> | undefined>;
316
- abstract find<T extends LiveObjectAny>(resource: T, options?: {
317
- where?: WhereClause<T>;
318
- include?: IncludeClause<T>;
319
- }): Promise<Record<string, InferLiveObject<T>>>;
320
- insert<T extends LiveObjectAny>(resource: T, value: Simplify<InferInsert<T>>): Promise<InferLiveObject<T>>;
321
- update<T extends LiveObjectAny>(resource: T, resourceId: string, value: InferUpdate<T>): Promise<InferLiveObject<T>>;
322
- abstract transaction<T>(fn: (opts: {
323
- trx: Storage;
324
- commit: () => Promise<void>;
325
- rollback: () => Promise<void>;
326
- }) => Promise<T>): Promise<T>;
327
- }
328
-
329
- /** biome-ignore-all lint/suspicious/noExplicitAny: any's are actually used correctly */
330
-
331
- interface BaseRequest {
332
- headers: Record<string, string>;
333
- cookies: Record<string, string>;
334
- queryParams: Record<string, string>;
335
- context: Record<string, any>;
336
- }
337
- interface QueryRequest extends BaseRequest, RawQueryRequest {
338
- type: "QUERY";
339
- }
340
- interface MutationRequest<TInput = any> extends BaseRequest {
341
- type: "MUTATE";
342
- input: TInput;
343
- resource: string;
344
- resourceId?: string;
345
- procedure: string;
346
- }
347
- type Request = QueryRequest | MutationRequest;
348
- type NextFunction<O, R = Request> = (req: R) => Awaitable<O>;
349
- type Middleware<T = any> = (opts: {
350
- req: Request;
351
- next: NextFunction<T>;
352
- }) => ReturnType<NextFunction<T>>;
353
-
354
- declare const useLiveQuery: <T extends {
355
- get: () => U;
356
- subscribe: (cb: (v: U) => void) => () => void;
357
- }, U>(observable: T) => ReturnType<T["get"]>;
358
- declare const SubscriptionProvider: ({ children, client, }: {
359
- children: React.ReactNode;
360
- client: Client<AnyRouter>["client"];
361
- }) => react_jsx_runtime.JSX.Element;
362
-
363
- declare const serverMessageSchema: z.ZodUnion<readonly [z.ZodObject<{
364
- id: z.ZodString;
365
- type: z.ZodLiteral<"REJECT">;
366
- resource: z.ZodString;
367
- message: z.ZodOptional<z.ZodString>;
368
- }, z.core.$strip>, z.ZodObject<{
369
- id: z.ZodString;
370
- type: z.ZodLiteral<"REPLY">;
371
- data: z.ZodAny;
372
- }, z.core.$strip>, z.ZodObject<{
373
- type: z.ZodLiteral<"MUTATE">;
374
- resource: z.ZodString;
375
- resourceId: z.ZodOptional<z.ZodString>;
376
- procedure: z.ZodEnum<{
377
- INSERT: "INSERT";
378
- UPDATE: "UPDATE";
379
- }>;
380
- payload: z.ZodRecord<z.ZodString, z.ZodObject<{
381
- value: z.ZodNullable<z.ZodUnion<[z.ZodUnion<[z.ZodUnion<[z.ZodString, z.ZodNumber]>, z.ZodBoolean]>, z.ZodDate]>>;
382
- _meta: z.ZodOptional<z.ZodObject<{
383
- timestamp: z.ZodNullable<z.ZodOptional<z.ZodString>>;
384
- }, z.core.$strip>>;
385
- }, z.core.$strip>>;
386
- id: z.ZodString;
387
- }, z.core.$strip>]>;
388
- type ServerMessage = z.infer<typeof serverMessageSchema>;
389
-
390
255
  /** biome-ignore-all lint/complexity/noBannedTypes: <explanation> */
391
256
 
392
257
  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>>[];
@@ -402,8 +267,6 @@ declare class QueryBuilder<TCollection extends LiveObjectAny, TInclude extends I
402
267
  private constructor();
403
268
  where(where: WhereClause<TCollection>): QueryBuilder<TCollection, TInclude, TSingle, TShouldAwait>;
404
269
  include<TNewInclude extends IncludeClause<TCollection>>(include: TNewInclude): QueryBuilder<TCollection, TInclude & TNewInclude, TSingle, TShouldAwait>;
405
- get(): ConditionalPromise<InferQueryResult<TCollection, TInclude, TSingle>, TShouldAwait>;
406
- subscribe(callback: (value: InferQueryResult<TCollection, TInclude, TSingle>) => void): () => void;
407
270
  limit(limit: number): QueryBuilder<TCollection, TInclude, TSingle, TShouldAwait>;
408
271
  one(id: string): QueryBuilder<TCollection, TInclude, true, TShouldAwait>;
409
272
  first(where?: WhereClause<TCollection>): QueryBuilder<TCollection, TInclude, true, TShouldAwait>;
@@ -418,9 +281,44 @@ declare class QueryBuilder<TCollection extends LiveObjectAny, TInclude extends I
418
281
  direction: "asc" | "desc";
419
282
  }[] | undefined;
420
283
  };
284
+ buildQueryRequest(): RawQueryRequest;
285
+ get(): ConditionalPromise<InferQueryResult<TCollection, TInclude, TSingle>, TShouldAwait>;
286
+ subscribe(callback: (value: InferQueryResult<TCollection, TInclude, TSingle>) => void): () => void;
421
287
  }
422
288
 
423
- type Client$1<TRouter extends AnyRouter, TShouldAwait extends boolean = false> = {
289
+ /**
290
+ * Extracts the output type from a Standard Schema validator.
291
+ * Supports Standard Schema (via ~standard property) and Zod schemas (via _output property for backward compatibility).
292
+ */
293
+ type InferSchema<T> = T extends {
294
+ "~standard": {
295
+ types?: {
296
+ output: infer U;
297
+ };
298
+ };
299
+ } ? U : T extends StandardSchemaV1<any, any> ? StandardSchemaV1.InferOutput<T> : T extends {
300
+ _output: infer U;
301
+ } ? U : never;
302
+ /**
303
+ * Helper type for custom mutation functions.
304
+ * When the input type is `never` or `undefined`, the function has no parameters.
305
+ */
306
+ type CustomMutationFunction<TInput, TOutput> = [TInput] extends [never] | [undefined] ? () => Promisify<TOutput> : (input: TInput) => Promisify<TOutput>;
307
+ /**
308
+ * Simplified router constraint for client-side usage.
309
+ * This avoids importing server-internal types like Storage and Hooks,
310
+ * which can cause type incompatibilities when the package is bundled.
311
+ */
312
+ type ClientRouterConstraint = {
313
+ routes: Record<string, {
314
+ resourceSchema: LiveObjectAny;
315
+ customMutations: Record<string, {
316
+ inputValidator: StandardSchemaV1<any, any>;
317
+ handler: (...args: any[]) => any;
318
+ }>;
319
+ }>;
320
+ };
321
+ type Client$1<TRouter extends ClientRouterConstraint, TShouldAwait extends boolean = false> = {
424
322
  query: {
425
323
  [K in keyof TRouter["routes"]]: QueryBuilder<TRouter["routes"][K]["resourceSchema"], {}, false, TShouldAwait>;
426
324
  };
@@ -429,11 +327,44 @@ type Client$1<TRouter extends AnyRouter, TShouldAwait extends boolean = false> =
429
327
  insert: (input: Simplify<InferInsert<TRouter["routes"][K]["resourceSchema"]>>) => ConditionalPromise<void, TShouldAwait>;
430
328
  update: (id: string, value: Simplify<InferUpdate<TRouter["routes"][K]["resourceSchema"]>>) => ConditionalPromise<void, TShouldAwait>;
431
329
  } & {
432
- [K2 in keyof TRouter["routes"][K]["customMutations"]]: (input: z.infer<TRouter["routes"][K]["customMutations"][K2]["inputValidator"]>) => Promisify<ReturnType<TRouter["routes"][K]["customMutations"][K2]["handler"]>>;
330
+ [K2 in keyof TRouter["routes"][K]["customMutations"]]: CustomMutationFunction<InferSchema<TRouter["routes"][K]["customMutations"][K2]["inputValidator"]>, ReturnType<TRouter["routes"][K]["customMutations"][K2]["handler"]>>;
433
331
  };
434
332
  };
435
333
  };
436
334
 
335
+ declare const useLiveQuery: <T extends {
336
+ get: () => U;
337
+ subscribe: (cb: (v: U) => void) => () => void;
338
+ }, U>(observable: T) => ReturnType<T["get"]>;
339
+ declare const useLoadData: (client: Client<ClientRouterConstraint>["client"], query: QueryBuilder<any, any>) => void;
340
+
341
+ declare const serverMessageSchema: z.ZodUnion<readonly [z.ZodObject<{
342
+ id: z.ZodString;
343
+ type: z.ZodLiteral<"REJECT">;
344
+ resource: z.ZodString;
345
+ message: z.ZodOptional<z.ZodString>;
346
+ }, z.core.$strip>, z.ZodObject<{
347
+ id: z.ZodString;
348
+ type: z.ZodLiteral<"REPLY">;
349
+ data: z.ZodAny;
350
+ }, z.core.$strip>, z.ZodObject<{
351
+ type: z.ZodLiteral<"MUTATE">;
352
+ resource: z.ZodString;
353
+ resourceId: z.ZodOptional<z.ZodString>;
354
+ procedure: z.ZodEnum<{
355
+ INSERT: "INSERT";
356
+ UPDATE: "UPDATE";
357
+ }>;
358
+ payload: z.ZodRecord<z.ZodString, z.ZodObject<{
359
+ value: z.ZodNullable<z.ZodAny>;
360
+ _meta: z.ZodOptional<z.ZodObject<{
361
+ timestamp: z.ZodNullable<z.ZodOptional<z.ZodString>>;
362
+ }, z.core.$strip>>;
363
+ }, z.core.$strip>>;
364
+ id: z.ZodString;
365
+ }, z.core.$strip>]>;
366
+ type ServerMessage = z.infer<typeof serverMessageSchema>;
367
+
437
368
  type WebSocketClientEventMap = WebSocketEventMap & {
438
369
  connectionChange: {
439
370
  open: boolean;
@@ -489,25 +420,97 @@ type MessageReceivedEvent = {
489
420
  type: "MESSAGE_RECEIVED";
490
421
  message: ServerMessage;
491
422
  };
492
- type ClientEvents = ConnectionStateChangeEvent | MessageReceivedEvent;
493
- type Client<TRouter extends AnyRouter> = {
423
+ type ClientStorageLoadedEvent = {
424
+ type: "CLIENT_STORAGE_LOADED";
425
+ resource: string;
426
+ itemCount: number;
427
+ };
428
+ type DataLoadRequestedEvent = {
429
+ type: "DATA_LOAD_REQUESTED";
430
+ query: RawQueryRequest;
431
+ subscriptionId: string;
432
+ };
433
+ type DataLoadReplyEvent = {
434
+ type: "DATA_LOAD_REPLY";
435
+ resource: string;
436
+ itemCount: number;
437
+ subscriptionId?: string;
438
+ };
439
+ type MutationSentEvent = {
440
+ type: "MUTATION_SENT";
441
+ mutationId: string;
442
+ resource: string;
443
+ resourceId: string;
444
+ procedure: string;
445
+ optimistic: boolean;
446
+ };
447
+ type MutationReceivedEvent = {
448
+ type: "MUTATION_RECEIVED";
449
+ mutationId: string;
450
+ resource: string;
451
+ resourceId: string;
452
+ procedure: string;
453
+ };
454
+ type MutationRejectedEvent = {
455
+ type: "MUTATION_REJECTED";
456
+ mutationId: string;
457
+ resource: string;
458
+ };
459
+ type SubscriptionCreatedEvent = {
460
+ type: "SUBSCRIPTION_CREATED";
461
+ query: RawQueryRequest;
462
+ subscriptionKey: string;
463
+ subscriberCount: number;
464
+ };
465
+ type SubscriptionRemovedEvent = {
466
+ type: "SUBSCRIPTION_REMOVED";
467
+ query: RawQueryRequest;
468
+ subscriptionKey: string;
469
+ };
470
+ type QueryExecutedEvent = {
471
+ type: "QUERY_EXECUTED";
472
+ query: RawQueryRequest;
473
+ resultCount: number;
474
+ };
475
+ type StoreStateUpdatedEvent = {
476
+ type: "STORE_STATE_UPDATED";
477
+ resource: string;
478
+ itemCount: number;
479
+ };
480
+ type OptimisticMutationAppliedEvent = {
481
+ type: "OPTIMISTIC_MUTATION_APPLIED";
482
+ mutationId: string;
483
+ resource: string;
484
+ resourceId: string;
485
+ procedure: string;
486
+ pendingMutations: number;
487
+ };
488
+ type OptimisticMutationUndoneEvent = {
489
+ type: "OPTIMISTIC_MUTATION_UNDONE";
490
+ mutationId: string;
491
+ resource: string;
492
+ resourceId: string;
493
+ pendingMutations: number;
494
+ };
495
+ type ClientEvents = ConnectionStateChangeEvent | MessageReceivedEvent | ClientStorageLoadedEvent | DataLoadRequestedEvent | DataLoadReplyEvent | MutationSentEvent | MutationReceivedEvent | MutationRejectedEvent | SubscriptionCreatedEvent | SubscriptionRemovedEvent | QueryExecutedEvent | StoreStateUpdatedEvent | OptimisticMutationAppliedEvent | OptimisticMutationUndoneEvent;
496
+ type Client<TRouter extends ClientRouterConstraint> = {
494
497
  client: {
495
498
  ws: WebSocketClient;
496
- subscribe: (resourceType?: string[]) => () => void;
497
499
  addEventListener: (listener: (event: ClientEvents) => void) => () => void;
500
+ load: (query: RawQueryRequest) => () => void;
498
501
  };
499
502
  store: Client$1<TRouter>;
500
503
  };
501
- declare const createClient: <TRouter extends AnyRouter>(opts: WebSocketClientOptions) => Client<TRouter>;
504
+ declare const createClient: <TRouter extends ClientRouterConstraint>(opts: WebSocketClientOptions) => Client<TRouter>;
502
505
 
503
506
  type ClientOptions = {
504
507
  url: string;
505
508
  schema: Schema<any>;
506
- credentials?: Generatable<Awaitable<Record<string, string>>>;
509
+ credentials?: Generatable<PromiseOrSync<Record<string, string>>>;
507
510
  storage: {
508
511
  name: string;
509
512
  } | false;
510
513
  logLevel?: LogLevel;
511
514
  };
512
515
 
513
- 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 };
516
+ 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 };
package/dist/index.cjs CHANGED
@@ -1 +1 @@
1
- 'use strict';require('js-xxhash');var s=class{_value;_meta;_encodeInput;_decodeInput};var T=class extends s{inner;constructor(e){super(),this.inner=e;}encodeMutation(e,n,t){return this.inner.encodeMutation(e,n,t)}mergeMutation(e,n,t){return this.inner.mergeMutation(e,n,t)}getStorageFieldType(){return {...this.inner.getStorageFieldType(),nullable:true}}},l=class i extends s{storageType;convertFunc;isIndex;isUnique;defaultValue;foreignReference;isPrimary;constructor(e,n,t,a,r,u,y){super(),this.storageType=e,this.convertFunc=n,this.isIndex=t??false,this.isUnique=a??false,this.defaultValue=r,this.foreignReference=u,this.isPrimary=y??false;}encodeMutation(e,n,t){return {value:n,_meta:{timestamp:t}}}mergeMutation(e,n,t){return t&&t._meta.timestamp&&n._meta.timestamp&&t._meta.timestamp.localeCompare(n._meta.timestamp)>=0?[t,null]:[{value:this.convertFunc?this.convertFunc(n.value):n.value,_meta:n._meta},n]}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 i(this.storageType,this.convertFunc,this.isIndex,true,this.defaultValue,this.foreignReference,this.isPrimary)}index(){return new i(this.storageType,this.convertFunc,true,this.isUnique,this.defaultValue,this.foreignReference,this.isPrimary)}default(e){return new i(this.storageType,this.convertFunc,this.isIndex,this.isUnique,e,this.foreignReference,this.isPrimary)}primary(){return new i(this.storageType,this.convertFunc,this.isIndex,this.isUnique,this.defaultValue,this.foreignReference,true)}nullable(){return new T(this)}},d=class i extends l{constructor(){super("integer",e=>Number(e));}static create(){return new i}},b=d.create,o=class i extends l{constructor(e){super("varchar",void 0,void 0,void 0,void 0,e);}static create(){return new i}static createId(){return new i().index().unique().primary()}static createReference(e){return new i(e)}},I=o.create,O=o.createId,K=o.createReference,p=class i extends l{constructor(){super("boolean",e=>typeof e=="string"?e.toLowerCase()==="true":!!e);}static create(){return new i}},A=p.create,f=class i extends l{constructor(){super("timestamp",e=>typeof e=="string"?new Date(e):e);}static create(){return new i}},j=f.create;var v=class i extends s{name;fields;relations;constructor(e,n,t){super(),this.name=e,this.fields=n,this.relations=t??{};}encodeMutation(e,n,t){return Object.fromEntries(Object.entries(n).map(([a,r])=>[a,(this.fields[a]??this.relations[a]).encodeMutation("set",r,t)]))}mergeMutation(e,n,t){let a={};return [{value:{...(t==null?void 0:t.value)??{},...Object.fromEntries(Object.entries(n).map(([r,u])=>{let y=this.fields[r]??this.relations[r];if(!y)return [r,u];let[x,L]=y.mergeMutation(e,u,t==null?void 0:t.value[r]);return L&&(a[r]=L),[r,x]}))}},a]}setRelations(e){return new i(this.name,this.fields,e)}getStorageFieldType(){throw new Error("Method not implemented.")}static create(e,n){return new i(e,n)}},w=v.create,c=class i extends s{entity;type;required;relationalColumn;foreignColumn;sourceEntity;constructor(e,n,t,a,r){super(),this.entity=e,this.type=n,this.required=r??false,this.relationalColumn=t,this.foreignColumn=a;}encodeMutation(e,n,t){if(e!=="set")throw new Error("Mutation type not implemented.");if(this.type==="many")throw new Error("Many not implemented.");return {value:n,_meta:{timestamp:t}}}mergeMutation(e,n,t){if(this.type==="many")throw new Error("Many not implemented.");return t&&t._meta.timestamp&&n._meta.timestamp&&t._meta.timestamp.localeCompare(n._meta.timestamp)>=0?[t,null]:[n,n]}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,n,t)=>new i(e,"one",n,void 0,t??false)}static createManyFactory(){return (e,n,t)=>new i(e,"many",void 0,n,t??false)}},C=(i,e)=>({$type:"relations",objectName:i.name,relations:e({one:c.createOneFactory(),many:c.createManyFactory()})}),m=i=>i?Array.isArray(i.value)?i.value.map(n=>m(n)):typeof i.value!="object"||i.value===null||i.value instanceof Date?i.value:Object.fromEntries(Object.entries(i.value).map(([n,t])=>Array.isArray(t)?[n,t.map(a=>m(a))]:[n,m(t)])):void 0,V=i=>Object.fromEntries(Object.entries(i).flatMap(([e,n])=>{if(n.$type==="relations")return [];let t=n,a=Object.values(i).find(r=>r.$type==="relations"&&r.objectName===n.name);return a&&(t=t.setRelations(a.relations)),[[t.name,t]]}));var h={CRITICAL:0,ERROR:1,WARN:2,INFO:3,DEBUG:4};exports.LiveAtomicType=l;exports.LiveBoolean=p;exports.LiveNumber=d;exports.LiveObject=v;exports.LiveString=o;exports.LiveTimestamp=f;exports.LiveType=s;exports.LogLevel=h;exports.NullableLiveType=T;exports.Relation=c;exports.boolean=A;exports.createRelations=C;exports.createSchema=V;exports.id=O;exports.inferValue=m;exports.number=b;exports.object=w;exports.reference=K;exports.string=I;exports.timestamp=j;
1
+ 'use strict';require('js-xxhash');var s=class{_value;_meta;_encodeInput;_decodeInput};var T=class extends s{inner;constructor(t){super(),this.inner=t;}encodeMutation(t,n,e){return this.inner.encodeMutation(t,n,e)}mergeMutation(t,n,e){return this.inner.mergeMutation(t,n,e)}getStorageFieldType(){return {...this.inner.getStorageFieldType(),nullable:true}}},l=class i extends s{storageType;convertFunc;isIndex;isUnique;defaultValue;foreignReference;isPrimary;constructor(t,n,e,a,r,u,y){super(),this.storageType=t,this.convertFunc=n,this.isIndex=e??false,this.isUnique=a??false,this.defaultValue=r,this.foreignReference=u,this.isPrimary=y??false;}encodeMutation(t,n,e){return {value:n,_meta:{timestamp:e}}}mergeMutation(t,n,e){return e&&e._meta.timestamp&&n._meta.timestamp&&e._meta.timestamp.localeCompare(n._meta.timestamp)>=0?[e,null]:[{value:this.convertFunc?this.convertFunc(n.value):n.value,_meta:n._meta},n]}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 i(this.storageType,this.convertFunc,this.isIndex,true,this.defaultValue,this.foreignReference,this.isPrimary)}index(){return new i(this.storageType,this.convertFunc,true,this.isUnique,this.defaultValue,this.foreignReference,this.isPrimary)}default(t){return new i(this.storageType,this.convertFunc,this.isIndex,this.isUnique,t,this.foreignReference,this.isPrimary)}primary(){return new i(this.storageType,this.convertFunc,this.isIndex,this.isUnique,this.defaultValue,this.foreignReference,true)}nullable(){return new T(this)}},d=class i extends l{constructor(){super("integer",t=>Number(t));}static create(){return new i}},b=d.create,o=class i extends l{constructor(t){super("varchar",void 0,void 0,void 0,void 0,t);}static create(){return new i}static createId(){return new i().index().unique().primary()}static createReference(t){return new i(t)}},I=o.create,O=o.createId,K=o.createReference,p=class i extends l{constructor(){super("boolean",t=>typeof t=="string"?t.toLowerCase()==="true":!!t);}static create(){return new i}},A=p.create,f=class i extends l{constructor(){super("timestamp",t=>typeof t=="string"?new Date(t):t);}static create(){return new i}},j=f.create;var v=class i extends s{name;fields;relations;constructor(t,n,e){super(),this.name=t,this.fields=n,this.relations=e??{};}encodeMutation(t,n,e){return Object.fromEntries(Object.entries(n).map(([a,r])=>[a,(this.fields[a]??this.relations[a]).encodeMutation("set",r,e)]))}mergeMutation(t,n,e){let a={};return [{value:{...(e==null?void 0:e.value)??{},...Object.fromEntries(Object.entries(n).map(([r,u])=>{let y=this.fields[r]??this.relations[r];if(!y)return [r,u];let[x,L]=y.mergeMutation(t,u,e==null?void 0:e.value[r]);return L&&(a[r]=L),[r,x]}))}},a]}setRelations(t){return new i(this.name,this.fields,t)}getStorageFieldType(){throw new Error("Method not implemented.")}static create(t,n){return new i(t,n)}},w=v.create,c=class i extends s{entity;type;required;relationalColumn;foreignColumn;sourceEntity;constructor(t,n,e,a,r){super(),this.entity=t,this.type=n,this.required=r??false,this.relationalColumn=e,this.foreignColumn=a;}encodeMutation(t,n,e){if(t!=="set")throw new Error("Mutation type not implemented.");if(this.type==="many")throw new Error("Many not implemented.");return {value:n,_meta:{timestamp:e}}}mergeMutation(t,n,e){var a;return this.type==="many"?e?[e,null]:[n,n]:(a=e==null?void 0:e._meta)!=null&&a.timestamp&&n._meta.timestamp&&e._meta.timestamp.localeCompare(n._meta.timestamp)>=0?[e,null]:[n,n]}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 (t,n,e)=>new i(t,"one",n,void 0,e??false)}static createManyFactory(){return (t,n,e)=>new i(t,"many",void 0,n,e??false)}},C=(i,t)=>({$type:"relations",objectName:i.name,relations:t({one:c.createOneFactory(),many:c.createManyFactory()})}),m=i=>i?Array.isArray(i.value)?i.value.map(n=>m(n)):typeof i.value!="object"||i.value===null||i.value instanceof Date?i.value:Object.fromEntries(Object.entries(i.value).map(([n,e])=>Array.isArray(e)?[n,e.map(a=>m(a))]:[n,m(e)])):void 0,V=i=>Object.fromEntries(Object.entries(i).flatMap(([t,n])=>{if(n.$type==="relations")return [];let e=n,a=Object.values(i).find(r=>r.$type==="relations"&&r.objectName===n.name);return a&&(e=e.setRelations(a.relations)),[[e.name,e]]}));var h={CRITICAL:0,ERROR:1,WARN:2,INFO:3,DEBUG:4};exports.LiveAtomicType=l;exports.LiveBoolean=p;exports.LiveNumber=d;exports.LiveObject=v;exports.LiveString=o;exports.LiveTimestamp=f;exports.LiveType=s;exports.LogLevel=h;exports.NullableLiveType=T;exports.Relation=c;exports.boolean=A;exports.createRelations=C;exports.createSchema=V;exports.id=O;exports.inferValue=m;exports.number=b;exports.object=w;exports.reference=K;exports.string=I;exports.timestamp=j;
package/dist/index.js CHANGED
@@ -1 +1 @@
1
- export{e as LiveAtomicType,l as LiveBoolean,f as LiveNumber,p as LiveObject,h as LiveString,n as LiveTimestamp,c as LiveType,x as LogLevel,d as NullableLiveType,r as Relation,m as boolean,s as createRelations,u as createSchema,j as id,t as inferValue,g as number,q as object,k as reference,i as string,o as timestamp}from'./chunk-NJ7LXJAY.js';
1
+ export{e as LiveAtomicType,l as LiveBoolean,f as LiveNumber,p as LiveObject,h as LiveString,n as LiveTimestamp,c as LiveType,y as LogLevel,d as NullableLiveType,r as Relation,m as boolean,s as createRelations,u as createSchema,j as id,t as inferValue,g as number,q as object,k as reference,i as string,o as timestamp}from'./chunk-IT5UC7TA.js';