@live-state/sync 0.0.4-beta.1 → 0.0.4-beta.3
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-LLHCJUB6.js → chunk-GNY3ZGE5.js} +1 -1
- package/dist/chunk-RWM6TXG7.js +1 -0
- package/dist/client.d.ts +3 -1
- package/dist/client.js +1 -1
- package/dist/fetch-client.d.ts +5 -2
- package/dist/fetch-client.js +1 -1
- package/dist/{index-C1FzxdnB.d.ts → index-ClRyEBhW.d.ts} +49 -17
- package/dist/index.cjs +1 -1
- package/dist/index.d.cts +3 -1
- package/dist/index.d.ts +3 -1
- package/dist/index.js +1 -1
- package/dist/server.cjs +2 -2
- package/dist/server.d.cts +59 -27
- package/dist/server.d.ts +59 -27
- package/dist/server.js +2 -2
- package/package.json +1 -1
- package/dist/chunk-G5SVBG4B.js +0 -1
|
@@ -1 +1 @@
|
|
|
1
|
-
var
|
|
1
|
+
var R=Object.create;var h=Object.defineProperty;var g=Object.getOwnPropertyDescriptor;var b=Object.getOwnPropertyNames;var I=Object.getPrototypeOf,M=Object.prototype.hasOwnProperty;var j=(t,e)=>()=>(e||t((e={exports:{}}).exports,e),e.exports);var O=(t,e,n,i)=>{if(e&&typeof e=="object"||typeof e=="function")for(let a of b(e))!M.call(t,a)&&a!==n&&h(t,a,{get:()=>e[a],enumerable:!(i=g(e,a))||i.enumerable});return t};var S=(t,e,n)=>(n=t!=null?R(I(t)):{},O(e||!t||!t.__esModule?h(n,"default",{value:t,enumerable:true}):n,t));var s=class{_value;_meta;_encodeInput;_decodeInput};var c=class extends s{inner;constructor(e){super(),this.inner=e;}encodeMutation(e,n,i){return this.inner.encodeMutation(e,n,i)}mergeMutation(e,n,i){return this.inner.mergeMutation(e,n,i)}getStorageFieldType(){return {...this.inner.getStorageFieldType(),nullable:true}}},o=class t extends s{storageType;convertFunc;isIndex;isUnique;defaultValue;foreignReference;isPrimary;constructor(e,n,i,a,r,u,y){super(),this.storageType=e,this.convertFunc=n,this.isIndex=i??false,this.isUnique=a??false,this.defaultValue=r,this.foreignReference=u,this.isPrimary=y??false;}encodeMutation(e,n,i){return {value:n,_meta:{timestamp:i}}}mergeMutation(e,n,i){return i&&i._meta.timestamp&&n._meta.timestamp&&i._meta.timestamp.localeCompare(n._meta.timestamp)>=0?[i,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)}nullable(){return new c(this)}},p=class t extends o{constructor(){super("integer",e=>Number(e));}static create(){return new t}},_=p.create,l=class t extends o{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)}},V=l.create,E=l.createId,C=l.createReference,d=class t extends o{constructor(){super("boolean",e=>typeof e=="string"?e.toLowerCase()==="true":!!e);}static create(){return new t}},F=d.create,m=class t extends o{constructor(){super("timestamp",e=>typeof e=="string"?new Date(e):e);}static create(){return new t}},q=m.create;var v=class t extends s{name;fields;relations;constructor(e,n,i){super(),this.name=e,this.fields=n,this.relations=i??{};}encodeMutation(e,n,i){return Object.fromEntries(Object.entries(n).map(([a,r])=>[a,(this.fields[a]??this.relations[a]).encodeMutation("set",r,i)]))}mergeMutation(e,n,i){let a={};return [{value:{...(i==null?void 0:i.value)??{},...Object.fromEntries(Object.entries(n).map(([r,u])=>{let y=this.fields[r]??this.relations[r];if(!y)return [r,u];let[L,f]=y.mergeMutation(e,u,i==null?void 0:i.value[r]);return f&&(a[r]=f),[r,L]}))}},a]}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)}},$=v.create,T=class t extends s{entity;type;required;relationalColumn;foreignColumn;sourceEntity;constructor(e,n,i,a,r){super(),this.entity=e,this.type=n,this.required=r??false,this.relationalColumn=i,this.foreignColumn=a;}encodeMutation(e,n,i){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:i}}}mergeMutation(e,n,i){if(this.type==="many")throw new Error("Many not implemented.");return i&&i._meta.timestamp&&n._meta.timestamp&&i._meta.timestamp.localeCompare(n._meta.timestamp)>=0?[i,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,i)=>new t(e,"one",n,void 0,i??false)}static createManyFactory(){return (e,n,i)=>new t(e,"many",void 0,n,i??false)}},D=(t,e)=>({$type:"relations",objectName:t.name,relations:e({one:T.createOneFactory(),many:T.createManyFactory()})}),x=t=>{if(t)return Array.isArray(t.value)?t.value.map(e=>x(e)):typeof t.value!="object"||t.value===null||t.value instanceof Date?t.value:Object.fromEntries(Object.entries(t.value).map(([e,n])=>[e,x(n)]))},k=t=>Object.fromEntries(Object.entries(t).flatMap(([e,n])=>{if(n.$type==="relations")return [];let i=n,a=Object.values(t).find(r=>r.$type==="relations"&&r.objectName===n.name);return a&&(i=i.setRelations(a.relations)),[[i.name,i]]}));export{j as a,S as b,s as c,p as d,_ as e,l as f,V as g,E as h,C as i,d as j,F as k,m as l,q as m,v as n,$ as o,T as p,D as q,x as r,k as s};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
var x="0123456789ABCDEFGHJKMNPQRSTVWXYZ";var l;(function(e){e.Base32IncorrectEncoding="B32_ENC_INVALID",e.DecodeTimeInvalidCharacter="DEC_TIME_CHAR",e.DecodeTimeValueMalformed="DEC_TIME_MALFORMED",e.EncodeTimeNegative="ENC_TIME_NEG",e.EncodeTimeSizeExceeded="ENC_TIME_SIZE_EXCEED",e.EncodeTimeValueMalformed="ENC_TIME_MALFORMED",e.PRNGDetectFailure="PRNG_DETECT",e.ULIDInvalid="ULID_INVALID",e.Unexpected="UNEXPECTED",e.UUIDInvalid="UUID_INVALID";})(l||(l={}));var u=class extends Error{constructor(t,n){super(`${n} (${t})`),this.name="ULIDError",this.code=t;}};function R(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=R(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 L(e,t){let n=I(),i=Date.now();return _(i,10)+E(16,n)}var j=()=>L().toLowerCase(),S=(e,t)=>typeof e=="function"?e(t):e;var O=(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]=O(typeof o[s]=="object"?o[s]:()=>{},t,[...n,a])),o[s]},apply:(i,a,r)=>{var o;return (o=t.apply)==null?void 0:o.call(t,i,n,r)}}),C=(e,t,n)=>{let i=[],a=0;for(let r=0;r<e.length&&(n===void 0||a<n);r++)t(e[r],r)&&(i.push(e[r]),a++);return i};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{j as a,S as b,O as c,C as d,b as e};
|
package/dist/client.d.ts
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
-
export {
|
|
1
|
+
export { g as Client, f as ClientEvents, C as ClientOptions, e as ConnectionStateChangeEvent, M as MessageReceivedEvent, d as SubscriptionProvider, h as createClient, u as useLiveQuery } from './index-ClRyEBhW.js';
|
|
2
2
|
import 'react/jsx-runtime';
|
|
3
3
|
import 'zod';
|
|
4
|
+
import 'zod/v3';
|
|
5
|
+
import 'zod/v4/core';
|
package/dist/client.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import {c,a,f,d,e,b}from'./chunk-G5SVBG4B.js';import {useSyncExternalStore,useEffect}from'react';import {xxHash32}from'js-xxhash';import {jsx,Fragment}from'react/jsx-runtime';import {z}from'zod';import {stringify}from'qs';import ae from'fast-deep-equal';import {openDB}from'idb';var m=d=>xxHash32(JSON.stringify(d)).toString(32);var k=class{subscriptions=new Map;getOrStoreSubscription(e){let t=m(e);return this.subscriptions.has(t)?this.subscriptions.get(t).subscribe:(this.subscriptions.set(t,{subscribe:i=>{var n;(n=this.subscriptions.get(t))==null||n.callbacks.add(i);let s=e.subscribe(()=>{var r;(r=this.subscriptions.get(t))==null||r.callbacks.forEach(o=>{o();});});return ()=>{var r;(r=this.subscriptions.get(t))==null||r.callbacks.delete(i),setTimeout(()=>{var o;((o=this.subscriptions.get(t))==null?void 0:o.callbacks.size)===0&&(this.subscriptions.delete(t),s());},10);}},callbacks:new Set}),this.subscriptions.get(t).subscribe)}},F=new k,he=d=>useSyncExternalStore(F.getOrStoreSubscription(d),d.get),pe=({children:d,client:e})=>(useEffect(()=>{e.subscribe();},[e.subscribe]),jsx(Fragment,{children:d}));var N=z.object({resource:z.string(),where:z.record(z.string(),z.any()).optional(),include:z.record(z.string(),z.any()).optional(),lastSyncedAt:z.string().optional(),limit:z.number().optional(),sort:z.array(z.object({key:z.string(),direction:z.enum(["asc","desc"])})).optional()}),x=z.record(z.string(),z.object({value:z.string().or(z.number()).or(z.boolean()).or(z.date()).nullable(),_meta:z.object({timestamp:z.string().optional().nullable()}).optional()})).superRefine((d,e)=>{d.id&&e.addIssue({code:z.ZodIssueCode.custom,message:"Payload cannot have an id"});}),H=z.object({id:z.string().optional(),type:z.literal("MUTATE"),resource:z.string()}),_=H.extend({procedure:z.string(),payload:z.any().optional()}),L=H.extend({resourceId:z.string(),payload:x});z.union([_,L]);var v=z.string(),ee=z.object({id:v,type:z.literal("SUBSCRIBE"),resource:z.string()}),te=N.extend({id:v,type:z.literal("QUERY")}),q=L.extend({id:v}),ie=_.extend({id:v}),se=z.union([ie,q]);z.union([ee,te,se]);var ne=z.object({id:v,type:z.literal("REJECT"),resource:z.string(),message:z.string().optional()}),re=z.object({id:v,type:z.literal("REPLY"),data:z.any()}),K=z.union([ne,re,q]),U=z.object({resource:z.string(),data:z.record(z.string(),x)});var M=class d{_collection;_client;_where;_include;_limit;_single;_sort;constructor(e,t,i,s,n,r,o){this._collection=e,this._client=t,this._where=i??{},this._include=s??{},this._limit=n,this._single=r,this._sort=o,this.get=this.get.bind(this),this.subscribe=this.subscribe.bind(this);}where(e){return new d(this._collection,this._client,{...this._where,...e},this._include,this._limit,this._single,this._sort)}include(e){return new d(this._collection,this._client,this._where,{...this._include,...e},this._limit,this._single,this._sort)}get(){let e=this._client.get({resource:this._collection.name,where:this._where,include:this._include,limit:this._limit,sort:this._sort});return this._single?e[0]:e}subscribe(e){return this._client.subscribe({resource:this._collection.name,where:this._where,include:this._include,limit:this._limit,sort:this._sort},t=>{if(this._single)return e(t[0]);e(t);})}limit(e){return new d(this._collection,this._client,this._where,this._include,e,this._single,this._sort)}one(e){return this.first({id:e})}first(e){return new d(this._collection,this._client,e??this._where,this._include,1,true,this._sort)}orderBy(e,t="asc"){let i=[...this._sort??[],{key:e,direction:t}];return new d(this._collection,this._client,this._where,this._include,this._limit,this._single,i)}toJSON(){return {resource:this._collection.name,where:this._where,include:this._include,limit:this._limit,sort:this._sort}}static _init(e,t){return new d(e,t)}};var E=class{ws=null;url;autoConnect;autoReconnect;reconnectTimeout;reconnectLimit;reconnectAttempts=0;eventListeners=new Map;reconnectTimer=null;intentionallyDisconnected=false;credentials;constructor(e){this.url=e.url,this.autoConnect=e.autoConnect??false,this.autoReconnect=e.autoReconnect??false,this.reconnectTimeout=e.reconnectTimeout??5e3,this.reconnectLimit=e.reconnectLimit,this.credentials=e.credentials,this.autoConnect&&this.connect();}connected(){var e;return ((e=this.ws)==null?void 0:e.readyState)===WebSocket.OPEN}async connect(){if(this.ws&&(this.ws.readyState===WebSocket.OPEN||this.ws.readyState===WebSocket.CONNECTING))return;this.intentionallyDisconnected=false;let e=await b(this.credentials);this.ws=new WebSocket(this.url+(e?`?${stringify(e)}`:"")),this.ws.addEventListener("open",this.handleOpen.bind(this)),this.ws.addEventListener("close",this.handleClose.bind(this)),this.ws.addEventListener("error",this.handleError.bind(this)),this.ws.addEventListener("message",this.handleMessage.bind(this));}disconnect(){this.reconnectTimer&&(clearTimeout(this.reconnectTimer),this.reconnectTimer=null),this.intentionallyDisconnected=true,this.ws&&(this.ws.close(),this.ws=null);}addEventListener(e,t){var i;this.eventListeners.has(e)||this.eventListeners.set(e,new Set),(i=this.eventListeners.get(e))==null||i.add(t);}removeEventListener(e,t){var i;this.eventListeners.has(e)&&((i=this.eventListeners.get(e))==null||i.delete(t));}send(e){if(this.ws&&this.ws.readyState===WebSocket.OPEN)this.ws.send(e);else throw new Error("WebSocket is not open")}handleOpen(e){this.reconnectAttempts=0,this.dispatchEvent("open",e),this.dispatchEvent("connectionChange",{open:true});}handleClose(e){this.dispatchEvent("close",e),this.dispatchEvent("connectionChange",{open:false}),this.autoReconnect&&!this.intentionallyDisconnected&&this.scheduleReconnect();}handleError(e){var t,i;this.dispatchEvent("error",e),this.dispatchEvent("connectionChange",{open:false}),(i=(t=e.error)==null?void 0:t.message)!=null&&i.includes("non-101")&&(this.ws&&(this.ws.close(),this.ws=null),this.autoReconnect&&!this.intentionallyDisconnected&&this.scheduleReconnect());}handleMessage(e){this.dispatchEvent("message",e);}scheduleReconnect(){this.reconnectLimit&&this.reconnectAttempts>=this.reconnectLimit||(this.reconnectAttempts++,this.reconnectTimer=setTimeout(()=>{this.connect();},this.reconnectTimeout));}dispatchEvent(e,t){var i;(i=this.eventListeners.get(e))==null||i.forEach(s=>{s(t);});}};var T=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(n=>[n,new Set])),references:new Map,subscriptions:new Set};return this.nodes.set(e,s),s}getNode(e){return this.nodes.get(e)}hasNode(e){return this.nodes.has(e)}createLink(e,t){let i=this.nodes.get(e),s=this.nodes.get(t);if(!i)throw new Error(`Source node with id ${e} does not exist`);if(!s)throw new Error(`Target node with id ${t} does not exist`);i.references.set(s.type,t);let n=s.referencedBy.get(i.type);n&&n instanceof Set?n.add(e):s.referencedBy.set(i.type,e),this.notifySubscribers(t);}removeLink(e,t){let i=this.nodes.get(e);if(!i)throw new Error(`Node with id ${e} does not exist`);let s=i.references.get(t);if(!s)return;i.references.delete(t);let n=this.nodes.get(s);if(!n)return;let r=n.referencedBy.get(i.type);r&&(r instanceof Set?r.delete(e):n.referencedBy.delete(i.type),this.notifySubscribers(s)),this.notifySubscribers(e);}subscribe(e,t){let i=this.nodes.get(e);if(!i)throw new Error(`Node with id ${e} does not exist`);return i.subscriptions.add(t),()=>{i.subscriptions.delete(t);}}removeNode(e){let t=this.nodes.get(e);t&&(Array.from(t.referencedBy.entries()).forEach(([i,s])=>{(s instanceof Set?Array.from(s.values()):[s]).forEach(r=>{let o=this.nodes.get(r);!o||!o.references.get(i)||(o.references.delete(i),this.notifySubscribers(r));});}),this.nodes.delete(e));}updateNode(e,t){let i=this.nodes.get(e);if(!i)throw new Error(`Node with id ${e} does not exist`);t(i),this.notifySubscribers(e);}notifySubscribers(e){let t=this.nodes.get(e);t&&Array.from(t.subscriptions).forEach(i=>{try{i(e);}catch(s){console.error(`Error in node subscription for node ${e}:`,s);}});}getAllNodes(){return Array.from(this.nodes.values())}};var A="__meta",C="databases",R=class{db;async init(e,t){var y,p;if(typeof window>"u")return;let s=((y=(await window.indexedDB.databases()).find(l=>l.name===t))==null?void 0:y.version)??1,n=await m(e),r=Object.fromEntries(await Promise.all(Object.entries(e).map(async([l,u])=>[l,await m(u)]))),o=await openDB("live-state-databases",1,{upgrade(l){l.objectStoreNames.contains(C)||l.createObjectStore(C);}}),c=(p=await this.getAll(o,C))==null?void 0:p[t];(c==null?void 0:c.schemaHash)!==n&&s++,this.db=await openDB(t,s,{async upgrade(l){[...Object.keys(e),A].forEach(u=>{(c==null?void 0:c.objectHashes[u])!==r[u]&&l.objectStoreNames.contains(u)&&l.deleteObjectStore(u),l.objectStoreNames.contains(u)||l.createObjectStore(u);}),await o.put(C,{schemaHash:n,objectHashes:r},t);},blocking(){window.location.reload();},blocked(){window.location.reload();}});}async get(e){return await this.getAll(this.db,e)??{}}getOne(e,t){return this.db?this.db.get(e,t):new Promise(i=>i(void 0))}set(e,t,i){var s;return (s=this.db)==null?void 0:s.put(e,i,t)}delete(e,t){var i;return (i=this.db)==null?void 0:i.delete(e,t)}getMeta(e){return this.db?this.db.get(A,e):new Promise(t=>t(void 0))}setMeta(e,t){var i;return (i=this.db)==null?void 0:i.put(A,t,e)}async getAll(e,t){if(!e)return;if(e.getAllRecords)return e.getAllRecords(t);let[i,s]=await Promise.all([e.getAll(t),e.getAllKeys(t)]);return Object.fromEntries(i.map((n,r)=>[s[r],n]))}};var O=class{constructor(e,t,i){this.schema=e;this.kvStorage=new R,t!==false&&this.kvStorage.init(this.schema,t.name).then(()=>{this.kvStorage.getMeta("mutationStack").then(s=>{!s||Object.keys(s).length===0||(this.optimisticMutationStack=s,i==null||i(this.optimisticMutationStack));}).then(()=>{Object.entries(this.schema).forEach(([s,n])=>{this.kvStorage.get(s).then(r=>{!r||Object.keys(r).length===0||this.loadConsolidatedState(s,r);});});}).catch(s=>{console.error("Failed to load state from storage",s);});});}rawObjPool={};optimisticMutationStack={};optimisticObjGraph=new T;optimisticRawObjPool={};collectionSubscriptions=new Map;querySnapshots={};kvStorage;get(e$1,t,i=false){var r;let s=t??m(e$1);if(this.querySnapshots[s]&&!i){let o=this.querySnapshots[s];if(o)return o}let n=((r=e$1.where)!=null&&r.id?[e$1.where.id]:Object.keys(this.optimisticRawObjPool[e$1.resource]??{})).flatMap(o=>{let c=f(this.materializeOneWithInclude(o,e$1.include));return c?[c]:[]});if(e$1.sort&&e$1.sort.length>0){let o=(c,y)=>{for(let p of e$1.sort){let l=c[p.key],u=y[p.key];if(l<u)return p.direction==="asc"?-1:1;if(l>u)return p.direction==="asc"?1:-1}return 0};n.sort(o);}if(e$1.where||e$1.limit){let o=e$1.where?c=>d(c,e$1.where):()=>true;n=e(n,o,e$1.limit);}return i||(this.querySnapshots[s]=n),n}subscribe(e,t){var r;let i=m(e),s=this.collectionSubscriptions.get(i),n=this.schema[e.resource];return s||this.collectionSubscriptions.set(i,{callbacks:new Set,query:e,flatInclude:e.include?Object.keys(e.include).map(o=>n.relations[o].entity.name):void 0}),(r=this.collectionSubscriptions.get(i))==null||r.callbacks.add(t),()=>{var o,c;(o=this.collectionSubscriptions.get(i))==null||o.callbacks.delete(t),((c=this.collectionSubscriptions.get(i))==null?void 0:c.callbacks.size)===0&&(this.collectionSubscriptions.delete(i),delete this.querySnapshots[i]);}}addMutation(e,t,i=false){var c,y,p,l;let s=this.schema[e];if(console.log("Adding mutation",t),!s)throw new Error("Schema not found");let n=(c=this.optimisticRawObjPool[e])==null?void 0:c[t.resourceId];if(i)this.optimisticMutationStack[e]??=[],this.optimisticMutationStack[e].push(t);else {this.optimisticMutationStack[e]=((p=(y=this.optimisticMutationStack)==null?void 0:y[e])==null?void 0:p.filter(b=>b.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 f=u.value;delete f.id,this.kvStorage.set(e,t.resourceId,f);}this.kvStorage.setMeta("mutationStack",this.optimisticMutationStack);let r=(l=this.rawObjPool[e])==null?void 0:l[t.resourceId],o=(this.optimisticMutationStack[e]??[]).reduce((u,f)=>f.resourceId!==t.resourceId?u:this.schema[e].mergeMutation("set",f.payload,u)[0],r);if(o&&(this.optimisticRawObjPool[e]??={},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(([f,b])=>b.type==="one"?[[b.relationalColumn,f]]:[]));Object.entries(t.payload).forEach(([f,b])=>{let j=s.relations[u[f]];if(!b||!u[f])return;let S=n==null?void 0:n.value[f],[,w]=j.mergeMutation("set",b,S);if(w){if(!this.optimisticObjGraph.hasNode(w.value)){let W=j.entity.name;this.optimisticObjGraph.createNode(w.value,W,Object.values(this.schema[W].relations).flatMap(P=>P.type==="many"?[P.entity.name]:[]));}S!=null&&S.value&&this.optimisticObjGraph.removeLink(t.resourceId,j.entity.name),this.optimisticObjGraph.createLink(t.resourceId,w.value);}});}this.notifyCollectionSubscribers(e),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});});}materializeOneWithInclude(e,t={}){var c;if(!e)return;let i=this.optimisticObjGraph.getNode(e);if(!i)return;let s=i.type,n=(c=this.optimisticRawObjPool[s])==null?void 0:c[e];if(!n)return;let[r,o]=Object.keys(t).reduce((y,p)=>{let l=this.schema[s].relations[p];return l.type==="one"?y[0].push([p,l.entity.name]):l.type==="many"&&y[1].push([p,l.entity.name]),y},[[],[]]);return {value:{...n.value,...Object.fromEntries(r.map(([y,p])=>[y,this.materializeOneWithInclude(i.references.get(p))])),...Object.fromEntries(o.map(([y,p])=>{let l=i.referencedBy.get(p),u=l instanceof Set;return [y,u?{value:Array.from(l.values()).map(f=>this.materializeOneWithInclude(f))}:this.materializeOneWithInclude(l)]}))}}}notifyCollectionSubscribers(e){this.collectionSubscriptions.forEach(t=>{if(t.query.resource===e||t.flatInclude&&t.flatInclude.includes(e)){let i=m(t.query),s=this.querySnapshots[i],n=this.get(t.query,void 0,true);if(ae(n,s))return;this.querySnapshots[i]=n,t.callbacks.forEach(r=>{r(n);});}});}};var I=class{url;ws;store;remoteSubCounters={};eventListeners=new Set;replyHandlers={};constructor(e){this.url=e.url,this.store=new O(e.schema,e.storage,t=>{var i,s;(s=(i=Object.values(t))==null?void 0:i.flat())==null||s.forEach(n=>{this.sendWsMessage(n);});}),this.ws=new E({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=>{this.emitEvent({type:"CONNECTION_STATE_CHANGE",open:t.open}),t.open&&(Object.keys(this.store.schema).forEach(i=>{this.sendWsMessage({id:a(),type:"QUERY",resource:i});}),Object.entries(this.remoteSubCounters).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){return this.store.get(e)}handleServerMessage(e){try{console.log("Message received from the server:",e);let t=K.parse(JSON.parse(e));if(console.log("Parsed message:",t),this.emitEvent({type:"MESSAGE_RECEIVED",message:t}),t.type==="MUTATE"){let{resource:i}=t;try{this.store.addMutation(i,t);}catch(s){console.error("Error merging mutation from the server:",s);}}else if(t.type!=="REJECT"){if(t.type==="REPLY"){let{id:i,data:s}=t;if(this.replyHandlers[i]){clearTimeout(this.replyHandlers[i].timeoutHandle),this.replyHandlers[i].handler(s);return}let n=U.parse(s);this.store.loadConsolidatedState(n.resource,n.data);}}}catch(t){console.error("Error parsing message from the server:",t);}}subscribeToRemote(e){return this.remoteSubCounters[e]=(this.remoteSubCounters[e]??0)+1,this.sendWsMessage({id:a(),type:"SUBSCRIBE",resource:e}),()=>{this.remoteSubCounters[e]-=1,this.remoteSubCounters[e];}}subscribe(e,t){return this.store.subscribe(e,t)}mutate(e,t,i){var n;let s={id:a(),type:"MUTATE",resource:e,payload:this.store.schema[e].encodeMutation("set",i,new Date().toISOString()),resourceId:t};(n=this.store)==null||n.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((n,r)=>{this.replyHandlers[s.id]={timeoutHandle:setTimeout(()=>{delete this.replyHandlers[s.id],r(new Error("Reply timeout"));},5e3),handler:o=>{delete this.replyHandlers[s.id],n(o);}};})}addEventListener(e){return this.eventListeners.add(e),()=>{this.eventListeners.delete(e);}}sendWsMessage(e){var t;(t=this.ws)!=null&&t.connected()&&this.ws.send(JSON.stringify(e));}emitEvent(e){this.eventListeners.forEach(t=>{t(e);});}},qe=d=>{let e=new I(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();});}},addEventListener:t=>e.addEventListener(t)},store:{query:Object.entries(d.schema).reduce((t,[i,s])=>(t[i]=M._init(s,e),t),{}),mutate:c(()=>{},{apply:(t,i,s)=>{if(i.length<2)return;if(i.length>2)throw new Error("Trying to access an invalid path");let[n,r]=i;if(r==="insert"){let{id:o,...c}=s[0];return e.mutate(n,o,c)}if(r==="update"){let[o,c]=s;return e.mutate(n,o,c)}return e.genericMutate(n,r,s[0])}})}}};export{pe as SubscriptionProvider,qe as createClient,he as useLiveQuery};
|
|
1
|
+
import {c,a,e,d,b}from'./chunk-RWM6TXG7.js';import {useSyncExternalStore,useEffect}from'react';import {xxHash32}from'js-xxhash';import {jsx,Fragment}from'react/jsx-runtime';import {z as z$1}from'zod';import {stringify}from'qs';import se from'fast-deep-equal';import {openDB}from'idb';var y=c=>xxHash32(JSON.stringify(c)).toString(32),g=(c,e,t=false)=>Object.entries(e).every(([i,s])=>{if(i==="$and")return s.every(n=>g(c,n,t));if(i==="$or")return s.some(n=>g(c,n,t));let r=(s==null?void 0:s.$eq)!==void 0?s==null?void 0:s.$eq:s;if(typeof s=="object"&&s!==null&&(s==null?void 0:s.$eq)===void 0){if(s.$in!==void 0){let n=c[i];return n===void 0?false:t?!s.$in.includes(n):s.$in.includes(n)}if(s.$not!==void 0&&!t)return g(c,{[i]:s.$not},true);if(s.$gt!==void 0){let n=c[i];return typeof n!="number"?false:t?n<=s.$gt:n>s.$gt}if(s.$gte!==void 0){let n=c[i];return typeof n!="number"?false:t?n<s.$gte:n>=s.$gte}if(s.$lt!==void 0){let n=c[i];return typeof n!="number"?false:t?n>=s.$lt:n<s.$lt}if(s.$lte!==void 0){let n=c[i];return typeof n!="number"?false:t?n>s.$lte:n<=s.$lte}return !c[i]||typeof c[i]!="object"?false:g(c[i],s,t)}return t?c[i]!==r:c[i]===r});var C=class{subscriptions=new Map;getOrStoreSubscription(e){let t=y(e);return this.subscriptions.has(t)?this.subscriptions.get(t).subscribe:(this.subscriptions.set(t,{subscribe:i=>{var r;(r=this.subscriptions.get(t))==null||r.callbacks.add(i);let s=e.subscribe(()=>{var n;(n=this.subscriptions.get(t))==null||n.callbacks.forEach(o=>{o();});});return ()=>{var n;(n=this.subscriptions.get(t))==null||n.callbacks.delete(i),setTimeout(()=>{var o;((o=this.subscriptions.get(t))==null?void 0:o.callbacks.size)===0&&(this.subscriptions.delete(t),s());},10);}},callbacks:new Set}),this.subscriptions.get(t).subscribe)}},N=new C,ce=c=>useSyncExternalStore(N.getOrStoreSubscription(c),c.get),le=({children:c,client:e})=>(useEffect(()=>{e.subscribe();},[e.subscribe]),jsx(Fragment,{children:c}));var Q=z$1.object({resource:z$1.string(),where:z$1.record(z$1.string(),z$1.any()).optional(),include:z$1.record(z$1.string(),z$1.any()).optional(),lastSyncedAt:z$1.string().optional(),limit:z$1.number().optional(),sort:z$1.array(z$1.object({key:z$1.string(),direction:z$1.enum(["asc","desc"])})).optional()}),R=z$1.record(z$1.string(),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((c,e)=>{c.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(),resourceId:z$1.string().optional()}),j=z.extend({procedure:z$1.string(),payload:z$1.any().optional()}),k=z.extend({procedure:z$1.enum(["INSERT","UPDATE"]),payload:R});z$1.union([k,j]);var v=z$1.string(),Y=z$1.object({id:v,type:z$1.literal("SUBSCRIBE"),resource:z$1.string()}),F=Q.extend({id:v,type:z$1.literal("QUERY")}),B=k.extend({id:v}),Z=j.extend({id:v}),X=z$1.union([Z,B]);z$1.union([Y,F,X]);var ee=z$1.object({id:v,type:z$1.literal("REJECT"),resource:z$1.string(),message:z$1.string().optional()}),te=z$1.object({id:v,type:z$1.literal("REPLY"),data:z$1.any()}),$=z$1.union([ee,te,B]),G=z$1.object({resource:z$1.string(),data:z$1.record(z$1.string(),R)});var S=class c{_collection;_client;_where;_include;_limit;_single;_sort;constructor(e,t,i,s,r,n,o){this._collection=e,this._client=t,this._where=i??{},this._include=s??{},this._limit=r,this._single=n,this._sort=o,this.get=this.get.bind(this),this.subscribe=this.subscribe.bind(this);}where(e){return new c(this._collection,this._client,{...this._where,...e},this._include,this._limit,this._single,this._sort)}include(e){return new c(this._collection,this._client,this._where,{...this._include,...e},this._limit,this._single,this._sort)}get(){let e=this._client.get({resource:this._collection.name,where:this._where,include:this._include,limit:this._limit,sort:this._sort});return this._single?e[0]:e}subscribe(e){return this._client.subscribe({resource:this._collection.name,where:this._where,include:this._include,limit:this._limit,sort:this._sort},t=>{if(this._single)return e(t[0]);e(t);})}limit(e){return new c(this._collection,this._client,this._where,this._include,e,this._single,this._sort)}one(e){return this.first({id:e})}first(e){return new c(this._collection,this._client,e??this._where,this._include,1,true,this._sort)}orderBy(e,t="asc"){let i=[...this._sort??[],{key:e,direction:t}];return new c(this._collection,this._client,this._where,this._include,this._limit,this._single,i)}toJSON(){return {resource:this._collection.name,where:this._where,include:this._include,limit:this._limit,sort:this._sort}}static _init(e,t){return new c(e,t)}};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){var i;this.eventListeners.has(e)||this.eventListeners.set(e,new Set),(i=this.eventListeners.get(e))==null||i.add(t);}removeEventListener(e,t){var i;this.eventListeners.has(e)&&((i=this.eventListeners.get(e))==null||i.delete(t));}send(e){if(this.ws&&this.ws.readyState===WebSocket.OPEN)this.ws.send(e);else throw new Error("WebSocket is not open")}handleOpen(e){this.reconnectAttempts=0,this.dispatchEvent("open",e),this.dispatchEvent("connectionChange",{open:true});}handleClose(e){this.dispatchEvent("close",e),this.dispatchEvent("connectionChange",{open:false}),this.autoReconnect&&!this.intentionallyDisconnected&&this.scheduleReconnect();}handleError(e){var t,i;this.dispatchEvent("error",e),this.dispatchEvent("connectionChange",{open:false}),(i=(t=e.error)==null?void 0:t.message)!=null&&i.includes("non-101")&&(this.ws&&(this.ws.close(),this.ws=null),this.autoReconnect&&!this.intentionallyDisconnected&&this.scheduleReconnect());}handleMessage(e){this.dispatchEvent("message",e);}scheduleReconnect(){this.reconnectLimit&&this.reconnectAttempts>=this.reconnectLimit||(this.reconnectAttempts++,this.reconnectTimer=setTimeout(()=>{this.connect();},this.reconnectTimeout));}dispatchEvent(e,t){var i;(i=this.eventListeners.get(e))==null||i.forEach(s=>{s(t);});}};var M=class{nodes;constructor(){this.nodes=new Map;}createNode(e,t,i){if(this.nodes.has(e))throw new Error(`Node with id ${e} already exists`);let s={id:e,type:t,referencedBy:new Map(i.map(r=>[r,new Set])),references:new Map,subscriptions:new Set};return this.nodes.set(e,s),s}getNode(e){return this.nodes.get(e)}hasNode(e){return this.nodes.has(e)}createLink(e,t){let i=this.nodes.get(e),s=this.nodes.get(t);if(!i)throw new Error(`Source node with id ${e} does not exist`);if(!s)throw new Error(`Target node with id ${t} does not exist`);i.references.set(s.type,t);let r=s.referencedBy.get(i.type);r&&r instanceof Set?r.add(e):s.referencedBy.set(i.type,e),this.notifySubscribers(t);}removeLink(e,t){let i=this.nodes.get(e);if(!i)throw new Error(`Node with id ${e} does not exist`);let s=i.references.get(t);if(!s)return;i.references.delete(t);let r=this.nodes.get(s);if(!r)return;let n=r.referencedBy.get(i.type);n&&(n instanceof Set?n.delete(e):r.referencedBy.delete(i.type),this.notifySubscribers(s)),this.notifySubscribers(e);}subscribe(e,t){let i=this.nodes.get(e);if(!i)throw new Error(`Node with id ${e} does not exist`);return i.subscriptions.add(t),()=>{i.subscriptions.delete(t);}}removeNode(e){let t=this.nodes.get(e);t&&(Array.from(t.referencedBy.entries()).forEach(([i,s])=>{(s instanceof Set?Array.from(s.values()):[s]).forEach(n=>{let o=this.nodes.get(n);!o||!o.references.get(i)||(o.references.delete(i),this.notifySubscribers(n));});}),this.nodes.delete(e));}updateNode(e,t){let i=this.nodes.get(e);if(!i)throw new Error(`Node with id ${e} does not exist`);t(i),this.notifySubscribers(e);}notifySubscribers(e){let t=this.nodes.get(e);t&&Array.from(t.subscriptions).forEach(i=>{try{i(e);}catch(s){console.error(`Error in node subscription for node ${e}:`,s);}});}getAllNodes(){return Array.from(this.nodes.values())}};var x="__meta",T="databases",E=class{db;async init(e,t){var d,h;if(typeof window>"u")return;let s=((d=(await window.indexedDB.databases()).find(u=>u.name===t))==null?void 0:d.version)??1,r=await y(e),n=Object.fromEntries(await Promise.all(Object.entries(e).map(async([u,f])=>[u,await y(f)]))),o=await openDB("live-state-databases",1,{upgrade(u){u.objectStoreNames.contains(T)||u.createObjectStore(T);}}),a=(h=await this.getAll(o,T))==null?void 0:h[t];(a==null?void 0:a.schemaHash)!==r&&s++,this.db=await openDB(t,s,{async upgrade(u){[...Object.keys(e),x].forEach(f=>{(a==null?void 0:a.objectHashes[f])!==n[f]&&u.objectStoreNames.contains(f)&&u.deleteObjectStore(f),u.objectStoreNames.contains(f)||u.createObjectStore(f);}),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(x,e):new Promise(t=>t(void 0))}setMeta(e,t){var i;return (i=this.db)==null?void 0:i.put(x,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 O=class{constructor(e,t,i){this.schema=e;this.kvStorage=new E,t!==false&&this.kvStorage.init(this.schema,t.name).then(()=>{this.kvStorage.getMeta("mutationStack").then(s=>{!s||Object.keys(s).length===0||(this.optimisticMutationStack=s,i==null||i(this.optimisticMutationStack));}).then(()=>{Object.entries(this.schema).forEach(([s,r])=>{this.kvStorage.get(s).then(n=>{!n||Object.keys(n).length===0||this.loadConsolidatedState(s,n);});});}).catch(s=>{console.error("Failed to load state from storage",s);});});}rawObjPool={};optimisticMutationStack={};optimisticObjGraph=new M;optimisticRawObjPool={};collectionSubscriptions=new Map;querySnapshots={};kvStorage;get(e$1,t,i=false){var n;let s=t??y(e$1);if(this.querySnapshots[s]&&!i){let o=this.querySnapshots[s];if(o)return o}let r=((n=e$1.where)!=null&&n.id?[e$1.where.id]:Object.keys(this.optimisticRawObjPool[e$1.resource]??{})).flatMap(o=>{let a=e(this.materializeOneWithInclude(o,e$1.include));return a?[a]:[]});if(e$1.sort&&e$1.sort.length>0){let o=(a,d)=>{for(let h of e$1.sort){let u=a[h.key],f=d[h.key];if(u<f)return h.direction==="asc"?-1:1;if(u>f)return h.direction==="asc"?1:-1}return 0};r.sort(o);}if(e$1.where||e$1.limit){let o=e$1.where?a=>g(a,e$1.where):()=>true;r=d(r,o,e$1.limit);}return i||(this.querySnapshots[s]=r),r}subscribe(e,t){var n;let i=y(e),s=this.collectionSubscriptions.get(i),r=this.schema[e.resource];return s||this.collectionSubscriptions.set(i,{callbacks:new Set,query:e,flatInclude:e.include?Object.keys(e.include).map(o=>r.relations[o].entity.name):void 0}),(n=this.collectionSubscriptions.get(i))==null||n.callbacks.add(t),()=>{var o,a;(o=this.collectionSubscriptions.get(i))==null||o.callbacks.delete(t),((a=this.collectionSubscriptions.get(i))==null?void 0:a.callbacks.size)===0&&(this.collectionSubscriptions.delete(i),delete this.querySnapshots[i]);}}addMutation(e,t,i=false){var n,o,a;let s=this.schema[e];if(console.log("Adding mutation",t),!s)throw new Error("Schema not found");let r=(n=this.optimisticRawObjPool[e])==null?void 0:n[t.resourceId];if(i)this.optimisticMutationStack[e]??=[],this.optimisticMutationStack[e].push(t);else {this.optimisticMutationStack[e]=((a=(o=this.optimisticMutationStack)==null?void 0:o[e])==null?void 0:a.filter(u=>u.id!==t.id))??[],this.rawObjPool[e]??={};let d={value:{...this.schema[e].mergeMutation("set",t.payload,this.rawObjPool[e][t.resourceId])[0].value,id:{value:t.resourceId}}};this.rawObjPool[e][t.resourceId]=d;let h=d.value;delete h.id,this.kvStorage.set(e,t.resourceId,h);}this.kvStorage.setMeta("mutationStack",this.optimisticMutationStack),this.updateRawObjPool(e,t.resourceId,t.payload,r);}undoMutation(e,t){var n,o;if(!this.optimisticMutationStack[e])return;let i=(n=this.optimisticMutationStack[e])==null?void 0:n.findIndex(a=>a.id===t);if(i===-1)return;let s=this.optimisticMutationStack[e][i];console.log("Removing mutation",s);let r=(o=this.optimisticRawObjPool[e])==null?void 0:o[s.resourceId];this.optimisticMutationStack[e].splice(i,1),this.kvStorage.setMeta("mutationStack",this.optimisticMutationStack),this.updateRawObjPool(e,s.resourceId,Object.fromEntries(Object.entries(s.payload).map(([a])=>[a,{value:null,_meta:{}}])),r);}loadConsolidatedState(e,t){Object.entries(t).forEach(([i,s])=>{this.addMutation(e,{id:i,type:"MUTATE",resource:e,resourceId:i,procedure:"INSERT",payload:s});});}updateRawObjPool(e,t,i,s){var o;if(!this.schema[e])return;let r=(o=this.rawObjPool[e])==null?void 0:o[t],n=(this.optimisticMutationStack[e]??[]).reduce((a,d)=>d.resourceId!==t?a:this.schema[e].mergeMutation("set",d.payload,a)[0],r);if(this.optimisticRawObjPool[e]??={},n?this.optimisticRawObjPool[e][t]={value:{...n.value,id:{value:t}}}:delete this.optimisticRawObjPool[e][t],!(!this.optimisticObjGraph.hasNode(t)&&!n)){if(this.optimisticObjGraph.hasNode(t)||this.optimisticObjGraph.createNode(t,e,Object.values(this.schema[e].relations).flatMap(a=>a.type==="many"?[a.entity.name]:[])),Object.keys(this.schema[e].relations).length>0){let a=Object.fromEntries(Object.entries(this.schema[e].relations).flatMap(([d,h])=>h.type==="one"?[[h.relationalColumn,d]]:[]));Object.entries(i).forEach(([d,h])=>{let u=this.schema[e].relations[a[d]];if(!a[d])return;let f=s==null?void 0:s.value[d],[,m]=u.mergeMutation("set",h,f);if(m){if(!this.optimisticObjGraph.hasNode(m.value)){let L=u.entity.name;this.optimisticObjGraph.createNode(m.value,L,Object.values(this.schema[L].relations).flatMap(A=>A.type==="many"?[A.entity.name]:[]));}f!=null&&f.value&&this.optimisticObjGraph.removeLink(t,u.entity.name),this.optimisticObjGraph.createLink(t,m.value);}});}this.notifyCollectionSubscribers(e),this.optimisticObjGraph.notifySubscribers(t);}}materializeOneWithInclude(e,t={}){var a;if(!e)return;let i=this.optimisticObjGraph.getNode(e);if(!i)return;let s=i.type,r=(a=this.optimisticRawObjPool[s])==null?void 0:a[e];if(!r)return;let[n,o]=Object.keys(t).reduce((d,h)=>{let u=this.schema[s].relations[h];return u.type==="one"?d[0].push([h,u.entity.name]):u.type==="many"&&d[1].push([h,u.entity.name]),d},[[],[]]);return {value:{...r.value,...Object.fromEntries(n.map(([d,h])=>[d,this.materializeOneWithInclude(i.references.get(h))])),...Object.fromEntries(o.map(([d,h])=>{let u=i.referencedBy.get(h),f=u instanceof Set;return [d,f?{value:Array.from(u.values()).map(m=>this.materializeOneWithInclude(m))}:this.materializeOneWithInclude(u)]}))}}}notifyCollectionSubscribers(e){this.collectionSubscriptions.forEach(t=>{if(t.query.resource===e||t.flatInclude&&t.flatInclude.includes(e)){let i=y(t.query),s=this.querySnapshots[i],r=this.get(t.query,void 0,true);if(se(r,s))return;this.querySnapshots[i]=r,t.callbacks.forEach(n=>{n(r);});}});}};var _=class{url;ws;store;remoteSubCounters={};eventListeners=new Set;replyHandlers={};constructor(e){this.url=e.url,this.store=new O(e.schema,e.storage,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=>{this.emitEvent({type:"CONNECTION_STATE_CHANGE",open:t.open}),t.open&&(Object.keys(this.store.schema).forEach(i=>{this.sendWsMessage({id:a(),type:"QUERY",resource:i});}),Object.entries(this.remoteSubCounters).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){return this.store.get(e)}handleServerMessage(e){try{console.log("Message received from the server:",e);let t=$.parse(JSON.parse(e));if(console.log("Parsed message:",t),this.emitEvent({type:"MESSAGE_RECEIVED",message:t}),t.type==="MUTATE"){let{resource:i}=t;try{this.store.addMutation(i,t);}catch(s){console.error("Error merging mutation from the server:",s);}}else if(t.type==="REJECT")this.store.undoMutation(t.resource,t.id);else if(t.type==="REPLY"){let{id:i,data:s}=t;if(this.replyHandlers[i]){clearTimeout(this.replyHandlers[i].timeoutHandle),this.replyHandlers[i].handler(s);return}let r=G.parse(s);this.store.loadConsolidatedState(r.resource,r.data);}}catch(t){console.error("Error parsing message from the server:",t);}}subscribeToRemote(e){return this.remoteSubCounters[e]=(this.remoteSubCounters[e]??0)+1,this.sendWsMessage({id:a(),type:"SUBSCRIBE",resource:e}),()=>{this.remoteSubCounters[e]-=1,this.remoteSubCounters[e];}}subscribe(e,t){return this.store.subscribe(e,t)}mutate(e,t,i,s){var n;let r={id:a(),type:"MUTATE",resource:e,payload:this.store.schema[e].encodeMutation("set",s,new Date().toISOString()),resourceId:t,procedure:i};(n=this.store)==null||n.addMutation(e,r,true),this.sendWsMessage(r);}genericMutate(e,t,i){if(!this.ws||!this.ws.connected())throw new Error("WebSocket not connected");let s={id:a(),type:"MUTATE",resource:e,procedure:t,payload:i};return this.sendWsMessage(s),new Promise((r,n)=>{this.replyHandlers[s.id]={timeoutHandle:setTimeout(()=>{delete this.replyHandlers[s.id],n(new Error("Reply timeout"));},5e3),handler:o=>{delete this.replyHandlers[s.id],r(o);}};})}addEventListener(e){return this.eventListeners.add(e),()=>{this.eventListeners.delete(e);}}sendWsMessage(e){var t;(t=this.ws)!=null&&t.connected()&&this.ws.send(JSON.stringify(e));}emitEvent(e){this.eventListeners.forEach(t=>{t(e);});}},Be=c$1=>{let e=new _(c$1);return {client:{ws:e.ws,subscribe:t=>{let i=[];for(let s of t??Object.keys(e.store.schema))i.push(e.subscribeToRemote(s));return ()=>{console.log("Removing listeners",i),i.forEach(s=>{s();});}},addEventListener:t=>e.addEventListener(t)},store:{query:Object.entries(c$1.schema).reduce((t,[i,s])=>(t[i]=S._init(s,e),t),{}),mutate:c(()=>{},{apply:(t,i,s)=>{if(i.length<2)return;if(i.length>2)throw new Error("Trying to access an invalid path");let[r,n]=i;if(n==="insert"){let{id:o,...a}=s[0];return e.mutate(r,o,"INSERT",a)}if(n==="update"){let[o,a]=s;return e.mutate(r,o,"UPDATE",a)}return e.genericMutate(r,n,s[0])}})}}};export{le as SubscriptionProvider,Be as createClient,ce as useLiveQuery};
|
package/dist/fetch-client.d.ts
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
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
|
|
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 InferInsert, c as InferUpdate } from './index-ClRyEBhW.js';
|
|
2
2
|
import 'react/jsx-runtime';
|
|
3
3
|
import 'zod';
|
|
4
|
+
import 'zod/v3';
|
|
5
|
+
import 'zod/v4/core';
|
|
4
6
|
|
|
5
7
|
type GetOptions<T extends LiveObjectAny> = {
|
|
6
8
|
headers?: Record<string, string>;
|
|
@@ -10,7 +12,8 @@ type GetOptions<T extends LiveObjectAny> = {
|
|
|
10
12
|
type FetchClient<TRouter extends AnyRouter> = {
|
|
11
13
|
[K in keyof TRouter["routes"]]: {
|
|
12
14
|
get: (opts?: GetOptions<TRouter["routes"][K]["_resourceSchema"]>) => Promise<Record<string, Simplify<InferLiveObject<TRouter["routes"][K]["_resourceSchema"]>>>>;
|
|
13
|
-
|
|
15
|
+
insert: (input: Simplify<InferInsert<TRouter["routes"][K]["_resourceSchema"]>>) => Promise<void>;
|
|
16
|
+
update: (id: string, input: Simplify<InferUpdate<TRouter["routes"][K]["_resourceSchema"]>>) => Promise<void>;
|
|
14
17
|
};
|
|
15
18
|
};
|
|
16
19
|
declare const createClient: <TRouter extends AnyRouter>(opts: Omit<ClientOptions, "storage">) => FetchClient<TRouter>;
|
package/dist/fetch-client.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import {c,b,
|
|
1
|
+
import {c,b as b$1,e}from'./chunk-RWM6TXG7.js';import {stringify}from'qs';var b=t=>c(()=>{},{apply:async(m,a,i)=>{if(a.length>2)throw new Error("Trying to access invalid property");let[n,o]=a,s=await b$1(t.credentials)??{};if(o==="get"){let e$1=i[0],r={};return e$1!=null&&e$1.where&&(r.where=e$1.where),e$1!=null&&e$1.include&&(r.include=e$1.include),fetch(`${t.url}/${n}${Object.keys(r).length>0?`?${stringify(r)}`:""}`,{headers:s}).then(async c=>Object.fromEntries(Object.entries(await c.json()??{}).map(([d,l])=>[d,e(l)])))}if(o==="insert"){let{id:e,...r}=i[0];return fetch(`${t.url}/${n}/insert`,{method:"POST",headers:{...s,"Content-Type":"application/json"},body:JSON.stringify({resourceId:e,payload:t.schema[n].encodeMutation("set",r,new Date().toISOString())})})}if(o==="update"){let e=i[0],{id:r,...c}=i[1];return fetch(`${t.url}/${n}/update`,{method:"POST",headers:{...s,"Content-Type":"application/json"},body:JSON.stringify({resourceId:e,payload:t.schema[n].encodeMutation("set",c,new Date().toISOString())})})}return fetch(`${t.url}/${n}/${o}`,{method:"POST",headers:{...s,"Content-Type":"application/json"},body:JSON.stringify(i[0])})}});export{b as createClient};
|
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
import * as react_jsx_runtime from 'react/jsx-runtime';
|
|
2
|
-
import {
|
|
2
|
+
import { z } from 'zod';
|
|
3
|
+
import * as z3 from 'zod/v3';
|
|
4
|
+
import * as z4 from 'zod/v4/core';
|
|
3
5
|
|
|
4
6
|
type Promisify<T> = T extends Promise<any> ? T : Promise<T>;
|
|
5
7
|
type Awaitable<T> = T | Promise<T>;
|
|
@@ -214,6 +216,15 @@ type WhereClause<T extends LiveObjectAny> = ({
|
|
|
214
216
|
type IncludeClause<T extends LiveObjectAny> = {
|
|
215
217
|
[K in keyof T["relations"]]?: boolean;
|
|
216
218
|
};
|
|
219
|
+
type InferInsert<T extends LiveObjectAny> = InferLiveObjectWithoutRelations<T>;
|
|
220
|
+
type InferUpdate<T extends LiveObjectAny> = Omit<LiveObjectMutationInput<T>, "id">;
|
|
221
|
+
|
|
222
|
+
type Simplify<T> = T extends Record<string, unknown> ? {
|
|
223
|
+
[K in keyof T]: Simplify<T[K]>;
|
|
224
|
+
} : T extends Array<infer U> ? Array<Simplify<U>> : T;
|
|
225
|
+
|
|
226
|
+
/** biome-ignore-all lint/suspicious/noExplicitAny: false positive */
|
|
227
|
+
/** biome-ignore-all lint/style/noNonNullAssertion: false positive */
|
|
217
228
|
|
|
218
229
|
type RouteRecord = Record<string, AnyRoute>;
|
|
219
230
|
declare class Router<TRoutes extends RouteRecord> {
|
|
@@ -229,22 +240,36 @@ type RequestHandler<TInput, TResult, TSchema extends Schema<any> = Schema<any>>
|
|
|
229
240
|
db: Storage;
|
|
230
241
|
schema: TSchema;
|
|
231
242
|
}) => Promise<TResult>;
|
|
232
|
-
type Mutation<TInputValidator extends ZodTypeAny, // TODO use StandardSchema instead
|
|
243
|
+
type Mutation<TInputValidator extends z3.ZodTypeAny | z4.$ZodType, // TODO use StandardSchema instead
|
|
233
244
|
THandler extends RequestHandler<z.infer<TInputValidator>, any, any>> = {
|
|
234
245
|
inputValidator: TInputValidator;
|
|
235
246
|
handler: THandler;
|
|
236
247
|
};
|
|
237
|
-
declare const mutationCreator: <TInputValidator extends ZodTypeAny>(validator?: TInputValidator) => {
|
|
248
|
+
declare const mutationCreator: <TInputValidator extends z3.ZodTypeAny | z4.$ZodType>(validator?: TInputValidator) => {
|
|
238
249
|
handler: <THandler extends RequestHandler<z.infer<TInputValidator>, any, any>>(handler: THandler) => Mutation<TInputValidator, THandler>;
|
|
239
250
|
};
|
|
251
|
+
type ReadAuthorizationHandler<TShape extends LiveObjectAny> = (opts: {
|
|
252
|
+
ctx: ParsedRequest["context"];
|
|
253
|
+
}) => WhereClause<TShape> | boolean;
|
|
254
|
+
type MutationAuthorizationHandler<TShape extends LiveObjectAny> = (opts: {
|
|
255
|
+
ctx: ParsedRequest["context"];
|
|
256
|
+
value: Simplify<InferLiveObjectWithRelationalIds<TShape>>;
|
|
257
|
+
}) => WhereClause<TShape> | boolean;
|
|
258
|
+
type Authorization<TShape extends LiveObjectAny> = {
|
|
259
|
+
read?: ReadAuthorizationHandler<TShape>;
|
|
260
|
+
insert?: MutationAuthorizationHandler<TShape>;
|
|
261
|
+
update?: {
|
|
262
|
+
preMutation?: MutationAuthorizationHandler<TShape>;
|
|
263
|
+
postMutation?: MutationAuthorizationHandler<TShape>;
|
|
264
|
+
};
|
|
265
|
+
};
|
|
240
266
|
declare class Route<TResourceSchema extends LiveObjectAny, TMiddleware extends Middleware<any>, TCustomMutations extends Record<string, Mutation<any, RequestHandler<any, any>>>> {
|
|
241
267
|
readonly _resourceSchema: TResourceSchema;
|
|
242
268
|
readonly resourceName: TResourceSchema["name"];
|
|
243
269
|
readonly middlewares: Set<TMiddleware>;
|
|
244
270
|
readonly customMutations: TCustomMutations;
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
private handleSet;
|
|
271
|
+
readonly authorization?: Authorization<TResourceSchema>;
|
|
272
|
+
constructor(resourceName: TResourceSchema["name"], customMutations?: TCustomMutations, authorization?: Authorization<TResourceSchema>);
|
|
248
273
|
handleRequest(opts: {
|
|
249
274
|
req: ParsedRequest;
|
|
250
275
|
db: Storage;
|
|
@@ -254,12 +279,10 @@ declare class Route<TResourceSchema extends LiveObjectAny, TMiddleware extends M
|
|
|
254
279
|
withMutations<T extends Record<string, Mutation<any, RequestHandler<any, any>>>>(mutationFactory: (opts: {
|
|
255
280
|
mutation: typeof mutationCreator;
|
|
256
281
|
}) => T): Route<TResourceSchema, TMiddleware, T>;
|
|
282
|
+
private handleFind;
|
|
283
|
+
private handleSet;
|
|
257
284
|
}
|
|
258
|
-
type AnyRoute = Route<
|
|
259
|
-
|
|
260
|
-
type Simplify<T> = T extends Record<string, unknown> ? {
|
|
261
|
-
[K in keyof T]: Simplify<T[K]>;
|
|
262
|
-
} : T;
|
|
285
|
+
type AnyRoute = Route<any, Middleware<any>, Record<string, any>>;
|
|
263
286
|
|
|
264
287
|
/** biome-ignore-all lint/suspicious/noExplicitAny: false positive */
|
|
265
288
|
|
|
@@ -271,8 +294,13 @@ declare abstract class Storage {
|
|
|
271
294
|
where?: WhereClause<T>;
|
|
272
295
|
include?: IncludeClause<T>;
|
|
273
296
|
}): Promise<Record<string, InferLiveObject<T>>>;
|
|
274
|
-
insert<T extends LiveObjectAny>(resource: T, value: Simplify<
|
|
275
|
-
update<T extends LiveObjectAny>(resource: T, resourceId: string, value:
|
|
297
|
+
insert<T extends LiveObjectAny>(resource: T, value: Simplify<InferInsert<T>>): Promise<InferLiveObject<T>>;
|
|
298
|
+
update<T extends LiveObjectAny>(resource: T, resourceId: string, value: InferUpdate<T>): Promise<InferLiveObject<T>>;
|
|
299
|
+
abstract transaction<T>(fn: (opts: {
|
|
300
|
+
trx: Storage;
|
|
301
|
+
commit: () => Promise<void>;
|
|
302
|
+
rollback: () => Promise<void>;
|
|
303
|
+
}) => Promise<T>): Promise<T>;
|
|
276
304
|
}
|
|
277
305
|
|
|
278
306
|
type ParsedRequest<TInput = any> = {
|
|
@@ -315,7 +343,11 @@ declare const serverMessageSchema: z.ZodUnion<readonly [z.ZodObject<{
|
|
|
315
343
|
}, z.core.$strip>, z.ZodObject<{
|
|
316
344
|
type: z.ZodLiteral<"MUTATE">;
|
|
317
345
|
resource: z.ZodString;
|
|
318
|
-
resourceId: z.ZodString
|
|
346
|
+
resourceId: z.ZodOptional<z.ZodString>;
|
|
347
|
+
procedure: z.ZodEnum<{
|
|
348
|
+
INSERT: "INSERT";
|
|
349
|
+
UPDATE: "UPDATE";
|
|
350
|
+
}>;
|
|
319
351
|
payload: z.ZodRecord<z.ZodString, z.ZodObject<{
|
|
320
352
|
value: z.ZodNullable<z.ZodUnion<[z.ZodUnion<[z.ZodUnion<[z.ZodString, z.ZodNumber]>, z.ZodBoolean]>, z.ZodDate]>>;
|
|
321
353
|
_meta: z.ZodOptional<z.ZodObject<{
|
|
@@ -364,8 +396,8 @@ type Client$1<TRouter extends AnyRouter> = {
|
|
|
364
396
|
};
|
|
365
397
|
mutate: {
|
|
366
398
|
[K in keyof TRouter["routes"]]: {
|
|
367
|
-
insert: (input: Simplify<
|
|
368
|
-
update: (id: string, value:
|
|
399
|
+
insert: (input: Simplify<InferInsert<TRouter["routes"][K]["_resourceSchema"]>>) => void;
|
|
400
|
+
update: (id: string, value: Simplify<InferUpdate<TRouter["routes"][K]["_resourceSchema"]>>) => void;
|
|
369
401
|
} & {
|
|
370
402
|
[K2 in keyof TRouter["routes"][K]["customMutations"]]: (input: z.infer<TRouter["routes"][K]["customMutations"][K2]["inputValidator"]>) => Promisify<ReturnType<TRouter["routes"][K]["customMutations"][K2]["handler"]>>;
|
|
371
403
|
};
|
|
@@ -439,4 +471,4 @@ type ClientOptions = {
|
|
|
439
471
|
} | false;
|
|
440
472
|
};
|
|
441
473
|
|
|
442
|
-
export { type AnyRouter as A, type ClientOptions as C, type IncludeClause as I, type LiveObjectAny as L, type MessageReceivedEvent as M, type Simplify as S, type WhereClause as W, type InferLiveObject as a, type
|
|
474
|
+
export { type AnyRouter as A, type ClientOptions as C, type IncludeClause as I, type LiveObjectAny as L, type MessageReceivedEvent as M, type Simplify as S, type WhereClause as W, type InferLiveObject as a, type InferInsert as b, type InferUpdate as c, SubscriptionProvider as d, type ConnectionStateChangeEvent as e, type ClientEvents as f, type Client as g, createClient as h, useLiveQuery as u };
|
package/dist/index.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
'use strict';var
|
|
1
|
+
'use strict';var o=class{_value;_meta;_encodeInput;_decodeInput};var c=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 i extends o{storageType;convertFunc;isIndex;isUnique;defaultValue;foreignReference;isPrimary;constructor(e,t,n,r,a,u,y){super(),this.storageType=e,this.convertFunc=t,this.isIndex=n??false,this.isUnique=r??false,this.defaultValue=a,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 i(this.storageType,this.convertFunc,this.isIndex,true,this.defaultValue,this.foreignReference,this.isPrimary)}index(){return new i(this.storageType,this.convertFunc,true,this.isUnique,this.defaultValue,this.foreignReference,this.isPrimary)}default(e){return new i(this.storageType,this.convertFunc,this.isIndex,this.isUnique,e,this.foreignReference,this.isPrimary)}primary(){return new i(this.storageType,this.convertFunc,this.isIndex,this.isUnique,this.defaultValue,this.foreignReference,true)}nullable(){return new c(this)}},p=class i extends s{constructor(){super("integer",e=>Number(e));}static create(){return new i}},g=p.create,l=class i extends s{constructor(e){super("varchar",void 0,void 0,void 0,void 0,e);}static create(){return new i}static createId(){return new i().index().unique().primary()}static createReference(e){return new i(e)}},b=l.create,I=l.createId,M=l.createReference,d=class i extends s{constructor(){super("boolean",e=>typeof e=="string"?e.toLowerCase()==="true":!!e);}static create(){return new i}},O=d.create,m=class i extends s{constructor(){super("timestamp",e=>typeof e=="string"?new Date(e):e);}static create(){return new i}},j=m.create;var v=class i 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,a])=>[r,(this.fields[r]??this.relations[r]).encodeMutation("set",a,n)]))}mergeMutation(e,t,n){let r={};return [{value:{...(n==null?void 0:n.value)??{},...Object.fromEntries(Object.entries(t).map(([a,u])=>{let y=this.fields[a]??this.relations[a];if(!y)return [a,u];let[x,f]=y.mergeMutation(e,u,n==null?void 0:n.value[a]);return f&&(r[a]=f),[a,x]}))}},r]}setRelations(e){return new i(this.name,this.fields,e)}getStorageFieldType(){throw new Error("Method not implemented.")}static create(e,t){return new i(e,t)}},A=v.create,T=class i extends o{entity;type;required;relationalColumn;foreignColumn;sourceEntity;constructor(e,t,n,r,a){super(),this.entity=e,this.type=t,this.required=a??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 i(e,"one",t,void 0,n??false)}static createManyFactory(){return (e,t,n)=>new i(e,"many",void 0,t,n??false)}},K=(i,e)=>({$type:"relations",objectName:i.name,relations:e({one:T.createOneFactory(),many:T.createManyFactory()})}),h=i=>{if(i)return Array.isArray(i.value)?i.value.map(e=>h(e)):typeof i.value!="object"||i.value===null||i.value instanceof Date?i.value:Object.fromEntries(Object.entries(i.value).map(([e,t])=>[e,h(t)]))},_=i=>Object.fromEntries(Object.entries(i).flatMap(([e,t])=>{if(t.$type==="relations")return [];let n=t,r=Object.values(i).find(a=>a.$type==="relations"&&a.objectName===t.name);return r&&(n=n.setRelations(r.relations)),[[n.name,n]]}));exports.LiveBoolean=d;exports.LiveNumber=p;exports.LiveObject=v;exports.LiveString=l;exports.LiveTimestamp=m;exports.LiveType=o;exports.Relation=T;exports.boolean=O;exports.createRelations=K;exports.createSchema=_;exports.id=I;exports.inferValue=h;exports.number=g;exports.object=A;exports.reference=M;exports.string=b;exports.timestamp=j;
|
package/dist/index.d.cts
CHANGED
|
@@ -232,5 +232,7 @@ type WhereClause<T extends LiveObjectAny> = ({
|
|
|
232
232
|
type IncludeClause<T extends LiveObjectAny> = {
|
|
233
233
|
[K in keyof T["relations"]]?: boolean;
|
|
234
234
|
};
|
|
235
|
+
type InferInsert<T extends LiveObjectAny> = InferLiveObjectWithoutRelations<T>;
|
|
236
|
+
type InferUpdate<T extends LiveObjectAny> = Omit<LiveObjectMutationInput<T>, "id">;
|
|
235
237
|
|
|
236
|
-
export { type IncludeClause, type InferIndex, type InferLiveObject, type InferLiveObjectWithRelationalIds, type InferLiveType, LiveBoolean, LiveNumber, LiveObject, type LiveObjectAny, type LiveObjectMutationInput, LiveString, LiveTimestamp, LiveType, type LiveTypeAny, type LiveTypeMeta, type MaterializedLiveType, type MutationType, Relation, type Schema, type StorageFieldType, type WhereClause, boolean, createRelations, createSchema, id, inferValue, number, object, reference, string, timestamp };
|
|
238
|
+
export { type IncludeClause, type InferIndex, type InferInsert, type InferLiveObject, type InferLiveObjectWithRelationalIds, type InferLiveType, type InferUpdate, LiveBoolean, LiveNumber, LiveObject, type LiveObjectAny, type LiveObjectMutationInput, LiveString, LiveTimestamp, LiveType, type LiveTypeAny, type LiveTypeMeta, type MaterializedLiveType, type MutationType, Relation, type Schema, type StorageFieldType, type WhereClause, boolean, createRelations, createSchema, id, inferValue, number, object, reference, string, timestamp };
|
package/dist/index.d.ts
CHANGED
|
@@ -232,5 +232,7 @@ type WhereClause<T extends LiveObjectAny> = ({
|
|
|
232
232
|
type IncludeClause<T extends LiveObjectAny> = {
|
|
233
233
|
[K in keyof T["relations"]]?: boolean;
|
|
234
234
|
};
|
|
235
|
+
type InferInsert<T extends LiveObjectAny> = InferLiveObjectWithoutRelations<T>;
|
|
236
|
+
type InferUpdate<T extends LiveObjectAny> = Omit<LiveObjectMutationInput<T>, "id">;
|
|
235
237
|
|
|
236
|
-
export { type IncludeClause, type InferIndex, type InferLiveObject, type InferLiveObjectWithRelationalIds, type InferLiveType, LiveBoolean, LiveNumber, LiveObject, type LiveObjectAny, type LiveObjectMutationInput, LiveString, LiveTimestamp, LiveType, type LiveTypeAny, type LiveTypeMeta, type MaterializedLiveType, type MutationType, Relation, type Schema, type StorageFieldType, type WhereClause, boolean, createRelations, createSchema, id, inferValue, number, object, reference, string, timestamp };
|
|
238
|
+
export { type IncludeClause, type InferIndex, type InferInsert, type InferLiveObject, type InferLiveObjectWithRelationalIds, type InferLiveType, type InferUpdate, LiveBoolean, LiveNumber, LiveObject, type LiveObjectAny, type LiveObjectMutationInput, LiveString, LiveTimestamp, LiveType, type LiveTypeAny, type LiveTypeMeta, type MaterializedLiveType, type MutationType, Relation, type Schema, type StorageFieldType, type WhereClause, boolean, createRelations, createSchema, id, inferValue, number, object, reference, string, timestamp };
|
package/dist/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
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-
|
|
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-GNY3ZGE5.js';
|
package/dist/server.cjs
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
'use strict';var _e=require('qs'),zod=require('zod'),$=require('crypto'),kysely=require('kysely'),postgres=require('kysely/helpers/postgres');function _interopDefault(e){return e&&e.__esModule?e:{default:e}}var _e__default=/*#__PURE__*/_interopDefault(_e);var $__default=/*#__PURE__*/_interopDefault($);var fe=Object.create;var Y=Object.defineProperty;var he=Object.getOwnPropertyDescriptor;var ge=Object.getOwnPropertyNames;var Re=Object.getPrototypeOf,xe=Object.prototype.hasOwnProperty;var be=(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 ge(e))!xe.call(n,a)&&a!==t&&Y(n,a,{get:()=>e[a],enumerable:!(r=he(e,a))||r.enumerable});return n};var J=(n,e,t)=>(t=n!=null?fe(Re(n)):{},ve(Y(t,"default",{value:n,enumerable:true}),n));var _=be(O=>{Object.defineProperty(O,"__esModule",{value:true});O.parse=Ee;O.serialize=Oe;var we=/^[\u0021-\u003A\u003C\u003E-\u007E]+$/,Me=/^[\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,Ie=/^[\u0020-\u003A\u003D-\u007E]*$/,Le=Object.prototype.toString,Ae=(()=>{let n=function(){};return n.prototype=Object.create(null),n})();function Ee(n,e){let t=new Ae,r=n.length;if(r<2)return t;let a=(e==null?void 0:e.decode)||je,o=0;do{let s=n.indexOf("=",o);if(s===-1)break;let c=n.indexOf(";",o),i=c===-1?r:c;if(s>i){o=n.lastIndexOf(";",s-1)+1;continue}let T=X(n,o,s),m=ee(n,s,T),u=n.slice(T,m);if(t[u]===void 0){let p=X(n,s+1,i),y=ee(n,i,p),l=a(n.slice(p,y));t[u]=l;}o=i+1;}while(o<r);return t}function X(n,e,t){do{let r=n.charCodeAt(e);if(r!==32&&r!==9)return e}while(++e<t);return t}function ee(n,e,t){for(;e>t;){let r=n.charCodeAt(--e);if(r!==32&&r!==9)return e+1}return t}function Oe(n,e,t){let r=(t==null?void 0:t.encode)||encodeURIComponent;if(!we.test(n))throw new TypeError(`argument name is invalid: ${n}`);let a=r(e);if(!Me.test(a))throw new TypeError(`argument val is invalid: ${e}`);let o=n+"="+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(!Ie.test(t.path))throw new TypeError(`option path is invalid: ${t.path}`);o+="; Path="+t.path;}if(t.expires){if(!$e(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 je(n){if(n.indexOf("%")===-1)return n;try{return decodeURIComponent(n)}catch{return n}}function $e(n){return Le.call(n)==="[object Date]"}});var re=J(_());var j=zod.z.object({resource:zod.z.string(),where:zod.z.record(zod.z.string(),zod.z.any()).optional(),include:zod.z.record(zod.z.string(),zod.z.any()).optional(),lastSyncedAt:zod.z.string().optional(),limit:zod.z.number().optional(),sort:zod.z.array(zod.z.object({key:zod.z.string(),direction:zod.z.enum(["asc","desc"])})).optional()}),P=zod.z.record(zod.z.string(),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"});}),te=zod.z.object({id:zod.z.string().optional(),type:zod.z.literal("MUTATE"),resource:zod.z.string()}),S=te.extend({procedure:zod.z.string(),payload:zod.z.any().optional()}),I=te.extend({resourceId:zod.z.string(),payload:P});zod.z.union([S,I]);var ne=j.omit({resource:true}),V=S.omit({id:true,type:true,resource:true,procedure:true}),F=I.omit({id:true,type:true,resource:true});zod.z.union([F,V]);var ae=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?re.default.parse(r.cookie):{}},o=new URL(e.url),s=o.pathname.split("/"),c=o.searchParams,i=_e__default.default.parse(c.toString()),T=await((t=n.contextProvider)==null?void 0:t.call(n,{transport:"HTTP",headers:a.headers,cookies:a.cookies,query:i}))??{};if(e.method==="GET"){let m=s[s.length-1],{success:u,data:p,error:y}=ne.safeParse(i);if(!u)return Response.json({message:"Invalid query",code:"INVALID_QUERY",details:y},{status:400});let l=await n.handleRequest({req:{...a,type:"QUERY",resourceName:m,context:T,where:p.where,include:p.include,query:i}});return !l||!l.data?Response.json({message:"Invalid resource",code:"INVALID_RESOURCE"},{status:400}):Response.json(l.data)}if(e.method==="POST")try{let m=s[s.length-1],u=s[s.length-2],p=e.body?await e.json():{},y;if(m==="set"){let{success:w,data:M,error:C}=F.safeParse(p);if(!w)return Response.json({message:"Invalid mutation",code:"INVALID_REQUEST",details:C},{status:400});y=M;}else {let{success:w,data:M,error:C}=V.safeParse(p);if(!w)return Response.json({message:"Invalid mutation",code:"INVALID_REQUEST",details:C},{status:400});y=M;}let l=await n.handleRequest({req:{...a,type:"MUTATE",resourceName:u,input:y.payload,context:T,resourceId:y.resourceId,procedure:m!=="set"?m:void 0,query:{}}});return Response.json(l)}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 de=J(_());var b=zod.z.string(),Pe=zod.z.object({id:b,type:zod.z.literal("SUBSCRIBE"),resource:zod.z.string()}),Ve=j.extend({id:b,type:zod.z.literal("QUERY")}),ie=I.extend({id:b}),Fe=S.extend({id:b}),Ne=zod.z.union([Fe,ie]),oe=zod.z.union([Pe,Ve,Ne]),De=zod.z.object({id:b,type:zod.z.literal("REJECT"),resource:zod.z.string(),message:zod.z.string().optional()}),Ke=zod.z.object({id:b,type:zod.z.literal("REPLY"),data:zod.z.any()});zod.z.union([De,Ke,ie]);zod.z.object({resource:zod.z.string(),data:zod.z.record(zod.z.string(),P)});var ce="0123456789ABCDEFGHJKMNPQRSTVWXYZ",L=32;var Ue=16,ue=10,se=0xffffffffffff;var g;(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";})(g||(g={}));var R=class extends Error{constructor(e,t){super(`${t} (${e})`),this.name="ULIDError",this.code=e;}};function ze(n){let e=Math.floor(n()*L);return e===L&&(e=L-1),ce.charAt(e)}function ke(n){var r;let e=qe(),t=e&&(e.crypto||e.msCrypto)||(typeof $__default.default<"u"?$__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=$__default.default)!=null&&r.randomBytes)return ()=>$__default.default.randomBytes(1).readUInt8()/255;throw new R(g.PRNGDetectFailure,"Failed to find a reliable PRNG")}function qe(){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=ze(e)+t;return t}function We(n,e=ue){if(isNaN(n))throw new R(g.EncodeTimeValueMalformed,`Time must be a number: ${n}`);if(n>se)throw new R(g.EncodeTimeSizeExceeded,`Cannot encode a time larger than ${se}: ${n}`);if(n<0)throw new R(g.EncodeTimeNegative,`Time must be positive: ${n}`);if(Number.isInteger(n)===false)throw new R(g.EncodeTimeValueMalformed,`Time must be an integer: ${n}`);let t,r="";for(let a=e;a>0;a--)t=n%L,r=ce.charAt(t)+r,n=(n-t)/L;return r}function He(){return typeof WorkerGlobalScope<"u"&&self instanceof WorkerGlobalScope}function le(n,e){let t=ke(),r=Date.now();return We(r,ue)+Ze(Ue,t)}var N=()=>le().toLowerCase();var pe=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(([o,s])=>{var c;(c=e[o])==null||c.send(JSON.stringify({...a,id:a.id??N()}));}));}),(r,a)=>{var m;let o=u=>{r.send(JSON.stringify(u));},s=N(),c={headers:a.headers,cookies:typeof a.headers.cookie=="string"?de.default.parse(a.headers.cookie):{}},i=_e.parse(a.url.split("?")[1]),T=(m=n.contextProvider)==null?void 0:m.call(n,{transport:"WEBSOCKET",headers:c.headers,cookies:c.cookies,query:i});e[s]=r,console.log("Client connected:",s),r.on("message",async u=>{try{console.log("Message received from the client:",u);let p=oe.parse(JSON.parse(u.toString()));if(p.type==="SUBSCRIBE"){let{resource:y}=p;t[y]||(t[y]={}),t[y][s]={};}else if(p.type==="QUERY"){let{resource:y}=p,l=await n.handleRequest({req:{...c,type:"QUERY",resourceName:y,context:await T??{},query:i}});if(!l||!l.data)throw new Error("Invalid resource");o({id:p.id,type:"REPLY",data:{resource:y,data:Object.fromEntries(Object.entries(l.data??{}).map(([w,M])=>[w,M.value]))}});}else if(p.type==="MUTATE"){let{resource:y}=p;console.log("Received mutation from client:",p);try{let l=await n.handleRequest({req:{...c,type:"MUTATE",resourceName:y,input:p.payload,context:{messageId:p.id,...await T??{}},resourceId:p.resourceId,procedure:p.procedure,query:i}});p.procedure&&o({id:p.id,type:"REPLY",data:l});}catch(l){o({id:p.id,type:"REJECT",resource:y,message:l.message}),console.error("Error parsing mutation from the client:",l);}}}catch(p){console.error("Error handling message from the client:",p);}}),r.on("close",()=>{console.log("Connection closed",s),delete e[s];for(let u of Object.values(t))delete u[s];});}};function ye(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`,pe(e)),n.use(`${(t==null?void 0:t.basePath)??""}/`,(r,a)=>{ae(e)(ye(r)).then(s=>s.json().then(c=>a.status(s.status).send(c)));});};var D=class n{routes;constructor(e){this.routes=e.routes;}static create(e){return new n(e)}},$t=n=>D.create({...n}),Qe=n=>({handler:e=>({inputValidator:n??zod.z.undefined(),handler:e})}),K=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),[o,s]=r[this.resourceName].mergeMutation("set",e.input,a);if(!s)throw new Error("Mutation rejected");return {data:await t.rawUpsert(e.resourceName,e.resourceId,o),acceptedValues:s}};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)=>o=>a({req:o,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}))}},U=class n{middlewares;constructor(e=[]){this.middlewares=e;}createBasicRoute(e){return new K(e.name).use(...this.middlewares)}use(...e){return new n([...this.middlewares,...e])}static create(){return new n}},Ct=U.create;var h=n=>{if(n)return Array.isArray(n.value)?n.value.map(e=>h(e)):typeof n.value!="object"||n.value===null||n.value instanceof Date?n.value:Object.fromEntries(Object.entries(n.value).map(([e,t])=>[e,h(t)]))};var E=class{async insert(e,t){let r=new Date().toISOString();return h(await this.rawUpsert(e.name,t.id,{value:Object.fromEntries(Object.entries(t).map(([a,o])=>[a,{value:o,_meta:{timestamp:r}}]))}))}async update(e,t,r){let a=new Date().toISOString(),{id:o,...s}=r;return h(await this.rawUpsert(e.name,t,{value:Object.fromEntries(Object.entries(s).map(([c,i])=>[c,{value:i,_meta:{timestamp:a}}]))}))}};function H(n,e,t,r){var o,s;if(!r)return t;if(!n)throw new Error("Schema not initialized");let a=n[e];if(!a)throw new Error("Resource not found");for(let[c,i]of Object.entries(r))if(a.fields[c])(i==null?void 0:i.$eq)!==void 0?t=t.where(`${e}.${c}`,i.$eq===null?"is":"=",i.$eq):(i==null?void 0:i.$in)!==void 0?t=t.where(`${e}.${c}`,"in",i.$in):(i==null?void 0:i.$not)!==void 0?((o=i==null?void 0:i.$not)==null?void 0:o.$in)!==void 0?t=t.where(`${e}.${c}`,"not in",i.$not.$in):((s=i==null?void 0:i.$not)==null?void 0:s.$eq)!==void 0?t=t.where(`${e}.${c}`,i.$not.$eq===null?"is not":"!=",i.$not.$eq):t=t.where(`${e}.${c}`,i.$not===null?"is not":"!=",i.$not):(i==null?void 0:i.$gt)!==void 0?t=t.where(`${e}.${c}`,">",i.$gt):(i==null?void 0:i.$gte)!==void 0?t=t.where(`${e}.${c}`,">=",i.$gte):(i==null?void 0:i.$lt)!==void 0?t=t.where(`${e}.${c}`,"<",i.$lt):(i==null?void 0:i.$lte)!==void 0?t=t.where(`${e}.${c}`,"<=",i.$lte):t=t.where(`${e}.${c}`,i===null?"is":"=",i);else if(a.relations[c]){let T=a.relations[c],m=T.entity.name,u=T.type==="one"?"id":T.foreignColumn,p=T.type==="one"?T.relationalColumn:"id";t=t.leftJoin(m,`${m}.${u}`,`${e}.${p}`),t=H(n,m,t,i);}return t}function B(n,e,t,r){if(!r)return t;if(!n)throw new Error("Schema not initialized");let a=n[e];if(!a)throw new Error(`Resource not found: ${e}`);for(let o of Object.keys(r)){if(!a.relations[o])throw new Error(`Relation ${o} not found in resource ${e}`);let s=a.relations[o],c=s.entity.name,i=s.type==="one"?"id":s.foreignColumn,T=s.type==="one"?s.relationalColumn:"id",m=s.type==="one"?postgres.jsonObjectFrom:postgres.jsonArrayFrom;t=t.select(u=>m(u.selectFrom(c).selectAll(c).whereRef(`${c}.${i}`,"=",`${e}.${T}`).select(p=>postgres.jsonObjectFrom(p.selectFrom(`${c}_meta`).selectAll(`${c}_meta`).whereRef(`${c}_meta.id`,"=",`${c}.id`)).as("_meta"))).as(o));}return t}var G=class extends E{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 o=t.find(i=>i.name===r);o||await this.db.schema.createTable(r).ifNotExists().execute();let s=`${r}_meta`,c=t.find(i=>i.name===s);c||await this.db.schema.createTable(s).ifNotExists().execute();for(let[i,T]of Object.entries(a.fields)){let m=o==null?void 0:o.columns.find(y=>y.name===i),u=T.getStorageFieldType();m?m.dataType!==u.type&&console.error("Column type mismatch:",i,"expected to have type:",u.type,"but has type:",m.dataType):(await this.db.schema.alterTable(r).addColumn(i,u.type,y=>{let l=y;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(y=>{throw console.error("Error adding column",i,y),y}),u.index&&await this.db.schema.createIndex(`${r}_${i}_index`).on(r).column(i).execute().catch(y=>{})),(c==null?void 0:c.columns.find(y=>y.name===i))||await this.db.schema.alterTable(s).addColumn(i,"varchar",y=>{let l=y;return u.primary&&(l=l.primaryKey().references(`${r}.${i}`)),l}).execute();}}}async rawFindById(e,t,r){if(!this.schema)throw new Error("Schema not initialized");let a=await this.db.selectFrom(e).where("id","=",t).selectAll(e).select(s=>postgres.jsonObjectFrom(s.selectFrom(`${e}_meta`).selectAll(`${e}_meta`).whereRef(`${e}_meta.id`,"=",`${e}.id`)).as("_meta"));a=B(this.schema,e,a,r);let o=await a.executeTakeFirst();if(o)return this.convertToMaterializedLiveType(o)}async findOne(e,t,r){let a=await this.rawFindById(e.name,t,r==null?void 0:r.include);if(a)return h(a)}async rawFind(e,t,r){if(!this.schema)throw new Error("Schema not initialized");let a=this.db.selectFrom(e).selectAll(e).select(i=>postgres.jsonObjectFrom(i.selectFrom(`${e}_meta`).selectAll(`${e}_meta`).whereRef(`${e}_meta.id`,"=",`${e}.id`)).as("_meta"));a=H(this.schema,e,a,t),a=B(this.schema,e,a,r);let o=await a.execute(),s=Object.fromEntries(o.map(i=>{let{id:T,...m}=i;return [T,m]}));return Object.keys(s).length===0?{}:Object.entries(s).reduce((i,[T,m])=>(i[T]=this.convertToMaterializedLiveType(m),i),{})}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,o])=>[a,h(o)]))}async rawUpsert(e,t,r){return await this.db.transaction().execute(async a=>{var i;let o=!!await a.selectFrom(e).select("id").where("id","=",t).executeTakeFirst(),s={},c={};for(let[T,m]of Object.entries(r.value)){let u=(i=m._meta)==null?void 0:i.timestamp;u&&(s[T]=m.value,c[T]=u);}o?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()]);}),r}convertToMaterializedLiveType(e){if(!e._meta)throw new Error("Missing _meta");return {value:Object.entries(e).reduce((t,[r,a])=>{var o,s,c;return r==="_meta"||(r==="id"?t[r]={value:a}:Array.isArray(a)?t[r]={value:a.map(i=>this.convertToMaterializedLiveType(i)),_meta:{timestamp:(o=e==null?void 0:e._meta)==null?void 0:o[r]}}:typeof a=="object"&&a!==null&&!(a instanceof Date)?t[r]={...this.convertToMaterializedLiveType(a),_meta:{timestamp:(s=e==null?void 0:e._meta)==null?void 0:s[r]}}:t[r]={value:a,_meta:{timestamp:(c=e==null?void 0:e._meta)==null?void 0:c[r]}}),t},{})}}};var Q=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)=>o=>a({req:o,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}},un=Q.create;
|
|
2
|
-
exports.Route=
|
|
1
|
+
'use strict';var Ve=require('qs'),zod=require('zod'),P=require('crypto');require('js-xxhash');var kysely=require('kysely'),postgres=require('kysely/helpers/postgres');function _interopDefault(e){return e&&e.__esModule?e:{default:e}}var Ve__default=/*#__PURE__*/_interopDefault(Ve);var P__default=/*#__PURE__*/_interopDefault(P);var xe=Object.create;var Y=Object.defineProperty;var Re=Object.getOwnPropertyDescriptor;var be=Object.getOwnPropertyNames;var Se=Object.getPrototypeOf,we=Object.prototype.hasOwnProperty;var ve=(r,e)=>()=>(e||r((e={exports:{}}).exports,e),e.exports);var Ie=(r,e,t,n)=>{if(e&&typeof e=="object"||typeof e=="function")for(let i of be(e))!we.call(r,i)&&i!==t&&Y(r,i,{get:()=>e[i],enumerable:!(n=Re(e,i))||n.enumerable});return r};var X=(r,e,t)=>(t=r!=null?xe(Se(r)):{},Ie(Y(t,"default",{value:r,enumerable:true}),r));var z=ve($=>{Object.defineProperty($,"__esModule",{value:true});$.parse=$e;$.serialize=Ce;var Me=/^[\u0021-\u003A\u003C\u003E-\u007E]+$/,Le=/^[\u0021-\u003A\u003C-\u007E]*$/,Ae=/^([.]?[a-z0-9]([a-z0-9-]{0,61}[a-z0-9])?)([.][a-z0-9]([a-z0-9-]{0,61}[a-z0-9])?)*$/i,Ee=/^[\u0020-\u003A\u003D-\u007E]*$/,Oe=Object.prototype.toString,je=(()=>{let r=function(){};return r.prototype=Object.create(null),r})();function $e(r,e){let t=new je,n=r.length;if(n<2)return t;let i=(e==null?void 0:e.decode)||Pe,a=0;do{let s=r.indexOf("=",a);if(s===-1)break;let c=r.indexOf(";",a),o=c===-1?n:c;if(s>o){a=r.lastIndexOf(";",s-1)+1;continue}let y=ee(r,a,s),f=te(r,s,y),u=r.slice(y,f);if(t[u]===void 0){let d=ee(r,s+1,o),p=te(r,o,d),l=i(r.slice(d,p));t[u]=l;}a=o+1;}while(a<n);return t}function ee(r,e,t){do{let n=r.charCodeAt(e);if(n!==32&&n!==9)return e}while(++e<t);return t}function te(r,e,t){for(;e>t;){let n=r.charCodeAt(--e);if(n!==32&&n!==9)return e+1}return t}function Ce(r,e,t){let n=(t==null?void 0:t.encode)||encodeURIComponent;if(!Me.test(r))throw new TypeError(`argument name is invalid: ${r}`);let i=n(e);if(!Le.test(i))throw new TypeError(`argument val is invalid: ${e}`);let a=r+"="+i;if(!t)return a;if(t.maxAge!==void 0){if(!Number.isInteger(t.maxAge))throw new TypeError(`option maxAge is invalid: ${t.maxAge}`);a+="; Max-Age="+t.maxAge;}if(t.domain){if(!Ae.test(t.domain))throw new TypeError(`option domain is invalid: ${t.domain}`);a+="; Domain="+t.domain;}if(t.path){if(!Ee.test(t.path))throw new TypeError(`option path is invalid: ${t.path}`);a+="; Path="+t.path;}if(t.expires){if(!_e(t.expires)||!Number.isFinite(t.expires.valueOf()))throw new TypeError(`option expires is invalid: ${t.expires}`);a+="; Expires="+t.expires.toUTCString();}if(t.httpOnly&&(a+="; HttpOnly"),t.secure&&(a+="; Secure"),t.partitioned&&(a+="; Partitioned"),t.priority)switch(typeof t.priority=="string"?t.priority.toLowerCase():void 0){case "low":a+="; Priority=Low";break;case "medium":a+="; Priority=Medium";break;case "high":a+="; 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":a+="; SameSite=Strict";break;case "lax":a+="; SameSite=Lax";break;case "none":a+="; SameSite=None";break;default:throw new TypeError(`option sameSite is invalid: ${t.sameSite}`)}return a}function Pe(r){if(r.indexOf("%")===-1)return r;try{return decodeURIComponent(r)}catch{return r}}function _e(r){return Oe.call(r)==="[object Date]"}});var ie=X(z());var C=zod.z.object({resource:zod.z.string(),where:zod.z.record(zod.z.string(),zod.z.any()).optional(),include:zod.z.record(zod.z.string(),zod.z.any()).optional(),lastSyncedAt:zod.z.string().optional(),limit:zod.z.number().optional(),sort:zod.z.array(zod.z.object({key:zod.z.string(),direction:zod.z.enum(["asc","desc"])})).optional()}),V=zod.z.record(zod.z.string(),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((r,e)=>{r.id&&e.addIssue({code:zod.z.ZodIssueCode.custom,message:"Payload cannot have an id"});}),ne=zod.z.object({id:zod.z.string().optional(),type:zod.z.literal("MUTATE"),resource:zod.z.string(),resourceId:zod.z.string().optional()}),L=ne.extend({procedure:zod.z.string(),payload:zod.z.any().optional()}),A=ne.extend({procedure:zod.z.enum(["INSERT","UPDATE"]),payload:V});zod.z.union([A,L]);var re=C.omit({resource:true}),N=L.omit({id:true,type:true,resource:true,procedure:true}),F=A.omit({id:true,type:true,resource:true,procedure:true});zod.z.union([F,N]);var ae=r=>async e=>{var t;try{let n=typeof e.headers.getSetCookie=="function"?Object.fromEntries(e.headers):e.headers,i={headers:n,cookies:n.cookie?ie.default.parse(n.cookie):{}},a=new URL(e.url),s=a.pathname.split("/"),c=a.searchParams,o=Ve__default.default.parse(c.toString()),y=await((t=r.contextProvider)==null?void 0:t.call(r,{transport:"HTTP",headers:i.headers,cookies:i.cookies,query:o}))??{};if(e.method==="GET"){let f=s[s.length-1],{success:u,data:d,error:p}=re.safeParse(o);if(!u)return Response.json({message:"Invalid query",code:"INVALID_QUERY",details:p},{status:400});let l=await r.handleRequest({req:{...i,type:"QUERY",resourceName:f,context:y,where:d.where,include:d.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 f=s[s.length-1],u=s[s.length-2],d=e.body?await e.json():{},p;if(f==="insert"||f==="update"){let{success:h,data:g,error:R}=F.safeParse(d);if(!h)return Response.json({message:"Invalid mutation",code:"INVALID_REQUEST",details:R},{status:400});p=g;}else {let{success:h,data:g,error:R}=N.safeParse(d);if(!h)return Response.json({message:"Invalid mutation",code:"INVALID_REQUEST",details:R},{status:400});p=g;}let l=await r.handleRequest({req:{...i,type:"MUTATE",resourceName:u,input:p.payload,context:y,resourceId:p.resourceId,procedure:f==="insert"||f==="update"?f.toUpperCase():f,query:{}}});return Response.json(l)}catch(f){return console.error("Error parsing mutation from the client:",f),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 pe=X(z());var I=zod.z.string(),Ne=zod.z.object({id:I,type:zod.z.literal("SUBSCRIBE"),resource:zod.z.string()}),Fe=C.extend({id:I,type:zod.z.literal("QUERY")}),oe=A.extend({id:I}),De=L.extend({id:I}),Ue=zod.z.union([De,oe]),se=zod.z.union([Ne,Fe,Ue]),Ke=zod.z.object({id:I,type:zod.z.literal("REJECT"),resource:zod.z.string(),message:zod.z.string().optional()}),qe=zod.z.object({id:I,type:zod.z.literal("REPLY"),data:zod.z.any()});zod.z.union([Ke,qe,oe]);zod.z.object({resource:zod.z.string(),data:zod.z.record(zod.z.string(),V)});var ue="0123456789ABCDEFGHJKMNPQRSTVWXYZ",E=32;var ke=16,de=10,ce=0xffffffffffff;var S;(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";})(S||(S={}));var w=class extends Error{constructor(e,t){super(`${t} (${e})`),this.name="ULIDError",this.code=e;}};function We(r){let e=Math.floor(r()*E);return e===E&&(e=E-1),ue.charAt(e)}function He(r){var n;let e=Ze(),t=e&&(e.crypto||e.msCrypto)||(typeof P__default.default<"u"?P__default.default:null);if(typeof(t==null?void 0:t.getRandomValues)=="function")return ()=>{let i=new Uint8Array(1);return t.getRandomValues(i),i[0]/255};if(typeof(t==null?void 0:t.randomBytes)=="function")return ()=>t.randomBytes(1).readUInt8()/255;if((n=P__default.default)!=null&&n.randomBytes)return ()=>P__default.default.randomBytes(1).readUInt8()/255;throw new w(S.PRNGDetectFailure,"Failed to find a reliable PRNG")}function Ze(){return Qe()?self:typeof window<"u"?window:typeof global<"u"?global:typeof globalThis<"u"?globalThis:null}function Be(r,e){let t="";for(;r>0;r--)t=We(e)+t;return t}function Ge(r,e=de){if(isNaN(r))throw new w(S.EncodeTimeValueMalformed,`Time must be a number: ${r}`);if(r>ce)throw new w(S.EncodeTimeSizeExceeded,`Cannot encode a time larger than ${ce}: ${r}`);if(r<0)throw new w(S.EncodeTimeNegative,`Time must be positive: ${r}`);if(Number.isInteger(r)===false)throw new w(S.EncodeTimeValueMalformed,`Time must be an integer: ${r}`);let t,n="";for(let i=e;i>0;i--)t=r%E,n=ue.charAt(t)+n,r=(r-t)/E;return n}function Qe(){return typeof WorkerGlobalScope<"u"&&self instanceof WorkerGlobalScope}function le(r,e){let t=He(),n=Date.now();return Ge(n,de)+Be(ke,t)}var D=()=>le().toLowerCase();var ye=r=>{let e={},t={};return r.subscribeToMutations(n=>{let i=n;!i.resourceId||!i.payload||(console.log("Mutation propagated:",i),Object.entries(t[i.resource]??{}).forEach(([a,s])=>{var c;(c=e[a])==null||c.send(JSON.stringify({...i,id:i.id??D()}));}));}),(n,i)=>{var f;let a=u=>{n.send(JSON.stringify(u));},s=D(),c={headers:i.headers,cookies:typeof i.headers.cookie=="string"?pe.default.parse(i.headers.cookie):{}},o=Ve.parse(i.url.split("?")[1]),y=(f=r.contextProvider)==null?void 0:f.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 d=se.parse(JSON.parse(u.toString()));if(d.type==="SUBSCRIBE"){let{resource:p}=d;t[p]||(t[p]={}),t[p][s]={};}else if(d.type==="QUERY"){let{resource:p}=d,l=await r.handleRequest({req:{...c,type:"QUERY",resourceName:p,context:await y??{},query:o}});if(!l||!l.data)throw new Error("Invalid resource");a({id:d.id,type:"REPLY",data:{resource:p,data:Object.fromEntries(Object.entries(l.data??{}).map(([h,g])=>[h,g.value]))}});}else if(d.type==="MUTATE"){let{resource:p}=d;console.log("Received mutation from client:",d);try{let l=await r.handleRequest({req:{...c,type:"MUTATE",resourceName:p,input:d.payload,context:{messageId:d.id,...await y??{}},resourceId:d.resourceId,procedure:d.procedure,query:o}});d.procedure&&d.procedure!=="INSERT"&&d.procedure!=="UPDATE"&&a({id:d.id,type:"REPLY",data:l});}catch(l){a({id:d.id,type:"REJECT",resource:p,message:l.message}),console.error("Error parsing mutation from the client:",l);}}}catch(d){console.error("Error handling message from the client:",d);}}),n.on("close",()=>{console.log("Connection closed",s),delete e[s];for(let u of Object.values(t))delete u[s];});}};function me(r){let e=`${r.protocol}://${r.hostname}${r.url}`,t=new Headers;return Object.entries(r.headers).forEach(([n,i])=>{i&&t.set(n,Array.isArray(i)?i.join(","):i);}),new Request(e,{method:r.method,headers:t,body:r.body&&r.method!=="GET"?JSON.stringify(r.body):void 0})}var Ct=(r,e,t)=>{r.ws(`${(t==null?void 0:t.basePath)??""}/ws`,ye(e)),r.use(`${(t==null?void 0:t.basePath)??""}/`,(n,i)=>{ae(e)(me(n)).then(s=>s.json().then(c=>i.status(s.status).send(c)));});};var x=r=>{if(r)return Array.isArray(r.value)?r.value.map(e=>x(e)):typeof r.value!="object"||r.value===null||r.value instanceof Date?r.value:Object.fromEntries(Object.entries(r.value).map(([e,t])=>[e,x(t)]))};var b=(r,e,t=false)=>Object.entries(e).every(([n,i])=>{if(n==="$and")return i.every(s=>b(r,s,t));if(n==="$or")return i.some(s=>b(r,s,t));let a=(i==null?void 0:i.$eq)!==void 0?i==null?void 0:i.$eq:i;if(typeof i=="object"&&i!==null&&(i==null?void 0:i.$eq)===void 0){if(i.$in!==void 0){let s=r[n];return s===void 0?false:t?!i.$in.includes(s):i.$in.includes(s)}if(i.$not!==void 0&&!t)return b(r,{[n]:i.$not},true);if(i.$gt!==void 0){let s=r[n];return typeof s!="number"?false:t?s<=i.$gt:s>i.$gt}if(i.$gte!==void 0){let s=r[n];return typeof s!="number"?false:t?s<i.$gte:s>=i.$gte}if(i.$lt!==void 0){let s=r[n];return typeof s!="number"?false:t?s>=i.$lt:s<i.$lt}if(i.$lte!==void 0){let s=r[n];return typeof s!="number"?false:t?s>i.$lte:s<=i.$lte}return !r[n]||typeof r[n]!="object"?false:b(r[n],i,t)}return t?r[n]!==a:r[n]===a});var H=class r{routes;constructor(e){this.routes=e.routes;}static create(e){return new r(e)}},tn=r=>H.create({...r}),Xe=r=>({handler:e=>({inputValidator:r??zod.z.undefined(),handler:e})}),Z=class r{_resourceSchema;resourceName;middlewares;customMutations;authorization;constructor(e,t,n){this.resourceName=e,this.middlewares=new Set,this.customMutations=t??{},this.authorization=n;}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)throw new Error("Procedure is required for mutations");let i=this.customMutations[n.procedure];if(i){let a=i.inputValidator.parse(n.input);return n.input=a,i.handler({req:n,db:e.db,schema:e.schema})}else if(n.procedure==="INSERT"||n.procedure==="UPDATE")return this.handleSet({req:n,db:e.db,schema:e.schema,operation:n.procedure})}throw new Error("Invalid request")})();return await Array.from(this.middlewares.values()).reduceRight((n,i)=>a=>i({req:a,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:Xe}))}handleFind=async({req:e,db:t})=>{var i,a;let n=(a=(i=this.authorization)==null?void 0:i.read)==null?void 0:a.call(i,{ctx:e.context});if(typeof n=="boolean"&&!n)throw new Error("Not authorized");return {data:await t.rawFind(e.resourceName,e.where&&n&&n!==true?{$and:[e.where,n]}:n&&n!==true?n:e.where,e.include),acceptedValues:null}};handleSet=async({req:e,db:t,schema:n,operation:i})=>{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);if(i==="INSERT"&&a)throw new Error("Resource already exists");if(i==="UPDATE"&&!a)throw new Error("Resource not found");return t.transaction(async({trx:s})=>{var f,u,d,p,l;let[c,o]=n[this.resourceName].mergeMutation("set",e.input,a);if(!o)throw new Error("Mutation rejected");if(i==="INSERT"){let h=await s.rawInsert(e.resourceName,e.resourceId,c);if((f=this.authorization)!=null&&f.insert){let g=x(h);g.id=g.id??e.resourceId;let R=this.authorization.insert({ctx:e.context,value:g});if(!(typeof R=="boolean"?R:b(g,R)))throw new Error("Not authorized")}return {data:h,acceptedValues:o}}if((d=(u=this.authorization)==null?void 0:u.update)!=null&&d.preMutation){let h=x(a);h.id=h.id??e.resourceId;let g=this.authorization.update.preMutation({ctx:e.context,value:h});if(!(typeof g=="boolean"?g:b(h,g)))throw new Error("Not authorized")}let y=await s.rawUpdate(e.resourceName,e.resourceId,c);if((l=(p=this.authorization)==null?void 0:p.update)!=null&&l.postMutation){let h=x(y);h.id=h.id??e.resourceId;let g=this.authorization.update.postMutation({ctx:e.context,value:h});if(!(typeof g=="boolean"?g:b(h,g)))throw new Error("Not authorized")}return {data:y,acceptedValues:o}})}},B=class r{middlewares;constructor(e=[]){this.middlewares=e;}collectionRoute(e,t){return new Z(e.name,void 0,t).use(...this.middlewares)}use(...e){return new r([...this.middlewares,...e])}static create(){return new r}},nn=B.create;var j=class{async insert(e,t){let n=new Date().toISOString();return x(await this.rawInsert(e.name,t.id,{value:Object.fromEntries(Object.entries(t).map(([i,a])=>[i,{value:a,_meta:{timestamp:n}}]))}))}async update(e,t,n){let i=new Date().toISOString(),{id:a,...s}=n;return x(await this.rawUpdate(e.name,t,{value:Object.fromEntries(Object.entries(s).map(([c,o])=>[c,{value:o,_meta:{timestamp:i}}]))}))}};function _(r,e,t,n){if(!r)throw new Error("Schema not initialized");let i=r[e];if(!i)throw new Error("Resource not found");let a=n.$or,s=n.$and;return (a?t.or:t.and)(a?n.$or.map(c=>_(r,e,t,c)):s?n.$and.map(c=>_(r,e,t,c)):Object.entries(n).map(([c,o])=>{var y,f;if(i.fields[c])return (o==null?void 0:o.$eq)!==void 0?t(`${e}.${c}`,o.$eq===null?"is":"=",o.$eq):(o==null?void 0:o.$in)!==void 0?t(`${e}.${c}`,"in",o.$in):(o==null?void 0:o.$not)!==void 0?((y=o==null?void 0:o.$not)==null?void 0:y.$in)!==void 0?t(`${e}.${c}`,"not in",o.$not.$in):((f=o==null?void 0:o.$not)==null?void 0:f.$eq)!==void 0?t(`${e}.${c}`,o.$not.$eq===null?"is not":"!=",o.$not.$eq):t(`${e}.${c}`,o.$not===null?"is not":"!=",o.$not):(o==null?void 0:o.$gt)!==void 0?t(`${e}.${c}`,">",o.$gt):(o==null?void 0:o.$gte)!==void 0?t(`${e}.${c}`,">=",o.$gte):(o==null?void 0:o.$lt)!==void 0?t(`${e}.${c}`,"<",o.$lt):(o==null?void 0:o.$lte)!==void 0?t(`${e}.${c}`,"<=",o.$lte):t(`${e}.${c}`,o===null?"is":"=",o);if(i.relations[c]){let u=i.relations[c],d=u.entity.name;u.type==="one"?"id":u.foreignColumn;u.type==="one"?u.relationalColumn:"id";return _(r,d,t,o)}return null}).filter(Boolean))}function tt(r,e,t,n){let i=r[e];if(!i)throw new Error("Resource not found");if(!n)return t;for(let a of Object.keys(n)){if(!i.relations[a])continue;let s=i.relations[a],c=s.entity.name,o=s.type==="one"?"id":s.foreignColumn,y=s.type==="one"?s.relationalColumn:"id";t=t.leftJoin(c,`${c}.${o}`,`${e}.${y}`);}return t}function Te(r,e,t,n){return !n||Object.keys(n).length===0?t:(t=tt(r,e,t,n),t.where(i=>_(r,e,i,n)))}function G(r,e,t,n){if(!n)return t;if(!r)throw new Error("Schema not initialized");let i=r[e];if(!i)throw new Error(`Resource not found: ${e}`);for(let a of Object.keys(n)){if(!i.relations[a])throw new Error(`Relation ${a} not found in resource ${e}`);let s=i.relations[a],c=s.entity.name,o=s.type==="one"?"id":s.foreignColumn,y=s.type==="one"?s.relationalColumn:"id",f=s.type==="one"?postgres.jsonObjectFrom:postgres.jsonArrayFrom;t=t.select(u=>f(u.selectFrom(c).selectAll(c).whereRef(`${c}.${o}`,"=",`${e}.${y}`).select(d=>postgres.jsonObjectFrom(d.selectFrom(`${c}_meta`).selectAll(`${c}_meta`).whereRef(`${c}_meta.id`,"=",`${c}.id`)).as("_meta"))).as(a));}return t}var Q=class r extends j{db;schema;constructor(e,t){super(),this.isKyselyLike(e)?this.db=e:this.db=new kysely.Kysely({dialect:new kysely.PostgresDialect({pool:e})}),this.schema=t,this.rawInsert=this.rawInsert.bind(this),this.rawUpdate=this.rawUpdate.bind(this);}async updateSchema(e){this.schema=e;let t=await this.db.introspection.getTables();for(let[n,i]of Object.entries(e)){let a=t.find(o=>o.name===n);a||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,y]of Object.entries(i.fields)){let f=a==null?void 0:a.columns.find(p=>p.name===o),u=y.getStorageFieldType();f?f.dataType!==u.type&&console.error("Column type mismatch:",o,"expected to have type:",u.type,"but has type:",f.dataType):(await this.db.schema.alterTable(n).addColumn(o,u.type,p=>{let l=p;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(p=>{throw console.error("Error adding column",o,p),p}),u.index&&await this.db.schema.createIndex(`${n}_${o}_index`).on(n).column(o).execute().catch(p=>{})),(c==null?void 0:c.columns.find(p=>p.name===o))||await this.db.schema.alterTable(s).addColumn(o,"varchar",p=>{let l=p;return u.primary&&(l=l.primaryKey().references(`${n}.${o}`)),l}).execute();}}}async rawFindById(e,t,n){if(!this.schema)throw new Error("Schema not initialized");let i=await this.db.selectFrom(e).where("id","=",t).selectAll(e).select(s=>postgres.jsonObjectFrom(s.selectFrom(`${e}_meta`).selectAll(`${e}_meta`).whereRef(`${e}_meta.id`,"=",`${e}.id`)).as("_meta"));i=G(this.schema,e,i,n);let a=await i.executeTakeFirst();if(a)return this.convertToMaterializedLiveType(a)}async findOne(e,t,n){let i=await this.rawFindById(e.name,t,n==null?void 0:n.include);if(i)return x(i)}async rawFind(e,t,n){if(!this.schema)throw new Error("Schema not initialized");let i=this.db.selectFrom(e).selectAll(e).select(o=>postgres.jsonObjectFrom(o.selectFrom(`${e}_meta`).selectAll(`${e}_meta`).whereRef(`${e}_meta.id`,"=",`${e}.id`)).as("_meta"));i=Te(this.schema,e,i,t),i=G(this.schema,e,i,n);let a=await i.execute(),s=Object.fromEntries(a.map(o=>{let{id:y,...f}=o;return [y,f]}));return Object.keys(s).length===0?{}:Object.entries(s).reduce((o,[y,f])=>(o[y]=this.convertToMaterializedLiveType(f),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(([i,a])=>[i,x(a)]))}async rawInsert(e,t,n){var s;let i={},a={};for(let[c,o]of Object.entries(n.value)){let y=(s=o._meta)==null?void 0:s.timestamp;y&&(i[c]=o.value,a[c]=y);}return await this.db.insertInto(e).values({...i,id:t}).execute().then(()=>{this.db.insertInto(`${e}_meta`).values({...a,id:t}).execute();}),n}async rawUpdate(e,t,n){var s;let i={},a={};for(let[c,o]of Object.entries(n.value)){let y=(s=o._meta)==null?void 0:s.timestamp;y&&(i[c]=o.value,a[c]=y);}return await Promise.all([this.db.updateTable(e).set(i).where("id","=",t).execute(),this.db.insertInto(`${e}_meta`).values({...a,id:t}).onConflict(c=>c.column("id").doUpdateSet(a)).execute()]),n}async transaction(e){if(!this.schema)throw new Error("Schema not initialized");if(this.db.isTransaction){let n=Math.random().toString(36).substring(2,15),i=await this.db.savepoint(n).execute();try{return await e({trx:this,commit:()=>i.releaseSavepoint(n).execute().then(()=>{}),rollback:()=>i.rollbackToSavepoint(n).execute().then(()=>{})}).then(a=>i.isCommitted||i.isRolledBack?a:i.releaseSavepoint(n).execute().then(()=>a))}catch(a){throw await i.rollbackToSavepoint(n).execute().catch(()=>{}),a}}let t=await this.db.startTransaction().execute();try{return await e({trx:new r(t,this.schema),commit:()=>t.commit().execute(),rollback:()=>t.rollback().execute()}).then(n=>t.isCommitted||t.isRolledBack?n:t.commit().execute().then(()=>n))}catch(n){throw await t.rollback().execute(),n}}convertToMaterializedLiveType(e){return {value:Object.entries(e).reduce((t,[n,i])=>{var a,s,c;return n==="_meta"||(n==="id"?t[n]={value:i}:Array.isArray(i)?t[n]={value:i.map(o=>this.convertToMaterializedLiveType(o)),_meta:{timestamp:(a=e==null?void 0:e._meta)==null?void 0:a[n]}}:typeof i=="object"&&i!==null&&!(i instanceof Date)?t[n]={...this.convertToMaterializedLiveType(i),_meta:{timestamp:(s=e==null?void 0:e._meta)==null?void 0:s[n]}}:t[n]={value:i,_meta:{timestamp:(c=e==null?void 0:e._meta)==null?void 0:c[n]}}),t},{})}}isKyselyLike(e){if(e instanceof kysely.Kysely)return true;if(!e||typeof e!="object")return false;let t=e,n=typeof t.selectFrom=="function",i=typeof t.startTransaction=="function",a=typeof t.savepoint=="function",s=typeof t.isTransaction=="boolean"||typeof t.isTransaction=="function";return n&&i||a&&s}};var J=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,i)=>a=>i({req:a,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&&(e.req.procedure==="INSERT"||e.req.procedure==="UPDATE")&&this.mutationSubscriptions.forEach(n=>{n({id:e.req.context.messageId,type:"MUTATE",resource:e.req.resourceName,payload:t.acceptedValues??{},resourceId:e.req.resourceId,procedure:e.req.procedure});}),t}use(e){return this.middlewares.add(e),this}context(e){return this.contextProvider=e,this}},gn=J.create;
|
|
2
|
+
exports.Route=Z;exports.RouteFactory=B;exports.Router=H;exports.SQLStorage=Q;exports.Server=J;exports.Storage=j;exports.expressAdapter=Ct;exports.routeFactory=nn;exports.router=tn;exports.server=gn;
|
package/dist/server.d.cts
CHANGED
|
@@ -1,27 +1,36 @@
|
|
|
1
|
-
import { z
|
|
2
|
-
import { LiveObjectAny, Schema, MaterializedLiveType, IncludeClause, InferLiveObject,
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
import { LiveObjectAny, Schema, WhereClause, InferLiveObjectWithRelationalIds, MaterializedLiveType, IncludeClause, InferLiveObject, InferInsert, InferUpdate } from './index.cjs';
|
|
3
|
+
import * as z3 from 'zod/v3';
|
|
4
|
+
import * as z4 from 'zod/v4/core';
|
|
3
5
|
import { PostgresPool } from 'kysely';
|
|
4
6
|
import { Application } from 'express-ws';
|
|
5
7
|
|
|
6
|
-
declare const
|
|
8
|
+
declare const defaultMutationSchema: z.ZodObject<{
|
|
7
9
|
id: z.ZodOptional<z.ZodString>;
|
|
8
10
|
type: z.ZodLiteral<"MUTATE">;
|
|
9
11
|
resource: z.ZodString;
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
resource: z.ZodString;
|
|
16
|
-
resourceId: z.ZodString;
|
|
12
|
+
resourceId: z.ZodOptional<z.ZodString>;
|
|
13
|
+
procedure: z.ZodEnum<{
|
|
14
|
+
INSERT: "INSERT";
|
|
15
|
+
UPDATE: "UPDATE";
|
|
16
|
+
}>;
|
|
17
17
|
payload: z.ZodRecord<z.ZodString, z.ZodObject<{
|
|
18
18
|
value: z.ZodNullable<z.ZodUnion<[z.ZodUnion<[z.ZodUnion<[z.ZodString, z.ZodNumber]>, z.ZodBoolean]>, z.ZodDate]>>;
|
|
19
19
|
_meta: z.ZodOptional<z.ZodObject<{
|
|
20
20
|
timestamp: z.ZodNullable<z.ZodOptional<z.ZodString>>;
|
|
21
21
|
}, z.core.$strip>>;
|
|
22
22
|
}, z.core.$strip>>;
|
|
23
|
-
}, z.core.$strip
|
|
24
|
-
type
|
|
23
|
+
}, z.core.$strip>;
|
|
24
|
+
type DefaultMutation = Omit<z.infer<typeof defaultMutationSchema>, "resourceId"> & {
|
|
25
|
+
resourceId: string;
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
type Simplify<T> = T extends Record<string, unknown> ? {
|
|
29
|
+
[K in keyof T]: Simplify<T[K]>;
|
|
30
|
+
} : T extends Array<infer U> ? Array<Simplify<U>> : T;
|
|
31
|
+
|
|
32
|
+
/** biome-ignore-all lint/suspicious/noExplicitAny: false positive */
|
|
33
|
+
/** biome-ignore-all lint/style/noNonNullAssertion: false positive */
|
|
25
34
|
|
|
26
35
|
type RouteRecord = Record<string, AnyRoute>;
|
|
27
36
|
declare class Router<TRoutes extends RouteRecord> {
|
|
@@ -48,22 +57,36 @@ type RequestHandler<TInput, TResult, TSchema extends Schema<any> = Schema<any>>
|
|
|
48
57
|
db: Storage;
|
|
49
58
|
schema: TSchema;
|
|
50
59
|
}) => Promise<TResult>;
|
|
51
|
-
type Mutation<TInputValidator extends ZodTypeAny, // TODO use StandardSchema instead
|
|
60
|
+
type Mutation<TInputValidator extends z3.ZodTypeAny | z4.$ZodType, // TODO use StandardSchema instead
|
|
52
61
|
THandler extends RequestHandler<z.infer<TInputValidator>, any, any>> = {
|
|
53
62
|
inputValidator: TInputValidator;
|
|
54
63
|
handler: THandler;
|
|
55
64
|
};
|
|
56
|
-
declare const mutationCreator: <TInputValidator extends ZodTypeAny>(validator?: TInputValidator) => {
|
|
65
|
+
declare const mutationCreator: <TInputValidator extends z3.ZodTypeAny | z4.$ZodType>(validator?: TInputValidator) => {
|
|
57
66
|
handler: <THandler extends RequestHandler<z.infer<TInputValidator>, any, any>>(handler: THandler) => Mutation<TInputValidator, THandler>;
|
|
58
67
|
};
|
|
68
|
+
type ReadAuthorizationHandler<TShape extends LiveObjectAny> = (opts: {
|
|
69
|
+
ctx: ParsedRequest["context"];
|
|
70
|
+
}) => WhereClause<TShape> | boolean;
|
|
71
|
+
type MutationAuthorizationHandler<TShape extends LiveObjectAny> = (opts: {
|
|
72
|
+
ctx: ParsedRequest["context"];
|
|
73
|
+
value: Simplify<InferLiveObjectWithRelationalIds<TShape>>;
|
|
74
|
+
}) => WhereClause<TShape> | boolean;
|
|
75
|
+
type Authorization<TShape extends LiveObjectAny> = {
|
|
76
|
+
read?: ReadAuthorizationHandler<TShape>;
|
|
77
|
+
insert?: MutationAuthorizationHandler<TShape>;
|
|
78
|
+
update?: {
|
|
79
|
+
preMutation?: MutationAuthorizationHandler<TShape>;
|
|
80
|
+
postMutation?: MutationAuthorizationHandler<TShape>;
|
|
81
|
+
};
|
|
82
|
+
};
|
|
59
83
|
declare class Route<TResourceSchema extends LiveObjectAny, TMiddleware extends Middleware<any>, TCustomMutations extends Record<string, Mutation<any, RequestHandler<any, any>>>> {
|
|
60
84
|
readonly _resourceSchema: TResourceSchema;
|
|
61
85
|
readonly resourceName: TResourceSchema["name"];
|
|
62
86
|
readonly middlewares: Set<TMiddleware>;
|
|
63
87
|
readonly customMutations: TCustomMutations;
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
private handleSet;
|
|
88
|
+
readonly authorization?: Authorization<TResourceSchema>;
|
|
89
|
+
constructor(resourceName: TResourceSchema["name"], customMutations?: TCustomMutations, authorization?: Authorization<TResourceSchema>);
|
|
67
90
|
handleRequest(opts: {
|
|
68
91
|
req: ParsedRequest;
|
|
69
92
|
db: Storage;
|
|
@@ -73,20 +96,18 @@ declare class Route<TResourceSchema extends LiveObjectAny, TMiddleware extends M
|
|
|
73
96
|
withMutations<T extends Record<string, Mutation<any, RequestHandler<any, any>>>>(mutationFactory: (opts: {
|
|
74
97
|
mutation: typeof mutationCreator;
|
|
75
98
|
}) => T): Route<TResourceSchema, TMiddleware, T>;
|
|
99
|
+
private handleFind;
|
|
100
|
+
private handleSet;
|
|
76
101
|
}
|
|
77
102
|
declare class RouteFactory {
|
|
78
103
|
private middlewares;
|
|
79
104
|
private constructor();
|
|
80
|
-
|
|
105
|
+
collectionRoute<T extends LiveObjectAny>(shape: T, authorization?: Authorization<T>): Route<T, Middleware<any>, Record<string, never>>;
|
|
81
106
|
use(...middlewares: Middleware<any>[]): RouteFactory;
|
|
82
107
|
static create(): RouteFactory;
|
|
83
108
|
}
|
|
84
109
|
declare const routeFactory: typeof RouteFactory.create;
|
|
85
|
-
type AnyRoute = Route<
|
|
86
|
-
|
|
87
|
-
type Simplify<T> = T extends Record<string, unknown> ? {
|
|
88
|
-
[K in keyof T]: Simplify<T[K]>;
|
|
89
|
-
} : T;
|
|
110
|
+
type AnyRoute = Route<any, Middleware<any>, Record<string, any>>;
|
|
90
111
|
|
|
91
112
|
/** biome-ignore-all lint/suspicious/noExplicitAny: false positive */
|
|
92
113
|
|
|
@@ -98,8 +119,13 @@ declare abstract class Storage {
|
|
|
98
119
|
where?: WhereClause<T>;
|
|
99
120
|
include?: IncludeClause<T>;
|
|
100
121
|
}): Promise<Record<string, InferLiveObject<T>>>;
|
|
101
|
-
insert<T extends LiveObjectAny>(resource: T, value: Simplify<
|
|
102
|
-
update<T extends LiveObjectAny>(resource: T, resourceId: string, value:
|
|
122
|
+
insert<T extends LiveObjectAny>(resource: T, value: Simplify<InferInsert<T>>): Promise<InferLiveObject<T>>;
|
|
123
|
+
update<T extends LiveObjectAny>(resource: T, resourceId: string, value: InferUpdate<T>): Promise<InferLiveObject<T>>;
|
|
124
|
+
abstract transaction<T>(fn: (opts: {
|
|
125
|
+
trx: Storage;
|
|
126
|
+
commit: () => Promise<void>;
|
|
127
|
+
rollback: () => Promise<void>;
|
|
128
|
+
}) => Promise<T>): Promise<T>;
|
|
103
129
|
}
|
|
104
130
|
|
|
105
131
|
/** biome-ignore-all lint/suspicious/noExplicitAny: false positive */
|
|
@@ -115,7 +141,13 @@ declare class SQLStorage extends Storage {
|
|
|
115
141
|
where?: WhereClause<T>;
|
|
116
142
|
include?: IncludeClause<T>;
|
|
117
143
|
}): Promise<Record<string, InferLiveObject<T>>>;
|
|
144
|
+
transaction<T>(fn: (opts: {
|
|
145
|
+
trx: Storage;
|
|
146
|
+
commit: () => Promise<void>;
|
|
147
|
+
rollback: () => Promise<void>;
|
|
148
|
+
}) => Promise<T>): Promise<T>;
|
|
118
149
|
private convertToMaterializedLiveType;
|
|
150
|
+
private isKyselyLike;
|
|
119
151
|
}
|
|
120
152
|
|
|
121
153
|
declare const expressAdapter: (app: Application, server: Server<AnyRouter>, options?: {
|
|
@@ -139,7 +171,7 @@ type ContextProvider = (req: Pick<ParsedRequest, "headers" | "cookies" | "query"
|
|
|
139
171
|
transport: "HTTP" | "WEBSOCKET";
|
|
140
172
|
}) => Record<string, any>;
|
|
141
173
|
type RequestType = ParsedRequest["type"];
|
|
142
|
-
type MutationHandler = (mutation:
|
|
174
|
+
type MutationHandler = (mutation: DefaultMutation) => void;
|
|
143
175
|
type NextFunction<T> = (req: ParsedRequest) => Promise<T> | T;
|
|
144
176
|
type Middleware<T = any> = (opts: {
|
|
145
177
|
req: ParsedRequest;
|
|
@@ -169,4 +201,4 @@ declare class Server<TRouter extends AnyRouter> {
|
|
|
169
201
|
}
|
|
170
202
|
declare const server: typeof Server.create;
|
|
171
203
|
|
|
172
|
-
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 };
|
|
204
|
+
export { type AnyRoute, type AnyRouter, type Authorization, type ContextProvider, type Middleware, type Mutation, type MutationAuthorizationHandler, type MutationHandler, type MutationResult, type NextFunction, type ParsedRequest, type QueryResult, type ReadAuthorizationHandler, type RequestHandler, type RequestType, Route, RouteFactory, type RouteRecord, Router, SQLStorage, Server, Storage, expressAdapter, routeFactory, router, server };
|
package/dist/server.d.ts
CHANGED
|
@@ -1,27 +1,36 @@
|
|
|
1
|
-
import { z
|
|
2
|
-
import { LiveObjectAny, Schema, MaterializedLiveType, IncludeClause, InferLiveObject,
|
|
1
|
+
import { z } from 'zod';
|
|
2
|
+
import { LiveObjectAny, Schema, WhereClause, InferLiveObjectWithRelationalIds, MaterializedLiveType, IncludeClause, InferLiveObject, InferInsert, InferUpdate } from './index.js';
|
|
3
|
+
import * as z3 from 'zod/v3';
|
|
4
|
+
import * as z4 from 'zod/v4/core';
|
|
3
5
|
import { PostgresPool } from 'kysely';
|
|
4
6
|
import { Application } from 'express-ws';
|
|
5
7
|
|
|
6
|
-
declare const
|
|
8
|
+
declare const defaultMutationSchema: z.ZodObject<{
|
|
7
9
|
id: z.ZodOptional<z.ZodString>;
|
|
8
10
|
type: z.ZodLiteral<"MUTATE">;
|
|
9
11
|
resource: z.ZodString;
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
resource: z.ZodString;
|
|
16
|
-
resourceId: z.ZodString;
|
|
12
|
+
resourceId: z.ZodOptional<z.ZodString>;
|
|
13
|
+
procedure: z.ZodEnum<{
|
|
14
|
+
INSERT: "INSERT";
|
|
15
|
+
UPDATE: "UPDATE";
|
|
16
|
+
}>;
|
|
17
17
|
payload: z.ZodRecord<z.ZodString, z.ZodObject<{
|
|
18
18
|
value: z.ZodNullable<z.ZodUnion<[z.ZodUnion<[z.ZodUnion<[z.ZodString, z.ZodNumber]>, z.ZodBoolean]>, z.ZodDate]>>;
|
|
19
19
|
_meta: z.ZodOptional<z.ZodObject<{
|
|
20
20
|
timestamp: z.ZodNullable<z.ZodOptional<z.ZodString>>;
|
|
21
21
|
}, z.core.$strip>>;
|
|
22
22
|
}, z.core.$strip>>;
|
|
23
|
-
}, z.core.$strip
|
|
24
|
-
type
|
|
23
|
+
}, z.core.$strip>;
|
|
24
|
+
type DefaultMutation = Omit<z.infer<typeof defaultMutationSchema>, "resourceId"> & {
|
|
25
|
+
resourceId: string;
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
type Simplify<T> = T extends Record<string, unknown> ? {
|
|
29
|
+
[K in keyof T]: Simplify<T[K]>;
|
|
30
|
+
} : T extends Array<infer U> ? Array<Simplify<U>> : T;
|
|
31
|
+
|
|
32
|
+
/** biome-ignore-all lint/suspicious/noExplicitAny: false positive */
|
|
33
|
+
/** biome-ignore-all lint/style/noNonNullAssertion: false positive */
|
|
25
34
|
|
|
26
35
|
type RouteRecord = Record<string, AnyRoute>;
|
|
27
36
|
declare class Router<TRoutes extends RouteRecord> {
|
|
@@ -48,22 +57,36 @@ type RequestHandler<TInput, TResult, TSchema extends Schema<any> = Schema<any>>
|
|
|
48
57
|
db: Storage;
|
|
49
58
|
schema: TSchema;
|
|
50
59
|
}) => Promise<TResult>;
|
|
51
|
-
type Mutation<TInputValidator extends ZodTypeAny, // TODO use StandardSchema instead
|
|
60
|
+
type Mutation<TInputValidator extends z3.ZodTypeAny | z4.$ZodType, // TODO use StandardSchema instead
|
|
52
61
|
THandler extends RequestHandler<z.infer<TInputValidator>, any, any>> = {
|
|
53
62
|
inputValidator: TInputValidator;
|
|
54
63
|
handler: THandler;
|
|
55
64
|
};
|
|
56
|
-
declare const mutationCreator: <TInputValidator extends ZodTypeAny>(validator?: TInputValidator) => {
|
|
65
|
+
declare const mutationCreator: <TInputValidator extends z3.ZodTypeAny | z4.$ZodType>(validator?: TInputValidator) => {
|
|
57
66
|
handler: <THandler extends RequestHandler<z.infer<TInputValidator>, any, any>>(handler: THandler) => Mutation<TInputValidator, THandler>;
|
|
58
67
|
};
|
|
68
|
+
type ReadAuthorizationHandler<TShape extends LiveObjectAny> = (opts: {
|
|
69
|
+
ctx: ParsedRequest["context"];
|
|
70
|
+
}) => WhereClause<TShape> | boolean;
|
|
71
|
+
type MutationAuthorizationHandler<TShape extends LiveObjectAny> = (opts: {
|
|
72
|
+
ctx: ParsedRequest["context"];
|
|
73
|
+
value: Simplify<InferLiveObjectWithRelationalIds<TShape>>;
|
|
74
|
+
}) => WhereClause<TShape> | boolean;
|
|
75
|
+
type Authorization<TShape extends LiveObjectAny> = {
|
|
76
|
+
read?: ReadAuthorizationHandler<TShape>;
|
|
77
|
+
insert?: MutationAuthorizationHandler<TShape>;
|
|
78
|
+
update?: {
|
|
79
|
+
preMutation?: MutationAuthorizationHandler<TShape>;
|
|
80
|
+
postMutation?: MutationAuthorizationHandler<TShape>;
|
|
81
|
+
};
|
|
82
|
+
};
|
|
59
83
|
declare class Route<TResourceSchema extends LiveObjectAny, TMiddleware extends Middleware<any>, TCustomMutations extends Record<string, Mutation<any, RequestHandler<any, any>>>> {
|
|
60
84
|
readonly _resourceSchema: TResourceSchema;
|
|
61
85
|
readonly resourceName: TResourceSchema["name"];
|
|
62
86
|
readonly middlewares: Set<TMiddleware>;
|
|
63
87
|
readonly customMutations: TCustomMutations;
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
private handleSet;
|
|
88
|
+
readonly authorization?: Authorization<TResourceSchema>;
|
|
89
|
+
constructor(resourceName: TResourceSchema["name"], customMutations?: TCustomMutations, authorization?: Authorization<TResourceSchema>);
|
|
67
90
|
handleRequest(opts: {
|
|
68
91
|
req: ParsedRequest;
|
|
69
92
|
db: Storage;
|
|
@@ -73,20 +96,18 @@ declare class Route<TResourceSchema extends LiveObjectAny, TMiddleware extends M
|
|
|
73
96
|
withMutations<T extends Record<string, Mutation<any, RequestHandler<any, any>>>>(mutationFactory: (opts: {
|
|
74
97
|
mutation: typeof mutationCreator;
|
|
75
98
|
}) => T): Route<TResourceSchema, TMiddleware, T>;
|
|
99
|
+
private handleFind;
|
|
100
|
+
private handleSet;
|
|
76
101
|
}
|
|
77
102
|
declare class RouteFactory {
|
|
78
103
|
private middlewares;
|
|
79
104
|
private constructor();
|
|
80
|
-
|
|
105
|
+
collectionRoute<T extends LiveObjectAny>(shape: T, authorization?: Authorization<T>): Route<T, Middleware<any>, Record<string, never>>;
|
|
81
106
|
use(...middlewares: Middleware<any>[]): RouteFactory;
|
|
82
107
|
static create(): RouteFactory;
|
|
83
108
|
}
|
|
84
109
|
declare const routeFactory: typeof RouteFactory.create;
|
|
85
|
-
type AnyRoute = Route<
|
|
86
|
-
|
|
87
|
-
type Simplify<T> = T extends Record<string, unknown> ? {
|
|
88
|
-
[K in keyof T]: Simplify<T[K]>;
|
|
89
|
-
} : T;
|
|
110
|
+
type AnyRoute = Route<any, Middleware<any>, Record<string, any>>;
|
|
90
111
|
|
|
91
112
|
/** biome-ignore-all lint/suspicious/noExplicitAny: false positive */
|
|
92
113
|
|
|
@@ -98,8 +119,13 @@ declare abstract class Storage {
|
|
|
98
119
|
where?: WhereClause<T>;
|
|
99
120
|
include?: IncludeClause<T>;
|
|
100
121
|
}): Promise<Record<string, InferLiveObject<T>>>;
|
|
101
|
-
insert<T extends LiveObjectAny>(resource: T, value: Simplify<
|
|
102
|
-
update<T extends LiveObjectAny>(resource: T, resourceId: string, value:
|
|
122
|
+
insert<T extends LiveObjectAny>(resource: T, value: Simplify<InferInsert<T>>): Promise<InferLiveObject<T>>;
|
|
123
|
+
update<T extends LiveObjectAny>(resource: T, resourceId: string, value: InferUpdate<T>): Promise<InferLiveObject<T>>;
|
|
124
|
+
abstract transaction<T>(fn: (opts: {
|
|
125
|
+
trx: Storage;
|
|
126
|
+
commit: () => Promise<void>;
|
|
127
|
+
rollback: () => Promise<void>;
|
|
128
|
+
}) => Promise<T>): Promise<T>;
|
|
103
129
|
}
|
|
104
130
|
|
|
105
131
|
/** biome-ignore-all lint/suspicious/noExplicitAny: false positive */
|
|
@@ -115,7 +141,13 @@ declare class SQLStorage extends Storage {
|
|
|
115
141
|
where?: WhereClause<T>;
|
|
116
142
|
include?: IncludeClause<T>;
|
|
117
143
|
}): Promise<Record<string, InferLiveObject<T>>>;
|
|
144
|
+
transaction<T>(fn: (opts: {
|
|
145
|
+
trx: Storage;
|
|
146
|
+
commit: () => Promise<void>;
|
|
147
|
+
rollback: () => Promise<void>;
|
|
148
|
+
}) => Promise<T>): Promise<T>;
|
|
118
149
|
private convertToMaterializedLiveType;
|
|
150
|
+
private isKyselyLike;
|
|
119
151
|
}
|
|
120
152
|
|
|
121
153
|
declare const expressAdapter: (app: Application, server: Server<AnyRouter>, options?: {
|
|
@@ -139,7 +171,7 @@ type ContextProvider = (req: Pick<ParsedRequest, "headers" | "cookies" | "query"
|
|
|
139
171
|
transport: "HTTP" | "WEBSOCKET";
|
|
140
172
|
}) => Record<string, any>;
|
|
141
173
|
type RequestType = ParsedRequest["type"];
|
|
142
|
-
type MutationHandler = (mutation:
|
|
174
|
+
type MutationHandler = (mutation: DefaultMutation) => void;
|
|
143
175
|
type NextFunction<T> = (req: ParsedRequest) => Promise<T> | T;
|
|
144
176
|
type Middleware<T = any> = (opts: {
|
|
145
177
|
req: ParsedRequest;
|
|
@@ -169,4 +201,4 @@ declare class Server<TRouter extends AnyRouter> {
|
|
|
169
201
|
}
|
|
170
202
|
declare const server: typeof Server.create;
|
|
171
203
|
|
|
172
|
-
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 };
|
|
204
|
+
export { type AnyRoute, type AnyRouter, type Authorization, type ContextProvider, type Middleware, type Mutation, type MutationAuthorizationHandler, type MutationHandler, type MutationResult, type NextFunction, type ParsedRequest, type QueryResult, type ReadAuthorizationHandler, 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,r}from'./chunk-LLHCJUB6.js';import Re,{parse}from'qs';import {z as z$1}from'zod';import $ from'node:crypto';import {Kysely,PostgresDialect}from'kysely';import {jsonObjectFrom,jsonArrayFrom}from'kysely/helpers/postgres';var O=a(v=>{Object.defineProperty(v,"__esModule",{value:true});v.parse=fe;v.serialize=ye;var ce=/^[\u0021-\u003A\u003C\u003E-\u007E]+$/,de=/^[\u0021-\u003A\u003C-\u007E]*$/,ue=/^([.]?[a-z0-9]([a-z0-9-]{0,61}[a-z0-9])?)([.][a-z0-9]([a-z0-9-]{0,61}[a-z0-9])?)*$/i,le=/^[\u0020-\u003A\u003D-\u007E]*$/,me=Object.prototype.toString,pe=(()=>{let r=function(){};return r.prototype=Object.create(null),r})();function fe(r,e){let t=new pe,n=r.length;if(n<2)return t;let a=(e==null?void 0:e.decode)||he,o=0;do{let s=r.indexOf("=",o);if(s===-1)break;let c=r.indexOf(";",o),i=c===-1?n:c;if(s>i){o=r.lastIndexOf(";",s-1)+1;continue}let y=q(r,o,s),f=B(r,s,y),d=r.slice(y,f);if(t[d]===void 0){let m=q(r,s+1,i),p=B(r,i,m),u=a(r.slice(m,p));t[d]=u;}o=i+1;}while(o<n);return t}function q(r,e,t){do{let n=r.charCodeAt(e);if(n!==32&&n!==9)return e}while(++e<t);return t}function B(r,e,t){for(;e>t;){let n=r.charCodeAt(--e);if(n!==32&&n!==9)return e+1}return t}function ye(r,e,t){let n=(t==null?void 0:t.encode)||encodeURIComponent;if(!ce.test(r))throw new TypeError(`argument name is invalid: ${r}`);let a=n(e);if(!de.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(!ue.test(t.domain))throw new TypeError(`option domain is invalid: ${t.domain}`);o+="; Domain="+t.domain;}if(t.path){if(!le.test(t.path))throw new TypeError(`option path is invalid: ${t.path}`);o+="; Path="+t.path;}if(t.expires){if(!Te(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 he(r){if(r.indexOf("%")===-1)return r;try{return decodeURIComponent(r)}catch{return r}}function Te(r){return me.call(r)==="[object Date]"}});var Q=b(O(),1);var A=z$1.object({resource:z$1.string(),where:z$1.record(z$1.string(),z$1.any()).optional(),include:z$1.record(z$1.string(),z$1.any()).optional(),lastSyncedAt:z$1.string().optional(),limit:z$1.number().optional(),sort:z$1.array(z$1.object({key:z$1.string(),direction:z$1.enum(["asc","desc"])})).optional()}),L=z$1.record(z$1.string(),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"});}),H=z$1.object({id:z$1.string().optional(),type:z$1.literal("MUTATE"),resource:z$1.string()}),S=H.extend({procedure:z$1.string(),payload:z$1.any().optional()}),M=H.extend({resourceId:z$1.string(),payload:L});z$1.union([S,M]);var G=A.omit({resource:true}),P=S.omit({id:true,type:true,resource:true,procedure:true}),C=M.omit({id:true,type:true,resource:true});z$1.union([C,P]);var W=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?Q.default.parse(n.cookie):{}},o=new URL(e.url),s=o.pathname.split("/"),c=o.searchParams,i=Re.parse(c.toString()),y=await((t=r.contextProvider)==null?void 0:t.call(r,{transport:"HTTP",headers:a.headers,cookies:a.cookies,query:i}))??{};if(e.method==="GET"){let f=s[s.length-1],{success:d,data:m,error:p}=G.safeParse(i);if(!d)return Response.json({message:"Invalid query",code:"INVALID_QUERY",details:p},{status:400});let u=await r.handleRequest({req:{...a,type:"QUERY",resourceName:f,context:y,where:m.where,include:m.include,query:i}});return !u||!u.data?Response.json({message:"Invalid resource",code:"INVALID_RESOURCE"},{status:400}):Response.json(u.data)}if(e.method==="POST")try{let f=s[s.length-1],d=s[s.length-2],m=e.body?await e.json():{},p;if(f==="set"){let{success:b,data:x,error:j}=C.safeParse(m);if(!b)return Response.json({message:"Invalid mutation",code:"INVALID_REQUEST",details:j},{status:400});p=x;}else {let{success:b,data:x,error:j}=P.safeParse(m);if(!b)return Response.json({message:"Invalid mutation",code:"INVALID_REQUEST",details:j},{status:400});p=x;}let u=await r.handleRequest({req:{...a,type:"MUTATE",resourceName:d,input:p.payload,context:y,resourceId:p.resourceId,procedure:f!=="set"?f:void 0,query:{}}});return Response.json(u)}catch(f){return console.error("Error parsing mutation from the client:",f),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 re=b(O(),1);var w=z$1.string(),we=z$1.object({id:w,type:z$1.literal("SUBSCRIBE"),resource:z$1.string()}),be=A.extend({id:w,type:z$1.literal("QUERY")}),Y=M.extend({id:w}),xe=S.extend({id:w}),Se=z$1.union([xe,Y]),J=z$1.union([we,be,Se]),Me=z$1.object({id:w,type:z$1.literal("REJECT"),resource:z$1.string(),message:z$1.string().optional()}),Ie=z$1.object({id:w,type:z$1.literal("REPLY"),data:z$1.any()});z$1.union([Me,Ie,Y]);z$1.object({resource:z$1.string(),data:z$1.record(z$1.string(),L)});var X="0123456789ABCDEFGHJKMNPQRSTVWXYZ",I=32;var Ee=16,ee=10,K=0xffffffffffff;var T;(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";})(T||(T={}));var g=class extends Error{constructor(e,t){super(`${t} (${e})`),this.name="ULIDError",this.code=e;}};function ve(r){let e=Math.floor(r()*I);return e===I&&(e=I-1),X.charAt(e)}function Ae(r){var n;let e=$e(),t=e&&(e.crypto||e.msCrypto)||(typeof $<"u"?$: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=$)!=null&&n.randomBytes)return ()=>$.randomBytes(1).readUInt8()/255;throw new g(T.PRNGDetectFailure,"Failed to find a reliable PRNG")}function $e(){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=ee){if(isNaN(r))throw new g(T.EncodeTimeValueMalformed,`Time must be a number: ${r}`);if(r>K)throw new g(T.EncodeTimeSizeExceeded,`Cannot encode a time larger than ${K}: ${r}`);if(r<0)throw new g(T.EncodeTimeNegative,`Time must be positive: ${r}`);if(Number.isInteger(r)===false)throw new g(T.EncodeTimeValueMalformed,`Time must be an integer: ${r}`);let t,n="";for(let a=e;a>0;a--)t=r%I,n=X.charAt(t)+n,r=(r-t)/I;return n}function Le(){return typeof WorkerGlobalScope<"u"&&self instanceof WorkerGlobalScope}function te(r,e){let t=Ae(),n=Date.now();return Oe(n,ee)+je(Ee,t)}var _=()=>te().toLowerCase();var ne=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,s])=>{var c;(c=e[o])==null||c.send(JSON.stringify({...a,id:a.id??_()}));}));}),(n,a)=>{var f;let o=d=>{n.send(JSON.stringify(d));},s=_(),c={headers:a.headers,cookies:typeof a.headers.cookie=="string"?re.default.parse(a.headers.cookie):{}},i=parse(a.url.split("?")[1]),y=(f=r.contextProvider)==null?void 0:f.call(r,{transport:"WEBSOCKET",headers:c.headers,cookies:c.cookies,query:i});e[s]=n,console.log("Client connected:",s),n.on("message",async d=>{try{console.log("Message received from the client:",d);let m=J.parse(JSON.parse(d.toString()));if(m.type==="SUBSCRIBE"){let{resource:p}=m;t[p]||(t[p]={}),t[p][s]={};}else if(m.type==="QUERY"){let{resource:p}=m,u=await r.handleRequest({req:{...c,type:"QUERY",resourceName:p,context:await y??{},query:i}});if(!u||!u.data)throw new Error("Invalid resource");o({id:m.id,type:"REPLY",data:{resource:p,data:Object.fromEntries(Object.entries(u.data??{}).map(([b,x])=>[b,x.value]))}});}else if(m.type==="MUTATE"){let{resource:p}=m;console.log("Received mutation from client:",m);try{let u=await r.handleRequest({req:{...c,type:"MUTATE",resourceName:p,input:m.payload,context:{messageId:m.id,...await y??{}},resourceId:m.resourceId,procedure:m.procedure,query:i}});m.procedure&&o({id:m.id,type:"REPLY",data:u});}catch(u){o({id:m.id,type:"REJECT",resource:p,message:u.message}),console.error("Error parsing mutation from the client:",u);}}}catch(m){console.error("Error handling message from the client:",m);}}),n.on("close",()=>{console.log("Connection closed",s),delete e[s];for(let d of Object.values(t))delete d[s];});}};function ae(r){let e=`${r.protocol}://${r.hostname}${r.url}`,t=new Headers;return Object.entries(r.headers).forEach(([n,a])=>{a&&t.set(n,Array.isArray(a)?a.join(","):a);}),new Request(e,{method:r.method,headers:t,body:r.body&&r.method!=="GET"?JSON.stringify(r.body):void 0})}var pt=(r,e,t)=>{r.ws(`${(t==null?void 0:t.basePath)??""}/ws`,ne(e)),r.use(`${(t==null?void 0:t.basePath)??""}/`,(n,a)=>{W(e)(ae(n)).then(s=>s.json().then(c=>a.status(s.status).send(c)));});};var U=class r{routes;constructor(e){this.routes=e.routes;}static create(e){return new r(e)}},ht=r=>U.create({...r}),_e=r=>({handler:e=>({inputValidator:r??z$1.undefined(),handler:e})}),N=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),[o,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,o),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)=>o=>a({req:o,next:n}),async n=>t(n))(e.req)}use(...e){for(let t of e)this.middlewares.add(t);return this}withMutations(e){return new r(this.resourceName,e({mutation:_e}))}},D=class r{middlewares;constructor(e=[]){this.middlewares=e;}createBasicRoute(e){return new N(e.name).use(...this.middlewares)}use(...e){return new r([...this.middlewares,...e])}static create(){return new r}},Tt=D.create;var E=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,o])=>[a,{value:o,_meta:{timestamp:n}}]))}))}async update(e,t,n){let a=new Date().toISOString(),{id:o,...s}=n;return r(await this.rawUpsert(e.name,t,{value:Object.fromEntries(Object.entries(s).map(([c,i])=>[c,{value:i,_meta:{timestamp:a}}]))}))}};function z(r,e,t,n){var o,s;if(!n)return t;if(!r)throw new Error("Schema not initialized");let a=r[e];if(!a)throw new Error("Resource not found");for(let[c,i]of Object.entries(n))if(a.fields[c])(i==null?void 0:i.$eq)!==void 0?t=t.where(`${e}.${c}`,i.$eq===null?"is":"=",i.$eq):(i==null?void 0:i.$in)!==void 0?t=t.where(`${e}.${c}`,"in",i.$in):(i==null?void 0:i.$not)!==void 0?((o=i==null?void 0:i.$not)==null?void 0:o.$in)!==void 0?t=t.where(`${e}.${c}`,"not in",i.$not.$in):((s=i==null?void 0:i.$not)==null?void 0:s.$eq)!==void 0?t=t.where(`${e}.${c}`,i.$not.$eq===null?"is not":"!=",i.$not.$eq):t=t.where(`${e}.${c}`,i.$not===null?"is not":"!=",i.$not):(i==null?void 0:i.$gt)!==void 0?t=t.where(`${e}.${c}`,">",i.$gt):(i==null?void 0:i.$gte)!==void 0?t=t.where(`${e}.${c}`,">=",i.$gte):(i==null?void 0:i.$lt)!==void 0?t=t.where(`${e}.${c}`,"<",i.$lt):(i==null?void 0:i.$lte)!==void 0?t=t.where(`${e}.${c}`,"<=",i.$lte):t=t.where(`${e}.${c}`,i===null?"is":"=",i);else if(a.relations[c]){let y=a.relations[c],f=y.entity.name,d=y.type==="one"?"id":y.foreignColumn,m=y.type==="one"?y.relationalColumn:"id";t=t.leftJoin(f,`${f}.${d}`,`${e}.${m}`),t=z(r,f,t,i);}return t}function V(r,e,t,n){if(!n)return t;if(!r)throw new Error("Schema not initialized");let a=r[e];if(!a)throw new Error(`Resource not found: ${e}`);for(let o of Object.keys(n)){if(!a.relations[o])throw new Error(`Relation ${o} not found in resource ${e}`);let s=a.relations[o],c=s.entity.name,i=s.type==="one"?"id":s.foreignColumn,y=s.type==="one"?s.relationalColumn:"id",f=s.type==="one"?jsonObjectFrom:jsonArrayFrom;t=t.select(d=>f(d.selectFrom(c).selectAll(c).whereRef(`${c}.${i}`,"=",`${e}.${y}`).select(m=>jsonObjectFrom(m.selectFrom(`${c}_meta`).selectAll(`${c}_meta`).whereRef(`${c}_meta.id`,"=",`${c}.id`)).as("_meta"))).as(o));}return t}var k=class extends E{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(i=>i.name===n);o||await this.db.schema.createTable(n).ifNotExists().execute();let s=`${n}_meta`,c=t.find(i=>i.name===s);c||await this.db.schema.createTable(s).ifNotExists().execute();for(let[i,y]of Object.entries(a.fields)){let f=o==null?void 0:o.columns.find(p=>p.name===i),d=y.getStorageFieldType();f?f.dataType!==d.type&&console.error("Column type mismatch:",i,"expected to have type:",d.type,"but has type:",f.dataType):(await this.db.schema.alterTable(n).addColumn(i,d.type,p=>{let u=p;return d.unique&&(u=u.unique()),d.nullable||(u=u.notNull()),d.references&&(u=u.references(d.references)),d.primary&&(u=u.primaryKey()),d.default!==void 0&&(u=u.defaultTo(d.default)),u}).execute().catch(p=>{throw console.error("Error adding column",i,p),p}),d.index&&await this.db.schema.createIndex(`${n}_${i}_index`).on(n).column(i).execute().catch(p=>{})),(c==null?void 0:c.columns.find(p=>p.name===i))||await this.db.schema.alterTable(s).addColumn(i,"varchar",p=>{let u=p;return d.primary&&(u=u.primaryKey().references(`${n}.${i}`)),u}).execute();}}}async rawFindById(e,t,n){if(!this.schema)throw new Error("Schema not initialized");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=V(this.schema,e,a,n);let o=await a.executeTakeFirst();if(o)return this.convertToMaterializedLiveType(o)}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){if(!this.schema)throw new Error("Schema not initialized");let a=this.db.selectFrom(e).selectAll(e).select(i=>jsonObjectFrom(i.selectFrom(`${e}_meta`).selectAll(`${e}_meta`).whereRef(`${e}_meta.id`,"=",`${e}.id`)).as("_meta"));a=z(this.schema,e,a,t),a=V(this.schema,e,a,n);let o=await a.execute(),s=Object.fromEntries(o.map(i=>{let{id:y,...f}=i;return [y,f]}));return Object.keys(s).length===0?{}:Object.entries(s).reduce((i,[y,f])=>(i[y]=this.convertToMaterializedLiveType(f),i),{})}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,o])=>[a,r(o)]))}async rawUpsert(e,t,n){return await this.db.transaction().execute(async a=>{var i;let o=!!await a.selectFrom(e).select("id").where("id","=",t).executeTakeFirst(),s={},c={};for(let[y,f]of Object.entries(n.value)){let d=(i=f._meta)==null?void 0:i.timestamp;d&&(s[y]=f.value,c[y]=d);}o?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 o,s,c;return n==="_meta"||(n==="id"?t[n]={value:a}:Array.isArray(a)?t[n]={value:a.map(i=>this.convertToMaterializedLiveType(i)),_meta:{timestamp:(o=e==null?void 0:e._meta)==null?void 0:o[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},{})}}};var F=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}},Lt=F.create;
|
|
2
|
-
export{
|
|
1
|
+
import {a,b as b$1,r}from'./chunk-GNY3ZGE5.js';import Se,{parse}from'qs';import {z as z$1}from'zod';import j from'node:crypto';import'js-xxhash';import {Kysely,PostgresDialect}from'kysely';import {jsonObjectFrom,jsonArrayFrom}from'kysely/helpers/postgres';var C=a(O=>{Object.defineProperty(O,"__esModule",{value:true});O.parse=Te;O.serialize=ge;var le=/^[\u0021-\u003A\u003C\u003E-\u007E]+$/,pe=/^[\u0021-\u003A\u003C-\u007E]*$/,me=/^([.]?[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]*$/,ye=Object.prototype.toString,he=(()=>{let i=function(){};return i.prototype=Object.create(null),i})();function Te(i,e){let t=new he,r=i.length;if(r<2)return t;let n=(e==null?void 0:e.decode)||xe,a=0;do{let s=i.indexOf("=",a);if(s===-1)break;let c=i.indexOf(";",a),o=c===-1?r:c;if(s>o){a=i.lastIndexOf(";",s-1)+1;continue}let y=H(i,a,s),f=Z(i,s,y),u=i.slice(y,f);if(t[u]===void 0){let d=H(i,s+1,o),p=Z(i,o,d),l=n(i.slice(d,p));t[u]=l;}a=o+1;}while(a<r);return t}function H(i,e,t){do{let r=i.charCodeAt(e);if(r!==32&&r!==9)return e}while(++e<t);return t}function Z(i,e,t){for(;e>t;){let r=i.charCodeAt(--e);if(r!==32&&r!==9)return e+1}return t}function ge(i,e,t){let r=(t==null?void 0:t.encode)||encodeURIComponent;if(!le.test(i))throw new TypeError(`argument name is invalid: ${i}`);let n=r(e);if(!pe.test(n))throw new TypeError(`argument val is invalid: ${e}`);let a=i+"="+n;if(!t)return a;if(t.maxAge!==void 0){if(!Number.isInteger(t.maxAge))throw new TypeError(`option maxAge is invalid: ${t.maxAge}`);a+="; Max-Age="+t.maxAge;}if(t.domain){if(!me.test(t.domain))throw new TypeError(`option domain is invalid: ${t.domain}`);a+="; Domain="+t.domain;}if(t.path){if(!fe.test(t.path))throw new TypeError(`option path is invalid: ${t.path}`);a+="; Path="+t.path;}if(t.expires){if(!Re(t.expires)||!Number.isFinite(t.expires.valueOf()))throw new TypeError(`option expires is invalid: ${t.expires}`);a+="; Expires="+t.expires.toUTCString();}if(t.httpOnly&&(a+="; HttpOnly"),t.secure&&(a+="; Secure"),t.partitioned&&(a+="; Partitioned"),t.priority)switch(typeof t.priority=="string"?t.priority.toLowerCase():void 0){case "low":a+="; Priority=Low";break;case "medium":a+="; Priority=Medium";break;case "high":a+="; 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":a+="; SameSite=Strict";break;case "lax":a+="; SameSite=Lax";break;case "none":a+="; SameSite=None";break;default:throw new TypeError(`option sameSite is invalid: ${t.sameSite}`)}return a}function xe(i){if(i.indexOf("%")===-1)return i;try{return decodeURIComponent(i)}catch{return i}}function Re(i){return ye.call(i)==="[object Date]"}});var Q=b$1(C(),1);var L=z$1.object({resource:z$1.string(),where:z$1.record(z$1.string(),z$1.any()).optional(),include:z$1.record(z$1.string(),z$1.any()).optional(),lastSyncedAt:z$1.string().optional(),limit:z$1.number().optional(),sort:z$1.array(z$1.object({key:z$1.string(),direction:z$1.enum(["asc","desc"])})).optional()}),z=z$1.record(z$1.string(),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((i,e)=>{i.id&&e.addIssue({code:z$1.ZodIssueCode.custom,message:"Payload cannot have an id"});}),B=z$1.object({id:z$1.string().optional(),type:z$1.literal("MUTATE"),resource:z$1.string(),resourceId:z$1.string().optional()}),M=B.extend({procedure:z$1.string(),payload:z$1.any().optional()}),E=B.extend({procedure:z$1.enum(["INSERT","UPDATE"]),payload:z});z$1.union([E,M]);var G=L.omit({resource:true}),v=M.omit({id:true,type:true,resource:true,procedure:true}),N=E.omit({id:true,type:true,resource:true,procedure:true});z$1.union([N,v]);var K=i=>async e=>{var t;try{let r=typeof e.headers.getSetCookie=="function"?Object.fromEntries(e.headers):e.headers,n={headers:r,cookies:r.cookie?Q.default.parse(r.cookie):{}},a=new URL(e.url),s=a.pathname.split("/"),c=a.searchParams,o=Se.parse(c.toString()),y=await((t=i.contextProvider)==null?void 0:t.call(i,{transport:"HTTP",headers:n.headers,cookies:n.cookies,query:o}))??{};if(e.method==="GET"){let f=s[s.length-1],{success:u,data:d,error:p}=G.safeParse(o);if(!u)return Response.json({message:"Invalid query",code:"INVALID_QUERY",details:p},{status:400});let l=await i.handleRequest({req:{...n,type:"QUERY",resourceName:f,context:y,where:d.where,include:d.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 f=s[s.length-1],u=s[s.length-2],d=e.body?await e.json():{},p;if(f==="insert"||f==="update"){let{success:T,data:g,error:R}=N.safeParse(d);if(!T)return Response.json({message:"Invalid mutation",code:"INVALID_REQUEST",details:R},{status:400});p=g;}else {let{success:T,data:g,error:R}=v.safeParse(d);if(!T)return Response.json({message:"Invalid mutation",code:"INVALID_REQUEST",details:R},{status:400});p=g;}let l=await i.handleRequest({req:{...n,type:"MUTATE",resourceName:u,input:p.payload,context:y,resourceId:p.resourceId,procedure:f==="insert"||f==="update"?f.toUpperCase():f,query:{}}});return Response.json(l)}catch(f){return console.error("Error parsing mutation from the client:",f),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 ne=b$1(C(),1);var I=z$1.string(),we=z$1.object({id:I,type:z$1.literal("SUBSCRIBE"),resource:z$1.string()}),Ie=L.extend({id:I,type:z$1.literal("QUERY")}),J=E.extend({id:I}),Me=M.extend({id:I}),Ee=z$1.union([Me,J]),Y=z$1.union([we,Ie,Ee]),Ae=z$1.object({id:I,type:z$1.literal("REJECT"),resource:z$1.string(),message:z$1.string().optional()}),$e=z$1.object({id:I,type:z$1.literal("REPLY"),data:z$1.any()});z$1.union([Ae,$e,J]);z$1.object({resource:z$1.string(),data:z$1.record(z$1.string(),z)});var ee="0123456789ABCDEFGHJKMNPQRSTVWXYZ",A=32;var Oe=16,te=10,X=0xffffffffffff;var S;(function(i){i.Base32IncorrectEncoding="B32_ENC_INVALID",i.DecodeTimeInvalidCharacter="DEC_TIME_CHAR",i.DecodeTimeValueMalformed="DEC_TIME_MALFORMED",i.EncodeTimeNegative="ENC_TIME_NEG",i.EncodeTimeSizeExceeded="ENC_TIME_SIZE_EXCEED",i.EncodeTimeValueMalformed="ENC_TIME_MALFORMED",i.PRNGDetectFailure="PRNG_DETECT",i.ULIDInvalid="ULID_INVALID",i.Unexpected="UNEXPECTED",i.UUIDInvalid="UUID_INVALID";})(S||(S={}));var w=class extends Error{constructor(e,t){super(`${t} (${e})`),this.name="ULIDError",this.code=e;}};function Le(i){let e=Math.floor(i()*A);return e===A&&(e=A-1),ee.charAt(e)}function je(i){var r;let e=Pe(),t=e&&(e.crypto||e.msCrypto)||(typeof j<"u"?j:null);if(typeof(t==null?void 0:t.getRandomValues)=="function")return ()=>{let n=new Uint8Array(1);return t.getRandomValues(n),n[0]/255};if(typeof(t==null?void 0:t.randomBytes)=="function")return ()=>t.randomBytes(1).readUInt8()/255;if((r=j)!=null&&r.randomBytes)return ()=>j.randomBytes(1).readUInt8()/255;throw new w(S.PRNGDetectFailure,"Failed to find a reliable PRNG")}function Pe(){return ve()?self:typeof window<"u"?window:typeof global<"u"?global:typeof globalThis<"u"?globalThis:null}function Ce(i,e){let t="";for(;i>0;i--)t=Le(e)+t;return t}function ze(i,e=te){if(isNaN(i))throw new w(S.EncodeTimeValueMalformed,`Time must be a number: ${i}`);if(i>X)throw new w(S.EncodeTimeSizeExceeded,`Cannot encode a time larger than ${X}: ${i}`);if(i<0)throw new w(S.EncodeTimeNegative,`Time must be positive: ${i}`);if(Number.isInteger(i)===false)throw new w(S.EncodeTimeValueMalformed,`Time must be an integer: ${i}`);let t,r="";for(let n=e;n>0;n--)t=i%A,r=ee.charAt(t)+r,i=(i-t)/A;return r}function ve(){return typeof WorkerGlobalScope<"u"&&self instanceof WorkerGlobalScope}function re(i,e){let t=je(),r=Date.now();return ze(r,te)+Ce(Oe,t)}var U=()=>re().toLowerCase();var ie=i=>{let e={},t={};return i.subscribeToMutations(r=>{let n=r;!n.resourceId||!n.payload||(console.log("Mutation propagated:",n),Object.entries(t[n.resource]??{}).forEach(([a,s])=>{var c;(c=e[a])==null||c.send(JSON.stringify({...n,id:n.id??U()}));}));}),(r,n)=>{var f;let a=u=>{r.send(JSON.stringify(u));},s=U(),c={headers:n.headers,cookies:typeof n.headers.cookie=="string"?ne.default.parse(n.headers.cookie):{}},o=parse(n.url.split("?")[1]),y=(f=i.contextProvider)==null?void 0:f.call(i,{transport:"WEBSOCKET",headers:c.headers,cookies:c.cookies,query:o});e[s]=r,console.log("Client connected:",s),r.on("message",async u=>{try{console.log("Message received from the client:",u);let d=Y.parse(JSON.parse(u.toString()));if(d.type==="SUBSCRIBE"){let{resource:p}=d;t[p]||(t[p]={}),t[p][s]={};}else if(d.type==="QUERY"){let{resource:p}=d,l=await i.handleRequest({req:{...c,type:"QUERY",resourceName:p,context:await y??{},query:o}});if(!l||!l.data)throw new Error("Invalid resource");a({id:d.id,type:"REPLY",data:{resource:p,data:Object.fromEntries(Object.entries(l.data??{}).map(([T,g])=>[T,g.value]))}});}else if(d.type==="MUTATE"){let{resource:p}=d;console.log("Received mutation from client:",d);try{let l=await i.handleRequest({req:{...c,type:"MUTATE",resourceName:p,input:d.payload,context:{messageId:d.id,...await y??{}},resourceId:d.resourceId,procedure:d.procedure,query:o}});d.procedure&&d.procedure!=="INSERT"&&d.procedure!=="UPDATE"&&a({id:d.id,type:"REPLY",data:l});}catch(l){a({id:d.id,type:"REJECT",resource:p,message:l.message}),console.error("Error parsing mutation from the client:",l);}}}catch(d){console.error("Error handling message from the client:",d);}}),r.on("close",()=>{console.log("Connection closed",s),delete e[s];for(let u of Object.values(t))delete u[s];});}};function oe(i){let e=`${i.protocol}://${i.hostname}${i.url}`,t=new Headers;return Object.entries(i.headers).forEach(([r,n])=>{n&&t.set(r,Array.isArray(n)?n.join(","):n);}),new Request(e,{method:i.method,headers:t,body:i.body&&i.method!=="GET"?JSON.stringify(i.body):void 0})}var Tt=(i,e,t)=>{i.ws(`${(t==null?void 0:t.basePath)??""}/ws`,ie(e)),i.use(`${(t==null?void 0:t.basePath)??""}/`,(r,n)=>{K(e)(oe(r)).then(s=>s.json().then(c=>n.status(s.status).send(c)));});};var b=(i,e,t=false)=>Object.entries(e).every(([r,n])=>{if(r==="$and")return n.every(s=>b(i,s,t));if(r==="$or")return n.some(s=>b(i,s,t));let a=(n==null?void 0:n.$eq)!==void 0?n==null?void 0:n.$eq:n;if(typeof n=="object"&&n!==null&&(n==null?void 0:n.$eq)===void 0){if(n.$in!==void 0){let s=i[r];return s===void 0?false:t?!n.$in.includes(s):n.$in.includes(s)}if(n.$not!==void 0&&!t)return b(i,{[r]:n.$not},true);if(n.$gt!==void 0){let s=i[r];return typeof s!="number"?false:t?s<=n.$gt:s>n.$gt}if(n.$gte!==void 0){let s=i[r];return typeof s!="number"?false:t?s<n.$gte:s>=n.$gte}if(n.$lt!==void 0){let s=i[r];return typeof s!="number"?false:t?s>=n.$lt:s<n.$lt}if(n.$lte!==void 0){let s=i[r];return typeof s!="number"?false:t?s>n.$lte:s<=n.$lte}return !i[r]||typeof i[r]!="object"?false:b(i[r],n,t)}return t?i[r]!==a:i[r]===a});var D=class i{routes;constructor(e){this.routes=e.routes;}static create(e){return new i(e)}},Mt=i=>D.create({...i}),De=i=>({handler:e=>({inputValidator:i??z$1.undefined(),handler:e})}),_=class i{_resourceSchema;resourceName;middlewares;customMutations;authorization;constructor(e,t,r){this.resourceName=e,this.middlewares=new Set,this.customMutations=t??{},this.authorization=r;}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)throw new Error("Procedure is required for mutations");let n=this.customMutations[r.procedure];if(n){let a=n.inputValidator.parse(r.input);return r.input=a,n.handler({req:r,db:e.db,schema:e.schema})}else if(r.procedure==="INSERT"||r.procedure==="UPDATE")return this.handleSet({req:r,db:e.db,schema:e.schema,operation:r.procedure})}throw new Error("Invalid request")})();return await Array.from(this.middlewares.values()).reduceRight((r,n)=>a=>n({req:a,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 i(this.resourceName,e({mutation:De}))}handleFind=async({req:e,db:t})=>{var n,a;let r=(a=(n=this.authorization)==null?void 0:n.read)==null?void 0:a.call(n,{ctx:e.context});if(typeof r=="boolean"&&!r)throw new Error("Not authorized");return {data:await t.rawFind(e.resourceName,e.where&&r&&r!==true?{$and:[e.where,r]}:r&&r!==true?r:e.where,e.include),acceptedValues:null}};handleSet=async({req:e,db:t,schema:r$1,operation: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);if(n==="INSERT"&&a)throw new Error("Resource already exists");if(n==="UPDATE"&&!a)throw new Error("Resource not found");return t.transaction(async({trx:s})=>{var f,u,d,p,l;let[c,o]=r$1[this.resourceName].mergeMutation("set",e.input,a);if(!o)throw new Error("Mutation rejected");if(n==="INSERT"){let T=await s.rawInsert(e.resourceName,e.resourceId,c);if((f=this.authorization)!=null&&f.insert){let g=r(T);g.id=g.id??e.resourceId;let R=this.authorization.insert({ctx:e.context,value:g});if(!(typeof R=="boolean"?R:b(g,R)))throw new Error("Not authorized")}return {data:T,acceptedValues:o}}if((d=(u=this.authorization)==null?void 0:u.update)!=null&&d.preMutation){let T=r(a);T.id=T.id??e.resourceId;let g=this.authorization.update.preMutation({ctx:e.context,value:T});if(!(typeof g=="boolean"?g:b(T,g)))throw new Error("Not authorized")}let y=await s.rawUpdate(e.resourceName,e.resourceId,c);if((l=(p=this.authorization)==null?void 0:p.update)!=null&&l.postMutation){let T=r(y);T.id=T.id??e.resourceId;let g=this.authorization.update.postMutation({ctx:e.context,value:T});if(!(typeof g=="boolean"?g:b(T,g)))throw new Error("Not authorized")}return {data:y,acceptedValues:o}})}},V=class i{middlewares;constructor(e=[]){this.middlewares=e;}collectionRoute(e,t){return new _(e.name,void 0,t).use(...this.middlewares)}use(...e){return new i([...this.middlewares,...e])}static create(){return new i}},Et=V.create;var $=class{async insert(e,t){let r$1=new Date().toISOString();return r(await this.rawInsert(e.name,t.id,{value:Object.fromEntries(Object.entries(t).map(([n,a])=>[n,{value:a,_meta:{timestamp:r$1}}]))}))}async update(e,t,r$1){let n=new Date().toISOString(),{id:a,...s}=r$1;return r(await this.rawUpdate(e.name,t,{value:Object.fromEntries(Object.entries(s).map(([c,o])=>[c,{value:o,_meta:{timestamp:n}}]))}))}};function P(i,e,t,r){if(!i)throw new Error("Schema not initialized");let n=i[e];if(!n)throw new Error("Resource not found");let a=r.$or,s=r.$and;return (a?t.or:t.and)(a?r.$or.map(c=>P(i,e,t,c)):s?r.$and.map(c=>P(i,e,t,c)):Object.entries(r).map(([c,o])=>{var y,f;if(n.fields[c])return (o==null?void 0:o.$eq)!==void 0?t(`${e}.${c}`,o.$eq===null?"is":"=",o.$eq):(o==null?void 0:o.$in)!==void 0?t(`${e}.${c}`,"in",o.$in):(o==null?void 0:o.$not)!==void 0?((y=o==null?void 0:o.$not)==null?void 0:y.$in)!==void 0?t(`${e}.${c}`,"not in",o.$not.$in):((f=o==null?void 0:o.$not)==null?void 0:f.$eq)!==void 0?t(`${e}.${c}`,o.$not.$eq===null?"is not":"!=",o.$not.$eq):t(`${e}.${c}`,o.$not===null?"is not":"!=",o.$not):(o==null?void 0:o.$gt)!==void 0?t(`${e}.${c}`,">",o.$gt):(o==null?void 0:o.$gte)!==void 0?t(`${e}.${c}`,">=",o.$gte):(o==null?void 0:o.$lt)!==void 0?t(`${e}.${c}`,"<",o.$lt):(o==null?void 0:o.$lte)!==void 0?t(`${e}.${c}`,"<=",o.$lte):t(`${e}.${c}`,o===null?"is":"=",o);if(n.relations[c]){let u=n.relations[c],d=u.entity.name;u.type==="one"?"id":u.foreignColumn;u.type==="one"?u.relationalColumn:"id";return P(i,d,t,o)}return null}).filter(Boolean))}function Ve(i,e,t,r){let n=i[e];if(!n)throw new Error("Resource not found");if(!r)return t;for(let a of Object.keys(r)){if(!n.relations[a])continue;let s=n.relations[a],c=s.entity.name,o=s.type==="one"?"id":s.foreignColumn,y=s.type==="one"?s.relationalColumn:"id";t=t.leftJoin(c,`${c}.${o}`,`${e}.${y}`);}return t}function se(i,e,t,r){return !r||Object.keys(r).length===0?t:(t=Ve(i,e,t,r),t.where(n=>P(i,e,n,r)))}function k(i,e,t,r){if(!r)return t;if(!i)throw new Error("Schema not initialized");let n=i[e];if(!n)throw new Error(`Resource not found: ${e}`);for(let a of Object.keys(r)){if(!n.relations[a])throw new Error(`Relation ${a} not found in resource ${e}`);let s=n.relations[a],c=s.entity.name,o=s.type==="one"?"id":s.foreignColumn,y=s.type==="one"?s.relationalColumn:"id",f=s.type==="one"?jsonObjectFrom:jsonArrayFrom;t=t.select(u=>f(u.selectFrom(c).selectAll(c).whereRef(`${c}.${o}`,"=",`${e}.${y}`).select(d=>jsonObjectFrom(d.selectFrom(`${c}_meta`).selectAll(`${c}_meta`).whereRef(`${c}_meta.id`,"=",`${c}.id`)).as("_meta"))).as(a));}return t}var F=class i extends ${db;schema;constructor(e,t){super(),this.isKyselyLike(e)?this.db=e:this.db=new Kysely({dialect:new PostgresDialect({pool:e})}),this.schema=t,this.rawInsert=this.rawInsert.bind(this),this.rawUpdate=this.rawUpdate.bind(this);}async updateSchema(e){this.schema=e;let t=await this.db.introspection.getTables();for(let[r,n]of Object.entries(e)){let a=t.find(o=>o.name===r);a||await this.db.schema.createTable(r).ifNotExists().execute();let s=`${r}_meta`,c=t.find(o=>o.name===s);c||await this.db.schema.createTable(s).ifNotExists().execute();for(let[o,y]of Object.entries(n.fields)){let f=a==null?void 0:a.columns.find(p=>p.name===o),u=y.getStorageFieldType();f?f.dataType!==u.type&&console.error("Column type mismatch:",o,"expected to have type:",u.type,"but has type:",f.dataType):(await this.db.schema.alterTable(r).addColumn(o,u.type,p=>{let l=p;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(p=>{throw console.error("Error adding column",o,p),p}),u.index&&await this.db.schema.createIndex(`${r}_${o}_index`).on(r).column(o).execute().catch(p=>{})),(c==null?void 0:c.columns.find(p=>p.name===o))||await this.db.schema.alterTable(s).addColumn(o,"varchar",p=>{let l=p;return u.primary&&(l=l.primaryKey().references(`${r}.${o}`)),l}).execute();}}}async rawFindById(e,t,r){if(!this.schema)throw new Error("Schema not initialized");let n=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"));n=k(this.schema,e,n,r);let a=await n.executeTakeFirst();if(a)return this.convertToMaterializedLiveType(a)}async findOne(e,t,r$1){let n=await this.rawFindById(e.name,t,r$1==null?void 0:r$1.include);if(n)return r(n)}async rawFind(e,t,r){if(!this.schema)throw new Error("Schema not initialized");let n=this.db.selectFrom(e).selectAll(e).select(o=>jsonObjectFrom(o.selectFrom(`${e}_meta`).selectAll(`${e}_meta`).whereRef(`${e}_meta.id`,"=",`${e}.id`)).as("_meta"));n=se(this.schema,e,n,t),n=k(this.schema,e,n,r);let a=await n.execute(),s=Object.fromEntries(a.map(o=>{let{id:y,...f}=o;return [y,f]}));return Object.keys(s).length===0?{}:Object.entries(s).reduce((o,[y,f])=>(o[y]=this.convertToMaterializedLiveType(f),o),{})}async find(e,t){let r$1=await this.rawFind(e.name,t==null?void 0:t.where,t==null?void 0:t.include);return Object.fromEntries(Object.entries(r$1).map(([n,a])=>[n,r(a)]))}async rawInsert(e,t,r){var s;let n={},a={};for(let[c,o]of Object.entries(r.value)){let y=(s=o._meta)==null?void 0:s.timestamp;y&&(n[c]=o.value,a[c]=y);}return await this.db.insertInto(e).values({...n,id:t}).execute().then(()=>{this.db.insertInto(`${e}_meta`).values({...a,id:t}).execute();}),r}async rawUpdate(e,t,r){var s;let n={},a={};for(let[c,o]of Object.entries(r.value)){let y=(s=o._meta)==null?void 0:s.timestamp;y&&(n[c]=o.value,a[c]=y);}return await Promise.all([this.db.updateTable(e).set(n).where("id","=",t).execute(),this.db.insertInto(`${e}_meta`).values({...a,id:t}).onConflict(c=>c.column("id").doUpdateSet(a)).execute()]),r}async transaction(e){if(!this.schema)throw new Error("Schema not initialized");if(this.db.isTransaction){let r=Math.random().toString(36).substring(2,15),n=await this.db.savepoint(r).execute();try{return await e({trx:this,commit:()=>n.releaseSavepoint(r).execute().then(()=>{}),rollback:()=>n.rollbackToSavepoint(r).execute().then(()=>{})}).then(a=>n.isCommitted||n.isRolledBack?a:n.releaseSavepoint(r).execute().then(()=>a))}catch(a){throw await n.rollbackToSavepoint(r).execute().catch(()=>{}),a}}let t=await this.db.startTransaction().execute();try{return await e({trx:new i(t,this.schema),commit:()=>t.commit().execute(),rollback:()=>t.rollback().execute()}).then(r=>t.isCommitted||t.isRolledBack?r:t.commit().execute().then(()=>r))}catch(r){throw await t.rollback().execute(),r}}convertToMaterializedLiveType(e){return {value:Object.entries(e).reduce((t,[r,n])=>{var a,s,c;return r==="_meta"||(r==="id"?t[r]={value:n}:Array.isArray(n)?t[r]={value:n.map(o=>this.convertToMaterializedLiveType(o)),_meta:{timestamp:(a=e==null?void 0:e._meta)==null?void 0:a[r]}}:typeof n=="object"&&n!==null&&!(n instanceof Date)?t[r]={...this.convertToMaterializedLiveType(n),_meta:{timestamp:(s=e==null?void 0:e._meta)==null?void 0:s[r]}}:t[r]={value:n,_meta:{timestamp:(c=e==null?void 0:e._meta)==null?void 0:c[r]}}),t},{})}}isKyselyLike(e){if(e instanceof Kysely)return true;if(!e||typeof e!="object")return false;let t=e,r=typeof t.selectFrom=="function",n=typeof t.startTransaction=="function",a=typeof t.savepoint=="function",s=typeof t.isTransaction=="boolean"||typeof t.isTransaction=="function";return r&&n||a&&s}};var q=class i{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 i(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,n)=>a=>n({req:a,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&&(e.req.procedure==="INSERT"||e.req.procedure==="UPDATE")&&this.mutationSubscriptions.forEach(r=>{r({id:e.req.context.messageId,type:"MUTATE",resource:e.req.resourceName,payload:t.acceptedValues??{},resourceId:e.req.resourceId,procedure:e.req.procedure});}),t}use(e){return this.middlewares.add(e),this}context(e){return this.contextProvider=e,this}},kt=q.create;
|
|
2
|
+
export{_ as Route,V as RouteFactory,D as Router,F as SQLStorage,q as Server,$ as Storage,Tt as expressAdapter,Et as routeFactory,Mt as router,kt as server};
|
package/package.json
CHANGED
package/dist/chunk-G5SVBG4B.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
var L="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 I(e){let t=Math.floor(e()*32);return t===32&&(t=31),L.charAt(t)}function M(e){let t=E(),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 E(){return O()?self:typeof window<"u"?window:typeof global<"u"?global:typeof globalThis<"u"?globalThis:null}function A(e,t){let n="";for(;e>0;e--)n=I(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 r=t;r>0;r--)n=e%32,i=L.charAt(n)+i,e=(e-n)/32;return i}function O(){return typeof WorkerGlobalScope<"u"&&self instanceof WorkerGlobalScope}function b(e,t){let n=M(),i=Date.now();return _(i,10)+A(16,n)}var j=()=>b().toLowerCase(),V=(e,t)=>typeof e=="function"?e(t):e;var w=(e,t,n=[])=>new Proxy(e,{get:(i,r)=>{var p,T;if(r==="__isProxy__")return true;let o=(p=t.get)==null?void 0:p.call(t,i,[...n,r]);if(o!==void 0)return o;let a=i,s=r;return (T=a[s])!=null&&T.__isProxy__||(a[s]=w(typeof a[s]=="object"?a[s]:()=>{},t,[...n,r])),a[s]},apply:(i,r,o)=>{var a;return (a=t.apply)==null?void 0:a.call(t,i,n,o)}}),f=(e,t,n=false)=>Object.entries(t).every(([i,r])=>{if(i==="$and")return r.every(a=>f(e,a,n));if(i==="$or")return r.some(a=>f(e,a,n));let o=(r==null?void 0:r.$eq)!==void 0?r==null?void 0:r.$eq:r;if(typeof r=="object"&&r!==null&&(r==null?void 0:r.$eq)===void 0){if(r.$in!==void 0){let a=e[i];return a===void 0?false:n?!r.$in.includes(a):r.$in.includes(a)}if(r.$not!==void 0&&!n)return f(e,{[i]:r.$not},true);if(r.$gt!==void 0){let a=e[i];return typeof a!="number"?false:n?a<=r.$gt:a>r.$gt}if(r.$gte!==void 0){let a=e[i];return typeof a!="number"?false:n?a<r.$gte:a>=r.$gte}if(r.$lt!==void 0){let a=e[i];return typeof a!="number"?false:n?a>=r.$lt:a<r.$lt}if(r.$lte!==void 0){let a=e[i];return typeof a!="number"?false:n?a>r.$lte:a<=r.$lte}return !e[i]||typeof e[i]!="object"?false:f(e[i],r)}return n?e[i]!==o:e[i]===o}),D=(e,t,n)=>{let i=[],r=0;for(let o=0;o<e.length&&(n===void 0||r<n);o++)t(e[o],o)&&(i.push(e[o]),r++);return i};var R=e=>{if(e)return Array.isArray(e.value)?e.value.map(t=>R(t)):typeof e.value!="object"||e.value===null||e.value instanceof Date?e.value:Object.fromEntries(Object.entries(e.value).map(([t,n])=>[t,R(n)]))};export{j as a,V as b,w as c,f as d,D as e,R as f};
|