@sveltebase/sync 1.0.5 → 1.0.6

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/README.md CHANGED
@@ -572,5 +572,34 @@ try {
572
572
  }
573
573
  ```
574
574
 
575
+ ### G. Type-Safe Backend Event Publishing (`createPublisher`)
576
+ When publishing backend events (e.g. from standard API routes, message queues, or cron triggers) to push updates to connected clients, you can create a type-safe publisher matched to your application's database schema. This checks channels (including dynamic channel patterns like `"todos:user_123"`), actions, and payloads at compile-time:
577
+
578
+ ```typescript
579
+ import { createPublisher } from "@sveltebase/sync";
580
+ import type { Todo } from "$lib/server/db/schema";
581
+
582
+ // Define schema matching channel names to model types
583
+ type AppSyncSchema = {
584
+ todos: Todo;
585
+ };
586
+
587
+ // Create typed publisher instance
588
+ const publisher = createPublisher<AppSyncSchema>();
589
+
590
+ // 1. Publish a create event (expects full Todo payload)
591
+ await publisher.publish("todos", "create", todo.id, todo);
592
+
593
+ // 2. Publish an update event (expects Partial<Todo> payload)
594
+ await publisher.publish("todos", "update", todo.id, { completed: true });
595
+
596
+ // 3. Publish a delete event (expects optional { updatedAt: string } metadata)
597
+ await publisher.publish("todos", "delete", todo.id, undefined);
598
+
599
+ // 4. Supports scoped/dynamic channels (e.g. "channelName:scopeId")
600
+ await publisher.publish("todos:user_123", "update", todo.id, { title: "New Title" });
601
+ ```
602
+
603
+
575
604
 
576
605
 
package/dist/index.d.ts CHANGED
@@ -1,6 +1,7 @@
1
1
  export { SyncClient, useLiveQuery } from "./client/index.js";
2
2
  export { defineSync } from "./server/index.js";
3
- export { handleUpgrade, publishEvent } from "./server/handler.js";
3
+ export { handleUpgrade, publishEvent, createPublisher } from "./server/handler.js";
4
+ export type { PublishEventData } from "./server/handler.js";
4
5
  export type { SyncContext, SyncHandler } from "./server/index.js";
5
6
  export type { SyncMessage } from "./protocol.js";
6
7
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAC;AAC7D,OAAO,EAAE,UAAU,EAAE,MAAM,mBAAmB,CAAC;AAC/C,OAAO,EAAE,aAAa,EAAE,YAAY,EAAE,MAAM,qBAAqB,CAAC;AAClE,YAAY,EAAE,WAAW,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAClE,YAAY,EAAE,WAAW,EAAE,MAAM,eAAe,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAC;AAC7D,OAAO,EAAE,UAAU,EAAE,MAAM,mBAAmB,CAAC;AAC/C,OAAO,EAAE,aAAa,EAAE,YAAY,EAAE,eAAe,EAAE,MAAM,qBAAqB,CAAC;AACnF,YAAY,EAAE,gBAAgB,EAAE,MAAM,qBAAqB,CAAC;AAC5D,YAAY,EAAE,WAAW,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAClE,YAAY,EAAE,WAAW,EAAE,MAAM,eAAe,CAAC"}
package/dist/index.js CHANGED
@@ -1,3 +1,3 @@
1
1
  export { SyncClient, useLiveQuery } from "./client/index.js";
2
2
  export { defineSync } from "./server/index.js";
3
- export { handleUpgrade, publishEvent } from "./server/handler.js";
3
+ export { handleUpgrade, publishEvent, createPublisher } from "./server/handler.js";
@@ -1,3 +1,9 @@
1
+ export type PublishEventData<TRecord, TAction extends "create" | "update" | "delete"> = TAction extends "create" ? TRecord : TAction extends "update" ? Partial<TRecord> : {
2
+ updatedAt?: string;
3
+ } | undefined;
4
+ export declare function createPublisher<TSchema extends Record<string, any>>(): {
5
+ publish: <TChannel extends keyof TSchema & string, TAction extends "create" | "update" | "delete">(channel: TChannel | `${TChannel}:${string}`, action: TAction, key: string | undefined, data: PublishEventData<TSchema[TChannel], TAction>) => Promise<void>;
6
+ };
1
7
  export declare function publishEvent(channel: string, action: "create" | "update" | "delete", key: string | undefined, data: any): Promise<void>;
2
8
  export declare function handleUpgrade(request: Request, platform: App.Platform | undefined): Promise<Response>;
3
9
  //# sourceMappingURL=handler.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"handler.d.ts","sourceRoot":"","sources":["../../src/server/handler.ts"],"names":[],"mappings":"AAAA,wBAAsB,YAAY,CAChC,OAAO,EAAE,MAAM,EACf,MAAM,EAAE,QAAQ,GAAG,QAAQ,GAAG,QAAQ,EACtC,GAAG,EAAE,MAAM,GAAG,SAAS,EACvB,IAAI,EAAE,GAAG,iBAgCV;AAED,wBAAsB,aAAa,CACjC,OAAO,EAAE,OAAO,EAChB,QAAQ,EAAE,GAAG,CAAC,QAAQ,GAAG,SAAS,GACjC,OAAO,CAAC,QAAQ,CAAC,CAqBnB"}
1
+ {"version":3,"file":"handler.d.ts","sourceRoot":"","sources":["../../src/server/handler.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,gBAAgB,CAAC,OAAO,EAAE,OAAO,SAAS,QAAQ,GAAG,QAAQ,GAAG,QAAQ,IAClF,OAAO,SAAS,QAAQ,GACpB,OAAO,GACP,OAAO,SAAS,QAAQ,GACtB,OAAO,CAAC,OAAO,CAAC,GAChB;IAAE,SAAS,CAAC,EAAE,MAAM,CAAA;CAAE,GAAG,SAAS,CAAC;AAE3C,wBAAgB,eAAe,CAAC,OAAO,SAAS,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC;cAG7D,QAAQ,SAAS,MAAM,OAAO,GAAG,MAAM,EACvC,OAAO,SAAS,QAAQ,GAAG,QAAQ,GAAG,QAAQ,WAErC,QAAQ,GAAG,GAAG,QAAQ,IAAI,MAAM,EAAE,UACnC,OAAO,OACV,MAAM,GAAG,SAAS,QACjB,gBAAgB,CAAC,OAAO,CAAC,QAAQ,CAAC,EAAE,OAAO,CAAC,KACjD,OAAO,CAAC,IAAI,CAAC;EAKnB;AAED,wBAAsB,YAAY,CAChC,OAAO,EAAE,MAAM,EACf,MAAM,EAAE,QAAQ,GAAG,QAAQ,GAAG,QAAQ,EACtC,GAAG,EAAE,MAAM,GAAG,SAAS,EACvB,IAAI,EAAE,GAAG,iBAgCV;AAED,wBAAsB,aAAa,CACjC,OAAO,EAAE,OAAO,EAChB,QAAQ,EAAE,GAAG,CAAC,QAAQ,GAAG,SAAS,GACjC,OAAO,CAAC,QAAQ,CAAC,CAqBnB"}
@@ -1,3 +1,11 @@
1
+ export function createPublisher() {
2
+ return {
3
+ publish: async (channel, action, key, data) => {
4
+ const resolvedChannel = String(channel);
5
+ return publishEvent(resolvedChannel, action, key, data);
6
+ },
7
+ };
8
+ }
1
9
  export async function publishEvent(channel, action, key, data) {
2
10
  const envId = "$app/environment";
3
11
  let isDev = false;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sveltebase/sync",
3
- "version": "1.0.5",
3
+ "version": "1.0.6",
4
4
  "type": "module",
5
5
  "publishConfig": {
6
6
  "access": "public"