From 3419dc11b4c31e6d092aec466b7192aac6986304 Mon Sep 17 00:00:00 2001 From: leumasic <53974949+leumasic@users.noreply.github.com> Date: Fri, 17 Apr 2026 08:29:21 -0400 Subject: [PATCH 1/2] =?UTF-8?q?=F0=9F=A4=96=20Merge=20PR=20#74882=20[share?= =?UTF-8?q?db]=20fetch=20and=20submit=20backend=20methods=20by=20@leumasic?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- types/sharedb/index.d.ts | 23 ++++++++++++++++++++++- types/sharedb/lib/sharedb.d.ts | 12 +++++++++--- types/sharedb/sharedb-tests.ts | 23 ++++++++++++++++++++--- 3 files changed, 51 insertions(+), 7 deletions(-) diff --git a/types/sharedb/index.d.ts b/types/sharedb/index.d.ts index edf7464412ee7b..894c505d119a5f 100644 --- a/types/sharedb/index.d.ts +++ b/types/sharedb/index.d.ts @@ -3,7 +3,7 @@ import { EventEmitter } from "events"; import { Duplex } from "stream"; import Agent = require("./lib/agent"); -import { Connection } from "./lib/client"; +import { Connection, Snapshot } from "./lib/client"; import * as ShareDB from "./lib/sharedb"; interface PubSubOptions { @@ -56,6 +56,27 @@ declare class sharedb extends EventEmitter { * or session info. */ listen(stream: Duplex, request?: any): Agent; + fetch( + agent: Agent, + index: string, + id: string, + callback: (error: Error | null, snapshot?: Snapshot) => void, + ): void; + fetch( + agent: Agent, + index: string, + id: string, + options: ShareDB.BackendFetchOptions | null, + callback: (error: Error | null, snapshot?: Snapshot) => void, + ): void; + submit( + agent: Agent, + index: string, + id: string, + op: SubmitRequest['op'], + options: ShareDB.BackendSubmitOptions | null, + callback: (error: Error | null, ops: any[], request?: SubmitRequest) => void, + ): void; close(callback?: BasicCallback): void; /** * Registers a server middleware function. diff --git a/types/sharedb/lib/sharedb.d.ts b/types/sharedb/lib/sharedb.d.ts index fc4e290ca33e83..beb8eebd7e0181 100644 --- a/types/sharedb/lib/sharedb.d.ts +++ b/types/sharedb/lib/sharedb.d.ts @@ -104,9 +104,9 @@ export interface RawOp { d: DocumentID; } -export type CreateOp = RawOp & { create: { type: string; data: any }; del: undefined; op: undefined }; -export type DeleteOp = RawOp & { del: boolean; create: undefined; op: undefined }; -export type EditOp = RawOp & { op: any[]; create: undefined; del: undefined }; +export type CreateOp = Partial & { create: { type: string; data: any }; }; +export type DeleteOp = Partial & { del: boolean; }; +export type EditOp = Partial & { op: any[]; }; export type OTType = "ot-text" | "ot-json0" | "ot-json1" | "ot-text-tp2" | "rich-text"; @@ -154,6 +154,12 @@ export interface ShareDBSourceOptions { // interface ShareDBCreateOptions extends ShareDBSourceOptions {} // interface ShareDBDelOptions extends ShareDBSourceOptions {} // interface ShareDBSubmitOpOptions extends ShareDBSourceOptions {} +export interface BackendFetchOptions { + snapshotOptions?: Record; +} +export interface BackendSubmitOptions { + [key: string]: unknown; +} export type Callback = (err: Error) => any; diff --git a/types/sharedb/sharedb-tests.ts b/types/sharedb/sharedb-tests.ts index a741d9e9bf6e25..bfe6dea35ad4ca 100644 --- a/types/sharedb/sharedb-tests.ts +++ b/types/sharedb/sharedb-tests.ts @@ -92,6 +92,23 @@ console.log(backend.extraDbs); backend.addProjection("notes_minimal", "notes", { title: true, creator: true, lastUpdateTime: true }); const readonlyProjection = backend.projections["notes_minimal"]; console.log(readonlyProjection.target, readonlyProjection.fields); +backend.submit({} as Agent, "notes_minimal", "doc1", { create: { data: {}, type: 'json uri type' } }, { + customField: true, + anotherOption: { nested: "value" }, +}, (error, ops, request) => { + if (error) { + console.error(error.message); + } + console.log(ops, request && request.collection); +}); +backend.fetch({} as Agent, "notes_minimal", "doc1", { + snapshotOptions: { foo: "bar" }, +}, (error, snapshot) => { + if (error) { + console.error(error.message); + } + console.log(snapshot && snapshot.data); +}); // backend.projections is used by sharedb internally, so they shouldn't be messed with. // Test that marking as readonly in API prevents external modification. // @ts-expect-error @@ -135,9 +152,9 @@ for (const action of submitRelatedActions) { request.snapshot, request.ops, request.channels, - request.op.op, - request.op.create, - request.op.del, + (request.op as ShareDB.EditOp).op, + (request.op as ShareDB.CreateOp).create, + (request.op as ShareDB.DeleteOp).del, request.extra.source, ); callback(); From 445e91d0d5f6fe068073a60b1f834408ea9e5148 Mon Sep 17 00:00:00 2001 From: TypeScript Bot Date: Fri, 17 Apr 2026 12:30:02 +0000 Subject: [PATCH 2/2] =?UTF-8?q?=F0=9F=A4=96=20dprint=20fmt?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- types/sharedb/index.d.ts | 2 +- types/sharedb/lib/sharedb.d.ts | 6 +++--- types/sharedb/sharedb-tests.ts | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/types/sharedb/index.d.ts b/types/sharedb/index.d.ts index 894c505d119a5f..640f5dc5f187ad 100644 --- a/types/sharedb/index.d.ts +++ b/types/sharedb/index.d.ts @@ -73,7 +73,7 @@ declare class sharedb extends EventEmitter { agent: Agent, index: string, id: string, - op: SubmitRequest['op'], + op: SubmitRequest["op"], options: ShareDB.BackendSubmitOptions | null, callback: (error: Error | null, ops: any[], request?: SubmitRequest) => void, ): void; diff --git a/types/sharedb/lib/sharedb.d.ts b/types/sharedb/lib/sharedb.d.ts index beb8eebd7e0181..f1506cb49283fa 100644 --- a/types/sharedb/lib/sharedb.d.ts +++ b/types/sharedb/lib/sharedb.d.ts @@ -104,9 +104,9 @@ export interface RawOp { d: DocumentID; } -export type CreateOp = Partial & { create: { type: string; data: any }; }; -export type DeleteOp = Partial & { del: boolean; }; -export type EditOp = Partial & { op: any[]; }; +export type CreateOp = Partial & { create: { type: string; data: any } }; +export type DeleteOp = Partial & { del: boolean }; +export type EditOp = Partial & { op: any[] }; export type OTType = "ot-text" | "ot-json0" | "ot-json1" | "ot-text-tp2" | "rich-text"; diff --git a/types/sharedb/sharedb-tests.ts b/types/sharedb/sharedb-tests.ts index bfe6dea35ad4ca..0a42123fb18ab6 100644 --- a/types/sharedb/sharedb-tests.ts +++ b/types/sharedb/sharedb-tests.ts @@ -92,7 +92,7 @@ console.log(backend.extraDbs); backend.addProjection("notes_minimal", "notes", { title: true, creator: true, lastUpdateTime: true }); const readonlyProjection = backend.projections["notes_minimal"]; console.log(readonlyProjection.target, readonlyProjection.fields); -backend.submit({} as Agent, "notes_minimal", "doc1", { create: { data: {}, type: 'json uri type' } }, { +backend.submit({} as Agent, "notes_minimal", "doc1", { create: { data: {}, type: "json uri type" } }, { customField: true, anotherOption: { nested: "value" }, }, (error, ops, request) => {