@live-state/sync 0.0.1-alpha.3 → 0.0.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 x="0123456789ABCDEFGHJKMNPQRSTVWXYZ";var l;(function(e){e.Base32IncorrectEncoding="B32_ENC_INVALID",e.DecodeTimeInvalidCharacter="DEC_TIME_CHAR",e.DecodeTimeValueMalformed="DEC_TIME_MALFORMED",e.EncodeTimeNegative="ENC_TIME_NEG",e.EncodeTimeSizeExceeded="ENC_TIME_SIZE_EXCEED",e.EncodeTimeValueMalformed="ENC_TIME_MALFORMED",e.PRNGDetectFailure="PRNG_DETECT",e.ULIDInvalid="ULID_INVALID",e.Unexpected="UNEXPECTED",e.UUIDInvalid="UUID_INVALID";})(l||(l={}));var u=class extends Error{constructor(t,n){super(`${n} (${t})`),this.name="ULIDError",this.code=t;}};function L(e){let t=Math.floor(e()*32);return t===32&&(t=31),x.charAt(t)}function I(e){let t=M(),n=t&&(t.crypto||t.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 M(){return A()?self:typeof window<"u"?window:typeof global<"u"?global:typeof globalThis<"u"?globalThis:null}function E(e,t){let n="";for(;e>0;e--)n=L(t)+n;return n}function _(e,t=10){if(isNaN(e))throw new u(l.EncodeTimeValueMalformed,`Time must be a number: ${e}`);if(e>0xffffffffffff)throw new u(l.EncodeTimeSizeExceeded,`Cannot encode a time larger than ${0xffffffffffff}: ${e}`);if(e<0)throw new u(l.EncodeTimeNegative,`Time must be positive: ${e}`);if(Number.isInteger(e)===false)throw new u(l.EncodeTimeValueMalformed,`Time must be an integer: ${e}`);let n,i="";for(let a=t;a>0;a--)n=e%32,i=x.charAt(n)+i,e=(e-n)/32;return i}function A(){return typeof WorkerGlobalScope<"u"&&self instanceof WorkerGlobalScope}function R(e,t){let n=I(),i=Date.now();return _(i,10)+E(16,n)}var S=()=>R().toLowerCase(),j=(e,t)=>typeof e=="function"?e(t):e;var w=(e,t,n=[])=>new Proxy(e,{get:(i,a)=>{var p,y;if(a==="__isProxy__")return true;let r=(p=t.get)==null?void 0:p.call(t,i,[...n,a]);if(r!==void 0)return r;let o=i,s=a;return (y=o[s])!=null&&y.__isProxy__||(o[s]=w(typeof o[s]=="object"?o[s]:function(){},t,[...n,a])),o[s]},apply:(i,a,r)=>{var o;return (o=t.apply)==null?void 0:o.call(t,i,n,r)}});var b=e=>{if(e)return Array.isArray(e.value)?e.value.map(t=>b(t)):typeof e.value!="object"||e.value===null||e.value instanceof Date?e.value:Object.fromEntries(Object.entries(e.value).map(([t,n])=>[t,b(n)]))};export{S as a,j as b,w as c,b as d};
@@ -0,0 +1 @@
1
+ var g=Object.create;var h=Object.defineProperty;var L=Object.getOwnPropertyDescriptor;var b=Object.getOwnPropertyNames;var M=Object.getPrototypeOf,O=Object.prototype.hasOwnProperty;var S=(t,e)=>()=>(e||t((e={exports:{}}).exports,e),e.exports);var j=(t,e,n,a)=>{if(e&&typeof e=="object"||typeof e=="function")for(let i of b(e))!O.call(t,i)&&i!==n&&h(t,i,{get:()=>e[i],enumerable:!(a=L(e,i))||a.enumerable});return t};var I=(t,e,n)=>(n=t!=null?g(M(t)):{},j(e||!t||!t.__esModule?h(n,"default",{value:t,enumerable:true}):n,t));var o=class{_value;_meta;_encodeInput;_decodeInput};var T=class extends o{inner;constructor(e){super(),this.inner=e;}encodeMutation(e,n,a){return this.inner.encodeMutation(e,n,a)}mergeMutation(e,n,a){return this.inner.mergeMutation(e,n,a)}getStorageFieldType(){return {...this.inner.getStorageFieldType(),nullable:true}}},s=class t extends o{storageType;convertFunc;isIndex;isUnique;defaultValue;foreignReference;isPrimary;constructor(e,n,a,i,r,u,y){super(),this.storageType=e,this.convertFunc=n,this.isIndex=a??false,this.isUnique=i??false,this.defaultValue=r,this.foreignReference=u,this.isPrimary=y??false;}encodeMutation(e,n,a){return {value:n,_meta:{timestamp:a}}}mergeMutation(e,n,a){return a&&a._meta.timestamp&&n._meta.timestamp&&a._meta.timestamp.localeCompare(n._meta.timestamp)>=0?[a,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 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)}optional(){return new T(this)}},d=class t extends s{constructor(){super("integer",e=>Number(e));}static create(){return new t}},C=d.create,l=class t extends s{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)}},q=l.create,N=l.createId,P=l.createReference,p=class t extends s{constructor(){super("boolean",e=>typeof e=="string"?e.toLowerCase()==="true":!!e);}static create(){return new t}},D=p.create,m=class t extends s{constructor(){super("timestamp",e=>typeof e=="string"?new Date(e):e);}static create(){return new t}},k=m.create;var v=class t extends o{name;fields;relations;constructor(e,n,a){super(),this.name=e,this.fields=n,this.relations=a??{};}encodeMutation(e,n,a){return Object.fromEntries(Object.entries(n).map(([i,r])=>[i,(this.fields[i]??this.relations[i]).encodeMutation("set",r,a)]))}mergeMutation(e,n,a){let i={};return [{value:{...(a==null?void 0:a.value)??{},...Object.fromEntries(Object.entries(n).map(([r,u])=>{let y=this.fields[r]??this.relations[r];if(!y)return [r,u];let[x,f]=y.mergeMutation(e,u,a==null?void 0:a.value[r]);return f&&(i[r]=f),[r,x]}))}},i]}setRelations(e){return new t(this.name,this.fields,e)}getStorageFieldType(){throw new Error("Method not implemented.")}static create(e,n){return new t(e,n)}},X=v.create,c=class t extends o{entity;type;required;relationalColumn;foreignColumn;sourceEntity;constructor(e,n,a,i,r){super(),this.entity=e,this.type=n,this.required=r??false,this.relationalColumn=a,this.foreignColumn=i;}encodeMutation(e,n,a){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:a}}}mergeMutation(e,n,a){if(this.type==="many")throw new Error("Many not implemented.");return a&&a._meta.timestamp&&n._meta.timestamp&&a._meta.timestamp.localeCompare(n._meta.timestamp)>=0?[a,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,a)=>new t(e,"one",n,void 0,a??false)}static createManyFactory(){return (e,n,a)=>new t(e,"many",void 0,n,a??false)}},Y=(t,e)=>({$type:"relations",objectName:t.name,relations:e({one:c.createOneFactory(),many:c.createManyFactory()})}),R=t=>{if(t)return Array.isArray(t.value)?t.value.map(e=>R(e)):typeof t.value!="object"||t.value===null||t.value instanceof Date?t.value:Object.fromEntries(Object.entries(t.value).map(([e,n])=>[e,R(n)]))},Z=t=>Object.fromEntries(Object.entries(t).flatMap(([e,n])=>{if(n.$type==="relations")return [];let a=n,i=Object.values(t).find(r=>r.$type==="relations"&&r.objectName===n.name);return i&&(a=a.setRelations(i.relations)),[[a.name,a]]}));export{S as a,I as b,o as c,d,C as e,l as f,q as g,N as h,P as i,p as j,D as k,m as l,k as m,v as n,X as o,c as p,Y as q,R as r,Z as s};
package/dist/client.d.ts CHANGED
@@ -1,3 +1,3 @@
1
- import 'zod';
2
- export { c as Client, C as ClientOptions, b as ClientState, O as ObservableClientState, R as RawObjPool, e as SubscriptionProvider, d as createClient, u as useLiveQuery } from './index-NDRWVwih.js';
1
+ export { d as Client, C as ClientOptions, c as SubscriptionProvider, e as createClient, u as useLiveQuery } from './index-Cri5dZ2i.js';
3
2
  import 'react/jsx-runtime';
3
+ import 'zod';
package/dist/client.js CHANGED
@@ -1 +1 @@
1
- import {d,a,c,b as b$1}from'./chunk-NIWX45UD.js';import {z as z$1}from'zod';import {stringify}from'qs';import {useState,useEffect}from'react';import {jsx,Fragment}from'react/jsx-runtime';z$1.object({type:z$1.literal("QUERY"),resource:z$1.string(),where:z$1.record(z$1.any()).optional(),include:z$1.record(z$1.any()).optional()});var T=z$1.record(z$1.object({value:z$1.string().or(z$1.number()).or(z$1.boolean()).or(z$1.date()),_meta:z$1.object({timestamp:z$1.string().optional()}).optional()})).superRefine((l,e)=>{l.id&&e.addIssue({code:z$1.ZodIssueCode.custom,message:"Payload cannot have an id"});}),x=z$1.object({id:z$1.string().optional(),type:z$1.literal("MUTATE"),resource:z$1.string()}),R=x.extend({procedure:z$1.string(),payload:z$1.any().optional()}),O=x.extend({resourceId:z$1.string(),payload:T});z$1.union([R,O]);var b=z$1.string(),k=z$1.object({id:b,type:z$1.literal("SUBSCRIBE"),resource:z$1.string()}),G=z$1.object({id:b,type:z$1.literal("SYNC"),lastSyncedAt:z$1.string().optional(),resources:z$1.string().array().optional(),where:z$1.record(z$1.any()).optional()}),A=O.extend({id:b}),W=R.extend({id:b}),K=z$1.union([W,A]);z$1.union([k,G,K]);var z=z$1.object({id:b,type:z$1.literal("SYNC"),resource:z$1.string(),data:z$1.record(T)}),I=z$1.object({id:b,type:z$1.literal("REJECT"),resource:z$1.string(),message:z$1.string().optional()}),B=z$1.object({id:b,type:z$1.literal("REPLY"),data:z$1.any()}),P=z$1.union([z,I,B,A]);var S=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 r={id:e,type:t,referencedBy:new Map(i.map(s=>[s,new Set])),references:new Map,subscriptions:new Set};return this.nodes.set(e,r),r}getNode(e){return this.nodes.get(e)}hasNode(e){return this.nodes.has(e)}createLink(e,t,i){let r=this.nodes.get(e),s=this.nodes.get(t);if(!r)throw new Error(`Source node with id ${e} does not exist`);if(!s)throw new Error(`Target node with id ${t} does not exist`);r.references.set(i,t);let n=s.referencedBy.get(i);n&&n instanceof Set?n.add(e):s.referencedBy.set(i,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 r=i.references.get(t);if(!r)return;i.references.delete(t);let s=this.nodes.get(r);if(!s)return;let n=s.referencedBy.get(t);n&&(n instanceof Set?n.delete(e):s.referencedBy.delete(t),this.notifySubscribers(r)),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,r])=>{(r instanceof Set?Array.from(r.values()):[r]).forEach(n=>{let d=this.nodes.get(n);!d||!d.references.get(i)||(d.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(r){console.error(`Error in node subscription for node ${e}:`,r);}});}getAllNodes(){return Array.from(this.nodes.values())}};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$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){this.eventListeners.has(e)||this.eventListeners.set(e,new Set),this.eventListeners.get(e).add(t);}removeEventListener(e,t){this.eventListeners.has(e)&&this.eventListeners.get(e).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){this.dispatchEvent("error",e);}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){this.eventListeners.has(e)&&this.eventListeners.get(e).forEach(i=>{i(t);});}};var re=(l,e)=>{let[t,i]=useState(()=>l.get());return useEffect(()=>{if(e!=null&&e.subscribeToRemote)return l.subscribeToRemote()},[e==null?void 0:e.subscribeToRemote]),useEffect(()=>l.subscribe(()=>{let r=l.get();i(r);}),[]),t},se=({children:l,client:e})=>(useEffect(()=>{e.subscribeToRemote();},[]),jsx(Fragment,{children:l}));var j=class{url;ws;schema;rawObjPool={};optimisticMutationStack={};optimisticObjGraph=new S;optimisticRawObjPool={};resourceTypeSubscriptions={};routeSubscriptions={};replyHandlers={};constructor(e){this.url=e.url,this.schema=e.schema,this.ws=new M({url:e.url,autoConnect:true,autoReconnect:true,reconnectTimeout:5e3,credentials:e.credentials}),this.ws.addEventListener("message",t=>{this.handleServerMessage(t.data);}),this.ws.addEventListener("connectionChange",t=>{t.open&&(this.sendWsMessage({id:a(),type:"SYNC"}),Object.entries(this.routeSubscriptions).forEach(([i,r])=>{r>0&&this.sendWsMessage({id:a(),type:"SUBSCRIBE",resource:i});}),Object.values(this.optimisticMutationStack).forEach(i=>{i.forEach(r=>this.sendWsMessage(r));}));});}get(e){if(e.length===0)throw new Error("Path must not be empty");if(e.length===1)return Object.fromEntries(Object.entries(this.optimisticRawObjPool[e[0]]??{}).map(([i,r])=>[i,c(r)]));let t=this.getFullObject(e[0],e[1]);if(!t)throw new Error("Object of type "+e[0]+" not found with id "+e[1]);return c(t)}handleServerMessage(e){try{console.log("Message received from the server:",e);let t=P.parse(JSON.parse(e));if(console.log("Parsed message:",t),t.type==="MUTATE"){let{resource:i}=t;try{this.addMutation(i,t);}catch(r){console.error("Error parsing mutation from the server:",r);}}else if(t.type==="SYNC"){let{resource:i,data:r}=t;console.log("Syncing resource:",r,t),Object.entries(r).forEach(([s,n])=>{this.addMutation(i,{id:s,type:"MUTATE",resource:i,resourceId:s,payload:n});});}else if(t.type!=="REJECT"){if(t.type==="REPLY"){let{id:i,data:r}=t;if(!this.replyHandlers[i])return;clearTimeout(this.replyHandlers[i].timeoutHandle),this.replyHandlers[i].handler(r);}}}catch(t){console.error("Error parsing message from the server:",t);}}subscribeToRemote(e){return this.routeSubscriptions[e]=(this.routeSubscriptions[e]??0)+1,this.sendWsMessage({id:a(),type:"SUBSCRIBE",resource:e}),()=>{this.routeSubscriptions[e]-=1,this.routeSubscriptions[e];}}subscribeToSlice(e,t){if(e.length===1)return this.resourceTypeSubscriptions[e[0]]||(this.resourceTypeSubscriptions[e[0]]=new Set),this.resourceTypeSubscriptions[e[0]].add(t),()=>{this.resourceTypeSubscriptions[e[0]].delete(t);};if(e.length===2){if(!this.optimisticObjGraph.getNode(e[1]))throw new Error("Node not found");return this.optimisticObjGraph.subscribe(e[1],t)}throw new Error("Not implemented")}mutate(e,t,i){let r={id:a(),type:"MUTATE",resource:e,payload:this.schema[e].encodeMutation("set",i,new Date().toISOString()),resourceId:t};this.addMutation(e,r,true),this.sendWsMessage(r);}genericMutate(e,t,i){if(!this.ws||!this.ws.connected())throw new Error("WebSocket not connected");let r={id:a(),type:"MUTATE",resource:e,procedure:t,payload:i};return this.sendWsMessage(r),new Promise((s,n)=>{this.replyHandlers[r.id]={timeoutHandle:setTimeout(()=>{delete this.replyHandlers[r.id],n(new Error("Reply timeout"));},5e3),handler:d=>{delete this.replyHandlers[r.id],s(d);}};})}sendWsMessage(e){this.ws&&this.ws.connected()&&this.ws.send(JSON.stringify(e));}addMutation(e,t,i=false){var d,p,g,v;let r=this.schema[e];if(console.log("Adding mutation",t),!r)throw new Error("Schema not found");i?(this.optimisticMutationStack[e]||(this.optimisticMutationStack[e]=[]),this.optimisticMutationStack[e].push(t)):this.optimisticMutationStack[e]&&(this.optimisticMutationStack[e]=this.optimisticMutationStack[e].filter(a=>a.id!==t.id)),this.optimisticObjGraph.getNode(t.resourceId)||this.optimisticObjGraph.createNode(t.resourceId,e,Object.values(r.relations).flatMap(a=>a.type==="many"?[a.foreignColumn]:[]));let s=((d=this.optimisticRawObjPool[e])==null?void 0:d[t.resourceId])??((p=this.rawObjPool[e])==null?void 0:p[t.resourceId]);i||(this.rawObjPool[e]??={},this.rawObjPool[e][t.resourceId]={value:{...this.schema[e].mergeMutation("set",t.payload,this.rawObjPool[e][t.resourceId])[0].value,id:{value:t.resourceId}}}),this.optimisticRawObjPool[e]??={};let n=(this.optimisticMutationStack[e]??[]).reduce((a,u)=>u.resourceId!==u.resourceId?a:this.schema[e].mergeMutation("set",u.payload,a)[0],(g=this.rawObjPool[e])==null?void 0:g[t.resourceId]);if(n&&(this.optimisticRawObjPool[e][t.resourceId]={value:{...n.value,id:{value:t.resourceId}}}),Object.keys(r.relations).length>0){let a=Object.fromEntries(Object.entries(r.relations).flatMap(([u,f])=>f.type==="one"?[[f.relationalColumn,u]]:[]));Object.entries(t.payload).forEach(([u,f])=>{if(!a[u])return;let[,h]=r.relations[a[u]].mergeMutation("set",f,s==null?void 0:s.value[u]);h&&this.optimisticObjGraph.createLink(t.resourceId,h.value,u);});}(v=this.resourceTypeSubscriptions[e])==null||v.forEach(a=>a()),this.optimisticObjGraph.notifySubscribers(t.resourceId);}getFullObject(e,t){var s;let i=this.optimisticObjGraph.getNode(t);if(!i)return;let r=(s=this.optimisticRawObjPool[e])==null?void 0:s[t];if(r)return {value:{...r.value,...Object.fromEntries(Array.from(i.referencedBy.entries()).map(([n,d])=>{var f;let p=d instanceof Set,g=p?Array.from(d.values()).flatMap(h=>{let m=this.optimisticObjGraph.getNode(h);return m?[m]:[]}):this.optimisticObjGraph.getNode(d);if(!g)return [n,void 0];let[v,a]=Object.entries(this.schema[e].relations).find(h=>h[1].relationalColumn===n||h[1].foreignColumn===n)??[],u=a==null?void 0:a.entity.name;return !u||!a?[n,p?[]:void 0]:[v,{value:p?g.map(h=>{var m;return (m=this.optimisticRawObjPool[u])==null?void 0:m[h.id]}):(f=this.optimisticRawObjPool[u])==null?void 0:f[g.id]}]}))}}}},Ce=l=>{let e=new j(l);return {client:{ws:e.ws,subscribeToRemote:t=>{let i=[];for(let r of t??Object.keys(e.schema))i.push(e.subscribeToRemote(r));return ()=>{i.forEach(r=>r());}}},store:d(()=>{},{apply:(t,i,r)=>{let s=i.slice(0,-1),n=i[i.length-1];if(n==="get")return e.get(s);if(n==="subscribe")return e.subscribeToSlice(s,r[0]);if(n==="subscribeToRemote")return e.subscribeToRemote(s[0]);if(n==="insert"){let{id:d,...p}=r[0];return e.mutate(s[0],d,p)}if(n==="update"){let[d,p]=r;return e.mutate(s[0],d,p)}return e.genericMutate(s[0],n,r[0])}})}};export{se as SubscriptionProvider,Ce as createClient,re as useLiveQuery};
1
+ import {c,a,d,b}from'./chunk-7PTISKJ7.js';import {useState,useRef,useEffect}from'react';import {jsx,Fragment}from'react/jsx-runtime';import {z as z$1}from'zod';import {stringify}from'qs';import {openDB}from'idb';import {sha256}from'crypto-hash';var se=f=>{let[e,t]=useState(()=>f.get()),i=useRef(false);return useEffect(()=>(i.current?t(f.get()):i.current=true,f.subscribe(()=>{let r=f.get();t(r);})),[f]),e},re=({children:f,client:e})=>(useEffect(()=>{e.subscribe();},[]),jsx(Fragment,{children:f}));z$1.object({type:z$1.literal("QUERY"),resource:z$1.string(),where:z$1.record(z$1.any()).optional(),include:z$1.record(z$1.any()).optional()});var x=z$1.record(z$1.object({value:z$1.string().or(z$1.number()).or(z$1.boolean()).or(z$1.date()).nullable(),_meta:z$1.object({timestamp:z$1.string().optional().nullable()}).optional()})).superRefine((f,e)=>{f.id&&e.addIssue({code:z$1.ZodIssueCode.custom,message:"Payload cannot have an id"});}),I=z$1.object({id:z$1.string().optional(),type:z$1.literal("MUTATE"),resource:z$1.string()}),L=I.extend({procedure:z$1.string(),payload:z$1.any().optional()}),C=I.extend({resourceId:z$1.string(),payload:x});z$1.union([L,C]);var S=z$1.string(),$=z$1.object({id:S,type:z$1.literal("SUBSCRIBE"),resource:z$1.string()}),J=z$1.object({id:S,type:z$1.literal("SYNC"),lastSyncedAt:z$1.string().optional(),resources:z$1.string().array().optional(),where:z$1.record(z$1.any()).optional()}),B=C.extend({id:S}),q=L.extend({id:S}),F=z$1.union([q,B]);z$1.union([$,J,F]);var Q=z$1.object({id:S,type:z$1.literal("SYNC"),resource:z$1.string(),data:z$1.record(x)}),Z=z$1.object({id:S,type:z$1.literal("REJECT"),resource:z$1.string(),message:z$1.string().optional()}),X=z$1.object({id:S,type:z$1.literal("REPLY"),data:z$1.any()}),z=z$1.union([Q,Z,X,B]);var w=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){this.eventListeners.has(e)||this.eventListeners.set(e,new Set),this.eventListeners.get(e).add(t);}removeEventListener(e,t){this.eventListeners.has(e)&&this.eventListeners.get(e).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){this.dispatchEvent("error",e);}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){this.eventListeners.has(e)&&this.eventListeners.get(e).forEach(i=>{i(t);});}};var O=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,i){let s=this.nodes.get(e),r=this.nodes.get(t);if(!s)throw new Error(`Source node with id ${e} does not exist`);if(!r)throw new Error(`Target node with id ${t} does not exist`);s.references.set(i,t);let n=r.referencedBy.get(i);n&&n instanceof Set?n.add(e):r.referencedBy.set(i,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(t);n&&(n instanceof Set?n.delete(e):r.referencedBy.delete(t),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=f=>sha256(JSON.stringify(f));var P="__meta",T="databases",R=class{db;async init(e,t){var b,m;if(typeof window>"u")return;let s=((b=(await window.indexedDB.databases()).find(d=>d.name===t))==null?void 0:b.version)??1,r=await A(e),n=Object.fromEntries(await Promise.all(Object.entries(e).map(async([d,p])=>[d,await A(p)]))),o=await openDB("live-state-databases",1,{upgrade(d){d.objectStoreNames.contains(T)||d.createObjectStore(T);}}),l=(m=await this.getAll(o,T))==null?void 0:m[t];(l==null?void 0:l.schemaHash)!==r&&s++,this.db=await openDB(t,s,{async upgrade(d){[...Object.keys(e),P].forEach(p=>{(l==null?void 0:l.objectHashes[p])!==n[p]&&d.objectStoreNames.contains(p)&&d.deleteObjectStore(p),d.objectStoreNames.contains(p)||d.createObjectStore(p);}),await o.put(T,{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(P,e):new Promise(t=>t(void 0))}setMeta(e,t){var i;return (i=this.db)==null?void 0:i.put(P,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 j=class{constructor(e,t,i){this.schema=e;this.kvStorage=new R,this.kvStorage.init(this.schema,t).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 O;optimisticRawObjPool={};resourceTypeSubscriptions={};kvStorage;get(e){return Object.fromEntries(Object.entries(this.optimisticRawObjPool[e]??{}).map(([t,i])=>[t,d(i)]))}getOne(e,t){var n;let i=this.optimisticObjGraph.getNode(t);if(!i)return;let s=(n=this.optimisticRawObjPool[e])==null?void 0:n[t];if(!s)return;let r={value:{...s.value,...Object.fromEntries(Array.from(i.references.entries()).map(([o,l])=>{var u;let b=this.optimisticObjGraph.getNode(l);if(!b)return [o,void 0];let[m,d]=Object.entries(this.schema[e].relations).find(h=>h[1].relationalColumn===o||h[1].foreignColumn===o)??[],p=d==null?void 0:d.entity.name;return !p||!d?[o,void 0]:[m,(u=this.optimisticRawObjPool[p])==null?void 0:u[b.id]]})),...Object.fromEntries(Array.from(i.referencedBy.entries()).map(([o,l])=>{var h;let b=l instanceof Set,m=b?Array.from(l.values()).flatMap(g=>{let y=this.optimisticObjGraph.getNode(g);return y?[y]:[]}):this.optimisticObjGraph.getNode(l);if(!m)return [o,void 0];let[d,p]=Object.entries(this.schema[e].relations).find(g=>g[1].entity.name===o)??[],u=p==null?void 0:p.entity.name;return !u||!p?[o,b?[]:void 0]:[d,b?{value:m.map(g=>{var y;return (y=this.optimisticRawObjPool[u])==null?void 0:y[g.id]})}:(h=this.optimisticRawObjPool[u])==null?void 0:h[m.id]]}))}};return d(r)}subscribe(e,t){if(e.length===1)return this.resourceTypeSubscriptions[e[0]]||(this.resourceTypeSubscriptions[e[0]]=new Set),this.resourceTypeSubscriptions[e[0]].add(t),()=>{this.resourceTypeSubscriptions[e[0]].delete(t);};if(e.length===2){if(!this.optimisticObjGraph.getNode(e[1]))throw new Error("Node not found");return this.optimisticObjGraph.subscribe(e[1],t)}throw new Error("Not implemented")}addMutation(e,t,i=false){var l,b,m,d,p;let s=this.schema[e];if(console.log("Adding mutation",t),!s)throw new Error("Schema not found");let r=(l=this.optimisticRawObjPool[e])==null?void 0:l[t.resourceId];if(i)(this.optimisticMutationStack[e]??=[]).push(t);else {this.optimisticMutationStack[e]=((m=(b=this.optimisticMutationStack)==null?void 0:b[e])==null?void 0:m.filter(g=>g.id!==t.id))??[],this.rawObjPool[e]??={};let u={value:{...this.schema[e].mergeMutation("set",t.payload,this.rawObjPool[e][t.resourceId])[0].value,id:{value:t.resourceId}}};this.rawObjPool[e][t.resourceId]=u;let h=u.value;delete h.id,this.kvStorage.set(e,t.resourceId,h);}this.kvStorage.setMeta("mutationStack",this.optimisticMutationStack);let n=(d=this.rawObjPool[e])==null?void 0:d[t.resourceId],o=(this.optimisticMutationStack[e]??[]).reduce((u,h)=>h.resourceId!==t.resourceId?u:this.schema[e].mergeMutation("set",h.payload,u)[0],n);if(o&&((this.optimisticRawObjPool[e]??={})[t.resourceId]={value:{...o.value,id:{value:t.resourceId}}}),this.optimisticObjGraph.hasNode(t.resourceId)||this.optimisticObjGraph.createNode(t.resourceId,e,Object.values(s.relations).flatMap(u=>u.type==="many"?[u.entity.name]:[])),Object.keys(s.relations).length>0){let u=Object.fromEntries(Object.entries(s.relations).flatMap(([h,g])=>g.type==="one"?[[g.relationalColumn,h]]:[]));Object.entries(t.payload).forEach(([h,g])=>{if(!g||!u[h])return;let y=r==null?void 0:r.value[h],[,M]=s.relations[u[h]].mergeMutation("set",g,y);if(M){if(!this.optimisticObjGraph.hasNode(M.value)){let D=s.relations[u[h]].entity.name;this.optimisticObjGraph.createNode(M.value,D,Object.values(this.schema[D].relations).flatMap(G=>G.type==="many"?[G.entity.name]:[]));}y!=null&&y.value&&this.optimisticObjGraph.removeLink(t.resourceId,h),this.optimisticObjGraph.createLink(t.resourceId,M.value,e);}});}(p=this.resourceTypeSubscriptions[e])==null||p.forEach(u=>u()),this.optimisticObjGraph.notifySubscribers(t.resourceId);}loadConsolidatedState(e,t){Object.entries(t).forEach(([i,s])=>{this.addMutation(e,{id:i,type:"MUTATE",resource:e,resourceId:i,payload:s});});}};var k=class{url;ws;store;routeSubscriptions={};replyHandlers={};constructor(e){this.url=e.url,this.store=new j(e.schema,e.storage.name,t=>{var i,s;(s=(i=Object.values(t))==null?void 0:i.flat())==null||s.forEach(r=>this.sendWsMessage(r));}),this.ws=new w({url:e.url,autoConnect:true,autoReconnect:true,reconnectTimeout:5e3,credentials:e.credentials}),this.ws.addEventListener("message",t=>{this.handleServerMessage(t.data);}),this.ws.addEventListener("connectionChange",t=>{t.open&&(this.sendWsMessage({id:a(),type:"SYNC"}),Object.entries(this.routeSubscriptions).forEach(([i,s])=>{s>0&&this.sendWsMessage({id:a(),type:"SUBSCRIBE",resource:i});}),Object.values(this.store.optimisticMutationStack).forEach(i=>{i&&i.forEach(s=>this.sendWsMessage(s));}));});}get(e){if(e.length===0)throw new Error("Path must not be empty");return e.length===1?this.store.get(e[0]):this.store.getOne(e[0],e[1])}handleServerMessage(e){try{console.log("Message received from the server:",e);let t=z.parse(JSON.parse(e));if(console.log("Parsed message:",t),t.type==="MUTATE"){let{resource:i}=t;try{this.store.addMutation(i,t);}catch(s){console.error("Error parsing mutation from the server:",s);}}else if(t.type==="SYNC"){let{resource:i,data:s}=t;console.log("Syncing resource:",s,t),this.store.loadConsolidatedState(i,s);}else if(t.type!=="REJECT"){if(t.type==="REPLY"){let{id:i,data:s}=t;if(!this.replyHandlers[i])return;clearTimeout(this.replyHandlers[i].timeoutHandle),this.replyHandlers[i].handler(s);}}}catch(t){console.error("Error parsing message from the server:",t);}}subscribeToRemote(e){return this.routeSubscriptions[e]=(this.routeSubscriptions[e]??0)+1,this.sendWsMessage({id:a(),type:"SUBSCRIBE",resource:e}),()=>{this.routeSubscriptions[e]-=1,this.routeSubscriptions[e];}}subscribeToStore(e,t){this.store.subscribe(e,t);}mutate(e,t,i){let s={id:a(),type:"MUTATE",resource:e,payload:this.store.schema[e].encodeMutation("set",i,new Date().toISOString()),resourceId:t};this.store.addMutation(e,s,true),this.sendWsMessage(s);}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);}};})}sendWsMessage(e){this.ws&&this.ws.connected()&&this.ws.send(JSON.stringify(e));}},Ve=f=>{let e=new k(f);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());}}},store:c(()=>{},{apply:(t,i,s)=>{let r=i.slice(0,-1),n=i[i.length-1];if(n==="get")return e.get(r);if(n==="subscribe")return e.subscribeToStore(r,s[0]);if(n==="subscribeToRemote")return e.subscribeToRemote(r[0]);if(n==="insert"){let{id:o,...l}=s[0];return e.mutate(r[0],o,l)}if(n==="update"){let[o,l]=s;return e.mutate(r[0],o,l)}return e.genericMutate(r[0],n,s[0])}})}};export{re as SubscriptionProvider,Ve as createClient,se as useLiveQuery};
@@ -1,14 +1,15 @@
1
- import { A as AnyRouter, C as ClientOptions, L as LiveObjectAny, W as WhereClause, S as Simplify, I as InferLiveObject, a as LiveObjectMutationInput } from './index-NDRWVwih.js';
2
- import 'zod';
1
+ import { A as AnyRouter, C as ClientOptions, L as LiveObjectAny, W as WhereClause, I as IncludeClause, S as Simplify, a as InferLiveObject, b as LiveObjectMutationInput } from './index-Cri5dZ2i.js';
3
2
  import 'react/jsx-runtime';
3
+ import 'zod';
4
4
 
5
5
  type GetOptions<T extends LiveObjectAny> = {
6
6
  headers?: Record<string, string>;
7
7
  where?: WhereClause<T>;
8
+ include?: IncludeClause<T>;
8
9
  };
9
10
  type FetchClient<TRouter extends AnyRouter> = {
10
11
  [K in keyof TRouter["routes"]]: {
11
- get: (opts?: GetOptions<TRouter["routes"][K]["_resourceSchema"]>) => Promise<Simplify<InferLiveObject<TRouter["routes"][K]["_resourceSchema"]>>>;
12
+ get: (opts?: GetOptions<TRouter["routes"][K]["_resourceSchema"]>) => Promise<Record<string, Simplify<InferLiveObject<TRouter["routes"][K]["_resourceSchema"]>>>>;
12
13
  upsert: (input: Simplify<LiveObjectMutationInput<TRouter["routes"][K]["_resourceSchema"]>>) => Promise<void>;
13
14
  };
14
15
  };
@@ -1 +1 @@
1
- import {d,b,c}from'./chunk-NIWX45UD.js';import {stringify}from'qs';var S=t=>d(()=>{},{apply:async(h,c$1,o)=>{if(c$1.length>2)throw new Error("Trying to access invalid property");let[n,i]=c$1,s=await b(t.credentials)??{};if(i==="get"){let e=o[0],r={};return e!=null&&e.where&&(r.where=e.where),fetch(`${t.url}/${n}${Object.keys(r).length>0?`?${stringify(r)}`:""}`,{headers:s}).then(async y=>Object.fromEntries(Object.entries(await y.json()??{}).map(([m,l])=>[m,c(l)])))}if(i==="upsert"){let{id:e,...r}=o[0];return fetch(`${t.url}/${n}/set`,{method:"POST",headers:{...s,"Content-Type":"application/json"},body:JSON.stringify({resourceId:e,payload:t.schema[n].encodeMutation("set",r,new Date().toISOString())})})}return fetch(`${t.url}/${n}/${i}`,{method:"POST",headers:{...s,"Content-Type":"application/json"},body:JSON.stringify(o[0])})}});export{S as createClient};
1
+ import {c,b,d}from'./chunk-7PTISKJ7.js';import {stringify}from'qs';var v=r=>c(()=>{},{apply:async(f,c,i)=>{if(c.length>2)throw new Error("Trying to access invalid property");let[n,o]=c,s=await b(r.credentials)??{};if(o==="get"){let e=i[0],t={};return e!=null&&e.where&&(t.where=e.where),e!=null&&e.include&&(t.include=e.include),fetch(`${r.url}/${n}${Object.keys(t).length>0?`?${stringify(t)}`:""}`,{headers:s}).then(async y=>Object.fromEntries(Object.entries(await y.json()??{}).map(([l,d$1])=>[l,d(d$1)])))}if(o==="upsert"){let{id:e,...t}=i[0];return fetch(`${r.url}/${n}/set`,{method:"POST",headers:{...s,"Content-Type":"application/json"},body:JSON.stringify({resourceId:e,payload:r.schema[n].encodeMutation("set",t,new Date().toISOString())})})}return fetch(`${r.url}/${n}/${o}`,{method:"POST",headers:{...s,"Content-Type":"application/json"},body:JSON.stringify(i[0])})}});export{v as createClient};
@@ -1,5 +1,5 @@
1
- import { ZodTypeAny, z } from 'zod';
2
1
  import * as react_jsx_runtime from 'react/jsx-runtime';
2
+ import { ZodTypeAny, z } from 'zod';
3
3
 
4
4
  type Promisify<T> = T extends Promise<any> ? T : Promise<T>;
5
5
  type Awaitable<T> = T | Promise<T>;
@@ -52,7 +52,7 @@ declare class OptionalLiveType<T extends LiveTypeAny> extends LiveType<T["_value
52
52
  getStorageFieldType(): StorageFieldType;
53
53
  }
54
54
  type LiveAtomicTypeMeta = {
55
- timestamp: string;
55
+ timestamp: string | null;
56
56
  } & LiveTypeMeta;
57
57
  declare class LiveAtomicType<Value> extends LiveType<Value, LiveAtomicTypeMeta, Value, {
58
58
  value: Value;
@@ -124,7 +124,7 @@ declare class LiveObject<TName extends string, TSchema extends Record<string, Li
124
124
  }
125
125
  type LiveObjectAny = LiveObject<string, Record<string, LiveTypeAny>, any>;
126
126
  declare class Relation<TEntity extends LiveObjectAny, TSourceEntity extends LiveObjectAny, TType extends "one" | "many", TRelationalColumn extends keyof TSourceEntity["fields"], TForeignColumn extends keyof TEntity["fields"], TRequired extends boolean> extends LiveType<InferIndex<TEntity>, {
127
- timestamp: string;
127
+ timestamp: string | null;
128
128
  } & LiveTypeMeta> {
129
129
  readonly entity: TEntity;
130
130
  readonly type: TType;
@@ -154,6 +154,13 @@ declare class Relation<TEntity extends LiveObjectAny, TSourceEntity extends Live
154
154
  } | null
155
155
  ];
156
156
  getStorageFieldType(): StorageFieldType;
157
+ toJSON(): {
158
+ entityName: string;
159
+ type: TType;
160
+ required: TRequired;
161
+ relationalColumn: TRelationalColumn | undefined;
162
+ foreignColumn: TForeignColumn | undefined;
163
+ };
157
164
  static createOneFactory<TOriginEntity extends LiveObjectAny>(): <TEntity extends LiveObjectAny, TColumn extends keyof TOriginEntity["fields"], TRequired extends boolean = false>(entity: TEntity, column: TColumn, required?: TRequired) => Relation<TEntity, TOriginEntity, "one", TColumn, never, TRequired>;
158
165
  static createManyFactory<TOriginEntity extends LiveObjectAny>(): <TEntity extends LiveObjectAny, TColumn extends keyof TEntity["fields"], TRequired extends boolean = false>(entity: TEntity, foreignColumn: TColumn, required?: TRequired) => Relation<TEntity, TOriginEntity, "many", never, TColumn, TRequired>;
159
166
  }
@@ -187,6 +194,9 @@ type WhereClause<T extends LiveObjectAny> = {
187
194
  } & {
188
195
  [K in keyof T["relations"]]?: WhereClause<T["relations"][K]["entity"]>;
189
196
  };
197
+ type IncludeClause<T extends LiveObjectAny> = {
198
+ [K in keyof T["relations"]]?: boolean;
199
+ };
190
200
 
191
201
  type RouteRecord = Record<string, AnyRoute>;
192
202
  declare class Router<TRoutes extends RouteRecord> {
@@ -230,11 +240,20 @@ declare class Route<TResourceSchema extends LiveObjectAny, TMiddleware extends M
230
240
  }
231
241
  type AnyRoute = Route<LiveObjectAny, Middleware<any>, Record<string, any>>;
232
242
 
243
+ type Simplify<T> = T extends Record<string, any> ? {
244
+ [K in keyof T]: Simplify<T[K]>;
245
+ } : T;
246
+
233
247
  declare abstract class Storage {
234
- abstract updateSchema(opts: Schema<any>): Promise<void>;
235
- abstract findById<T extends LiveObjectAny>(resourceName: string, id: string): Promise<MaterializedLiveType<T> | undefined>;
236
- abstract find<T extends LiveObjectAny>(resourceName: string, where?: Record<string, any>): Promise<Record<string, MaterializedLiveType<T>>>;
237
- abstract upsert<T extends LiveObjectAny>(resourceName: string, resourceId: string, value: MaterializedLiveType<T>): Promise<MaterializedLiveType<T>>;
248
+ abstract findOne<T extends LiveObjectAny>(resource: T, id: string, options?: {
249
+ include?: IncludeClause<T>;
250
+ }): Promise<InferLiveObject<T> | undefined>;
251
+ abstract find<T extends LiveObjectAny>(resource: T, options?: {
252
+ where?: WhereClause<T>;
253
+ include?: IncludeClause<T>;
254
+ }): Promise<Record<string, InferLiveObject<T>>>;
255
+ insert<T extends LiveObjectAny>(resource: T, value: Simplify<LiveObjectMutationInput<T>>): Promise<InferLiveObject<T>>;
256
+ update<T extends LiveObjectAny>(resource: T, resourceId: string, value: LiveObjectMutationInput<T>): Promise<InferLiveObject<T>>;
238
257
  }
239
258
 
240
259
  type ParsedRequest<TInput = any> = {
@@ -245,6 +264,7 @@ type ParsedRequest<TInput = any> = {
245
264
  procedure?: string;
246
265
  context: Record<string, any>;
247
266
  where?: Record<string, any>;
267
+ include?: Record<string, any>;
248
268
  type: "QUERY" | "MUTATE";
249
269
  resourceId?: string;
250
270
  input?: TInput;
@@ -255,9 +275,18 @@ type Middleware<T = any> = (opts: {
255
275
  next: NextFunction<T>;
256
276
  }) => ReturnType<NextFunction<T>>;
257
277
 
258
- type Simplify<T> = T extends Record<string, any> ? {
259
- [K in keyof T]: Simplify<T[K]>;
260
- } : T;
278
+ type DeepSubscribable<T> = {
279
+ [K in keyof T]: DeepSubscribable<T[K]>;
280
+ } & {
281
+ get: () => T;
282
+ subscribe: (callback: (value: T) => void) => () => void;
283
+ };
284
+
285
+ declare const useLiveQuery: <T extends DeepSubscribable<U>, U>(observable: T) => Simplify<ReturnType<T["get"]>>;
286
+ declare const SubscriptionProvider: ({ children, client, }: {
287
+ children: React.ReactNode;
288
+ client: Client<AnyRouter>["client"];
289
+ }) => react_jsx_runtime.JSX.Element;
261
290
 
262
291
  type WebSocketClientEventMap = WebSocketEventMap & {
263
292
  connectionChange: {
@@ -298,31 +327,15 @@ declare class WebSocketClient {
298
327
  private dispatchEvent;
299
328
  }
300
329
 
301
- declare const useLiveQuery: <T extends ObservableClientState<U>, U>(observable: T, opts?: {
302
- subscribeToRemote?: boolean;
303
- }) => Simplify<ReturnType<T["get"]>>;
304
- declare const SubscriptionProvider: ({ children, client, }: {
305
- children: React.ReactNode;
306
- client: Client<AnyRouter>["client"];
307
- }) => react_jsx_runtime.JSX.Element;
308
-
309
- type RawObjPool = Record<string, Record<string, MaterializedLiveType<LiveObjectAny> | undefined> | undefined>;
310
330
  type ClientState<TRouter extends AnyRouter> = {
311
331
  [K in keyof TRouter["routes"]]: Record<InferIndex<TRouter["routes"][K]["_resourceSchema"]>, InferLiveObject<TRouter["routes"][K]["_resourceSchema"]>> | undefined;
312
332
  };
313
- type ObservableClientState<T> = {
314
- [K in keyof T]: ObservableClientState<T[K]>;
315
- } & {
316
- get: () => T;
317
- subscribe: (callback: (value: T) => void) => () => void;
318
- subscribeToRemote: () => () => void;
319
- };
320
333
  type Client<TRouter extends AnyRouter> = {
321
334
  client: {
322
335
  ws: WebSocketClient;
323
- subscribeToRemote: (resourceType?: string[]) => () => void;
336
+ subscribe: (resourceType?: string[]) => () => void;
324
337
  };
325
- store: ObservableClientState<ClientState<TRouter>> & {
338
+ store: DeepSubscribable<ClientState<TRouter>> & {
326
339
  [K in keyof TRouter["routes"]]: {
327
340
  insert: (input: Simplify<LiveObjectMutationInput<TRouter["routes"][K]["_resourceSchema"]>>) => void;
328
341
  update: (id: string, value: Omit<Simplify<LiveObjectMutationInput<TRouter["routes"][K]["_resourceSchema"]>>, "id">) => void;
@@ -333,11 +346,16 @@ type Client<TRouter extends AnyRouter> = {
333
346
  };
334
347
  };
335
348
  };
349
+ declare const createClient: <TRouter extends AnyRouter>(opts: ClientOptions & {
350
+ storage: {
351
+ name: string;
352
+ };
353
+ }) => Client<TRouter>;
354
+
336
355
  type ClientOptions = {
337
356
  url: string;
338
357
  schema: Schema<any>;
339
358
  credentials?: Generatable<Awaitable<Record<string, string>>>;
340
359
  };
341
- declare const createClient: <TRouter extends AnyRouter>(opts: ClientOptions) => Client<TRouter>;
342
360
 
343
- export { type AnyRouter as A, type ClientOptions as C, type InferLiveObject as I, type LiveObjectAny as L, type ObservableClientState as O, type RawObjPool as R, type Simplify as S, type WhereClause as W, type LiveObjectMutationInput as a, type ClientState as b, type Client as c, createClient as d, SubscriptionProvider as e, useLiveQuery as u };
361
+ export { type AnyRouter as A, type ClientOptions as C, type IncludeClause as I, type LiveObjectAny as L, type Simplify as S, type WhereClause as W, type InferLiveObject as a, type LiveObjectMutationInput as b, SubscriptionProvider as c, type Client as d, createClient as e, useLiveQuery as u };
package/dist/index.cjs CHANGED
@@ -1 +1 @@
1
- 'use strict';var o=class{_value;_meta;_encodeInput;_decodeInput};var T=class extends o{inner;constructor(e){super(),this.inner=e;}encodeMutation(e,t,n){return this.inner.encodeMutation(e,t,n)}mergeMutation(e,t,n){return this.inner.mergeMutation(e,t,n)}getStorageFieldType(){return {...this.inner.getStorageFieldType(),nullable:true}}},s=class a extends o{storageType;convertFunc;isIndex;isUnique;defaultValue;foreignReference;isPrimary;constructor(e,t,n,r,i,y,c){super(),this.storageType=e,this.convertFunc=t,this.isIndex=n??false,this.isUnique=r??false,this.defaultValue=i,this.foreignReference=y,this.isPrimary=c??false;}encodeMutation(e,t,n){return {value:t,_meta:{timestamp:n}}}mergeMutation(e,t,n){return n&&n._meta.timestamp.localeCompare(t._meta.timestamp)>=0?[n,null]:[{value:this.convertFunc?this.convertFunc(t.value):t.value,_meta:t._meta},t]}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 a(this.storageType,this.convertFunc,this.isIndex,true,this.defaultValue,this.foreignReference,this.isPrimary)}index(){return new a(this.storageType,this.convertFunc,true,this.isUnique,this.defaultValue,this.foreignReference,this.isPrimary)}default(e){return new a(this.storageType,this.convertFunc,this.isIndex,this.isUnique,e,this.foreignReference,this.isPrimary)}primary(){return new a(this.storageType,this.convertFunc,this.isIndex,this.isUnique,this.defaultValue,this.foreignReference,true)}optional(){return new T(this)}},d=class a extends s{constructor(){super("integer",e=>Number(e));}static create(){return new a}},O=d.create,l=class a extends s{constructor(e){super("varchar",void 0,void 0,void 0,void 0,e);}static create(){return new a}static createId(){return new a().index().unique().primary()}static createReference(e){return new a(e)}},j=l.create,S=l.createId,w=l.createReference,p=class a extends s{constructor(){super("boolean",e=>typeof e=="string"?e.toLowerCase()==="true":!!e);}static create(){return new a}},I=p.create,m=class a extends s{constructor(){super("timestamp",e=>typeof e=="string"?new Date(e):e);}static create(){return new a}},A=m.create;var v=class a extends o{name;fields;relations;constructor(e,t,n){super(),this.name=e,this.fields=t,this.relations=n??{};}encodeMutation(e,t,n){return Object.fromEntries(Object.entries(t).map(([r,i])=>[r,(this.fields[r]??this.relations[r]).encodeMutation("set",i,n)]))}mergeMutation(e,t,n){let r={};return [{value:{...(n==null?void 0:n.value)??{},...Object.fromEntries(Object.entries(t).map(([i,y])=>{let[c,f]=(this.fields[i]??this.relations[i]).mergeMutation(e,y,n==null?void 0:n.value[i]);return f&&(r[i]=f),[i,c]}))}},r]}setRelations(e){return new a(this.name,this.fields,e)}getStorageFieldType(){throw new Error("Method not implemented.")}static create(e,t){return new a(e,t)}},P=v.create,u=class a extends o{entity;type;required;relationalColumn;foreignColumn;sourceEntity;constructor(e,t,n,r,i){super(),this.entity=e,this.type=t,this.required=i??false,this.relationalColumn=n,this.foreignColumn=r;}encodeMutation(e,t,n){if(e!=="set")throw new Error("Mutation type not implemented.");if(this.type==="many")throw new Error("Many not implemented.");return {value:t,_meta:{timestamp:n}}}mergeMutation(e,t,n){if(this.type==="many")throw new Error("Many not implemented.");return n&&n._meta.timestamp.localeCompare(t._meta.timestamp)>=0?[n,null]:[t,t]}getStorageFieldType(){return {type:"varchar",nullable:!this.required,references:`${this.entity.name}.${String(this.foreignColumn??this.relationalColumn??"id")}`}}static createOneFactory(){return (e,t,n)=>new a(e,"one",t,void 0,n??false)}static createManyFactory(){return (e,t,n)=>new a(e,"many",void 0,t,n??false)}},D=(a,e)=>({$type:"relations",objectName:a.name,relations:e({one:u.createOneFactory(),many:u.createManyFactory()})}),h=a=>{if(a)return Array.isArray(a.value)?a.value.map(e=>h(e)):typeof a.value!="object"?a.value:Object.fromEntries(Object.entries(a.value).map(([e,t])=>[e,h(t)]))},k=a=>Object.fromEntries(Object.entries(a).flatMap(([e,t])=>{if(t.$type==="relations")return [];let n=t,r=Object.values(a).find(i=>i.$type==="relations"&&i.objectName===t.name);return r&&(n=n.setRelations(r.relations)),[[n.name,n]]}));exports.LiveBoolean=p;exports.LiveNumber=d;exports.LiveObject=v;exports.LiveString=l;exports.LiveTimestamp=m;exports.LiveType=o;exports.Relation=u;exports.boolean=I;exports.createRelations=D;exports.createSchema=k;exports.id=S;exports.inferValue=h;exports.number=O;exports.object=P;exports.reference=w;exports.string=j;exports.timestamp=A;
1
+ 'use strict';var o=class{_value;_meta;_encodeInput;_decodeInput};var T=class extends o{inner;constructor(e){super(),this.inner=e;}encodeMutation(e,t,n){return this.inner.encodeMutation(e,t,n)}mergeMutation(e,t,n){return this.inner.mergeMutation(e,t,n)}getStorageFieldType(){return {...this.inner.getStorageFieldType(),nullable:true}}},s=class a extends o{storageType;convertFunc;isIndex;isUnique;defaultValue;foreignReference;isPrimary;constructor(e,t,n,r,i,u,y){super(),this.storageType=e,this.convertFunc=t,this.isIndex=n??false,this.isUnique=r??false,this.defaultValue=i,this.foreignReference=u,this.isPrimary=y??false;}encodeMutation(e,t,n){return {value:t,_meta:{timestamp:n}}}mergeMutation(e,t,n){return n&&n._meta.timestamp&&t._meta.timestamp&&n._meta.timestamp.localeCompare(t._meta.timestamp)>=0?[n,null]:[{value:this.convertFunc?this.convertFunc(t.value):t.value,_meta:t._meta},t]}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 a(this.storageType,this.convertFunc,this.isIndex,true,this.defaultValue,this.foreignReference,this.isPrimary)}index(){return new a(this.storageType,this.convertFunc,true,this.isUnique,this.defaultValue,this.foreignReference,this.isPrimary)}default(e){return new a(this.storageType,this.convertFunc,this.isIndex,this.isUnique,e,this.foreignReference,this.isPrimary)}primary(){return new a(this.storageType,this.convertFunc,this.isIndex,this.isUnique,this.defaultValue,this.foreignReference,true)}optional(){return new T(this)}},d=class a extends s{constructor(){super("integer",e=>Number(e));}static create(){return new a}},j=d.create,l=class a extends s{constructor(e){super("varchar",void 0,void 0,void 0,void 0,e);}static create(){return new a}static createId(){return new a().index().unique().primary()}static createReference(e){return new a(e)}},S=l.create,I=l.createId,w=l.createReference,p=class a extends s{constructor(){super("boolean",e=>typeof e=="string"?e.toLowerCase()==="true":!!e);}static create(){return new a}},A=p.create,m=class a extends s{constructor(){super("timestamp",e=>typeof e=="string"?new Date(e):e);}static create(){return new a}},_=m.create;var v=class a extends o{name;fields;relations;constructor(e,t,n){super(),this.name=e,this.fields=t,this.relations=n??{};}encodeMutation(e,t,n){return Object.fromEntries(Object.entries(t).map(([r,i])=>[r,(this.fields[r]??this.relations[r]).encodeMutation("set",i,n)]))}mergeMutation(e,t,n){let r={};return [{value:{...(n==null?void 0:n.value)??{},...Object.fromEntries(Object.entries(t).map(([i,u])=>{let y=this.fields[i]??this.relations[i];if(!y)return [i,u];let[R,f]=y.mergeMutation(e,u,n==null?void 0:n.value[i]);return f&&(r[i]=f),[i,R]}))}},r]}setRelations(e){return new a(this.name,this.fields,e)}getStorageFieldType(){throw new Error("Method not implemented.")}static create(e,t){return new a(e,t)}},D=v.create,c=class a extends o{entity;type;required;relationalColumn;foreignColumn;sourceEntity;constructor(e,t,n,r,i){super(),this.entity=e,this.type=t,this.required=i??false,this.relationalColumn=n,this.foreignColumn=r;}encodeMutation(e,t,n){if(e!=="set")throw new Error("Mutation type not implemented.");if(this.type==="many")throw new Error("Many not implemented.");return {value:t,_meta:{timestamp:n}}}mergeMutation(e,t,n){if(this.type==="many")throw new Error("Many not implemented.");return n&&n._meta.timestamp&&t._meta.timestamp&&n._meta.timestamp.localeCompare(t._meta.timestamp)>=0?[n,null]:[t,t]}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,t,n)=>new a(e,"one",t,void 0,n??false)}static createManyFactory(){return (e,t,n)=>new a(e,"many",void 0,t,n??false)}},k=(a,e)=>({$type:"relations",objectName:a.name,relations:e({one:c.createOneFactory(),many:c.createManyFactory()})}),h=a=>{if(a)return Array.isArray(a.value)?a.value.map(e=>h(e)):typeof a.value!="object"||a.value===null||a.value instanceof Date?a.value:Object.fromEntries(Object.entries(a.value).map(([e,t])=>[e,h(t)]))},W=a=>Object.fromEntries(Object.entries(a).flatMap(([e,t])=>{if(t.$type==="relations")return [];let n=t,r=Object.values(a).find(i=>i.$type==="relations"&&i.objectName===t.name);return r&&(n=n.setRelations(r.relations)),[[n.name,n]]}));exports.LiveBoolean=p;exports.LiveNumber=d;exports.LiveObject=v;exports.LiveString=l;exports.LiveTimestamp=m;exports.LiveType=o;exports.Relation=c;exports.boolean=A;exports.createRelations=k;exports.createSchema=W;exports.id=I;exports.inferValue=h;exports.number=j;exports.object=D;exports.reference=w;exports.string=S;exports.timestamp=_;
package/dist/index.d.cts CHANGED
@@ -45,7 +45,7 @@ declare class OptionalLiveType<T extends LiveTypeAny> extends LiveType<T["_value
45
45
  getStorageFieldType(): StorageFieldType;
46
46
  }
47
47
  type LiveAtomicTypeMeta = {
48
- timestamp: string;
48
+ timestamp: string | null;
49
49
  } & LiveTypeMeta;
50
50
  declare class LiveAtomicType<Value> extends LiveType<Value, LiveAtomicTypeMeta, Value, {
51
51
  value: Value;
@@ -136,7 +136,7 @@ declare class LiveObject<TName extends string, TSchema extends Record<string, Li
136
136
  declare const object: typeof LiveObject.create;
137
137
  type LiveObjectAny = LiveObject<string, Record<string, LiveTypeAny>, any>;
138
138
  declare class Relation<TEntity extends LiveObjectAny, TSourceEntity extends LiveObjectAny, TType extends "one" | "many", TRelationalColumn extends keyof TSourceEntity["fields"], TForeignColumn extends keyof TEntity["fields"], TRequired extends boolean> extends LiveType<InferIndex<TEntity>, {
139
- timestamp: string;
139
+ timestamp: string | null;
140
140
  } & LiveTypeMeta> {
141
141
  readonly entity: TEntity;
142
142
  readonly type: TType;
@@ -166,6 +166,13 @@ declare class Relation<TEntity extends LiveObjectAny, TSourceEntity extends Live
166
166
  } | null
167
167
  ];
168
168
  getStorageFieldType(): StorageFieldType;
169
+ toJSON(): {
170
+ entityName: string;
171
+ type: TType;
172
+ required: TRequired;
173
+ relationalColumn: TRelationalColumn | undefined;
174
+ foreignColumn: TForeignColumn | undefined;
175
+ };
169
176
  static createOneFactory<TOriginEntity extends LiveObjectAny>(): <TEntity extends LiveObjectAny, TColumn extends keyof TOriginEntity["fields"], TRequired extends boolean = false>(entity: TEntity, column: TColumn, required?: TRequired) => Relation<TEntity, TOriginEntity, "one", TColumn, never, TRequired>;
170
177
  static createManyFactory<TOriginEntity extends LiveObjectAny>(): <TEntity extends LiveObjectAny, TColumn extends keyof TEntity["fields"], TRequired extends boolean = false>(entity: TEntity, foreignColumn: TColumn, required?: TRequired) => Relation<TEntity, TOriginEntity, "many", never, TColumn, TRequired>;
171
178
  }
@@ -205,5 +212,8 @@ type WhereClause<T extends LiveObjectAny> = {
205
212
  } & {
206
213
  [K in keyof T["relations"]]?: WhereClause<T["relations"][K]["entity"]>;
207
214
  };
215
+ type IncludeClause<T extends LiveObjectAny> = {
216
+ [K in keyof T["relations"]]?: boolean;
217
+ };
208
218
 
209
- export { type InferIndex, type InferLiveObject, type InferLiveObjectWithRelationalIds, type InferLiveType, LiveBoolean, LiveNumber, LiveObject, type LiveObjectAny, type LiveObjectMutationInput, LiveString, LiveTimestamp, LiveType, type LiveTypeAny, type LiveTypeMeta, type MaterializedLiveType, type MutationType, Relation, type Schema, type StorageFieldType, type WhereClause, boolean, createRelations, createSchema, id, inferValue, number, object, reference, string, timestamp };
219
+ export { type IncludeClause, type InferIndex, type InferLiveObject, type InferLiveObjectWithRelationalIds, type InferLiveType, LiveBoolean, LiveNumber, LiveObject, type LiveObjectAny, type LiveObjectMutationInput, LiveString, LiveTimestamp, LiveType, type LiveTypeAny, type LiveTypeMeta, type MaterializedLiveType, type MutationType, Relation, type Schema, type StorageFieldType, type WhereClause, boolean, createRelations, createSchema, id, inferValue, number, object, reference, string, timestamp };
package/dist/index.d.ts CHANGED
@@ -45,7 +45,7 @@ declare class OptionalLiveType<T extends LiveTypeAny> extends LiveType<T["_value
45
45
  getStorageFieldType(): StorageFieldType;
46
46
  }
47
47
  type LiveAtomicTypeMeta = {
48
- timestamp: string;
48
+ timestamp: string | null;
49
49
  } & LiveTypeMeta;
50
50
  declare class LiveAtomicType<Value> extends LiveType<Value, LiveAtomicTypeMeta, Value, {
51
51
  value: Value;
@@ -136,7 +136,7 @@ declare class LiveObject<TName extends string, TSchema extends Record<string, Li
136
136
  declare const object: typeof LiveObject.create;
137
137
  type LiveObjectAny = LiveObject<string, Record<string, LiveTypeAny>, any>;
138
138
  declare class Relation<TEntity extends LiveObjectAny, TSourceEntity extends LiveObjectAny, TType extends "one" | "many", TRelationalColumn extends keyof TSourceEntity["fields"], TForeignColumn extends keyof TEntity["fields"], TRequired extends boolean> extends LiveType<InferIndex<TEntity>, {
139
- timestamp: string;
139
+ timestamp: string | null;
140
140
  } & LiveTypeMeta> {
141
141
  readonly entity: TEntity;
142
142
  readonly type: TType;
@@ -166,6 +166,13 @@ declare class Relation<TEntity extends LiveObjectAny, TSourceEntity extends Live
166
166
  } | null
167
167
  ];
168
168
  getStorageFieldType(): StorageFieldType;
169
+ toJSON(): {
170
+ entityName: string;
171
+ type: TType;
172
+ required: TRequired;
173
+ relationalColumn: TRelationalColumn | undefined;
174
+ foreignColumn: TForeignColumn | undefined;
175
+ };
169
176
  static createOneFactory<TOriginEntity extends LiveObjectAny>(): <TEntity extends LiveObjectAny, TColumn extends keyof TOriginEntity["fields"], TRequired extends boolean = false>(entity: TEntity, column: TColumn, required?: TRequired) => Relation<TEntity, TOriginEntity, "one", TColumn, never, TRequired>;
170
177
  static createManyFactory<TOriginEntity extends LiveObjectAny>(): <TEntity extends LiveObjectAny, TColumn extends keyof TEntity["fields"], TRequired extends boolean = false>(entity: TEntity, foreignColumn: TColumn, required?: TRequired) => Relation<TEntity, TOriginEntity, "many", never, TColumn, TRequired>;
171
178
  }
@@ -205,5 +212,8 @@ type WhereClause<T extends LiveObjectAny> = {
205
212
  } & {
206
213
  [K in keyof T["relations"]]?: WhereClause<T["relations"][K]["entity"]>;
207
214
  };
215
+ type IncludeClause<T extends LiveObjectAny> = {
216
+ [K in keyof T["relations"]]?: boolean;
217
+ };
208
218
 
209
- export { type InferIndex, type InferLiveObject, type InferLiveObjectWithRelationalIds, type InferLiveType, LiveBoolean, LiveNumber, LiveObject, type LiveObjectAny, type LiveObjectMutationInput, LiveString, LiveTimestamp, LiveType, type LiveTypeAny, type LiveTypeMeta, type MaterializedLiveType, type MutationType, Relation, type Schema, type StorageFieldType, type WhereClause, boolean, createRelations, createSchema, id, inferValue, number, object, reference, string, timestamp };
219
+ export { type IncludeClause, type InferIndex, type InferLiveObject, type InferLiveObjectWithRelationalIds, type InferLiveType, LiveBoolean, LiveNumber, LiveObject, type LiveObjectAny, type LiveObjectMutationInput, LiveString, LiveTimestamp, LiveType, type LiveTypeAny, type LiveTypeMeta, type MaterializedLiveType, type MutationType, Relation, type Schema, type StorageFieldType, type WhereClause, boolean, createRelations, createSchema, id, inferValue, number, object, reference, string, timestamp };
package/dist/index.js CHANGED
@@ -1 +1 @@
1
- import'./chunk-EK7ODJWE.js';var o=class{_value;_meta;_encodeInput;_decodeInput};var T=class extends o{inner;constructor(e){super(),this.inner=e;}encodeMutation(e,t,n){return this.inner.encodeMutation(e,t,n)}mergeMutation(e,t,n){return this.inner.mergeMutation(e,t,n)}getStorageFieldType(){return {...this.inner.getStorageFieldType(),nullable:true}}},s=class a extends o{storageType;convertFunc;isIndex;isUnique;defaultValue;foreignReference;isPrimary;constructor(e,t,n,r,i,y,c){super(),this.storageType=e,this.convertFunc=t,this.isIndex=n??false,this.isUnique=r??false,this.defaultValue=i,this.foreignReference=y,this.isPrimary=c??false;}encodeMutation(e,t,n){return {value:t,_meta:{timestamp:n}}}mergeMutation(e,t,n){return n&&n._meta.timestamp.localeCompare(t._meta.timestamp)>=0?[n,null]:[{value:this.convertFunc?this.convertFunc(t.value):t.value,_meta:t._meta},t]}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 a(this.storageType,this.convertFunc,this.isIndex,true,this.defaultValue,this.foreignReference,this.isPrimary)}index(){return new a(this.storageType,this.convertFunc,true,this.isUnique,this.defaultValue,this.foreignReference,this.isPrimary)}default(e){return new a(this.storageType,this.convertFunc,this.isIndex,this.isUnique,e,this.foreignReference,this.isPrimary)}primary(){return new a(this.storageType,this.convertFunc,this.isIndex,this.isUnique,this.defaultValue,this.foreignReference,true)}optional(){return new T(this)}},d=class a extends s{constructor(){super("integer",e=>Number(e));}static create(){return new a}},O=d.create,l=class a extends s{constructor(e){super("varchar",void 0,void 0,void 0,void 0,e);}static create(){return new a}static createId(){return new a().index().unique().primary()}static createReference(e){return new a(e)}},j=l.create,S=l.createId,w=l.createReference,p=class a extends s{constructor(){super("boolean",e=>typeof e=="string"?e.toLowerCase()==="true":!!e);}static create(){return new a}},I=p.create,m=class a extends s{constructor(){super("timestamp",e=>typeof e=="string"?new Date(e):e);}static create(){return new a}},A=m.create;var v=class a extends o{name;fields;relations;constructor(e,t,n){super(),this.name=e,this.fields=t,this.relations=n??{};}encodeMutation(e,t,n){return Object.fromEntries(Object.entries(t).map(([r,i])=>[r,(this.fields[r]??this.relations[r]).encodeMutation("set",i,n)]))}mergeMutation(e,t,n){let r={};return [{value:{...(n==null?void 0:n.value)??{},...Object.fromEntries(Object.entries(t).map(([i,y])=>{let[c,f]=(this.fields[i]??this.relations[i]).mergeMutation(e,y,n==null?void 0:n.value[i]);return f&&(r[i]=f),[i,c]}))}},r]}setRelations(e){return new a(this.name,this.fields,e)}getStorageFieldType(){throw new Error("Method not implemented.")}static create(e,t){return new a(e,t)}},P=v.create,u=class a extends o{entity;type;required;relationalColumn;foreignColumn;sourceEntity;constructor(e,t,n,r,i){super(),this.entity=e,this.type=t,this.required=i??false,this.relationalColumn=n,this.foreignColumn=r;}encodeMutation(e,t,n){if(e!=="set")throw new Error("Mutation type not implemented.");if(this.type==="many")throw new Error("Many not implemented.");return {value:t,_meta:{timestamp:n}}}mergeMutation(e,t,n){if(this.type==="many")throw new Error("Many not implemented.");return n&&n._meta.timestamp.localeCompare(t._meta.timestamp)>=0?[n,null]:[t,t]}getStorageFieldType(){return {type:"varchar",nullable:!this.required,references:`${this.entity.name}.${String(this.foreignColumn??this.relationalColumn??"id")}`}}static createOneFactory(){return (e,t,n)=>new a(e,"one",t,void 0,n??false)}static createManyFactory(){return (e,t,n)=>new a(e,"many",void 0,t,n??false)}},D=(a,e)=>({$type:"relations",objectName:a.name,relations:e({one:u.createOneFactory(),many:u.createManyFactory()})}),h=a=>{if(a)return Array.isArray(a.value)?a.value.map(e=>h(e)):typeof a.value!="object"?a.value:Object.fromEntries(Object.entries(a.value).map(([e,t])=>[e,h(t)]))},k=a=>Object.fromEntries(Object.entries(a).flatMap(([e,t])=>{if(t.$type==="relations")return [];let n=t,r=Object.values(a).find(i=>i.$type==="relations"&&i.objectName===t.name);return r&&(n=n.setRelations(r.relations)),[[n.name,n]]}));export{p as LiveBoolean,d as LiveNumber,v as LiveObject,l as LiveString,m as LiveTimestamp,o as LiveType,u as Relation,I as boolean,D as createRelations,k as createSchema,S as id,h as inferValue,O as number,P as object,w as reference,j as string,A as timestamp};
1
+ export{j as LiveBoolean,d as LiveNumber,n as LiveObject,f as LiveString,l as LiveTimestamp,c as LiveType,p as Relation,k as boolean,q as createRelations,s as createSchema,h as id,r as inferValue,e as number,o as object,i as reference,g as string,m as timestamp}from'./chunk-ECP6AHZL.js';
package/dist/server.cjs CHANGED
@@ -1,2 +1,2 @@
1
- 'use strict';var Se=require('qs'),zod=require('zod'),I=require('crypto'),kysely=require('kysely');function _interopDefault(e){return e&&e.__esModule?e:{default:e}}var Se__default=/*#__PURE__*/_interopDefault(Se);var I__default=/*#__PURE__*/_interopDefault(I);var oe=Object.create;var U=Object.defineProperty;var ie=Object.getOwnPropertyDescriptor;var se=Object.getOwnPropertyNames;var ce=Object.getPrototypeOf,ue=Object.prototype.hasOwnProperty;var de=(r,e)=>()=>(e||r((e={exports:{}}).exports,e),e.exports);var le=(r,e,t,n)=>{if(e&&typeof e=="object"||typeof e=="function")for(let a of se(e))!ue.call(r,a)&&a!==t&&U(r,a,{get:()=>e[a],enumerable:!(n=ie(e,a))||n.enumerable});return r};var V=(r,e,t)=>(t=r!=null?oe(ce(r)):{},le(U(t,"default",{value:r,enumerable:true}),r));var P=de(E=>{Object.defineProperty(E,"__esModule",{value:true});E.parse=Te;E.serialize=Re;var pe=/^[\u0021-\u003A\u003C\u003E-\u007E]+$/,me=/^[\u0021-\u003A\u003C-\u007E]*$/,ye=/^([.]?[a-z0-9]([a-z0-9-]{0,61}[a-z0-9])?)([.][a-z0-9]([a-z0-9-]{0,61}[a-z0-9])?)*$/i,fe=/^[\u0020-\u003A\u003D-\u007E]*$/,he=Object.prototype.toString,ge=(()=>{let r=function(){};return r.prototype=Object.create(null),r})();function Te(r,e){let t=new ge,n=r.length;if(n<2)return t;let a=(e==null?void 0:e.decode)||xe,o=0;do{let i=r.indexOf("=",o);if(i===-1)break;let u=r.indexOf(";",o),s=u===-1?n:u;if(i>s){o=r.lastIndexOf(";",i-1)+1;continue}let h=_(r,o,i),m=$(r,i,h),l=r.slice(h,m);if(t[l]===void 0){let p=_(r,i+1,s),d=$(r,s,p),c=a(r.slice(p,d));t[l]=c;}o=s+1;}while(o<n);return t}function _(r,e,t){do{let n=r.charCodeAt(e);if(n!==32&&n!==9)return e}while(++e<t);return t}function $(r,e,t){for(;e>t;){let n=r.charCodeAt(--e);if(n!==32&&n!==9)return e+1}return t}function Re(r,e,t){let n=(t==null?void 0:t.encode)||encodeURIComponent;if(!pe.test(r))throw new TypeError(`argument name is invalid: ${r}`);let a=n(e);if(!me.test(a))throw new TypeError(`argument val is invalid: ${e}`);let o=r+"="+a;if(!t)return o;if(t.maxAge!==void 0){if(!Number.isInteger(t.maxAge))throw new TypeError(`option maxAge is invalid: ${t.maxAge}`);o+="; Max-Age="+t.maxAge;}if(t.domain){if(!ye.test(t.domain))throw new TypeError(`option domain is invalid: ${t.domain}`);o+="; Domain="+t.domain;}if(t.path){if(!fe.test(t.path))throw new TypeError(`option path is invalid: ${t.path}`);o+="; Path="+t.path;}if(t.expires){if(!be(t.expires)||!Number.isFinite(t.expires.valueOf()))throw new TypeError(`option expires is invalid: ${t.expires}`);o+="; Expires="+t.expires.toUTCString();}if(t.httpOnly&&(o+="; HttpOnly"),t.secure&&(o+="; Secure"),t.partitioned&&(o+="; Partitioned"),t.priority)switch(typeof t.priority=="string"?t.priority.toLowerCase():void 0){case "low":o+="; Priority=Low";break;case "medium":o+="; Priority=Medium";break;case "high":o+="; Priority=High";break;default:throw new TypeError(`option priority is invalid: ${t.priority}`)}if(t.sameSite)switch(typeof t.sameSite=="string"?t.sameSite.toLowerCase():t.sameSite){case true:case "strict":o+="; SameSite=Strict";break;case "lax":o+="; SameSite=Lax";break;case "none":o+="; SameSite=None";break;default:throw new TypeError(`option sameSite is invalid: ${t.sameSite}`)}return o}function xe(r){if(r.indexOf("%")===-1)return r;try{return decodeURIComponent(r)}catch{return r}}function be(r){return he.call(r)==="[object Date]"}});var H=V(P());var q=zod.z.object({type:zod.z.literal("QUERY"),resource:zod.z.string(),where:zod.z.record(zod.z.any()).optional(),include:zod.z.record(zod.z.any()).optional()}),L=zod.z.record(zod.z.object({value:zod.z.string().or(zod.z.number()).or(zod.z.boolean()).or(zod.z.date()),_meta:zod.z.object({timestamp:zod.z.string().optional()}).optional()})).superRefine((r,e)=>{r.id&&e.addIssue({code:zod.z.ZodIssueCode.custom,message:"Payload cannot have an id"});}),F=zod.z.object({id:zod.z.string().optional(),type:zod.z.literal("MUTATE"),resource:zod.z.string()}),S=F.extend({procedure:zod.z.string(),payload:zod.z.any().optional()}),M=F.extend({resourceId:zod.z.string(),payload:L});zod.z.union([S,M]);var Z=q.omit({type:true,resource:true}),N=S.omit({id:true,type:true,resource:true,procedure:true}),j=M.omit({id:true,type:true,resource:true});zod.z.union([j,N]);var G=r=>async e=>{var t;try{let n=typeof e.headers.getSetCookie=="function"?Object.fromEntries(e.headers):e.headers,a={headers:n,cookies:n.cookie?H.default.parse(n.cookie):{}},o=new URL(e.url),i=o.pathname.split("/"),u=o.searchParams,s=Se__default.default.parse(u.toString()),h=await((t=r.contextProvider)==null?void 0:t.call(r,{transport:"HTTP",headers:a.headers,cookies:a.cookies,query:s}))??{};if(e.method==="GET"){let m=i[i.length-1],{success:l,data:p,error:d}=Z.safeParse(s);if(!l)return Response.json({message:"Invalid query",code:"INVALID_QUERY",details:d},{status:400});let c=await r.handleRequest({req:{...a,type:"QUERY",resourceName:m,context:h,where:p.where,query:s}});return !c||!c.data?Response.json({message:"Invalid resource",code:"INVALID_RESOURCE"},{status:400}):Response.json(c.data)}if(e.method==="POST")try{let m=i[i.length-1],l=i[i.length-2],p=e.body?await e.json():{},d;if(m==="set"){let{success:b,data:g,error:w}=j.safeParse(p);if(!b)return Response.json({message:"Invalid mutation",code:"INVALID_REQUEST",details:w},{status:400});d=g;}else {let{success:b,data:g,error:w}=N.safeParse(p);if(!b)return Response.json({message:"Invalid mutation",code:"INVALID_REQUEST",details:w},{status:400});d=g;}let c=await r.handleRequest({req:{...a,type:"MUTATE",resourceName:l,input:d.payload,context:h,resourceId:d.resourceId,procedure:m!=="set"?m:void 0,query:{}}});return Response.json(c)}catch(m){return console.error("Error parsing mutation from the client:",m),Response.json({message:"Internal server error",code:"INTERNAL_SERVER_ERROR"},{status:500})}return Response.json({message:"Not found",code:"NOT_FOUND"},{status:404})}catch(n){return console.error("Unexpected error:",n),Response.json({message:"Internal server error",code:"INTERNAL_SERVER_ERROR"},{status:500})}};var X=V(P());var T=zod.z.string(),Me=zod.z.object({id:T,type:zod.z.literal("SUBSCRIBE"),resource:zod.z.string()}),ve=zod.z.object({id:T,type:zod.z.literal("SYNC"),lastSyncedAt:zod.z.string().optional(),resources:zod.z.string().array().optional(),where:zod.z.record(zod.z.any()).optional()}),B=M.extend({id:T}),Ee=S.extend({id:T}),Ie=zod.z.union([Ee,B]),Q=zod.z.union([Me,ve,Ie]),Ae=zod.z.object({id:T,type:zod.z.literal("SYNC"),resource:zod.z.string(),data:zod.z.record(L)}),Pe=zod.z.object({id:T,type:zod.z.literal("REJECT"),resource:zod.z.string(),message:zod.z.string().optional()}),Le=zod.z.object({id:T,type:zod.z.literal("REPLY"),data:zod.z.any()});zod.z.union([Ae,Pe,Le,B]);var K="0123456789ABCDEFGHJKMNPQRSTVWXYZ",v=32;var Ne=16,Y=10,W=0xffffffffffff;var R;(function(r){r.Base32IncorrectEncoding="B32_ENC_INVALID",r.DecodeTimeInvalidCharacter="DEC_TIME_CHAR",r.DecodeTimeValueMalformed="DEC_TIME_MALFORMED",r.EncodeTimeNegative="ENC_TIME_NEG",r.EncodeTimeSizeExceeded="ENC_TIME_SIZE_EXCEED",r.EncodeTimeValueMalformed="ENC_TIME_MALFORMED",r.PRNGDetectFailure="PRNG_DETECT",r.ULIDInvalid="ULID_INVALID",r.Unexpected="UNEXPECTED",r.UUIDInvalid="UUID_INVALID";})(R||(R={}));var x=class extends Error{constructor(e,t){super(`${t} (${e})`),this.name="ULIDError",this.code=e;}};function je(r){let e=Math.floor(r()*v);return e===v&&(e=v-1),K.charAt(e)}function Oe(r){var n;let e=Ce(),t=e&&(e.crypto||e.msCrypto)||(typeof I__default.default<"u"?I__default.default:null);if(typeof(t==null?void 0:t.getRandomValues)=="function")return ()=>{let a=new Uint8Array(1);return t.getRandomValues(a),a[0]/255};if(typeof(t==null?void 0:t.randomBytes)=="function")return ()=>t.randomBytes(1).readUInt8()/255;if((n=I__default.default)!=null&&n.randomBytes)return ()=>I__default.default.randomBytes(1).readUInt8()/255;throw new x(R.PRNGDetectFailure,"Failed to find a reliable PRNG")}function Ce(){return De()?self:typeof window<"u"?window:typeof global<"u"?global:typeof globalThis<"u"?globalThis:null}function ze(r,e){let t="";for(;r>0;r--)t=je(e)+t;return t}function ke(r,e=Y){if(isNaN(r))throw new x(R.EncodeTimeValueMalformed,`Time must be a number: ${r}`);if(r>W)throw new x(R.EncodeTimeSizeExceeded,`Cannot encode a time larger than ${W}: ${r}`);if(r<0)throw new x(R.EncodeTimeNegative,`Time must be positive: ${r}`);if(Number.isInteger(r)===false)throw new x(R.EncodeTimeValueMalformed,`Time must be an integer: ${r}`);let t,n="";for(let a=e;a>0;a--)t=r%v,n=K.charAt(t)+n,r=(r-t)/v;return n}function De(){return typeof WorkerGlobalScope<"u"&&self instanceof WorkerGlobalScope}function J(r,e){let t=Oe(),n=Date.now();return ke(n,Y)+ze(Ne,t)}var O=()=>J().toLowerCase();var ee=r=>{let e={},t={};return r.subscribeToMutations(n=>{let a=n;!a.resourceId||!a.payload||(console.log("Mutation propagated:",a),Object.entries(t[a.resource]??{}).forEach(([o,i])=>{var u;(u=e[o])==null||u.send(JSON.stringify({...a,id:a.id??O()}));}));}),(n,a)=>{var m;let o=l=>{n.send(JSON.stringify(l));},i=O(),u={headers:a.headers,cookies:typeof a.headers.cookie=="string"?X.default.parse(a.headers.cookie):{}},s=Se.parse(a.url.split("?")[1]),h=(m=r.contextProvider)==null?void 0:m.call(r,{transport:"WEBSOCKET",headers:u.headers,cookies:u.cookies,query:s});e[i]=n,console.log("Client connected:",i),n.on("message",async l=>{try{console.log("Message received from the client:",l);let p=Q.parse(JSON.parse(l.toString()));if(p.type==="SUBSCRIBE"){let{resource:d}=p;t[d]||(t[d]={}),t[d][i]={};}else if(p.type==="SYNC"){let{resources:d}=p,c=d??Object.keys(r.schema);console.log("Syncing resources:",c),await Promise.all(c.map(async b=>{let g=await r.handleRequest({req:{...u,type:"QUERY",resourceName:b,context:await h??{},query:s}});if(!g||!g.data)throw new Error("Invalid resource");o({id:p.id,type:"SYNC",resource:b,data:Object.fromEntries(Object.entries(g.data??{}).map(([w,ae])=>[w,ae.value]))});}));}else if(p.type==="MUTATE"){let{resource:d}=p;console.log("Received mutation from client:",p);try{let c=await r.handleRequest({req:{...u,type:"MUTATE",resourceName:d,input:p.payload,context:{messageId:p.id,...await h??{}},resourceId:p.resourceId,procedure:p.procedure,query:s}});p.procedure&&o({id:p.id,type:"REPLY",data:c});}catch(c){o({id:p.id,type:"REJECT",resource:d,message:c.message}),console.error("Error parsing mutation from the client:",c);}}}catch(p){console.error("Error handling message from the client:",p);}}),n.on("close",()=>{console.log("Connection closed",i),delete e[i];for(let l of Object.values(t))delete l[i];});}};function te(r){let e=`${r.protocol}://${r.hostname}${r.url}`,t=new Headers;return Object.entries(r.headers).forEach(([n,a])=>{a&&t.set(n,Array.isArray(a)?a.join(","):a);}),new Request(e,{method:r.method,headers:t,body:r.body&&r.method!=="GET"?JSON.stringify(r.body):void 0})}var xt=(r,e,t)=>{r.ws(`${(t==null?void 0:t.basePath)??""}/ws`,ee(e)),r.use(`${(t==null?void 0:t.basePath)??""}/`,(n,a)=>{G(e)(te(n)).then(i=>i.json().then(u=>a.status(i.status).send(u)));});};var C=class r{routes;constructor(e){this.routes=e.routes;}static create(e){return new r(e)}},Mt=r=>C.create({...r}),_e=r=>({handler:e=>({inputValidator:r??zod.z.undefined(),handler:e})}),z=class r{_resourceSchema;resourceName;middlewares;customMutations;constructor(e,t){this.resourceName=e,this.middlewares=new Set,this.customMutations=t??{};}handleFind=async({req:e,db:t})=>({data:await t.find(e.resourceName,e.where),acceptedValues:null});handleSet=async({req:e,db:t,schema:n})=>{if(!e.input)throw new Error("Payload is required");if(!e.resourceId)throw new Error("ResourceId is required");let a=await t.findById(e.resourceName,e.resourceId),[o,i]=n[this.resourceName].mergeMutation("set",e.input,a);if(!i)throw new Error("Mutation rejected");return {data:await t.upsert(e.resourceName,e.resourceId,o),acceptedValues:i}};async handleRequest(e){let t=n=>(()=>{if(n.type==="QUERY")return this.handleFind({req:n,db:e.db,schema:e.schema});if(n.type==="MUTATE")if(n.procedure){if(this.customMutations[n.procedure]){let a=this.customMutations[n.procedure].inputValidator.parse(n.input);return n.input=a,this.customMutations[n.procedure].handler({req:n,db:e.db,schema:e.schema})}}else return this.handleSet({req:n,db:e.db,schema:e.schema});throw new Error("Invalid request")})();return await Array.from(this.middlewares.values()).reduceRight((n,a)=>o=>a({req:o,next:n}),async n=>t(n))(e.req)}use(...e){for(let t of e)this.middlewares.add(t);return this}withMutations(e){return new r(this.resourceName,e({mutation:_e}))}},k=class r{middlewares;constructor(e=[]){this.middlewares=e;}createBasicRoute(e){return new z(e.name).use(...this.middlewares)}use(...e){return new r([...this.middlewares,...e])}static create(){return new r}},vt=k.create;var A=class{},re=class extends A{storage={};async updateSchema(e){this.storage=Object.entries(e).reduce((t,[n,a])=>(t[a.name]={},t),{});}async findById(e,t){var n;return (n=this.storage[e])==null?void 0:n[t]}async find(e,t){return this.storage[e]??{}}async upsert(e,t,n){return this.storage[e]??={},this.storage[e][t]=n,n}},ne=class extends A{db;schema;constructor(e){super(),this.db=new kysely.Kysely({dialect:new kysely.PostgresDialect({pool:e})});}async updateSchema(e){this.schema=e;let t=await this.db.introspection.getTables();for(let[n,a]of Object.entries(e)){let o=t.find(s=>s.name===n);o||await this.db.schema.createTable(n).ifNotExists().execute();let i=`${n}_meta`,u=t.find(s=>s.name===i);u||await this.db.schema.createTable(i).ifNotExists().execute();for(let[s,h]of Object.entries(a.fields)){let m=o==null?void 0:o.columns.find(d=>d.name===s),l=h.getStorageFieldType();m?m.dataType!==l.type&&console.error("Column type mismatch:",s,"expected to have type:",l.type,"but has type:",m.dataType):(await this.db.schema.alterTable(n).addColumn(s,l.type,d=>{let c=d;return l.unique&&(c=c.unique()),l.nullable||(c=c.notNull()),l.references&&(c=c.references(l.references)),l.primary&&(c=c.primaryKey()),l.default!==void 0&&(c=c.defaultTo(l.default)),c}).execute().catch(d=>{throw console.error("Error adding column",s,d),d}),l.index&&await this.db.schema.createIndex(`${n}_${s}_index`).on(n).column(s).execute().catch(d=>{})),(u==null?void 0:u.columns.find(d=>d.name===s))||await this.db.schema.alterTable(i).addColumn(s,"varchar",d=>{let c=d;return l.primary&&(c=c.primaryKey().references(`${n}.${s}`)),c}).execute();}}}async findById(e,t){let n=await this.db.selectFrom(e).where("id","=",t).selectAll(e).executeTakeFirst(),a=await this.db.selectFrom(`${e}_meta`).where("id","=",t).selectAll(`${e}_meta`).executeTakeFirst();if(!(!n||!a))return this.convertToMaterializedLiveType(n,a)}async find(e,t){let a=await this.applyWhere(e,this.db.selectFrom(e).selectAll(e),t).execute(),o=Object.fromEntries(a.map(s=>{let{id:h,...m}=s;return [h,m]}));if(Object.keys(o).length===0)return {};let i=Object.fromEntries((await this.db.selectFrom(`${e}_meta`).selectAll().where("id","in",Object.keys(o)).execute()).map(s=>{let{id:h,...m}=s;return [h,m]}));return Object.entries(o).reduce((s,[h,m])=>(i[h]&&(s[h]=this.convertToMaterializedLiveType(m,i[h])),s),{})}async upsert(e,t,n){return await this.db.transaction().execute(async a=>{let o=!!await a.selectFrom(e).select("id").where("id","=",t).executeTakeFirst(),i={},u={};for(let[s,h]of Object.entries(n.value))i[s]=h.value,u[s]=h._meta.timestamp;o?await Promise.all([a.updateTable(e).set(i).where("id","=",t).execute(),a.updateTable(`${e}_meta`).set(u).where("id","=",t).execute()]):await Promise.all([a.insertInto(e).values({...i,id:t}).execute(),a.insertInto(`${e}_meta`).values({...u,id:t}).execute()]);}),n}convertToMaterializedLiveType(e,t){return {value:Object.fromEntries(Object.entries(e).flatMap(([n,a])=>t?[[n,{value:a,_meta:{timestamp:t==null?void 0:t[n]}}]]:[]))}}applyWhere(e,t,n){if(!n)return t;if(!this.schema)throw new Error("Schema not initialized");let a=this.schema[e];if(!a)throw new Error("Resource not found");for(let[o,i]of Object.entries(n))if(a.fields[o])t=t.where(`${e}.${o}`,"=",i);else if(a.relations[o]){let u=a.relations[o],s=u.entity.name,h=u.type==="one"?"id":u.foreignColumn,m=u.type==="one"?u.relationalColumn:"id";t=t.leftJoin(s,`${s}.${h}`,`${e}.${m}`),t=this.applyWhere(s,t,i);}return t}};var D=class r{router;storage;schema;middlewares=new Set;contextProvider;mutationSubscriptions=new Set;constructor(e){var t;this.router=e.router,this.storage=e.storage,this.schema=e.schema,(t=e.middlewares)==null||t.forEach(n=>this.middlewares.add(n)),this.storage.updateSchema(this.schema),this.contextProvider=e.contextProvider;}static create(e){return new r(e)}subscribeToMutations(e){return this.mutationSubscriptions.add(e),()=>{this.mutationSubscriptions.delete(e);}}async handleRequest(e){if(!this.router.routes[e.req.resourceName])throw new Error("Invalid resource");let t=await Array.from(this.middlewares.values()).reduceRight((n,a)=>o=>a({req:o,next:n}),async n=>this.router.routes[e.req.resourceName].handleRequest({req:n,db:this.storage,schema:this.schema}))(e.req);return t&&e.req.type==="MUTATE"&&t.acceptedValues&&Object.keys(t.acceptedValues).length>0&&this.mutationSubscriptions.forEach(n=>{n({id:e.req.context.messageId,type:"MUTATE",resource:e.req.resourceName,payload:t.acceptedValues??{},resourceId:e.req.resourceId});}),t}use(e){return this.middlewares.add(e),this}context(e){return this.contextProvider=e,this}},Nt=D.create;
2
- exports.InMemoryStorage=re;exports.Route=z;exports.RouteFactory=k;exports.Router=C;exports.SQLStorage=ne;exports.Server=D;exports.Storage=A;exports.expressAdapter=xt;exports.routeFactory=vt;exports.router=Mt;exports.server=Nt;
1
+ 'use strict';var Ce=require('qs'),zod=require('zod'),j=require('crypto'),kysely=require('kysely'),postgres=require('kysely/helpers/postgres');function _interopDefault(e){return e&&e.__esModule?e:{default:e}}var Ce__default=/*#__PURE__*/_interopDefault(Ce);var j__default=/*#__PURE__*/_interopDefault(j);var pe=Object.create;var W=Object.defineProperty;var Te=Object.getOwnPropertyDescriptor;var fe=Object.getOwnPropertyNames;var he=Object.getPrototypeOf,ge=Object.prototype.hasOwnProperty;var Re=(n,e)=>()=>(e||n((e={exports:{}}).exports,e),e.exports);var ve=(n,e,t,r)=>{if(e&&typeof e=="object"||typeof e=="function")for(let a of fe(e))!ge.call(n,a)&&a!==t&&W(n,a,{get:()=>e[a],enumerable:!(r=Te(e,a))||r.enumerable});return n};var Q=(n,e,t)=>(t=n!=null?pe(he(n)):{},ve(W(t,"default",{value:n,enumerable:true}),n));var _=Re(E=>{Object.defineProperty(E,"__esModule",{value:true});E.parse=Le;E.serialize=Ae;var be=/^[\u0021-\u003A\u003C\u003E-\u007E]+$/,xe=/^[\u0021-\u003A\u003C-\u007E]*$/,we=/^([.]?[a-z0-9]([a-z0-9-]{0,61}[a-z0-9])?)([.][a-z0-9]([a-z0-9-]{0,61}[a-z0-9])?)*$/i,Me=/^[\u0020-\u003A\u003D-\u007E]*$/,Se=Object.prototype.toString,Ie=(()=>{let n=function(){};return n.prototype=Object.create(null),n})();function Le(n,e){let t=new Ie,r=n.length;if(r<2)return t;let a=(e==null?void 0:e.decode)||Oe,i=0;do{let o=n.indexOf("=",i);if(o===-1)break;let c=n.indexOf(";",i),s=c===-1?r:c;if(o>s){i=n.lastIndexOf(";",o-1)+1;continue}let p=B(n,i,o),m=Y(n,o,p),u=n.slice(p,m);if(t[u]===void 0){let y=B(n,o+1,s),l=Y(n,s,y),d=a(n.slice(y,l));t[u]=d;}i=s+1;}while(i<r);return t}function B(n,e,t){do{let r=n.charCodeAt(e);if(r!==32&&r!==9)return e}while(++e<t);return t}function Y(n,e,t){for(;e>t;){let r=n.charCodeAt(--e);if(r!==32&&r!==9)return e+1}return t}function Ae(n,e,t){let r=(t==null?void 0:t.encode)||encodeURIComponent;if(!be.test(n))throw new TypeError(`argument name is invalid: ${n}`);let a=r(e);if(!xe.test(a))throw new TypeError(`argument val is invalid: ${e}`);let i=n+"="+a;if(!t)return i;if(t.maxAge!==void 0){if(!Number.isInteger(t.maxAge))throw new TypeError(`option maxAge is invalid: ${t.maxAge}`);i+="; Max-Age="+t.maxAge;}if(t.domain){if(!we.test(t.domain))throw new TypeError(`option domain is invalid: ${t.domain}`);i+="; Domain="+t.domain;}if(t.path){if(!Me.test(t.path))throw new TypeError(`option path is invalid: ${t.path}`);i+="; Path="+t.path;}if(t.expires){if(!Ee(t.expires)||!Number.isFinite(t.expires.valueOf()))throw new TypeError(`option expires is invalid: ${t.expires}`);i+="; Expires="+t.expires.toUTCString();}if(t.httpOnly&&(i+="; HttpOnly"),t.secure&&(i+="; Secure"),t.partitioned&&(i+="; Partitioned"),t.priority)switch(typeof t.priority=="string"?t.priority.toLowerCase():void 0){case "low":i+="; Priority=Low";break;case "medium":i+="; Priority=Medium";break;case "high":i+="; Priority=High";break;default:throw new TypeError(`option priority is invalid: ${t.priority}`)}if(t.sameSite)switch(typeof t.sameSite=="string"?t.sameSite.toLowerCase():t.sameSite){case true:case "strict":i+="; SameSite=Strict";break;case "lax":i+="; SameSite=Lax";break;case "none":i+="; SameSite=None";break;default:throw new TypeError(`option sameSite is invalid: ${t.sameSite}`)}return i}function Oe(n){if(n.indexOf("%")===-1)return n;try{return decodeURIComponent(n)}catch{return n}}function Ee(n){return Se.call(n)==="[object Date]"}});var te=Q(_());var J=zod.z.object({type:zod.z.literal("QUERY"),resource:zod.z.string(),where:zod.z.record(zod.z.any()).optional(),include:zod.z.record(zod.z.any()).optional()}),P=zod.z.record(zod.z.object({value:zod.z.string().or(zod.z.number()).or(zod.z.boolean()).or(zod.z.date()).nullable(),_meta:zod.z.object({timestamp:zod.z.string().optional().nullable()}).optional()})).superRefine((n,e)=>{n.id&&e.addIssue({code:zod.z.ZodIssueCode.custom,message:"Payload cannot have an id"});}),X=zod.z.object({id:zod.z.string().optional(),type:zod.z.literal("MUTATE"),resource:zod.z.string()}),I=X.extend({procedure:zod.z.string(),payload:zod.z.any().optional()}),L=X.extend({resourceId:zod.z.string(),payload:P});zod.z.union([I,L]);var ee=J.omit({type:true,resource:true}),V=I.omit({id:true,type:true,resource:true,procedure:true}),N=L.omit({id:true,type:true,resource:true});zod.z.union([N,V]);var ne=n=>async e=>{var t;try{let r=typeof e.headers.getSetCookie=="function"?Object.fromEntries(e.headers):e.headers,a={headers:r,cookies:r.cookie?te.default.parse(r.cookie):{}},i=new URL(e.url),o=i.pathname.split("/"),c=i.searchParams,s=Ce__default.default.parse(c.toString()),p=await((t=n.contextProvider)==null?void 0:t.call(n,{transport:"HTTP",headers:a.headers,cookies:a.cookies,query:s}))??{};if(e.method==="GET"){let m=o[o.length-1],{success:u,data:y,error:l}=ee.safeParse(s);if(!u)return Response.json({message:"Invalid query",code:"INVALID_QUERY",details:l},{status:400});let d=await n.handleRequest({req:{...a,type:"QUERY",resourceName:m,context:p,where:y.where,include:y.include,query:s}});return !d||!d.data?Response.json({message:"Invalid resource",code:"INVALID_RESOURCE"},{status:400}):Response.json(d.data)}if(e.method==="POST")try{let m=o[o.length-1],u=o[o.length-2],y=e.body?await e.json():{},l;if(m==="set"){let{success:w,data:h,error:S}=N.safeParse(y);if(!w)return Response.json({message:"Invalid mutation",code:"INVALID_REQUEST",details:S},{status:400});l=h;}else {let{success:w,data:h,error:S}=V.safeParse(y);if(!w)return Response.json({message:"Invalid mutation",code:"INVALID_REQUEST",details:S},{status:400});l=h;}let d=await n.handleRequest({req:{...a,type:"MUTATE",resourceName:u,input:l.payload,context:p,resourceId:l.resourceId,procedure:m!=="set"?m:void 0,query:{}}});return Response.json(d)}catch(m){return console.error("Error parsing mutation from the client:",m),Response.json({message:"Internal server error",code:"INTERNAL_SERVER_ERROR"},{status:500})}return Response.json({message:"Not found",code:"NOT_FOUND"},{status:404})}catch(r){return console.error("Unexpected error:",r),Response.json({message:"Internal server error",code:"INTERNAL_SERVER_ERROR"},{status:500})}};var ue=Q(_());var g=zod.z.string(),_e=zod.z.object({id:g,type:zod.z.literal("SUBSCRIBE"),resource:zod.z.string()}),Pe=zod.z.object({id:g,type:zod.z.literal("SYNC"),lastSyncedAt:zod.z.string().optional(),resources:zod.z.string().array().optional(),where:zod.z.record(zod.z.any()).optional()}),re=L.extend({id:g}),Ve=I.extend({id:g}),Ne=zod.z.union([Ve,re]),ae=zod.z.union([_e,Pe,Ne]),Fe=zod.z.object({id:g,type:zod.z.literal("SYNC"),resource:zod.z.string(),data:zod.z.record(P)}),De=zod.z.object({id:g,type:zod.z.literal("REJECT"),resource:zod.z.string(),message:zod.z.string().optional()}),$e=zod.z.object({id:g,type:zod.z.literal("REPLY"),data:zod.z.any()});zod.z.union([Fe,De,$e,re]);var oe="0123456789ABCDEFGHJKMNPQRSTVWXYZ",A=32;var qe=16,se=10,ie=0xffffffffffff;var R;(function(n){n.Base32IncorrectEncoding="B32_ENC_INVALID",n.DecodeTimeInvalidCharacter="DEC_TIME_CHAR",n.DecodeTimeValueMalformed="DEC_TIME_MALFORMED",n.EncodeTimeNegative="ENC_TIME_NEG",n.EncodeTimeSizeExceeded="ENC_TIME_SIZE_EXCEED",n.EncodeTimeValueMalformed="ENC_TIME_MALFORMED",n.PRNGDetectFailure="PRNG_DETECT",n.ULIDInvalid="ULID_INVALID",n.Unexpected="UNEXPECTED",n.UUIDInvalid="UUID_INVALID";})(R||(R={}));var v=class extends Error{constructor(e,t){super(`${t} (${e})`),this.name="ULIDError",this.code=e;}};function Ue(n){let e=Math.floor(n()*A);return e===A&&(e=A-1),oe.charAt(e)}function Ke(n){var r;let e=ke(),t=e&&(e.crypto||e.msCrypto)||(typeof j__default.default<"u"?j__default.default:null);if(typeof(t==null?void 0:t.getRandomValues)=="function")return ()=>{let a=new Uint8Array(1);return t.getRandomValues(a),a[0]/255};if(typeof(t==null?void 0:t.randomBytes)=="function")return ()=>t.randomBytes(1).readUInt8()/255;if((r=j__default.default)!=null&&r.randomBytes)return ()=>j__default.default.randomBytes(1).readUInt8()/255;throw new v(R.PRNGDetectFailure,"Failed to find a reliable PRNG")}function ke(){return He()?self:typeof window<"u"?window:typeof global<"u"?global:typeof globalThis<"u"?globalThis:null}function ze(n,e){let t="";for(;n>0;n--)t=Ue(e)+t;return t}function Ze(n,e=se){if(isNaN(n))throw new v(R.EncodeTimeValueMalformed,`Time must be a number: ${n}`);if(n>ie)throw new v(R.EncodeTimeSizeExceeded,`Cannot encode a time larger than ${ie}: ${n}`);if(n<0)throw new v(R.EncodeTimeNegative,`Time must be positive: ${n}`);if(Number.isInteger(n)===false)throw new v(R.EncodeTimeValueMalformed,`Time must be an integer: ${n}`);let t,r="";for(let a=e;a>0;a--)t=n%A,r=oe.charAt(t)+r,n=(n-t)/A;return r}function He(){return typeof WorkerGlobalScope<"u"&&self instanceof WorkerGlobalScope}function ce(n,e){let t=Ke(),r=Date.now();return Ze(r,se)+ze(qe,t)}var F=()=>ce().toLowerCase();var le=n=>{let e={},t={};return n.subscribeToMutations(r=>{let a=r;!a.resourceId||!a.payload||(console.log("Mutation propagated:",a),Object.entries(t[a.resource]??{}).forEach(([i,o])=>{var c;(c=e[i])==null||c.send(JSON.stringify({...a,id:a.id??F()}));}));}),(r,a)=>{var m;let i=u=>{r.send(JSON.stringify(u));},o=F(),c={headers:a.headers,cookies:typeof a.headers.cookie=="string"?ue.default.parse(a.headers.cookie):{}},s=Ce.parse(a.url.split("?")[1]),p=(m=n.contextProvider)==null?void 0:m.call(n,{transport:"WEBSOCKET",headers:c.headers,cookies:c.cookies,query:s});e[o]=r,console.log("Client connected:",o),r.on("message",async u=>{try{console.log("Message received from the client:",u);let y=ae.parse(JSON.parse(u.toString()));if(y.type==="SUBSCRIBE"){let{resource:l}=y;t[l]||(t[l]={}),t[l][o]={};}else if(y.type==="SYNC"){let{resources:l}=y,d=l??Object.keys(n.schema);console.log("Syncing resources:",d),await Promise.all(d.map(async w=>{let h=await n.handleRequest({req:{...c,type:"QUERY",resourceName:w,context:await p??{},query:s}});if(!h||!h.data)throw new Error("Invalid resource");i({id:y.id,type:"SYNC",resource:w,data:Object.fromEntries(Object.entries(h.data??{}).map(([S,me])=>[S,me.value]))});}));}else if(y.type==="MUTATE"){let{resource:l}=y;console.log("Received mutation from client:",y);try{let d=await n.handleRequest({req:{...c,type:"MUTATE",resourceName:l,input:y.payload,context:{messageId:y.id,...await p??{}},resourceId:y.resourceId,procedure:y.procedure,query:s}});y.procedure&&i({id:y.id,type:"REPLY",data:d});}catch(d){i({id:y.id,type:"REJECT",resource:l,message:d.message}),console.error("Error parsing mutation from the client:",d);}}}catch(y){console.error("Error handling message from the client:",y);}}),r.on("close",()=>{console.log("Connection closed",o),delete e[o];for(let u of Object.values(t))delete u[o];});}};function de(n){let e=`${n.protocol}://${n.hostname}${n.url}`,t=new Headers;return Object.entries(n.headers).forEach(([r,a])=>{a&&t.set(r,Array.isArray(a)?a.join(","):a);}),new Request(e,{method:n.method,headers:t,body:n.body&&n.method!=="GET"?JSON.stringify(n.body):void 0})}var Et=(n,e,t)=>{n.ws(`${(t==null?void 0:t.basePath)??""}/ws`,le(e)),n.use(`${(t==null?void 0:t.basePath)??""}/`,(r,a)=>{ne(e)(de(r)).then(o=>o.json().then(c=>a.status(o.status).send(c)));});};var D=class n{routes;constructor(e){this.routes=e.routes;}static create(e){return new n(e)}},Pt=n=>D.create({...n}),Qe=n=>({handler:e=>({inputValidator:n??zod.z.undefined(),handler:e})}),$=class n{_resourceSchema;resourceName;middlewares;customMutations;constructor(e,t){this.resourceName=e,this.middlewares=new Set,this.customMutations=t??{};}handleFind=async({req:e,db:t})=>({data:await t.rawFind(e.resourceName,e.where,e.include),acceptedValues:null});handleSet=async({req:e,db:t,schema:r})=>{if(!e.input)throw new Error("Payload is required");if(!e.resourceId)throw new Error("ResourceId is required");let a=await t.rawFindById(e.resourceName,e.resourceId),[i,o]=r[this.resourceName].mergeMutation("set",e.input,a);if(!o)throw new Error("Mutation rejected");return {data:await t.rawUpsert(e.resourceName,e.resourceId,i),acceptedValues:o}};async handleRequest(e){let t=r=>(()=>{if(r.type==="QUERY")return this.handleFind({req:r,db:e.db,schema:e.schema});if(r.type==="MUTATE")if(r.procedure){if(this.customMutations[r.procedure]){let a=this.customMutations[r.procedure].inputValidator.parse(r.input);return r.input=a,this.customMutations[r.procedure].handler({req:r,db:e.db,schema:e.schema})}}else return this.handleSet({req:r,db:e.db,schema:e.schema});throw new Error("Invalid request")})();return await Array.from(this.middlewares.values()).reduceRight((r,a)=>i=>a({req:i,next:r}),async r=>t(r))(e.req)}use(...e){for(let t of e)this.middlewares.add(t);return this}withMutations(e){return new n(this.resourceName,e({mutation:Qe}))}},q=class n{middlewares;constructor(e=[]){this.middlewares=e;}createBasicRoute(e){return new $(e.name).use(...this.middlewares)}use(...e){return new n([...this.middlewares,...e])}static create(){return new n}},Vt=q.create;var x=n=>{if(n)return Array.isArray(n.value)?n.value.map(e=>x(e)):typeof n.value!="object"||n.value===null||n.value instanceof Date?n.value:Object.fromEntries(Object.entries(n.value).map(([e,t])=>[e,x(t)]))};var H=class{async insert(e,t){let r=new Date().toISOString();return x(await this.rawUpsert(e.name,t.id,{value:Object.fromEntries(Object.entries(t).map(([a,i])=>[a,{value:i,_meta:{timestamp:r}}]))}))}async update(e,t,r){let a=new Date().toISOString(),{id:i,...o}=r;return x(await this.rawUpsert(e.name,t,{value:Object.fromEntries(Object.entries(o).map(([c,s])=>[c,{value:s,_meta:{timestamp:a}}]))}))}},ye=class extends H{db;schema;constructor(e){super(),this.db=new kysely.Kysely({dialect:new kysely.PostgresDialect({pool:e})});}async updateSchema(e){this.schema=e;let t=await this.db.introspection.getTables();for(let[r,a]of Object.entries(e)){let i=t.find(s=>s.name===r);i||await this.db.schema.createTable(r).ifNotExists().execute();let o=`${r}_meta`,c=t.find(s=>s.name===o);c||await this.db.schema.createTable(o).ifNotExists().execute();for(let[s,p]of Object.entries(a.fields)){let m=i==null?void 0:i.columns.find(l=>l.name===s),u=p.getStorageFieldType();m?m.dataType!==u.type&&console.error("Column type mismatch:",s,"expected to have type:",u.type,"but has type:",m.dataType):(await this.db.schema.alterTable(r).addColumn(s,u.type,l=>{let d=l;return u.unique&&(d=d.unique()),u.nullable||(d=d.notNull()),u.references&&(d=d.references(u.references)),u.primary&&(d=d.primaryKey()),u.default!==void 0&&(d=d.defaultTo(u.default)),d}).execute().catch(l=>{throw console.error("Error adding column",s,l),l}),u.index&&await this.db.schema.createIndex(`${r}_${s}_index`).on(r).column(s).execute().catch(l=>{})),(c==null?void 0:c.columns.find(l=>l.name===s))||await this.db.schema.alterTable(o).addColumn(s,"varchar",l=>{let d=l;return u.primary&&(d=d.primaryKey().references(`${r}.${s}`)),d}).execute();}}}async rawFindById(e,t,r){let a=await this.db.selectFrom(e).where("id","=",t).selectAll(e).select(o=>postgres.jsonObjectFrom(o.selectFrom(`${e}_meta`).selectAll(`${e}_meta`).whereRef(`${e}_meta.id`,"=",`${e}.id`)).as("_meta"));a=this.applyInclude(e,a,r);let i=await a.executeTakeFirst();if(i)return this.convertToMaterializedLiveType(i)}async findOne(e,t,r){let a=await this.rawFindById(e.name,t,r==null?void 0:r.include);if(a)return x(a)}async rawFind(e,t,r){let a=this.db.selectFrom(e).selectAll(e).select(s=>postgres.jsonObjectFrom(s.selectFrom(`${e}_meta`).selectAll(`${e}_meta`).whereRef(`${e}_meta.id`,"=",`${e}.id`)).as("_meta"));a=this.applyWhere(e,a,t),a=this.applyInclude(e,a,r);let i=await a.execute(),o=Object.fromEntries(i.map(s=>{let{id:p,...m}=s;return [p,m]}));return Object.keys(o).length===0?{}:Object.entries(o).reduce((s,[p,m])=>(s[p]=this.convertToMaterializedLiveType(m),s),{})}async find(e,t){let r=await this.rawFind(e.name,t==null?void 0:t.where,t==null?void 0:t.include);return Object.fromEntries(Object.entries(r).map(([a,i])=>[a,x(i)]))}async rawUpsert(e,t,r){return await this.db.transaction().execute(async a=>{var s;let i=!!await a.selectFrom(e).select("id").where("id","=",t).executeTakeFirst(),o={},c={};for(let[p,m]of Object.entries(r.value)){let u=(s=m._meta)==null?void 0:s.timestamp;u&&(o[p]=m.value,c[p]=u);}i?await Promise.all([a.updateTable(e).set(o).where("id","=",t).execute(),a.updateTable(`${e}_meta`).set(c).where("id","=",t).execute()]):await Promise.all([a.insertInto(e).values({...o,id:t}).execute(),a.insertInto(`${e}_meta`).values({...c,id:t}).execute()]);}),r}convertToMaterializedLiveType(e){if(!e._meta)throw new Error("Missing _meta");return {value:Object.entries(e).reduce((t,[r,a])=>{var i,o,c;return r==="_meta"||(r==="id"?t[r]={value:a}:Array.isArray(a)?t[r]={value:a.map(s=>this.convertToMaterializedLiveType(s)),_meta:{timestamp:(i=e==null?void 0:e._meta)==null?void 0:i[r]}}:typeof a=="object"&&a!==null&&!(a instanceof Date)?t[r]={...this.convertToMaterializedLiveType(a),_meta:{timestamp:(o=e==null?void 0:e._meta)==null?void 0:o[r]}}:t[r]={value:a,_meta:{timestamp:(c=e==null?void 0:e._meta)==null?void 0:c[r]}}),t},{})}}applyWhere(e,t,r){if(!r)return t;if(!this.schema)throw new Error("Schema not initialized");let a=this.schema[e];if(!a)throw new Error("Resource not found");for(let[i,o]of Object.entries(r))if(a.fields[i])t=t.where(`${e}.${i}`,"=",o);else if(a.relations[i]){let c=a.relations[i],s=c.entity.name,p=c.type==="one"?"id":c.foreignColumn,m=c.type==="one"?c.relationalColumn:"id";t=t.leftJoin(s,`${s}.${p}`,`${e}.${m}`),t=this.applyWhere(s,t,o);}return t}applyInclude(e,t,r){if(!r)return t;if(!this.schema)throw new Error("Schema not initialized");let a=this.schema[e];if(!a)throw new Error(`Resource not found: ${e}`);for(let[i,o]of Object.entries(r)){if(!a.relations[i])throw new Error(`Relation ${i} not found in resource ${e}`);let c=a.relations[i],s=c.entity.name,p=c.type==="one"?"id":c.foreignColumn,m=c.type==="one"?c.relationalColumn:"id",u=c.type==="one"?postgres.jsonObjectFrom:postgres.jsonArrayFrom;t=t.select(y=>u(y.selectFrom(s).selectAll(s).whereRef(`${s}.${p}`,"=",`${e}.${m}`).select(l=>postgres.jsonObjectFrom(l.selectFrom(`${s}_meta`).selectAll(`${s}_meta`).whereRef(`${s}_meta.id`,"=",`${s}.id`)).as("_meta"))).as(i));}return t}};var G=class n{router;storage;schema;middlewares=new Set;contextProvider;mutationSubscriptions=new Set;constructor(e){var t;this.router=e.router,this.storage=e.storage,this.schema=e.schema,(t=e.middlewares)==null||t.forEach(r=>this.middlewares.add(r)),this.storage.updateSchema(this.schema),this.contextProvider=e.contextProvider;}static create(e){return new n(e)}subscribeToMutations(e){return this.mutationSubscriptions.add(e),()=>{this.mutationSubscriptions.delete(e);}}async handleRequest(e){if(!this.router.routes[e.req.resourceName])throw new Error("Invalid resource");let t=await Array.from(this.middlewares.values()).reduceRight((r,a)=>i=>a({req:i,next:r}),async r=>this.router.routes[e.req.resourceName].handleRequest({req:r,db:this.storage,schema:this.schema}))(e.req);return t&&e.req.type==="MUTATE"&&t.acceptedValues&&Object.keys(t.acceptedValues).length>0&&this.mutationSubscriptions.forEach(r=>{r({id:e.req.context.messageId,type:"MUTATE",resource:e.req.resourceName,payload:t.acceptedValues??{},resourceId:e.req.resourceId});}),t}use(e){return this.middlewares.add(e),this}context(e){return this.contextProvider=e,this}},xn=G.create;
2
+ exports.Route=$;exports.RouteFactory=q;exports.Router=D;exports.SQLStorage=ye;exports.Server=G;exports.Storage=H;exports.expressAdapter=Et;exports.routeFactory=Vt;exports.router=Pt;exports.server=xn;
package/dist/server.d.cts CHANGED
@@ -1,5 +1,5 @@
1
1
  import { z, ZodTypeAny } from 'zod';
2
- import { LiveObjectAny, Schema, MaterializedLiveType, WhereClause } from './index.cjs';
2
+ import { LiveObjectAny, Schema, MaterializedLiveType, IncludeClause, InferLiveObject, WhereClause, LiveObjectMutationInput } from './index.cjs';
3
3
  import { PostgresPool } from 'kysely';
4
4
  import { Application } from 'express-ws';
5
5
 
@@ -29,33 +29,33 @@ declare const mutationSchema: z.ZodUnion<[z.ZodObject<z.objectUtil.extendShape<{
29
29
  }, {
30
30
  resourceId: z.ZodString;
31
31
  payload: z.ZodEffects<z.ZodRecord<z.ZodString, z.ZodObject<{
32
- value: z.ZodUnion<[z.ZodUnion<[z.ZodUnion<[z.ZodString, z.ZodNumber]>, z.ZodBoolean]>, z.ZodDate]>;
32
+ value: z.ZodNullable<z.ZodUnion<[z.ZodUnion<[z.ZodUnion<[z.ZodString, z.ZodNumber]>, z.ZodBoolean]>, z.ZodDate]>>;
33
33
  _meta: z.ZodOptional<z.ZodObject<{
34
- timestamp: z.ZodOptional<z.ZodString>;
34
+ timestamp: z.ZodNullable<z.ZodOptional<z.ZodString>>;
35
35
  }, "strip", z.ZodTypeAny, {
36
- timestamp?: string | undefined;
36
+ timestamp?: string | null | undefined;
37
37
  }, {
38
- timestamp?: string | undefined;
38
+ timestamp?: string | null | undefined;
39
39
  }>>;
40
40
  }, "strip", z.ZodTypeAny, {
41
- value: string | number | boolean | Date;
41
+ value: string | number | boolean | Date | null;
42
42
  _meta?: {
43
- timestamp?: string | undefined;
43
+ timestamp?: string | null | undefined;
44
44
  } | undefined;
45
45
  }, {
46
- value: string | number | boolean | Date;
46
+ value: string | number | boolean | Date | null;
47
47
  _meta?: {
48
- timestamp?: string | undefined;
48
+ timestamp?: string | null | undefined;
49
49
  } | undefined;
50
50
  }>>, Record<string, {
51
- value: string | number | boolean | Date;
51
+ value: string | number | boolean | Date | null;
52
52
  _meta?: {
53
- timestamp?: string | undefined;
53
+ timestamp?: string | null | undefined;
54
54
  } | undefined;
55
55
  }>, Record<string, {
56
- value: string | number | boolean | Date;
56
+ value: string | number | boolean | Date | null;
57
57
  _meta?: {
58
- timestamp?: string | undefined;
58
+ timestamp?: string | null | undefined;
59
59
  } | undefined;
60
60
  }>>;
61
61
  }>, "strip", z.ZodTypeAny, {
@@ -63,9 +63,9 @@ declare const mutationSchema: z.ZodUnion<[z.ZodObject<z.objectUtil.extendShape<{
63
63
  resourceId: string;
64
64
  resource: string;
65
65
  payload: Record<string, {
66
- value: string | number | boolean | Date;
66
+ value: string | number | boolean | Date | null;
67
67
  _meta?: {
68
- timestamp?: string | undefined;
68
+ timestamp?: string | null | undefined;
69
69
  } | undefined;
70
70
  }>;
71
71
  id?: string | undefined;
@@ -74,9 +74,9 @@ declare const mutationSchema: z.ZodUnion<[z.ZodObject<z.objectUtil.extendShape<{
74
74
  resourceId: string;
75
75
  resource: string;
76
76
  payload: Record<string, {
77
- value: string | number | boolean | Date;
77
+ value: string | number | boolean | Date | null;
78
78
  _meta?: {
79
- timestamp?: string | undefined;
79
+ timestamp?: string | null | undefined;
80
80
  } | undefined;
81
81
  }>;
82
82
  id?: string | undefined;
@@ -144,29 +144,35 @@ declare class RouteFactory {
144
144
  declare const routeFactory: typeof RouteFactory.create;
145
145
  type AnyRoute = Route<LiveObjectAny, Middleware<any>, Record<string, any>>;
146
146
 
147
+ type Simplify<T> = T extends Record<string, any> ? {
148
+ [K in keyof T]: Simplify<T[K]>;
149
+ } : T;
150
+
147
151
  declare abstract class Storage {
148
- abstract updateSchema(opts: Schema<any>): Promise<void>;
149
- abstract findById<T extends LiveObjectAny>(resourceName: string, id: string): Promise<MaterializedLiveType<T> | undefined>;
150
- abstract find<T extends LiveObjectAny>(resourceName: string, where?: Record<string, any>): Promise<Record<string, MaterializedLiveType<T>>>;
151
- abstract upsert<T extends LiveObjectAny>(resourceName: string, resourceId: string, value: MaterializedLiveType<T>): Promise<MaterializedLiveType<T>>;
152
- }
153
- declare class InMemoryStorage extends Storage {
154
- private storage;
155
- updateSchema(opts: Schema<any>): Promise<void>;
156
- findById<T extends LiveObjectAny>(resourceName: string, id: string): Promise<MaterializedLiveType<T> | undefined>;
157
- find<T extends LiveObjectAny>(resourceName: string, where?: Record<string, any>): Promise<Record<string, MaterializedLiveType<T>>>;
158
- upsert<T extends LiveObjectAny>(resourceName: string, resourceId: string, value: MaterializedLiveType<T>): Promise<MaterializedLiveType<T>>;
152
+ abstract findOne<T extends LiveObjectAny>(resource: T, id: string, options?: {
153
+ include?: IncludeClause<T>;
154
+ }): Promise<InferLiveObject<T> | undefined>;
155
+ abstract find<T extends LiveObjectAny>(resource: T, options?: {
156
+ where?: WhereClause<T>;
157
+ include?: IncludeClause<T>;
158
+ }): Promise<Record<string, InferLiveObject<T>>>;
159
+ insert<T extends LiveObjectAny>(resource: T, value: Simplify<LiveObjectMutationInput<T>>): Promise<InferLiveObject<T>>;
160
+ update<T extends LiveObjectAny>(resource: T, resourceId: string, value: LiveObjectMutationInput<T>): Promise<InferLiveObject<T>>;
159
161
  }
160
162
  declare class SQLStorage extends Storage {
161
163
  private db;
162
164
  private schema?;
163
165
  constructor(pool: PostgresPool);
164
- updateSchema(opts: Schema<any>): Promise<void>;
165
- findById<T extends LiveObjectAny>(resourceName: string, id: string): Promise<MaterializedLiveType<T> | undefined>;
166
- find<T extends LiveObjectAny>(resourceName: string, where?: WhereClause<T>): Promise<Record<string, MaterializedLiveType<T>>>;
167
- upsert<T extends LiveObjectAny>(resourceName: string, resourceId: string, value: MaterializedLiveType<T>): Promise<MaterializedLiveType<T>>;
166
+ findOne<T extends LiveObjectAny>(resource: T, id: string, options?: {
167
+ include?: IncludeClause<T>;
168
+ }): Promise<InferLiveObject<T> | undefined>;
169
+ find<T extends LiveObjectAny>(resource: T, options?: {
170
+ where?: WhereClause<T>;
171
+ include?: IncludeClause<T>;
172
+ }): Promise<Record<string, InferLiveObject<T>>>;
168
173
  private convertToMaterializedLiveType;
169
174
  private applyWhere;
175
+ private applyInclude;
170
176
  }
171
177
 
172
178
  declare const expressAdapter: (app: Application, server: Server<AnyRouter>, options?: {
@@ -181,6 +187,7 @@ type ParsedRequest<TInput = any> = {
181
187
  procedure?: string;
182
188
  context: Record<string, any>;
183
189
  where?: Record<string, any>;
190
+ include?: Record<string, any>;
184
191
  type: "QUERY" | "MUTATE";
185
192
  resourceId?: string;
186
193
  input?: TInput;
@@ -219,4 +226,4 @@ declare class Server<TRouter extends AnyRouter> {
219
226
  }
220
227
  declare const server: typeof Server.create;
221
228
 
222
- export { type AnyRoute, type AnyRouter, type ContextProvider, InMemoryStorage, type Middleware, type Mutation, type MutationHandler, type MutationResult, type NextFunction, type ParsedRequest, type QueryResult, type RequestHandler, type RequestType, Route, RouteFactory, type RouteRecord, Router, SQLStorage, Server, Storage, expressAdapter, routeFactory, router, server };
229
+ export { type AnyRoute, type AnyRouter, type ContextProvider, type Middleware, type Mutation, type MutationHandler, type MutationResult, type NextFunction, type ParsedRequest, type QueryResult, type RequestHandler, type RequestType, Route, RouteFactory, type RouteRecord, Router, SQLStorage, Server, Storage, expressAdapter, routeFactory, router, server };
package/dist/server.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import { z, ZodTypeAny } from 'zod';
2
- import { LiveObjectAny, Schema, MaterializedLiveType, WhereClause } from './index.js';
2
+ import { LiveObjectAny, Schema, MaterializedLiveType, IncludeClause, InferLiveObject, WhereClause, LiveObjectMutationInput } from './index.js';
3
3
  import { PostgresPool } from 'kysely';
4
4
  import { Application } from 'express-ws';
5
5
 
@@ -29,33 +29,33 @@ declare const mutationSchema: z.ZodUnion<[z.ZodObject<z.objectUtil.extendShape<{
29
29
  }, {
30
30
  resourceId: z.ZodString;
31
31
  payload: z.ZodEffects<z.ZodRecord<z.ZodString, z.ZodObject<{
32
- value: z.ZodUnion<[z.ZodUnion<[z.ZodUnion<[z.ZodString, z.ZodNumber]>, z.ZodBoolean]>, z.ZodDate]>;
32
+ value: z.ZodNullable<z.ZodUnion<[z.ZodUnion<[z.ZodUnion<[z.ZodString, z.ZodNumber]>, z.ZodBoolean]>, z.ZodDate]>>;
33
33
  _meta: z.ZodOptional<z.ZodObject<{
34
- timestamp: z.ZodOptional<z.ZodString>;
34
+ timestamp: z.ZodNullable<z.ZodOptional<z.ZodString>>;
35
35
  }, "strip", z.ZodTypeAny, {
36
- timestamp?: string | undefined;
36
+ timestamp?: string | null | undefined;
37
37
  }, {
38
- timestamp?: string | undefined;
38
+ timestamp?: string | null | undefined;
39
39
  }>>;
40
40
  }, "strip", z.ZodTypeAny, {
41
- value: string | number | boolean | Date;
41
+ value: string | number | boolean | Date | null;
42
42
  _meta?: {
43
- timestamp?: string | undefined;
43
+ timestamp?: string | null | undefined;
44
44
  } | undefined;
45
45
  }, {
46
- value: string | number | boolean | Date;
46
+ value: string | number | boolean | Date | null;
47
47
  _meta?: {
48
- timestamp?: string | undefined;
48
+ timestamp?: string | null | undefined;
49
49
  } | undefined;
50
50
  }>>, Record<string, {
51
- value: string | number | boolean | Date;
51
+ value: string | number | boolean | Date | null;
52
52
  _meta?: {
53
- timestamp?: string | undefined;
53
+ timestamp?: string | null | undefined;
54
54
  } | undefined;
55
55
  }>, Record<string, {
56
- value: string | number | boolean | Date;
56
+ value: string | number | boolean | Date | null;
57
57
  _meta?: {
58
- timestamp?: string | undefined;
58
+ timestamp?: string | null | undefined;
59
59
  } | undefined;
60
60
  }>>;
61
61
  }>, "strip", z.ZodTypeAny, {
@@ -63,9 +63,9 @@ declare const mutationSchema: z.ZodUnion<[z.ZodObject<z.objectUtil.extendShape<{
63
63
  resourceId: string;
64
64
  resource: string;
65
65
  payload: Record<string, {
66
- value: string | number | boolean | Date;
66
+ value: string | number | boolean | Date | null;
67
67
  _meta?: {
68
- timestamp?: string | undefined;
68
+ timestamp?: string | null | undefined;
69
69
  } | undefined;
70
70
  }>;
71
71
  id?: string | undefined;
@@ -74,9 +74,9 @@ declare const mutationSchema: z.ZodUnion<[z.ZodObject<z.objectUtil.extendShape<{
74
74
  resourceId: string;
75
75
  resource: string;
76
76
  payload: Record<string, {
77
- value: string | number | boolean | Date;
77
+ value: string | number | boolean | Date | null;
78
78
  _meta?: {
79
- timestamp?: string | undefined;
79
+ timestamp?: string | null | undefined;
80
80
  } | undefined;
81
81
  }>;
82
82
  id?: string | undefined;
@@ -144,29 +144,35 @@ declare class RouteFactory {
144
144
  declare const routeFactory: typeof RouteFactory.create;
145
145
  type AnyRoute = Route<LiveObjectAny, Middleware<any>, Record<string, any>>;
146
146
 
147
+ type Simplify<T> = T extends Record<string, any> ? {
148
+ [K in keyof T]: Simplify<T[K]>;
149
+ } : T;
150
+
147
151
  declare abstract class Storage {
148
- abstract updateSchema(opts: Schema<any>): Promise<void>;
149
- abstract findById<T extends LiveObjectAny>(resourceName: string, id: string): Promise<MaterializedLiveType<T> | undefined>;
150
- abstract find<T extends LiveObjectAny>(resourceName: string, where?: Record<string, any>): Promise<Record<string, MaterializedLiveType<T>>>;
151
- abstract upsert<T extends LiveObjectAny>(resourceName: string, resourceId: string, value: MaterializedLiveType<T>): Promise<MaterializedLiveType<T>>;
152
- }
153
- declare class InMemoryStorage extends Storage {
154
- private storage;
155
- updateSchema(opts: Schema<any>): Promise<void>;
156
- findById<T extends LiveObjectAny>(resourceName: string, id: string): Promise<MaterializedLiveType<T> | undefined>;
157
- find<T extends LiveObjectAny>(resourceName: string, where?: Record<string, any>): Promise<Record<string, MaterializedLiveType<T>>>;
158
- upsert<T extends LiveObjectAny>(resourceName: string, resourceId: string, value: MaterializedLiveType<T>): Promise<MaterializedLiveType<T>>;
152
+ abstract findOne<T extends LiveObjectAny>(resource: T, id: string, options?: {
153
+ include?: IncludeClause<T>;
154
+ }): Promise<InferLiveObject<T> | undefined>;
155
+ abstract find<T extends LiveObjectAny>(resource: T, options?: {
156
+ where?: WhereClause<T>;
157
+ include?: IncludeClause<T>;
158
+ }): Promise<Record<string, InferLiveObject<T>>>;
159
+ insert<T extends LiveObjectAny>(resource: T, value: Simplify<LiveObjectMutationInput<T>>): Promise<InferLiveObject<T>>;
160
+ update<T extends LiveObjectAny>(resource: T, resourceId: string, value: LiveObjectMutationInput<T>): Promise<InferLiveObject<T>>;
159
161
  }
160
162
  declare class SQLStorage extends Storage {
161
163
  private db;
162
164
  private schema?;
163
165
  constructor(pool: PostgresPool);
164
- updateSchema(opts: Schema<any>): Promise<void>;
165
- findById<T extends LiveObjectAny>(resourceName: string, id: string): Promise<MaterializedLiveType<T> | undefined>;
166
- find<T extends LiveObjectAny>(resourceName: string, where?: WhereClause<T>): Promise<Record<string, MaterializedLiveType<T>>>;
167
- upsert<T extends LiveObjectAny>(resourceName: string, resourceId: string, value: MaterializedLiveType<T>): Promise<MaterializedLiveType<T>>;
166
+ findOne<T extends LiveObjectAny>(resource: T, id: string, options?: {
167
+ include?: IncludeClause<T>;
168
+ }): Promise<InferLiveObject<T> | undefined>;
169
+ find<T extends LiveObjectAny>(resource: T, options?: {
170
+ where?: WhereClause<T>;
171
+ include?: IncludeClause<T>;
172
+ }): Promise<Record<string, InferLiveObject<T>>>;
168
173
  private convertToMaterializedLiveType;
169
174
  private applyWhere;
175
+ private applyInclude;
170
176
  }
171
177
 
172
178
  declare const expressAdapter: (app: Application, server: Server<AnyRouter>, options?: {
@@ -181,6 +187,7 @@ type ParsedRequest<TInput = any> = {
181
187
  procedure?: string;
182
188
  context: Record<string, any>;
183
189
  where?: Record<string, any>;
190
+ include?: Record<string, any>;
184
191
  type: "QUERY" | "MUTATE";
185
192
  resourceId?: string;
186
193
  input?: TInput;
@@ -219,4 +226,4 @@ declare class Server<TRouter extends AnyRouter> {
219
226
  }
220
227
  declare const server: typeof Server.create;
221
228
 
222
- export { type AnyRoute, type AnyRouter, type ContextProvider, InMemoryStorage, type Middleware, type Mutation, type MutationHandler, type MutationResult, type NextFunction, type ParsedRequest, type QueryResult, type RequestHandler, type RequestType, Route, RouteFactory, type RouteRecord, Router, SQLStorage, Server, Storage, expressAdapter, routeFactory, router, server };
229
+ export { type AnyRoute, type AnyRouter, type ContextProvider, type Middleware, type Mutation, type MutationHandler, type MutationResult, type NextFunction, type ParsedRequest, type QueryResult, type RequestHandler, type RequestType, Route, RouteFactory, type RouteRecord, Router, SQLStorage, Server, Storage, expressAdapter, routeFactory, router, server };
package/dist/server.js CHANGED
@@ -1,2 +1,2 @@
1
- import {a,b}from'./chunk-EK7ODJWE.js';import he,{parse}from'qs';import {z as z$1}from'zod';import I from'node:crypto';import {Kysely,PostgresDialect}from'kysely';var P=a(E=>{Object.defineProperty(E,"__esModule",{value:true});E.parse=le;E.serialize=pe;var oe=/^[\u0021-\u003A\u003C\u003E-\u007E]+$/,ie=/^[\u0021-\u003A\u003C-\u007E]*$/,se=/^([.]?[a-z0-9]([a-z0-9-]{0,61}[a-z0-9])?)([.][a-z0-9]([a-z0-9-]{0,61}[a-z0-9])?)*$/i,ce=/^[\u0020-\u003A\u003D-\u007E]*$/,ue=Object.prototype.toString,de=(()=>{let r=function(){};return r.prototype=Object.create(null),r})();function le(r,e){let t=new de,n=r.length;if(n<2)return t;let a=(e==null?void 0:e.decode)||me,o=0;do{let i=r.indexOf("=",o);if(i===-1)break;let u=r.indexOf(";",o),s=u===-1?n:u;if(i>s){o=r.lastIndexOf(";",i-1)+1;continue}let h=V(r,o,i),m=_(r,i,h),l=r.slice(h,m);if(t[l]===void 0){let p=V(r,i+1,s),d=_(r,s,p),c=a(r.slice(p,d));t[l]=c;}o=s+1;}while(o<n);return t}function V(r,e,t){do{let n=r.charCodeAt(e);if(n!==32&&n!==9)return e}while(++e<t);return t}function _(r,e,t){for(;e>t;){let n=r.charCodeAt(--e);if(n!==32&&n!==9)return e+1}return t}function pe(r,e,t){let n=(t==null?void 0:t.encode)||encodeURIComponent;if(!oe.test(r))throw new TypeError(`argument name is invalid: ${r}`);let a=n(e);if(!ie.test(a))throw new TypeError(`argument val is invalid: ${e}`);let o=r+"="+a;if(!t)return o;if(t.maxAge!==void 0){if(!Number.isInteger(t.maxAge))throw new TypeError(`option maxAge is invalid: ${t.maxAge}`);o+="; Max-Age="+t.maxAge;}if(t.domain){if(!se.test(t.domain))throw new TypeError(`option domain is invalid: ${t.domain}`);o+="; Domain="+t.domain;}if(t.path){if(!ce.test(t.path))throw new TypeError(`option path is invalid: ${t.path}`);o+="; Path="+t.path;}if(t.expires){if(!ye(t.expires)||!Number.isFinite(t.expires.valueOf()))throw new TypeError(`option expires is invalid: ${t.expires}`);o+="; Expires="+t.expires.toUTCString();}if(t.httpOnly&&(o+="; HttpOnly"),t.secure&&(o+="; Secure"),t.partitioned&&(o+="; Partitioned"),t.priority)switch(typeof t.priority=="string"?t.priority.toLowerCase():void 0){case "low":o+="; Priority=Low";break;case "medium":o+="; Priority=Medium";break;case "high":o+="; Priority=High";break;default:throw new TypeError(`option priority is invalid: ${t.priority}`)}if(t.sameSite)switch(typeof t.sameSite=="string"?t.sameSite.toLowerCase():t.sameSite){case true:case "strict":o+="; SameSite=Strict";break;case "lax":o+="; SameSite=Lax";break;case "none":o+="; SameSite=None";break;default:throw new TypeError(`option sameSite is invalid: ${t.sameSite}`)}return o}function me(r){if(r.indexOf("%")===-1)return r;try{return decodeURIComponent(r)}catch{return r}}function ye(r){return ue.call(r)==="[object Date]"}});var Z=b(P(),1);var $=z$1.object({type:z$1.literal("QUERY"),resource:z$1.string(),where:z$1.record(z$1.any()).optional(),include:z$1.record(z$1.any()).optional()}),L=z$1.record(z$1.object({value:z$1.string().or(z$1.number()).or(z$1.boolean()).or(z$1.date()),_meta:z$1.object({timestamp:z$1.string().optional()}).optional()})).superRefine((r,e)=>{r.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()}),S=q.extend({procedure:z$1.string(),payload:z$1.any().optional()}),M=q.extend({resourceId:z$1.string(),payload:L});z$1.union([S,M]);var F=$.omit({type:true,resource:true}),N=S.omit({id:true,type:true,resource:true,procedure:true}),j=M.omit({id:true,type:true,resource:true});z$1.union([j,N]);var H=r=>async e=>{var t;try{let n=typeof e.headers.getSetCookie=="function"?Object.fromEntries(e.headers):e.headers,a={headers:n,cookies:n.cookie?Z.default.parse(n.cookie):{}},o=new URL(e.url),i=o.pathname.split("/"),u=o.searchParams,s=he.parse(u.toString()),h=await((t=r.contextProvider)==null?void 0:t.call(r,{transport:"HTTP",headers:a.headers,cookies:a.cookies,query:s}))??{};if(e.method==="GET"){let m=i[i.length-1],{success:l,data:p,error:d}=F.safeParse(s);if(!l)return Response.json({message:"Invalid query",code:"INVALID_QUERY",details:d},{status:400});let c=await r.handleRequest({req:{...a,type:"QUERY",resourceName:m,context:h,where:p.where,query:s}});return !c||!c.data?Response.json({message:"Invalid resource",code:"INVALID_RESOURCE"},{status:400}):Response.json(c.data)}if(e.method==="POST")try{let m=i[i.length-1],l=i[i.length-2],p=e.body?await e.json():{},d;if(m==="set"){let{success:b,data:g,error:w}=j.safeParse(p);if(!b)return Response.json({message:"Invalid mutation",code:"INVALID_REQUEST",details:w},{status:400});d=g;}else {let{success:b,data:g,error:w}=N.safeParse(p);if(!b)return Response.json({message:"Invalid mutation",code:"INVALID_REQUEST",details:w},{status:400});d=g;}let c=await r.handleRequest({req:{...a,type:"MUTATE",resourceName:l,input:d.payload,context:h,resourceId:d.resourceId,procedure:m!=="set"?m:void 0,query:{}}});return Response.json(c)}catch(m){return console.error("Error parsing mutation from the client:",m),Response.json({message:"Internal server error",code:"INTERNAL_SERVER_ERROR"},{status:500})}return Response.json({message:"Not found",code:"NOT_FOUND"},{status:404})}catch(n){return console.error("Unexpected error:",n),Response.json({message:"Internal server error",code:"INTERNAL_SERVER_ERROR"},{status:500})}};var J=b(P(),1);var T=z$1.string(),ge=z$1.object({id:T,type:z$1.literal("SUBSCRIBE"),resource:z$1.string()}),Te=z$1.object({id:T,type:z$1.literal("SYNC"),lastSyncedAt:z$1.string().optional(),resources:z$1.string().array().optional(),where:z$1.record(z$1.any()).optional()}),G=M.extend({id:T}),Re=S.extend({id:T}),xe=z$1.union([Re,G]),B=z$1.union([ge,Te,xe]),be=z$1.object({id:T,type:z$1.literal("SYNC"),resource:z$1.string(),data:z$1.record(L)}),we=z$1.object({id:T,type:z$1.literal("REJECT"),resource:z$1.string(),message:z$1.string().optional()}),Se=z$1.object({id:T,type:z$1.literal("REPLY"),data:z$1.any()});z$1.union([be,we,Se,G]);var W="0123456789ABCDEFGHJKMNPQRSTVWXYZ",v=32;var Me=16,K=10,Q=0xffffffffffff;var R;(function(r){r.Base32IncorrectEncoding="B32_ENC_INVALID",r.DecodeTimeInvalidCharacter="DEC_TIME_CHAR",r.DecodeTimeValueMalformed="DEC_TIME_MALFORMED",r.EncodeTimeNegative="ENC_TIME_NEG",r.EncodeTimeSizeExceeded="ENC_TIME_SIZE_EXCEED",r.EncodeTimeValueMalformed="ENC_TIME_MALFORMED",r.PRNGDetectFailure="PRNG_DETECT",r.ULIDInvalid="ULID_INVALID",r.Unexpected="UNEXPECTED",r.UUIDInvalid="UUID_INVALID";})(R||(R={}));var x=class extends Error{constructor(e,t){super(`${t} (${e})`),this.name="ULIDError",this.code=e;}};function ve(r){let e=Math.floor(r()*v);return e===v&&(e=v-1),W.charAt(e)}function Ee(r){var n;let e=Ie(),t=e&&(e.crypto||e.msCrypto)||(typeof I<"u"?I:null);if(typeof(t==null?void 0:t.getRandomValues)=="function")return ()=>{let a=new Uint8Array(1);return t.getRandomValues(a),a[0]/255};if(typeof(t==null?void 0:t.randomBytes)=="function")return ()=>t.randomBytes(1).readUInt8()/255;if((n=I)!=null&&n.randomBytes)return ()=>I.randomBytes(1).readUInt8()/255;throw new x(R.PRNGDetectFailure,"Failed to find a reliable PRNG")}function Ie(){return Le()?self:typeof window<"u"?window:typeof global<"u"?global:typeof globalThis<"u"?globalThis:null}function Ae(r,e){let t="";for(;r>0;r--)t=ve(e)+t;return t}function Pe(r,e=K){if(isNaN(r))throw new x(R.EncodeTimeValueMalformed,`Time must be a number: ${r}`);if(r>Q)throw new x(R.EncodeTimeSizeExceeded,`Cannot encode a time larger than ${Q}: ${r}`);if(r<0)throw new x(R.EncodeTimeNegative,`Time must be positive: ${r}`);if(Number.isInteger(r)===false)throw new x(R.EncodeTimeValueMalformed,`Time must be an integer: ${r}`);let t,n="";for(let a=e;a>0;a--)t=r%v,n=W.charAt(t)+n,r=(r-t)/v;return n}function Le(){return typeof WorkerGlobalScope<"u"&&self instanceof WorkerGlobalScope}function Y(r,e){let t=Ee(),n=Date.now();return Pe(n,K)+Ae(Me,t)}var O=()=>Y().toLowerCase();var X=r=>{let e={},t={};return r.subscribeToMutations(n=>{let a=n;!a.resourceId||!a.payload||(console.log("Mutation propagated:",a),Object.entries(t[a.resource]??{}).forEach(([o,i])=>{var u;(u=e[o])==null||u.send(JSON.stringify({...a,id:a.id??O()}));}));}),(n,a)=>{var m;let o=l=>{n.send(JSON.stringify(l));},i=O(),u={headers:a.headers,cookies:typeof a.headers.cookie=="string"?J.default.parse(a.headers.cookie):{}},s=parse(a.url.split("?")[1]),h=(m=r.contextProvider)==null?void 0:m.call(r,{transport:"WEBSOCKET",headers:u.headers,cookies:u.cookies,query:s});e[i]=n,console.log("Client connected:",i),n.on("message",async l=>{try{console.log("Message received from the client:",l);let p=B.parse(JSON.parse(l.toString()));if(p.type==="SUBSCRIBE"){let{resource:d}=p;t[d]||(t[d]={}),t[d][i]={};}else if(p.type==="SYNC"){let{resources:d}=p,c=d??Object.keys(r.schema);console.log("Syncing resources:",c),await Promise.all(c.map(async b=>{let g=await r.handleRequest({req:{...u,type:"QUERY",resourceName:b,context:await h??{},query:s}});if(!g||!g.data)throw new Error("Invalid resource");o({id:p.id,type:"SYNC",resource:b,data:Object.fromEntries(Object.entries(g.data??{}).map(([w,ne])=>[w,ne.value]))});}));}else if(p.type==="MUTATE"){let{resource:d}=p;console.log("Received mutation from client:",p);try{let c=await r.handleRequest({req:{...u,type:"MUTATE",resourceName:d,input:p.payload,context:{messageId:p.id,...await h??{}},resourceId:p.resourceId,procedure:p.procedure,query:s}});p.procedure&&o({id:p.id,type:"REPLY",data:c});}catch(c){o({id:p.id,type:"REJECT",resource:d,message:c.message}),console.error("Error parsing mutation from the client:",c);}}}catch(p){console.error("Error handling message from the client:",p);}}),n.on("close",()=>{console.log("Connection closed",i),delete e[i];for(let l of Object.values(t))delete l[i];});}};function ee(r){let e=`${r.protocol}://${r.hostname}${r.url}`,t=new Headers;return Object.entries(r.headers).forEach(([n,a])=>{a&&t.set(n,Array.isArray(a)?a.join(","):a);}),new Request(e,{method:r.method,headers:t,body:r.body&&r.method!=="GET"?JSON.stringify(r.body):void 0})}var pt=(r,e,t)=>{r.ws(`${(t==null?void 0:t.basePath)??""}/ws`,X(e)),r.use(`${(t==null?void 0:t.basePath)??""}/`,(n,a)=>{H(e)(ee(n)).then(i=>i.json().then(u=>a.status(i.status).send(u)));});};var C=class r{routes;constructor(e){this.routes=e.routes;}static create(e){return new r(e)}},ht=r=>C.create({...r}),Oe=r=>({handler:e=>({inputValidator:r??z$1.undefined(),handler:e})}),z=class r{_resourceSchema;resourceName;middlewares;customMutations;constructor(e,t){this.resourceName=e,this.middlewares=new Set,this.customMutations=t??{};}handleFind=async({req:e,db:t})=>({data:await t.find(e.resourceName,e.where),acceptedValues:null});handleSet=async({req:e,db:t,schema:n})=>{if(!e.input)throw new Error("Payload is required");if(!e.resourceId)throw new Error("ResourceId is required");let a=await t.findById(e.resourceName,e.resourceId),[o,i]=n[this.resourceName].mergeMutation("set",e.input,a);if(!i)throw new Error("Mutation rejected");return {data:await t.upsert(e.resourceName,e.resourceId,o),acceptedValues:i}};async handleRequest(e){let t=n=>(()=>{if(n.type==="QUERY")return this.handleFind({req:n,db:e.db,schema:e.schema});if(n.type==="MUTATE")if(n.procedure){if(this.customMutations[n.procedure]){let a=this.customMutations[n.procedure].inputValidator.parse(n.input);return n.input=a,this.customMutations[n.procedure].handler({req:n,db:e.db,schema:e.schema})}}else return this.handleSet({req:n,db:e.db,schema:e.schema});throw new Error("Invalid request")})();return await Array.from(this.middlewares.values()).reduceRight((n,a)=>o=>a({req:o,next:n}),async n=>t(n))(e.req)}use(...e){for(let t of e)this.middlewares.add(t);return this}withMutations(e){return new r(this.resourceName,e({mutation:Oe}))}},k=class r{middlewares;constructor(e=[]){this.middlewares=e;}createBasicRoute(e){return new z(e.name).use(...this.middlewares)}use(...e){return new r([...this.middlewares,...e])}static create(){return new r}},gt=k.create;var A=class{},te=class extends A{storage={};async updateSchema(e){this.storage=Object.entries(e).reduce((t,[n,a])=>(t[a.name]={},t),{});}async findById(e,t){var n;return (n=this.storage[e])==null?void 0:n[t]}async find(e,t){return this.storage[e]??{}}async upsert(e,t,n){return this.storage[e]??={},this.storage[e][t]=n,n}},re=class extends A{db;schema;constructor(e){super(),this.db=new Kysely({dialect:new PostgresDialect({pool:e})});}async updateSchema(e){this.schema=e;let t=await this.db.introspection.getTables();for(let[n,a]of Object.entries(e)){let o=t.find(s=>s.name===n);o||await this.db.schema.createTable(n).ifNotExists().execute();let i=`${n}_meta`,u=t.find(s=>s.name===i);u||await this.db.schema.createTable(i).ifNotExists().execute();for(let[s,h]of Object.entries(a.fields)){let m=o==null?void 0:o.columns.find(d=>d.name===s),l=h.getStorageFieldType();m?m.dataType!==l.type&&console.error("Column type mismatch:",s,"expected to have type:",l.type,"but has type:",m.dataType):(await this.db.schema.alterTable(n).addColumn(s,l.type,d=>{let c=d;return l.unique&&(c=c.unique()),l.nullable||(c=c.notNull()),l.references&&(c=c.references(l.references)),l.primary&&(c=c.primaryKey()),l.default!==void 0&&(c=c.defaultTo(l.default)),c}).execute().catch(d=>{throw console.error("Error adding column",s,d),d}),l.index&&await this.db.schema.createIndex(`${n}_${s}_index`).on(n).column(s).execute().catch(d=>{})),(u==null?void 0:u.columns.find(d=>d.name===s))||await this.db.schema.alterTable(i).addColumn(s,"varchar",d=>{let c=d;return l.primary&&(c=c.primaryKey().references(`${n}.${s}`)),c}).execute();}}}async findById(e,t){let n=await this.db.selectFrom(e).where("id","=",t).selectAll(e).executeTakeFirst(),a=await this.db.selectFrom(`${e}_meta`).where("id","=",t).selectAll(`${e}_meta`).executeTakeFirst();if(!(!n||!a))return this.convertToMaterializedLiveType(n,a)}async find(e,t){let a=await this.applyWhere(e,this.db.selectFrom(e).selectAll(e),t).execute(),o=Object.fromEntries(a.map(s=>{let{id:h,...m}=s;return [h,m]}));if(Object.keys(o).length===0)return {};let i=Object.fromEntries((await this.db.selectFrom(`${e}_meta`).selectAll().where("id","in",Object.keys(o)).execute()).map(s=>{let{id:h,...m}=s;return [h,m]}));return Object.entries(o).reduce((s,[h,m])=>(i[h]&&(s[h]=this.convertToMaterializedLiveType(m,i[h])),s),{})}async upsert(e,t,n){return await this.db.transaction().execute(async a=>{let o=!!await a.selectFrom(e).select("id").where("id","=",t).executeTakeFirst(),i={},u={};for(let[s,h]of Object.entries(n.value))i[s]=h.value,u[s]=h._meta.timestamp;o?await Promise.all([a.updateTable(e).set(i).where("id","=",t).execute(),a.updateTable(`${e}_meta`).set(u).where("id","=",t).execute()]):await Promise.all([a.insertInto(e).values({...i,id:t}).execute(),a.insertInto(`${e}_meta`).values({...u,id:t}).execute()]);}),n}convertToMaterializedLiveType(e,t){return {value:Object.fromEntries(Object.entries(e).flatMap(([n,a])=>t?[[n,{value:a,_meta:{timestamp:t==null?void 0:t[n]}}]]:[]))}}applyWhere(e,t,n){if(!n)return t;if(!this.schema)throw new Error("Schema not initialized");let a=this.schema[e];if(!a)throw new Error("Resource not found");for(let[o,i]of Object.entries(n))if(a.fields[o])t=t.where(`${e}.${o}`,"=",i);else if(a.relations[o]){let u=a.relations[o],s=u.entity.name,h=u.type==="one"?"id":u.foreignColumn,m=u.type==="one"?u.relationalColumn:"id";t=t.leftJoin(s,`${s}.${h}`,`${e}.${m}`),t=this.applyWhere(s,t,i);}return t}};var D=class r{router;storage;schema;middlewares=new Set;contextProvider;mutationSubscriptions=new Set;constructor(e){var t;this.router=e.router,this.storage=e.storage,this.schema=e.schema,(t=e.middlewares)==null||t.forEach(n=>this.middlewares.add(n)),this.storage.updateSchema(this.schema),this.contextProvider=e.contextProvider;}static create(e){return new r(e)}subscribeToMutations(e){return this.mutationSubscriptions.add(e),()=>{this.mutationSubscriptions.delete(e);}}async handleRequest(e){if(!this.router.routes[e.req.resourceName])throw new Error("Invalid resource");let t=await Array.from(this.middlewares.values()).reduceRight((n,a)=>o=>a({req:o,next:n}),async n=>this.router.routes[e.req.resourceName].handleRequest({req:n,db:this.storage,schema:this.schema}))(e.req);return t&&e.req.type==="MUTATE"&&t.acceptedValues&&Object.keys(t.acceptedValues).length>0&&this.mutationSubscriptions.forEach(n=>{n({id:e.req.context.messageId,type:"MUTATE",resource:e.req.resourceName,payload:t.acceptedValues??{},resourceId:e.req.resourceId});}),t}use(e){return this.middlewares.add(e),this}context(e){return this.contextProvider=e,this}},St=D.create;
2
- export{te as InMemoryStorage,z as Route,k as RouteFactory,C as Router,re as SQLStorage,D as Server,A as Storage,pt as expressAdapter,gt as routeFactory,ht as router,St as server};
1
+ import {a,b as b$1,r}from'./chunk-ECP6AHZL.js';import Te,{parse}from'qs';import {z as z$1}from'zod';import A from'node:crypto';import {Kysely,PostgresDialect}from'kysely';import {jsonObjectFrom,jsonArrayFrom}from'kysely/helpers/postgres';var O=a(E=>{Object.defineProperty(E,"__esModule",{value:true});E.parse=me;E.serialize=pe;var oe=/^[\u0021-\u003A\u003C\u003E-\u007E]+$/,se=/^[\u0021-\u003A\u003C-\u007E]*$/,ce=/^([.]?[a-z0-9]([a-z0-9-]{0,61}[a-z0-9])?)([.][a-z0-9]([a-z0-9-]{0,61}[a-z0-9])?)*$/i,ue=/^[\u0020-\u003A\u003D-\u007E]*$/,de=Object.prototype.toString,le=(()=>{let r=function(){};return r.prototype=Object.create(null),r})();function me(r,e){let t=new le,n=r.length;if(n<2)return t;let a=(e==null?void 0:e.decode)||ye,i=0;do{let s=r.indexOf("=",i);if(s===-1)break;let c=r.indexOf(";",i),o=c===-1?n:c;if(s>o){i=r.lastIndexOf(";",s-1)+1;continue}let h=V(r,i,s),p=F(r,s,h),u=r.slice(h,p);if(t[u]===void 0){let m=V(r,s+1,o),d=F(r,o,m),l=a(r.slice(m,d));t[u]=l;}i=o+1;}while(i<n);return t}function V(r,e,t){do{let n=r.charCodeAt(e);if(n!==32&&n!==9)return e}while(++e<t);return t}function F(r,e,t){for(;e>t;){let n=r.charCodeAt(--e);if(n!==32&&n!==9)return e+1}return t}function pe(r,e,t){let n=(t==null?void 0:t.encode)||encodeURIComponent;if(!oe.test(r))throw new TypeError(`argument name is invalid: ${r}`);let a=n(e);if(!se.test(a))throw new TypeError(`argument val is invalid: ${e}`);let i=r+"="+a;if(!t)return i;if(t.maxAge!==void 0){if(!Number.isInteger(t.maxAge))throw new TypeError(`option maxAge is invalid: ${t.maxAge}`);i+="; Max-Age="+t.maxAge;}if(t.domain){if(!ce.test(t.domain))throw new TypeError(`option domain is invalid: ${t.domain}`);i+="; Domain="+t.domain;}if(t.path){if(!ue.test(t.path))throw new TypeError(`option path is invalid: ${t.path}`);i+="; Path="+t.path;}if(t.expires){if(!fe(t.expires)||!Number.isFinite(t.expires.valueOf()))throw new TypeError(`option expires is invalid: ${t.expires}`);i+="; Expires="+t.expires.toUTCString();}if(t.httpOnly&&(i+="; HttpOnly"),t.secure&&(i+="; Secure"),t.partitioned&&(i+="; Partitioned"),t.priority)switch(typeof t.priority=="string"?t.priority.toLowerCase():void 0){case "low":i+="; Priority=Low";break;case "medium":i+="; Priority=Medium";break;case "high":i+="; Priority=High";break;default:throw new TypeError(`option priority is invalid: ${t.priority}`)}if(t.sameSite)switch(typeof t.sameSite=="string"?t.sameSite.toLowerCase():t.sameSite){case true:case "strict":i+="; SameSite=Strict";break;case "lax":i+="; SameSite=Lax";break;case "none":i+="; SameSite=None";break;default:throw new TypeError(`option sameSite is invalid: ${t.sameSite}`)}return i}function ye(r){if(r.indexOf("%")===-1)return r;try{return decodeURIComponent(r)}catch{return r}}function fe(r){return de.call(r)==="[object Date]"}});var G=b$1(O(),1);var q=z$1.object({type:z$1.literal("QUERY"),resource:z$1.string(),where:z$1.record(z$1.any()).optional(),include:z$1.record(z$1.any()).optional()}),L=z$1.record(z$1.object({value:z$1.string().or(z$1.number()).or(z$1.boolean()).or(z$1.date()).nullable(),_meta:z$1.object({timestamp:z$1.string().optional().nullable()}).optional()})).superRefine((r,e)=>{r.id&&e.addIssue({code:z$1.ZodIssueCode.custom,message:"Payload cannot have an id"});}),Z=z$1.object({id:z$1.string().optional(),type:z$1.literal("MUTATE"),resource:z$1.string()}),M=Z.extend({procedure:z$1.string(),payload:z$1.any().optional()}),I=Z.extend({resourceId:z$1.string(),payload:L});z$1.union([M,I]);var H=q.omit({type:true,resource:true}),P=M.omit({id:true,type:true,resource:true,procedure:true}),C=I.omit({id:true,type:true,resource:true});z$1.union([C,P]);var B=r=>async e=>{var t;try{let n=typeof e.headers.getSetCookie=="function"?Object.fromEntries(e.headers):e.headers,a={headers:n,cookies:n.cookie?G.default.parse(n.cookie):{}},i=new URL(e.url),s=i.pathname.split("/"),c=i.searchParams,o=Te.parse(c.toString()),h=await((t=r.contextProvider)==null?void 0:t.call(r,{transport:"HTTP",headers:a.headers,cookies:a.cookies,query:o}))??{};if(e.method==="GET"){let p=s[s.length-1],{success:u,data:m,error:d}=H.safeParse(o);if(!u)return Response.json({message:"Invalid query",code:"INVALID_QUERY",details:d},{status:400});let l=await r.handleRequest({req:{...a,type:"QUERY",resourceName:p,context:h,where:m.where,include:m.include,query:o}});return !l||!l.data?Response.json({message:"Invalid resource",code:"INVALID_RESOURCE"},{status:400}):Response.json(l.data)}if(e.method==="POST")try{let p=s[s.length-1],u=s[s.length-2],m=e.body?await e.json():{},d;if(p==="set"){let{success:w,data:T,error:x}=C.safeParse(m);if(!w)return Response.json({message:"Invalid mutation",code:"INVALID_REQUEST",details:x},{status:400});d=T;}else {let{success:w,data:T,error:x}=P.safeParse(m);if(!w)return Response.json({message:"Invalid mutation",code:"INVALID_REQUEST",details:x},{status:400});d=T;}let l=await r.handleRequest({req:{...a,type:"MUTATE",resourceName:u,input:d.payload,context:h,resourceId:d.resourceId,procedure:p!=="set"?p:void 0,query:{}}});return Response.json(l)}catch(p){return console.error("Error parsing mutation from the client:",p),Response.json({message:"Internal server error",code:"INTERNAL_SERVER_ERROR"},{status:500})}return Response.json({message:"Not found",code:"NOT_FOUND"},{status:404})}catch(n){return console.error("Unexpected error:",n),Response.json({message:"Internal server error",code:"INTERNAL_SERVER_ERROR"},{status:500})}};var ee=b$1(O(),1);var g=z$1.string(),ge=z$1.object({id:g,type:z$1.literal("SUBSCRIBE"),resource:z$1.string()}),Re=z$1.object({id:g,type:z$1.literal("SYNC"),lastSyncedAt:z$1.string().optional(),resources:z$1.string().array().optional(),where:z$1.record(z$1.any()).optional()}),Q=I.extend({id:g}),be=M.extend({id:g}),we=z$1.union([be,Q]),W=z$1.union([ge,Re,we]),xe=z$1.object({id:g,type:z$1.literal("SYNC"),resource:z$1.string(),data:z$1.record(L)}),Se=z$1.object({id:g,type:z$1.literal("REJECT"),resource:z$1.string(),message:z$1.string().optional()}),Me=z$1.object({id:g,type:z$1.literal("REPLY"),data:z$1.any()});z$1.union([xe,Se,Me,Q]);var Y="0123456789ABCDEFGHJKMNPQRSTVWXYZ",v=32;var Ie=16,J=10,K=0xffffffffffff;var R;(function(r){r.Base32IncorrectEncoding="B32_ENC_INVALID",r.DecodeTimeInvalidCharacter="DEC_TIME_CHAR",r.DecodeTimeValueMalformed="DEC_TIME_MALFORMED",r.EncodeTimeNegative="ENC_TIME_NEG",r.EncodeTimeSizeExceeded="ENC_TIME_SIZE_EXCEED",r.EncodeTimeValueMalformed="ENC_TIME_MALFORMED",r.PRNGDetectFailure="PRNG_DETECT",r.ULIDInvalid="ULID_INVALID",r.Unexpected="UNEXPECTED",r.UUIDInvalid="UUID_INVALID";})(R||(R={}));var b=class extends Error{constructor(e,t){super(`${t} (${e})`),this.name="ULIDError",this.code=e;}};function ve(r){let e=Math.floor(r()*v);return e===v&&(e=v-1),Y.charAt(e)}function Ee(r){var n;let e=Ae(),t=e&&(e.crypto||e.msCrypto)||(typeof A<"u"?A:null);if(typeof(t==null?void 0:t.getRandomValues)=="function")return ()=>{let a=new Uint8Array(1);return t.getRandomValues(a),a[0]/255};if(typeof(t==null?void 0:t.randomBytes)=="function")return ()=>t.randomBytes(1).readUInt8()/255;if((n=A)!=null&&n.randomBytes)return ()=>A.randomBytes(1).readUInt8()/255;throw new b(R.PRNGDetectFailure,"Failed to find a reliable PRNG")}function Ae(){return Le()?self:typeof window<"u"?window:typeof global<"u"?global:typeof globalThis<"u"?globalThis:null}function je(r,e){let t="";for(;r>0;r--)t=ve(e)+t;return t}function Oe(r,e=J){if(isNaN(r))throw new b(R.EncodeTimeValueMalformed,`Time must be a number: ${r}`);if(r>K)throw new b(R.EncodeTimeSizeExceeded,`Cannot encode a time larger than ${K}: ${r}`);if(r<0)throw new b(R.EncodeTimeNegative,`Time must be positive: ${r}`);if(Number.isInteger(r)===false)throw new b(R.EncodeTimeValueMalformed,`Time must be an integer: ${r}`);let t,n="";for(let a=e;a>0;a--)t=r%v,n=Y.charAt(t)+n,r=(r-t)/v;return n}function Le(){return typeof WorkerGlobalScope<"u"&&self instanceof WorkerGlobalScope}function X(r,e){let t=Ee(),n=Date.now();return Oe(n,J)+je(Ie,t)}var N=()=>X().toLowerCase();var te=r=>{let e={},t={};return r.subscribeToMutations(n=>{let a=n;!a.resourceId||!a.payload||(console.log("Mutation propagated:",a),Object.entries(t[a.resource]??{}).forEach(([i,s])=>{var c;(c=e[i])==null||c.send(JSON.stringify({...a,id:a.id??N()}));}));}),(n,a)=>{var p;let i=u=>{n.send(JSON.stringify(u));},s=N(),c={headers:a.headers,cookies:typeof a.headers.cookie=="string"?ee.default.parse(a.headers.cookie):{}},o=parse(a.url.split("?")[1]),h=(p=r.contextProvider)==null?void 0:p.call(r,{transport:"WEBSOCKET",headers:c.headers,cookies:c.cookies,query:o});e[s]=n,console.log("Client connected:",s),n.on("message",async u=>{try{console.log("Message received from the client:",u);let m=W.parse(JSON.parse(u.toString()));if(m.type==="SUBSCRIBE"){let{resource:d}=m;t[d]||(t[d]={}),t[d][s]={};}else if(m.type==="SYNC"){let{resources:d}=m,l=d??Object.keys(r.schema);console.log("Syncing resources:",l),await Promise.all(l.map(async w=>{let T=await r.handleRequest({req:{...c,type:"QUERY",resourceName:w,context:await h??{},query:o}});if(!T||!T.data)throw new Error("Invalid resource");i({id:m.id,type:"SYNC",resource:w,data:Object.fromEntries(Object.entries(T.data??{}).map(([x,ae])=>[x,ae.value]))});}));}else if(m.type==="MUTATE"){let{resource:d}=m;console.log("Received mutation from client:",m);try{let l=await r.handleRequest({req:{...c,type:"MUTATE",resourceName:d,input:m.payload,context:{messageId:m.id,...await h??{}},resourceId:m.resourceId,procedure:m.procedure,query:o}});m.procedure&&i({id:m.id,type:"REPLY",data:l});}catch(l){i({id:m.id,type:"REJECT",resource:d,message:l.message}),console.error("Error parsing mutation from the client:",l);}}}catch(m){console.error("Error handling message from the client:",m);}}),n.on("close",()=>{console.log("Connection closed",s),delete e[s];for(let u of Object.values(t))delete u[s];});}};function re(r){let e=`${r.protocol}://${r.hostname}${r.url}`,t=new Headers;return Object.entries(r.headers).forEach(([n,a])=>{a&&t.set(n,Array.isArray(a)?a.join(","):a);}),new Request(e,{method:r.method,headers:t,body:r.body&&r.method!=="GET"?JSON.stringify(r.body):void 0})}var yt=(r,e,t)=>{r.ws(`${(t==null?void 0:t.basePath)??""}/ws`,te(e)),r.use(`${(t==null?void 0:t.basePath)??""}/`,(n,a)=>{B(e)(re(n)).then(s=>s.json().then(c=>a.status(s.status).send(c)));});};var _=class r{routes;constructor(e){this.routes=e.routes;}static create(e){return new r(e)}},gt=r=>_.create({...r}),Ne=r=>({handler:e=>({inputValidator:r??z$1.undefined(),handler:e})}),$=class r{_resourceSchema;resourceName;middlewares;customMutations;constructor(e,t){this.resourceName=e,this.middlewares=new Set,this.customMutations=t??{};}handleFind=async({req:e,db:t})=>({data:await t.rawFind(e.resourceName,e.where,e.include),acceptedValues:null});handleSet=async({req:e,db:t,schema:n})=>{if(!e.input)throw new Error("Payload is required");if(!e.resourceId)throw new Error("ResourceId is required");let a=await t.rawFindById(e.resourceName,e.resourceId),[i,s]=n[this.resourceName].mergeMutation("set",e.input,a);if(!s)throw new Error("Mutation rejected");return {data:await t.rawUpsert(e.resourceName,e.resourceId,i),acceptedValues:s}};async handleRequest(e){let t=n=>(()=>{if(n.type==="QUERY")return this.handleFind({req:n,db:e.db,schema:e.schema});if(n.type==="MUTATE")if(n.procedure){if(this.customMutations[n.procedure]){let a=this.customMutations[n.procedure].inputValidator.parse(n.input);return n.input=a,this.customMutations[n.procedure].handler({req:n,db:e.db,schema:e.schema})}}else return this.handleSet({req:n,db:e.db,schema:e.schema});throw new Error("Invalid request")})();return await Array.from(this.middlewares.values()).reduceRight((n,a)=>i=>a({req:i,next:n}),async n=>t(n))(e.req)}use(...e){for(let t of e)this.middlewares.add(t);return this}withMutations(e){return new r(this.resourceName,e({mutation:Ne}))}},U=class r{middlewares;constructor(e=[]){this.middlewares=e;}createBasicRoute(e){return new $(e.name).use(...this.middlewares)}use(...e){return new r([...this.middlewares,...e])}static create(){return new r}},Rt=U.create;var D=class{async insert(e,t){let n=new Date().toISOString();return r(await this.rawUpsert(e.name,t.id,{value:Object.fromEntries(Object.entries(t).map(([a,i])=>[a,{value:i,_meta:{timestamp:n}}]))}))}async update(e,t,n){let a=new Date().toISOString(),{id:i,...s}=n;return r(await this.rawUpsert(e.name,t,{value:Object.fromEntries(Object.entries(s).map(([c,o])=>[c,{value:o,_meta:{timestamp:a}}]))}))}},ne=class extends D{db;schema;constructor(e){super(),this.db=new Kysely({dialect:new PostgresDialect({pool:e})});}async updateSchema(e){this.schema=e;let t=await this.db.introspection.getTables();for(let[n,a]of Object.entries(e)){let i=t.find(o=>o.name===n);i||await this.db.schema.createTable(n).ifNotExists().execute();let s=`${n}_meta`,c=t.find(o=>o.name===s);c||await this.db.schema.createTable(s).ifNotExists().execute();for(let[o,h]of Object.entries(a.fields)){let p=i==null?void 0:i.columns.find(d=>d.name===o),u=h.getStorageFieldType();p?p.dataType!==u.type&&console.error("Column type mismatch:",o,"expected to have type:",u.type,"but has type:",p.dataType):(await this.db.schema.alterTable(n).addColumn(o,u.type,d=>{let l=d;return u.unique&&(l=l.unique()),u.nullable||(l=l.notNull()),u.references&&(l=l.references(u.references)),u.primary&&(l=l.primaryKey()),u.default!==void 0&&(l=l.defaultTo(u.default)),l}).execute().catch(d=>{throw console.error("Error adding column",o,d),d}),u.index&&await this.db.schema.createIndex(`${n}_${o}_index`).on(n).column(o).execute().catch(d=>{})),(c==null?void 0:c.columns.find(d=>d.name===o))||await this.db.schema.alterTable(s).addColumn(o,"varchar",d=>{let l=d;return u.primary&&(l=l.primaryKey().references(`${n}.${o}`)),l}).execute();}}}async rawFindById(e,t,n){let a=await this.db.selectFrom(e).where("id","=",t).selectAll(e).select(s=>jsonObjectFrom(s.selectFrom(`${e}_meta`).selectAll(`${e}_meta`).whereRef(`${e}_meta.id`,"=",`${e}.id`)).as("_meta"));a=this.applyInclude(e,a,n);let i=await a.executeTakeFirst();if(i)return this.convertToMaterializedLiveType(i)}async findOne(e,t,n){let a=await this.rawFindById(e.name,t,n==null?void 0:n.include);if(a)return r(a)}async rawFind(e,t,n){let a=this.db.selectFrom(e).selectAll(e).select(o=>jsonObjectFrom(o.selectFrom(`${e}_meta`).selectAll(`${e}_meta`).whereRef(`${e}_meta.id`,"=",`${e}.id`)).as("_meta"));a=this.applyWhere(e,a,t),a=this.applyInclude(e,a,n);let i=await a.execute(),s=Object.fromEntries(i.map(o=>{let{id:h,...p}=o;return [h,p]}));return Object.keys(s).length===0?{}:Object.entries(s).reduce((o,[h,p])=>(o[h]=this.convertToMaterializedLiveType(p),o),{})}async find(e,t){let n=await this.rawFind(e.name,t==null?void 0:t.where,t==null?void 0:t.include);return Object.fromEntries(Object.entries(n).map(([a,i])=>[a,r(i)]))}async rawUpsert(e,t,n){return await this.db.transaction().execute(async a=>{var o;let i=!!await a.selectFrom(e).select("id").where("id","=",t).executeTakeFirst(),s={},c={};for(let[h,p]of Object.entries(n.value)){let u=(o=p._meta)==null?void 0:o.timestamp;u&&(s[h]=p.value,c[h]=u);}i?await Promise.all([a.updateTable(e).set(s).where("id","=",t).execute(),a.updateTable(`${e}_meta`).set(c).where("id","=",t).execute()]):await Promise.all([a.insertInto(e).values({...s,id:t}).execute(),a.insertInto(`${e}_meta`).values({...c,id:t}).execute()]);}),n}convertToMaterializedLiveType(e){if(!e._meta)throw new Error("Missing _meta");return {value:Object.entries(e).reduce((t,[n,a])=>{var i,s,c;return n==="_meta"||(n==="id"?t[n]={value:a}:Array.isArray(a)?t[n]={value:a.map(o=>this.convertToMaterializedLiveType(o)),_meta:{timestamp:(i=e==null?void 0:e._meta)==null?void 0:i[n]}}:typeof a=="object"&&a!==null&&!(a instanceof Date)?t[n]={...this.convertToMaterializedLiveType(a),_meta:{timestamp:(s=e==null?void 0:e._meta)==null?void 0:s[n]}}:t[n]={value:a,_meta:{timestamp:(c=e==null?void 0:e._meta)==null?void 0:c[n]}}),t},{})}}applyWhere(e,t,n){if(!n)return t;if(!this.schema)throw new Error("Schema not initialized");let a=this.schema[e];if(!a)throw new Error("Resource not found");for(let[i,s]of Object.entries(n))if(a.fields[i])t=t.where(`${e}.${i}`,"=",s);else if(a.relations[i]){let c=a.relations[i],o=c.entity.name,h=c.type==="one"?"id":c.foreignColumn,p=c.type==="one"?c.relationalColumn:"id";t=t.leftJoin(o,`${o}.${h}`,`${e}.${p}`),t=this.applyWhere(o,t,s);}return t}applyInclude(e,t,n){if(!n)return t;if(!this.schema)throw new Error("Schema not initialized");let a=this.schema[e];if(!a)throw new Error(`Resource not found: ${e}`);for(let[i,s]of Object.entries(n)){if(!a.relations[i])throw new Error(`Relation ${i} not found in resource ${e}`);let c=a.relations[i],o=c.entity.name,h=c.type==="one"?"id":c.foreignColumn,p=c.type==="one"?c.relationalColumn:"id",u=c.type==="one"?jsonObjectFrom:jsonArrayFrom;t=t.select(m=>u(m.selectFrom(o).selectAll(o).whereRef(`${o}.${h}`,"=",`${e}.${p}`).select(d=>jsonObjectFrom(d.selectFrom(`${o}_meta`).selectAll(`${o}_meta`).whereRef(`${o}_meta.id`,"=",`${o}.id`)).as("_meta"))).as(i));}return t}};var z=class r{router;storage;schema;middlewares=new Set;contextProvider;mutationSubscriptions=new Set;constructor(e){var t;this.router=e.router,this.storage=e.storage,this.schema=e.schema,(t=e.middlewares)==null||t.forEach(n=>this.middlewares.add(n)),this.storage.updateSchema(this.schema),this.contextProvider=e.contextProvider;}static create(e){return new r(e)}subscribeToMutations(e){return this.mutationSubscriptions.add(e),()=>{this.mutationSubscriptions.delete(e);}}async handleRequest(e){if(!this.router.routes[e.req.resourceName])throw new Error("Invalid resource");let t=await Array.from(this.middlewares.values()).reduceRight((n,a)=>i=>a({req:i,next:n}),async n=>this.router.routes[e.req.resourceName].handleRequest({req:n,db:this.storage,schema:this.schema}))(e.req);return t&&e.req.type==="MUTATE"&&t.acceptedValues&&Object.keys(t.acceptedValues).length>0&&this.mutationSubscriptions.forEach(n=>{n({id:e.req.context.messageId,type:"MUTATE",resource:e.req.resourceName,payload:t.acceptedValues??{},resourceId:e.req.resourceId});}),t}use(e){return this.middlewares.add(e),this}context(e){return this.contextProvider=e,this}},_t=z.create;
2
+ export{$ as Route,U as RouteFactory,_ as Router,ne as SQLStorage,z as Server,D as Storage,yt as expressAdapter,Rt as routeFactory,gt as router,_t as server};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@live-state/sync",
3
- "version": "0.0.1-alpha.3",
3
+ "version": "0.0.2",
4
4
  "private": false,
5
5
  "publishConfig": {
6
6
  "access": "public"
@@ -35,6 +35,8 @@
35
35
  "@repo/typescript-config": "0.0.0"
36
36
  },
37
37
  "dependencies": {
38
+ "crypto-hash": "^3.1.0",
39
+ "idb": "^8.0.3",
38
40
  "kysely": "^0.28.2",
39
41
  "qs": "^6.14.0",
40
42
  "ws": "^8.18.0",
@@ -75,8 +77,8 @@
75
77
  }
76
78
  },
77
79
  "scripts": {
78
- "build": "tsup",
79
- "dev": "tsup --watch",
80
+ "build": "NODE_ENV=production tsup",
81
+ "dev": "NODE_ENV=development tsup --watch",
80
82
  "lint": "eslint src/",
81
83
  "typecheck": "tsc --noEmit"
82
84
  }
@@ -1 +0,0 @@
1
- var g=Object.create;var f=Object.defineProperty;var h=Object.getOwnPropertyDescriptor;var i=Object.getOwnPropertyNames;var j=Object.getPrototypeOf,k=Object.prototype.hasOwnProperty;var m=(b,a)=>()=>(a||b((a={exports:{}}).exports,a),a.exports);var l=(b,a,c,e)=>{if(a&&typeof a=="object"||typeof a=="function")for(let d of i(a))!k.call(b,d)&&d!==c&&f(b,d,{get:()=>a[d],enumerable:!(e=h(a,d))||e.enumerable});return b};var n=(b,a,c)=>(c=b!=null?g(j(b)):{},l(a||!b||!b.__esModule?f(c,"default",{value:b,enumerable:true}):c,b));export{m as a,n as b};
@@ -1 +0,0 @@
1
- var R="0123456789ABCDEFGHJKMNPQRSTVWXYZ";var c;(function(e){e.Base32IncorrectEncoding="B32_ENC_INVALID",e.DecodeTimeInvalidCharacter="DEC_TIME_CHAR",e.DecodeTimeValueMalformed="DEC_TIME_MALFORMED",e.EncodeTimeNegative="ENC_TIME_NEG",e.EncodeTimeSizeExceeded="ENC_TIME_SIZE_EXCEED",e.EncodeTimeValueMalformed="ENC_TIME_MALFORMED",e.PRNGDetectFailure="PRNG_DETECT",e.ULIDInvalid="ULID_INVALID",e.Unexpected="UNEXPECTED",e.UUIDInvalid="UUID_INVALID";})(c||(c={}));var l=class extends Error{constructor(t,n){super(`${n} (${t})`),this.name="ULIDError",this.code=t;}};function L(e){let t=Math.floor(e()*32);return t===32&&(t=31),R.charAt(t)}function I(e){let t=M(),n=t&&(t.crypto||t.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 l(c.PRNGDetectFailure,"Failed to find a reliable PRNG")}function M(){return A()?self:typeof window<"u"?window:typeof global<"u"?global:typeof globalThis<"u"?globalThis:null}function E(e,t){let n="";for(;e>0;e--)n=L(t)+n;return n}function _(e,t=10){if(isNaN(e))throw new l(c.EncodeTimeValueMalformed,`Time must be a number: ${e}`);if(e>0xffffffffffff)throw new l(c.EncodeTimeSizeExceeded,`Cannot encode a time larger than ${0xffffffffffff}: ${e}`);if(e<0)throw new l(c.EncodeTimeNegative,`Time must be positive: ${e}`);if(Number.isInteger(e)===false)throw new l(c.EncodeTimeValueMalformed,`Time must be an integer: ${e}`);let n,i="";for(let a=t;a>0;a--)n=e%32,i=R.charAt(n)+i,e=(e-n)/32;return i}function A(){return typeof WorkerGlobalScope<"u"&&self instanceof WorkerGlobalScope}function x(e,t){let n=I(),i=Date.now();return _(i,10)+E(16,n)}var S=()=>x().toLowerCase(),j=(e,t)=>typeof e=="function"?e(t):e;var b=e=>{if(e)return Array.isArray(e.value)?e.value.map(t=>b(t)):typeof e.value!="object"?e.value:Object.fromEntries(Object.entries(e.value).map(([t,n])=>[t,b(n)]))};var w=(e,t,n=[])=>new Proxy(e,{get:(i,a)=>{var y,h;if(a==="__isProxy__")return true;let r=(y=t.get)==null?void 0:y.call(t,i,[...n,a]);if(r!==void 0)return r;let o=i,s=a;return (h=o[s])!=null&&h.__isProxy__||(o[s]=w(typeof o[s]=="object"?o[s]:function(){},t,[...n,a])),o[s]},apply:(i,a,r)=>{var o;return (o=t.apply)==null?void 0:o.call(t,i,n,r)}});export{S as a,j as b,b as c,w as d};