@live-state/sync 0.0.1-alpha.3 → 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/{chunk-NIWX45UD.js → chunk-2W2QGOPU.js} +1 -1
- package/dist/client.d.ts +2 -2
- package/dist/client.js +1 -1
- package/dist/fetch-client.d.ts +4 -3
- package/dist/fetch-client.js +1 -1
- package/dist/{index-NDRWVwih.d.ts → index-BS5EvFM-.d.ts} +24 -22
- package/dist/index.cjs +1 -1
- package/dist/index.d.cts +4 -1
- package/dist/index.d.ts +4 -1
- package/dist/index.js +1 -1
- package/dist/server.cjs +2 -2
- package/dist/server.d.cts +6 -4
- package/dist/server.d.ts +6 -4
- package/dist/server.js +2 -2
- package/package.json +4 -3
|
@@ -1 +1 @@
|
|
|
1
|
-
var
|
|
1
|
+
var x="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),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 l(c.PRNGDetectFailure,"Failed to find a reliable PRNG")}function M(){return _()?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 A(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=x.charAt(n)+i,e=(e-n)/32;return i}function _(){return typeof WorkerGlobalScope<"u"&&self instanceof WorkerGlobalScope}function R(e,t){let n=I(),i=Date.now();return A(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 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)}});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)]))};export{S as a,j as b,w as c,b as d};
|
package/dist/client.d.ts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
|
|
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-BS5EvFM-.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-2W2QGOPU.js';import {useState,useEffect}from'react';import {jsx,Fragment}from'react/jsx-runtime';import {z}from'zod';import {stringify}from'qs';import {openDB}from'idb';var X=d=>{let[e,t]=useState(()=>d.get());return useEffect(()=>d.subscribe(()=>{let i=d.get();t(i);}),[]),e},ee=({children:d,client:e})=>(useEffect(()=>{e.subscribe();},[]),jsx(Fragment,{children:d}));z.object({type:z.literal("QUERY"),resource:z.string(),where:z.record(z.any()).optional(),include:z.record(z.any()).optional()});var j=z.record(z.object({value:z.string().or(z.number()).or(z.boolean()).or(z.date()),_meta:z.object({timestamp:z.string().optional()}).optional()})).superRefine((d,e)=>{d.id&&e.addIssue({code:z.ZodIssueCode.custom,message:"Payload cannot have an id"});}),K=z.object({id:z.string().optional(),type:z.literal("MUTATE"),resource:z.string()}),C=K.extend({procedure:z.string(),payload:z.any().optional()}),L=K.extend({resourceId:z.string(),payload:j});z.union([C,L]);var v=z.string(),H=z.object({id:v,type:z.literal("SUBSCRIBE"),resource:z.string()}),_=z.object({id:v,type:z.literal("SYNC"),lastSyncedAt:z.string().optional(),resources:z.string().array().optional(),where:z.record(z.any()).optional()}),N=L.extend({id:v}),V=C.extend({id:v}),Y=z.union([V,N]);z.union([H,_,Y]);var $=z.object({id:v,type:z.literal("SYNC"),resource:z.string(),data:z.record(j)}),J=z.object({id:v,type:z.literal("REJECT"),resource:z.string(),message:z.string().optional()}),q=z.object({id:v,type:z.literal("REPLY"),data:z.any()}),W=z.union([$,J,q,N]);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 c=this.nodes.get(n);!c||!c.references.get(i)||(c.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 x="__meta",T=class{db;async init(e){this.db=await openDB("live-state",1,{upgrade(t){Object.keys(e).forEach(i=>t.createObjectStore(i)),t.createObjectStore(x);}});}async get(e){if(this.db.getAllRecords)return this.db.getAllRecords(e);let[t,i]=await Promise.all([this.db.getAll(e),this.db.getAllKeys(e)]);return Object.fromEntries(t.map((s,r)=>[i[r],s]))}getOne(e,t){return this.db.get(e,t)}set(e,t,i){return this.db.put(e,i,t)}delete(e,t){return this.db.delete(e,t)}getMeta(e){return this.db.get(x,e)}setMeta(e,t){return this.db.put(x,t,e)}};var R=class{constructor(e,t){this.schema=e;this.kvStorage=new T,this.kvStorage.init(this.schema).then(()=>{this.kvStorage.getMeta("mutationStack").then(i=>{!i||Object.keys(i).length===0||(this.optimisticMutationStack=i,t==null||t(this.optimisticMutationStack));}).then(()=>{Object.entries(this.schema).forEach(([i,s])=>{this.kvStorage.get(i).then(r=>{!r||Object.keys(r).length===0||this.loadConsolidatedState(i,r);});});}).catch(i=>{console.error("Failed to load state from storage",i);});});}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(([c,h])=>{var l;let g=this.optimisticObjGraph.getNode(h);if(!g)return [c,void 0];let[y,m]=Object.entries(this.schema[e].relations).find(u=>u[1].relationalColumn===c||u[1].foreignColumn===c)??[],f=m==null?void 0:m.entity.name;return !f||!m?[c,void 0]:[y,(l=this.optimisticRawObjPool[f])==null?void 0:l[g.id]]})),...Object.fromEntries(Array.from(i.referencedBy.entries()).map(([c,h])=>{var u;let g=h instanceof Set,y=g?Array.from(h.values()).flatMap(p=>{let b=this.optimisticObjGraph.getNode(p);return b?[b]:[]}):this.optimisticObjGraph.getNode(h);if(!y)return [c,void 0];let[m,f]=Object.entries(this.schema[e].relations).find(p=>p[1].relationalColumn===c||p[1].foreignColumn===c)??[],l=f==null?void 0:f.entity.name;return !l||!f?[c,g?[]:void 0]:[m,g?{value:y.map(p=>{var b;return (b=this.optimisticRawObjPool[l])==null?void 0:b[p.id]})}:(u=this.optimisticRawObjPool[l])==null?void 0:u[y.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 h,g,y,m,f;let s=this.schema[e];if(console.log("Adding mutation",t),!s)throw new Error("Schema not found");let r=(h=this.optimisticRawObjPool[e])==null?void 0:h[t.resourceId];if(i)(this.optimisticMutationStack[e]??=[]).push(t);else {this.optimisticMutationStack[e]=(y=(g=this.optimisticMutationStack)==null?void 0:g[e])==null?void 0:y.filter(p=>p.id!==t.id),this.kvStorage.setMeta("mutationStack",this.optimisticMutationStack),this.rawObjPool[e]??={};let l={value:{...this.schema[e].mergeMutation("set",t.payload,this.rawObjPool[e][t.resourceId])[0].value,id:{value:t.resourceId}}};this.rawObjPool[e][t.resourceId]=l;let u=l.value;delete u.id,this.kvStorage.set(e,t.resourceId,u);}let n=(m=this.rawObjPool[e])==null?void 0:m[t.resourceId],c=(this.optimisticMutationStack[e]??[]).reduce((l,u)=>u.resourceId!==t.resourceId?l:this.schema[e].mergeMutation("set",u.payload,l)[0],n);if(c&&((this.optimisticRawObjPool[e]??={})[t.resourceId]={value:{...c.value,id:{value:t.resourceId}}}),this.optimisticObjGraph.hasNode(t.resourceId)||this.optimisticObjGraph.createNode(t.resourceId,e,Object.values(s.relations).flatMap(l=>l.type==="many"?[l.foreignColumn]:[])),Object.keys(s.relations).length>0){let l=Object.fromEntries(Object.entries(s.relations).flatMap(([u,p])=>p.type==="one"?[[p.relationalColumn,u]]:[]));Object.entries(t.payload).forEach(([u,p])=>{if(!p||!l[u])return;let b=r==null?void 0:r.value[u],[,M]=s.relations[l[u]].mergeMutation("set",p,b);if(M){if(!this.optimisticObjGraph.hasNode(M.value)){let P=s.relations[l[u]].entity.name;this.optimisticObjGraph.createNode(M.value,P,Object.values(this.schema[P].relations).flatMap(k=>k.type==="many"?[k.foreignColumn]:[]));}b!=null&&b.value&&this.optimisticObjGraph.removeLink(t.resourceId,u),this.optimisticObjGraph.createLink(t.resourceId,M.value,u);}});}(f=this.resourceTypeSubscriptions[e])==null||f.forEach(l=>l()),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 A=class{url;ws;store;routeSubscriptions={};replyHandlers={};constructor(e){this.url=e.url,this.store=new R(e.schema,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=W.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:c=>{delete this.replyHandlers[s.id],r(c);}};})}sendWsMessage(e){this.ws&&this.ws.connected()&&this.ws.send(JSON.stringify(e));}},Ne=d=>{let e=new A(d);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:c,...h}=s[0];return e.mutate(r[0],c,h)}if(n==="update"){let[c,h]=s;return e.mutate(r[0],c,h)}return e.genericMutate(r[0],n,s[0])}})}};export{ee as SubscriptionProvider,Ne as createClient,X as useLiveQuery};
|
package/dist/fetch-client.d.ts
CHANGED
|
@@ -1,14 +1,15 @@
|
|
|
1
|
-
import { A as AnyRouter, C as ClientOptions, L as LiveObjectAny, W as WhereClause, S as Simplify,
|
|
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-BS5EvFM-.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
|
};
|
package/dist/fetch-client.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {c,b,d}from'./chunk-2W2QGOPU.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>;
|
|
@@ -187,6 +187,9 @@ type WhereClause<T extends LiveObjectAny> = {
|
|
|
187
187
|
} & {
|
|
188
188
|
[K in keyof T["relations"]]?: WhereClause<T["relations"][K]["entity"]>;
|
|
189
189
|
};
|
|
190
|
+
type IncludeClause<T extends LiveObjectAny> = {
|
|
191
|
+
[K in keyof T["relations"]]?: boolean;
|
|
192
|
+
};
|
|
190
193
|
|
|
191
194
|
type RouteRecord = Record<string, AnyRoute>;
|
|
192
195
|
declare class Router<TRoutes extends RouteRecord> {
|
|
@@ -233,7 +236,7 @@ type AnyRoute = Route<LiveObjectAny, Middleware<any>, Record<string, any>>;
|
|
|
233
236
|
declare abstract class Storage {
|
|
234
237
|
abstract updateSchema(opts: Schema<any>): Promise<void>;
|
|
235
238
|
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>>>;
|
|
239
|
+
abstract find<T extends LiveObjectAny>(resourceName: string, where?: Record<string, any>, include?: Record<string, any>): Promise<Record<string, MaterializedLiveType<T>>>;
|
|
237
240
|
abstract upsert<T extends LiveObjectAny>(resourceName: string, resourceId: string, value: MaterializedLiveType<T>): Promise<MaterializedLiveType<T>>;
|
|
238
241
|
}
|
|
239
242
|
|
|
@@ -245,6 +248,7 @@ type ParsedRequest<TInput = any> = {
|
|
|
245
248
|
procedure?: string;
|
|
246
249
|
context: Record<string, any>;
|
|
247
250
|
where?: Record<string, any>;
|
|
251
|
+
include?: Record<string, any>;
|
|
248
252
|
type: "QUERY" | "MUTATE";
|
|
249
253
|
resourceId?: string;
|
|
250
254
|
input?: TInput;
|
|
@@ -259,6 +263,19 @@ type Simplify<T> = T extends Record<string, any> ? {
|
|
|
259
263
|
[K in keyof T]: Simplify<T[K]>;
|
|
260
264
|
} : T;
|
|
261
265
|
|
|
266
|
+
type DeepSubscribable<T> = {
|
|
267
|
+
[K in keyof T]: DeepSubscribable<T[K]>;
|
|
268
|
+
} & {
|
|
269
|
+
get: () => T;
|
|
270
|
+
subscribe: (callback: (value: T) => void) => () => void;
|
|
271
|
+
};
|
|
272
|
+
|
|
273
|
+
declare const useLiveQuery: <T extends DeepSubscribable<U>, U>(observable: T) => Simplify<ReturnType<T["get"]>>;
|
|
274
|
+
declare const SubscriptionProvider: ({ children, client, }: {
|
|
275
|
+
children: React.ReactNode;
|
|
276
|
+
client: Client<AnyRouter>["client"];
|
|
277
|
+
}) => react_jsx_runtime.JSX.Element;
|
|
278
|
+
|
|
262
279
|
type WebSocketClientEventMap = WebSocketEventMap & {
|
|
263
280
|
connectionChange: {
|
|
264
281
|
open: boolean;
|
|
@@ -298,31 +315,15 @@ declare class WebSocketClient {
|
|
|
298
315
|
private dispatchEvent;
|
|
299
316
|
}
|
|
300
317
|
|
|
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
318
|
type ClientState<TRouter extends AnyRouter> = {
|
|
311
319
|
[K in keyof TRouter["routes"]]: Record<InferIndex<TRouter["routes"][K]["_resourceSchema"]>, InferLiveObject<TRouter["routes"][K]["_resourceSchema"]>> | undefined;
|
|
312
320
|
};
|
|
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
321
|
type Client<TRouter extends AnyRouter> = {
|
|
321
322
|
client: {
|
|
322
323
|
ws: WebSocketClient;
|
|
323
|
-
|
|
324
|
+
subscribe: (resourceType?: string[]) => () => void;
|
|
324
325
|
};
|
|
325
|
-
store:
|
|
326
|
+
store: DeepSubscribable<ClientState<TRouter>> & {
|
|
326
327
|
[K in keyof TRouter["routes"]]: {
|
|
327
328
|
insert: (input: Simplify<LiveObjectMutationInput<TRouter["routes"][K]["_resourceSchema"]>>) => void;
|
|
328
329
|
update: (id: string, value: Omit<Simplify<LiveObjectMutationInput<TRouter["routes"][K]["_resourceSchema"]>>, "id">) => void;
|
|
@@ -333,11 +334,12 @@ type Client<TRouter extends AnyRouter> = {
|
|
|
333
334
|
};
|
|
334
335
|
};
|
|
335
336
|
};
|
|
337
|
+
declare const createClient: <TRouter extends AnyRouter>(opts: ClientOptions) => Client<TRouter>;
|
|
338
|
+
|
|
336
339
|
type ClientOptions = {
|
|
337
340
|
url: string;
|
|
338
341
|
schema: Schema<any>;
|
|
339
342
|
credentials?: Generatable<Awaitable<Record<string, string>>>;
|
|
340
343
|
};
|
|
341
|
-
declare const createClient: <TRouter extends AnyRouter>(opts: ClientOptions) => Client<TRouter>;
|
|
342
344
|
|
|
343
|
-
export { type AnyRouter as A, type ClientOptions as C, type
|
|
345
|
+
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,
|
|
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,I=l.createReference,p=class a extends s{constructor(){super("boolean",e=>typeof e=="string"?e.toLowerCase()==="true":!!e);}static create(){return new a}},w=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=w;exports.createRelations=D;exports.createSchema=k;exports.id=S;exports.inferValue=h;exports.number=O;exports.object=P;exports.reference=I;exports.string=j;exports.timestamp=A;
|
package/dist/index.d.cts
CHANGED
|
@@ -205,5 +205,8 @@ type WhereClause<T extends LiveObjectAny> = {
|
|
|
205
205
|
} & {
|
|
206
206
|
[K in keyof T["relations"]]?: WhereClause<T["relations"][K]["entity"]>;
|
|
207
207
|
};
|
|
208
|
+
type IncludeClause<T extends LiveObjectAny> = {
|
|
209
|
+
[K in keyof T["relations"]]?: boolean;
|
|
210
|
+
};
|
|
208
211
|
|
|
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 };
|
|
212
|
+
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
|
@@ -205,5 +205,8 @@ type WhereClause<T extends LiveObjectAny> = {
|
|
|
205
205
|
} & {
|
|
206
206
|
[K in keyof T["relations"]]?: WhereClause<T["relations"][K]["entity"]>;
|
|
207
207
|
};
|
|
208
|
+
type IncludeClause<T extends LiveObjectAny> = {
|
|
209
|
+
[K in keyof T["relations"]]?: boolean;
|
|
210
|
+
};
|
|
208
211
|
|
|
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 };
|
|
212
|
+
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,
|
|
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,I=l.createReference,p=class a extends s{constructor(){super("boolean",e=>typeof e=="string"?e.toLowerCase()==="true":!!e);}static create(){return new a}},w=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,w as boolean,D as createRelations,k as createSchema,S as id,h as inferValue,O as number,P as object,I as reference,j as string,A as timestamp};
|
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=
|
|
1
|
+
'use strict';var Se=require('qs'),zod=require('zod'),E=require('crypto'),kysely=require('kysely'),postgres=require('kysely/helpers/postgres');function _interopDefault(e){return e&&e.__esModule?e:{default:e}}var Se__default=/*#__PURE__*/_interopDefault(Se);var E__default=/*#__PURE__*/_interopDefault(E);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 $=(r,e,t)=>(t=r!=null?oe(ce(r)):{},le(U(t,"default",{value:r,enumerable:true}),r));var P=de(I=>{Object.defineProperty(I,"__esModule",{value:true});I.parse=Te;I.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 s=r.indexOf(";",o),c=s===-1?n:s;if(i>c){o=r.lastIndexOf(";",i-1)+1;continue}let y=V(r,o,i),p=_(r,i,y),u=r.slice(y,p);if(t[u]===void 0){let m=V(r,i+1,c),l=_(r,c,m),d=a(r.slice(m,l));t[u]=d;}o=c+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 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=$(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}),j=S.omit({id:true,type:true,resource:true,procedure:true}),O=M.omit({id:true,type:true,resource:true});zod.z.union([O,j]);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("/"),s=o.searchParams,c=Se__default.default.parse(s.toString()),y=await((t=r.contextProvider)==null?void 0:t.call(r,{transport:"HTTP",headers:a.headers,cookies:a.cookies,query:c}))??{};if(e.method==="GET"){let p=i[i.length-1],{success:u,data:m,error:l}=Z.safeParse(c);if(!u)return Response.json({message:"Invalid query",code:"INVALID_QUERY",details:l},{status:400});let d=await r.handleRequest({req:{...a,type:"QUERY",resourceName:p,context:y,where:m.where,include:m.include,query:c}});return !d||!d.data?Response.json({message:"Invalid resource",code:"INVALID_RESOURCE"},{status:400}):Response.json(d.data)}if(e.method==="POST")try{let p=i[i.length-1],u=i[i.length-2],m=e.body?await e.json():{},l;if(p==="set"){let{success:b,data:g,error:w}=O.safeParse(m);if(!b)return Response.json({message:"Invalid mutation",code:"INVALID_REQUEST",details:w},{status:400});l=g;}else {let{success:b,data:g,error:w}=j.safeParse(m);if(!b)return Response.json({message:"Invalid mutation",code:"INVALID_REQUEST",details:w},{status:400});l=g;}let d=await r.handleRequest({req:{...a,type:"MUTATE",resourceName:u,input:l.payload,context:y,resourceId:l.resourceId,procedure:p!=="set"?p:void 0,query:{}}});return Response.json(d)}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 X=$(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}),Ie=S.extend({id:T}),Ee=zod.z.union([Ie,B]),Q=zod.z.union([Me,ve,Ee]),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 je=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 Oe(r){let e=Math.floor(r()*v);return e===v&&(e=v-1),K.charAt(e)}function Ne(r){var n;let e=Ce(),t=e&&(e.crypto||e.msCrypto)||(typeof E__default.default<"u"?E__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=E__default.default)!=null&&n.randomBytes)return ()=>E__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=Oe(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=Ne(),n=Date.now();return ke(n,Y)+ze(je,t)}var N=()=>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 s;(s=e[o])==null||s.send(JSON.stringify({...a,id:a.id??N()}));}));}),(n,a)=>{var p;let o=u=>{n.send(JSON.stringify(u));},i=N(),s={headers:a.headers,cookies:typeof a.headers.cookie=="string"?X.default.parse(a.headers.cookie):{}},c=Se.parse(a.url.split("?")[1]),y=(p=r.contextProvider)==null?void 0:p.call(r,{transport:"WEBSOCKET",headers:s.headers,cookies:s.cookies,query:c});e[i]=n,console.log("Client connected:",i),n.on("message",async u=>{try{console.log("Message received from the client:",u);let m=Q.parse(JSON.parse(u.toString()));if(m.type==="SUBSCRIBE"){let{resource:l}=m;t[l]||(t[l]={}),t[l][i]={};}else if(m.type==="SYNC"){let{resources:l}=m,d=l??Object.keys(r.schema);console.log("Syncing resources:",d),await Promise.all(d.map(async b=>{let g=await r.handleRequest({req:{...s,type:"QUERY",resourceName:b,context:await y??{},query:c}});if(!g||!g.data)throw new Error("Invalid resource");o({id:m.id,type:"SYNC",resource:b,data:Object.fromEntries(Object.entries(g.data??{}).map(([w,ae])=>[w,ae.value]))});}));}else if(m.type==="MUTATE"){let{resource:l}=m;console.log("Received mutation from client:",m);try{let d=await r.handleRequest({req:{...s,type:"MUTATE",resourceName:l,input:m.payload,context:{messageId:m.id,...await y??{}},resourceId:m.resourceId,procedure:m.procedure,query:c}});m.procedure&&o({id:m.id,type:"REPLY",data:d});}catch(d){o({id:m.id,type:"REJECT",resource:l,message:d.message}),console.error("Error parsing mutation from the client:",d);}}}catch(m){console.error("Error handling message from the client:",m);}}),n.on("close",()=>{console.log("Connection closed",i),delete e[i];for(let u of Object.values(t))delete u[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 bt=(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(s=>a.status(i.status).send(s)));});};var C=class r{routes;constructor(e){this.routes=e.routes;}static create(e){return new r(e)}},vt=r=>C.create({...r}),Ve=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,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.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:Ve}))}},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}},It=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,n){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(c=>c.name===n);o||await this.db.schema.createTable(n).ifNotExists().execute();let i=`${n}_meta`,s=t.find(c=>c.name===i);s||await this.db.schema.createTable(i).ifNotExists().execute();for(let[c,y]of Object.entries(a.fields)){let p=o==null?void 0:o.columns.find(l=>l.name===c),u=y.getStorageFieldType();p?p.dataType!==u.type&&console.error("Column type mismatch:",c,"expected to have type:",u.type,"but has type:",p.dataType):(await this.db.schema.alterTable(n).addColumn(c,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",c,l),l}),u.index&&await this.db.schema.createIndex(`${n}_${c}_index`).on(n).column(c).execute().catch(l=>{})),(s==null?void 0:s.columns.find(l=>l.name===c))||await this.db.schema.alterTable(i).addColumn(c,"varchar",l=>{let d=l;return u.primary&&(d=d.primaryKey().references(`${n}.${c}`)),d}).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,n){let a=this.applyWhere(e,this.db.selectFrom(e).selectAll(e),t);a=this.applyInclude(e,a,n);let o=await a.execute(),i=Object.fromEntries(o.map(y=>{let{id:p,...u}=y;return [p,u]}));if(Object.keys(i).length===0)return {};let s=Object.fromEntries((await this.db.selectFrom(`${e}_meta`).selectAll().where("id","in",Object.keys(i)).execute()).map(y=>{let{id:p,...u}=y;return [p,u]}));return Object.entries(i).reduce((y,[p,u])=>(s[p]&&(y[p]=this.convertToMaterializedLiveType(u,s[p])),y),{})}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={},s={};for(let[c,y]of Object.entries(n.value))i[c]=y.value,s[c]=y._meta.timestamp;o?await Promise.all([a.updateTable(e).set(i).where("id","=",t).execute(),a.updateTable(`${e}_meta`).set(s).where("id","=",t).execute()]):await Promise.all([a.insertInto(e).values({...i,id:t}).execute(),a.insertInto(`${e}_meta`).values({...s,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 s=a.relations[o],c=s.entity.name,y=s.type==="one"?"id":s.foreignColumn,p=s.type==="one"?s.relationalColumn:"id";t=t.leftJoin(c,`${c}.${y}`,`${e}.${p}`),t=this.applyWhere(c,t,i);}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[o,i]of Object.entries(n)){if(!a.relations[o])throw new Error(`Relation ${o} not found in resource ${e}`);let s=a.relations[o],c=s.entity.name,y=s.type==="one"?"id":s.foreignColumn,p=s.type==="one"?s.relationalColumn:"id";t=t.select(u=>postgres.jsonArrayFrom(u.selectFrom(c).selectAll(c).whereRef(`${c}.${y}`,"=",`${e}.${p}`)).as(o));}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=bt;exports.routeFactory=It;exports.router=vt;exports.server=Nt;
|
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, WhereClause, IncludeClause } from './index.cjs';
|
|
3
3
|
import { PostgresPool } from 'kysely';
|
|
4
4
|
import { Application } from 'express-ws';
|
|
5
5
|
|
|
@@ -147,14 +147,14 @@ type AnyRoute = Route<LiveObjectAny, Middleware<any>, Record<string, any>>;
|
|
|
147
147
|
declare abstract class Storage {
|
|
148
148
|
abstract updateSchema(opts: Schema<any>): Promise<void>;
|
|
149
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>>>;
|
|
150
|
+
abstract find<T extends LiveObjectAny>(resourceName: string, where?: Record<string, any>, include?: Record<string, any>): Promise<Record<string, MaterializedLiveType<T>>>;
|
|
151
151
|
abstract upsert<T extends LiveObjectAny>(resourceName: string, resourceId: string, value: MaterializedLiveType<T>): Promise<MaterializedLiveType<T>>;
|
|
152
152
|
}
|
|
153
153
|
declare class InMemoryStorage extends Storage {
|
|
154
154
|
private storage;
|
|
155
155
|
updateSchema(opts: Schema<any>): Promise<void>;
|
|
156
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>>>;
|
|
157
|
+
find<T extends LiveObjectAny>(resourceName: string, where?: Record<string, any>, include?: Record<string, any>): Promise<Record<string, MaterializedLiveType<T>>>;
|
|
158
158
|
upsert<T extends LiveObjectAny>(resourceName: string, resourceId: string, value: MaterializedLiveType<T>): Promise<MaterializedLiveType<T>>;
|
|
159
159
|
}
|
|
160
160
|
declare class SQLStorage extends Storage {
|
|
@@ -163,10 +163,11 @@ declare class SQLStorage extends Storage {
|
|
|
163
163
|
constructor(pool: PostgresPool);
|
|
164
164
|
updateSchema(opts: Schema<any>): Promise<void>;
|
|
165
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>>>;
|
|
166
|
+
find<T extends LiveObjectAny>(resourceName: string, where?: WhereClause<T>, include?: IncludeClause<T>): Promise<Record<string, MaterializedLiveType<T>>>;
|
|
167
167
|
upsert<T extends LiveObjectAny>(resourceName: string, resourceId: string, value: MaterializedLiveType<T>): Promise<MaterializedLiveType<T>>;
|
|
168
168
|
private convertToMaterializedLiveType;
|
|
169
169
|
private applyWhere;
|
|
170
|
+
private applyInclude;
|
|
170
171
|
}
|
|
171
172
|
|
|
172
173
|
declare const expressAdapter: (app: Application, server: Server<AnyRouter>, options?: {
|
|
@@ -181,6 +182,7 @@ type ParsedRequest<TInput = any> = {
|
|
|
181
182
|
procedure?: string;
|
|
182
183
|
context: Record<string, any>;
|
|
183
184
|
where?: Record<string, any>;
|
|
185
|
+
include?: Record<string, any>;
|
|
184
186
|
type: "QUERY" | "MUTATE";
|
|
185
187
|
resourceId?: string;
|
|
186
188
|
input?: TInput;
|
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, WhereClause, IncludeClause } from './index.js';
|
|
3
3
|
import { PostgresPool } from 'kysely';
|
|
4
4
|
import { Application } from 'express-ws';
|
|
5
5
|
|
|
@@ -147,14 +147,14 @@ type AnyRoute = Route<LiveObjectAny, Middleware<any>, Record<string, any>>;
|
|
|
147
147
|
declare abstract class Storage {
|
|
148
148
|
abstract updateSchema(opts: Schema<any>): Promise<void>;
|
|
149
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>>>;
|
|
150
|
+
abstract find<T extends LiveObjectAny>(resourceName: string, where?: Record<string, any>, include?: Record<string, any>): Promise<Record<string, MaterializedLiveType<T>>>;
|
|
151
151
|
abstract upsert<T extends LiveObjectAny>(resourceName: string, resourceId: string, value: MaterializedLiveType<T>): Promise<MaterializedLiveType<T>>;
|
|
152
152
|
}
|
|
153
153
|
declare class InMemoryStorage extends Storage {
|
|
154
154
|
private storage;
|
|
155
155
|
updateSchema(opts: Schema<any>): Promise<void>;
|
|
156
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>>>;
|
|
157
|
+
find<T extends LiveObjectAny>(resourceName: string, where?: Record<string, any>, include?: Record<string, any>): Promise<Record<string, MaterializedLiveType<T>>>;
|
|
158
158
|
upsert<T extends LiveObjectAny>(resourceName: string, resourceId: string, value: MaterializedLiveType<T>): Promise<MaterializedLiveType<T>>;
|
|
159
159
|
}
|
|
160
160
|
declare class SQLStorage extends Storage {
|
|
@@ -163,10 +163,11 @@ declare class SQLStorage extends Storage {
|
|
|
163
163
|
constructor(pool: PostgresPool);
|
|
164
164
|
updateSchema(opts: Schema<any>): Promise<void>;
|
|
165
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>>>;
|
|
166
|
+
find<T extends LiveObjectAny>(resourceName: string, where?: WhereClause<T>, include?: IncludeClause<T>): Promise<Record<string, MaterializedLiveType<T>>>;
|
|
167
167
|
upsert<T extends LiveObjectAny>(resourceName: string, resourceId: string, value: MaterializedLiveType<T>): Promise<MaterializedLiveType<T>>;
|
|
168
168
|
private convertToMaterializedLiveType;
|
|
169
169
|
private applyWhere;
|
|
170
|
+
private applyInclude;
|
|
170
171
|
}
|
|
171
172
|
|
|
172
173
|
declare const expressAdapter: (app: Application, server: Server<AnyRouter>, options?: {
|
|
@@ -181,6 +182,7 @@ type ParsedRequest<TInput = any> = {
|
|
|
181
182
|
procedure?: string;
|
|
182
183
|
context: Record<string, any>;
|
|
183
184
|
where?: Record<string, any>;
|
|
185
|
+
include?: Record<string, any>;
|
|
184
186
|
type: "QUERY" | "MUTATE";
|
|
185
187
|
resourceId?: string;
|
|
186
188
|
input?: TInput;
|
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,
|
|
1
|
+
import {a,b}from'./chunk-EK7ODJWE.js';import he,{parse}from'qs';import {z as z$1}from'zod';import E from'node:crypto';import {Kysely,PostgresDialect}from'kysely';import {jsonArrayFrom}from'kysely/helpers/postgres';var P=a(I=>{Object.defineProperty(I,"__esModule",{value:true});I.parse=le;I.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 s=r.indexOf(";",o),c=s===-1?n:s;if(i>c){o=r.lastIndexOf(";",i-1)+1;continue}let y=$(r,o,i),p=V(r,i,y),u=r.slice(y,p);if(t[u]===void 0){let m=$(r,i+1,c),l=V(r,c,m),d=a(r.slice(m,l));t[u]=d;}o=c+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 V(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}),j=S.omit({id:true,type:true,resource:true,procedure:true}),O=M.omit({id:true,type:true,resource:true});z$1.union([O,j]);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("/"),s=o.searchParams,c=he.parse(s.toString()),y=await((t=r.contextProvider)==null?void 0:t.call(r,{transport:"HTTP",headers:a.headers,cookies:a.cookies,query:c}))??{};if(e.method==="GET"){let p=i[i.length-1],{success:u,data:m,error:l}=F.safeParse(c);if(!u)return Response.json({message:"Invalid query",code:"INVALID_QUERY",details:l},{status:400});let d=await r.handleRequest({req:{...a,type:"QUERY",resourceName:p,context:y,where:m.where,include:m.include,query:c}});return !d||!d.data?Response.json({message:"Invalid resource",code:"INVALID_RESOURCE"},{status:400}):Response.json(d.data)}if(e.method==="POST")try{let p=i[i.length-1],u=i[i.length-2],m=e.body?await e.json():{},l;if(p==="set"){let{success:b,data:g,error:w}=O.safeParse(m);if(!b)return Response.json({message:"Invalid mutation",code:"INVALID_REQUEST",details:w},{status:400});l=g;}else {let{success:b,data:g,error:w}=j.safeParse(m);if(!b)return Response.json({message:"Invalid mutation",code:"INVALID_REQUEST",details:w},{status:400});l=g;}let d=await r.handleRequest({req:{...a,type:"MUTATE",resourceName:u,input:l.payload,context:y,resourceId:l.resourceId,procedure:p!=="set"?p:void 0,query:{}}});return Response.json(d)}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 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 Ie(r){var n;let e=Ee(),t=e&&(e.crypto||e.msCrypto)||(typeof E<"u"?E: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=E)!=null&&n.randomBytes)return ()=>E.randomBytes(1).readUInt8()/255;throw new x(R.PRNGDetectFailure,"Failed to find a reliable PRNG")}function Ee(){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=Ie(),n=Date.now();return Pe(n,K)+Ae(Me,t)}var N=()=>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 s;(s=e[o])==null||s.send(JSON.stringify({...a,id:a.id??N()}));}));}),(n,a)=>{var p;let o=u=>{n.send(JSON.stringify(u));},i=N(),s={headers:a.headers,cookies:typeof a.headers.cookie=="string"?J.default.parse(a.headers.cookie):{}},c=parse(a.url.split("?")[1]),y=(p=r.contextProvider)==null?void 0:p.call(r,{transport:"WEBSOCKET",headers:s.headers,cookies:s.cookies,query:c});e[i]=n,console.log("Client connected:",i),n.on("message",async u=>{try{console.log("Message received from the client:",u);let m=B.parse(JSON.parse(u.toString()));if(m.type==="SUBSCRIBE"){let{resource:l}=m;t[l]||(t[l]={}),t[l][i]={};}else if(m.type==="SYNC"){let{resources:l}=m,d=l??Object.keys(r.schema);console.log("Syncing resources:",d),await Promise.all(d.map(async b=>{let g=await r.handleRequest({req:{...s,type:"QUERY",resourceName:b,context:await y??{},query:c}});if(!g||!g.data)throw new Error("Invalid resource");o({id:m.id,type:"SYNC",resource:b,data:Object.fromEntries(Object.entries(g.data??{}).map(([w,ne])=>[w,ne.value]))});}));}else if(m.type==="MUTATE"){let{resource:l}=m;console.log("Received mutation from client:",m);try{let d=await r.handleRequest({req:{...s,type:"MUTATE",resourceName:l,input:m.payload,context:{messageId:m.id,...await y??{}},resourceId:m.resourceId,procedure:m.procedure,query:c}});m.procedure&&o({id:m.id,type:"REPLY",data:d});}catch(d){o({id:m.id,type:"REJECT",resource:l,message:d.message}),console.error("Error parsing mutation from the client:",d);}}}catch(m){console.error("Error handling message from the client:",m);}}),n.on("close",()=>{console.log("Connection closed",i),delete e[i];for(let u of Object.values(t))delete u[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 mt=(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(s=>a.status(i.status).send(s)));});};var C=class r{routes;constructor(e){this.routes=e.routes;}static create(e){return new r(e)}},gt=r=>C.create({...r}),Ne=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,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.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:Ne}))}},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}},Tt=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,n){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(c=>c.name===n);o||await this.db.schema.createTable(n).ifNotExists().execute();let i=`${n}_meta`,s=t.find(c=>c.name===i);s||await this.db.schema.createTable(i).ifNotExists().execute();for(let[c,y]of Object.entries(a.fields)){let p=o==null?void 0:o.columns.find(l=>l.name===c),u=y.getStorageFieldType();p?p.dataType!==u.type&&console.error("Column type mismatch:",c,"expected to have type:",u.type,"but has type:",p.dataType):(await this.db.schema.alterTable(n).addColumn(c,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",c,l),l}),u.index&&await this.db.schema.createIndex(`${n}_${c}_index`).on(n).column(c).execute().catch(l=>{})),(s==null?void 0:s.columns.find(l=>l.name===c))||await this.db.schema.alterTable(i).addColumn(c,"varchar",l=>{let d=l;return u.primary&&(d=d.primaryKey().references(`${n}.${c}`)),d}).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,n){let a=this.applyWhere(e,this.db.selectFrom(e).selectAll(e),t);a=this.applyInclude(e,a,n);let o=await a.execute(),i=Object.fromEntries(o.map(y=>{let{id:p,...u}=y;return [p,u]}));if(Object.keys(i).length===0)return {};let s=Object.fromEntries((await this.db.selectFrom(`${e}_meta`).selectAll().where("id","in",Object.keys(i)).execute()).map(y=>{let{id:p,...u}=y;return [p,u]}));return Object.entries(i).reduce((y,[p,u])=>(s[p]&&(y[p]=this.convertToMaterializedLiveType(u,s[p])),y),{})}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={},s={};for(let[c,y]of Object.entries(n.value))i[c]=y.value,s[c]=y._meta.timestamp;o?await Promise.all([a.updateTable(e).set(i).where("id","=",t).execute(),a.updateTable(`${e}_meta`).set(s).where("id","=",t).execute()]):await Promise.all([a.insertInto(e).values({...i,id:t}).execute(),a.insertInto(`${e}_meta`).values({...s,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 s=a.relations[o],c=s.entity.name,y=s.type==="one"?"id":s.foreignColumn,p=s.type==="one"?s.relationalColumn:"id";t=t.leftJoin(c,`${c}.${y}`,`${e}.${p}`),t=this.applyWhere(c,t,i);}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[o,i]of Object.entries(n)){if(!a.relations[o])throw new Error(`Relation ${o} not found in resource ${e}`);let s=a.relations[o],c=s.entity.name,y=s.type==="one"?"id":s.foreignColumn,p=s.type==="one"?s.relationalColumn:"id";t=t.select(u=>jsonArrayFrom(u.selectFrom(c).selectAll(c).whereRef(`${c}.${y}`,"=",`${e}.${p}`)).as(o));}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}},vt=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,mt as expressAdapter,Tt as routeFactory,gt as router,vt as server};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@live-state/sync",
|
|
3
|
-
"version": "0.0.1
|
|
3
|
+
"version": "0.0.1",
|
|
4
4
|
"private": false,
|
|
5
5
|
"publishConfig": {
|
|
6
6
|
"access": "public"
|
|
@@ -35,6 +35,7 @@
|
|
|
35
35
|
"@repo/typescript-config": "0.0.0"
|
|
36
36
|
},
|
|
37
37
|
"dependencies": {
|
|
38
|
+
"idb": "^8.0.3",
|
|
38
39
|
"kysely": "^0.28.2",
|
|
39
40
|
"qs": "^6.14.0",
|
|
40
41
|
"ws": "^8.18.0",
|
|
@@ -75,8 +76,8 @@
|
|
|
75
76
|
}
|
|
76
77
|
},
|
|
77
78
|
"scripts": {
|
|
78
|
-
"build": "tsup",
|
|
79
|
-
"dev": "tsup --watch",
|
|
79
|
+
"build": "NODE_ENV=production tsup",
|
|
80
|
+
"dev": "NODE_ENV=development tsup --watch",
|
|
80
81
|
"lint": "eslint src/",
|
|
81
82
|
"typecheck": "tsc --noEmit"
|
|
82
83
|
}
|