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