@electric-sql/y-electric

中文文档
0.1.54发布于 2026/09/09Apache-2.0TypeScriptESM + CJS构建来源已验证近 30 天下载 2527

YJS network provider for ElectricSQL

原始使用文档
文档目录6 节
  1. How It Works
  2. Key Features
  3. Basic Setup
  4. Handling Writes
  5. Schema mapping in the client
  6. Storage providers

Y-Electric

A Yjs provider that enables real-time collaborative document editing using YJS, ElectricSQL and Postgres. It supports Awareness and can be used with any Yjs database providers. See a full example here.

How It Works

The typical flow for syncing shared documents using Yjs and Electric is the following:

  1. Developer exposes a shape proxy for authorizing shape requests
  2. Clients define a shape for syncing changes for a Y.Doc
  3. Developer exposes a [write API](#Handling Writes) for handling Yjs updates
  4. Voilà! Y-Electric automatically shares updates across all connected clients

Key Features

Basic Setup

import * as Y from 'yjs'
import {
  ElectricProvider,
  LocalStorageResumeStateProvider,
} from '@electric-sql/y-electric'
import { Awareness } from 'y-protocols/awareness'
import { parseToDecoder } from '@electric-sql/y-electric/utils'

const ydoc = new Y.Doc()
const awareness = new Awareness(ydoc)
const resumeStateProvider = new LocalStorageResumeStateProvider('my-doc')

const provider = new ElectricProvider({
  doc: ydoc,
  documentUpdates: {
    shape: {
      url: SHAPE_PROXY_URL,
      params: {
        table: `ydoc_update`,
        where: `room = '${room}'`,
      },
      parser: parseToDecoder,
    },
    sendUrl: DOC_UPDATES_SEND_URL,
    getUpdateFromRow: (row) => row.op,
  },
  awarenessUpdates: {
    shape: {
      url: SHAPE_PROXY_URL,
      params: {
        table: `ydoc_awareness`,
        where: `room = '${room}'`,
      },
      parser: parseToDecoder,
    },
    sendUrl: AWARENESS_UPDATES_SEND_URL,
    protocol: awareness,
    getUpdateFromRow: (row) => row.op,
  },
  resumeState: resumeStateProvider.load(),
})

// Subscribe to resume state changes to persist them
resumeStateProvider.subscribeToResumeState(provider)

Handling Writes

ElectricSQL is a read-path sync engine. This means that you bring your own API for handling document and awareness updates. See our sample server implementation here. It's very easy!

Document Updates

Y-Electric sends YJS document updates as binary data. You can directly save the body of the request as a bytea column into the database.

-- Schema definition
CREATE TABLE ydoc_updates(
    id uuid DEFAULT uuid_generate_v4() PRIMARY KEY,
    room text NOT NULL,
    op bytea NOT NULL
)
-- Save updates into individual rows
INSERT INTO ydoc_updates (room, op) VALUES ($1, $2)`

Awareness Updates

The awareness protocol implementation saves vector clock for each individual cliendId in separate rows:

Here is an example schema definition for ydoc_awareness:

-- Schema definitions
CREATE TABLE ydoc_awareness(
  client_id TEXT,
  room TEXT,
  op BYTEA NOT NULL,
  updated TIMESTAMPTZ DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (client_id, room)
);
-- Save
INSERT INTO ydoc_awareness (room, client_id, op, updated) VALUES ($1, $2, $3, now())
         ON CONFLICT (client_id, room) DO UPDATE SET op = $3, updated = now()

It's recommended that you can garbage collect old client rows using a database trigger since the provider can't reliability detect when a client goes away:

CREATE OR REPLACE FUNCTION gc_awareness_timeouts()
RETURNS TRIGGER AS $$
BEGIN
    DELETE FROM ydoc_awareness
    WHERE updated < (CURRENT_TIMESTAMP - INTERVAL '30 seconds') AND room = NEW.room;
    RETURN NEW;
END;
$$ LANGUAGE plpgsql;

CREATE TRIGGER gc_awareness_timeouts_trigger
AFTER INSERT OR UPDATE ON ydoc_awareness
FOR EACH ROW
EXECUTE FUNCTION gc_awareness_timeouts();

Schema mapping in the client

In the client, you need to pass a getUpdateFromRow to extract the column with the update binary. This allows Y-Electric to work with any backend schema.

Storage providers

Y-Electric works with existing database providers to store documents locally. When saving documents locally, we recommend providing a ElectricResumeStateProvider to save a resume point for the document, otherwise the entire document will be retransmitted when a new client session starts.

版本与发布记录

@electric-sql/y-electric 共发布 55 个版本,首次发布于 2025/05/13;过去 12 个月发布了 38 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
0.1.54latest2026/09/092 个月前
0.1.532026/09/012 个月前
0.1.522026/08/142 个月前
0.1.512026/08/043 个月前
0.1.502026/07/084 个月前
0.1.492026/07/024 个月前

关于 @electric-sql/y-electric 的常见问题

@electric-sql/y-electric 是什么?

YJS network provider for ElectricSQL

如何安装 @electric-sql/y-electric?

在项目目录运行 npm install @electric-sql/y-electric,或使用 pnpm add / yarn add / bun add。它还提供命令行工具 intent,可以用 npx intent 直接运行。

@electric-sql/y-electric 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@electric-sql/y-electric 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@electric-sql/y-electric 可以商用吗?

@electric-sql/y-electric 使用 Apache-2.0 许可。宽松许可:允许商业使用、修改和再分发,并附带明确的专利授权;分发时需保留许可与 NOTICE 文件,修改过的文件要注明改动。以上为概括,不构成法律意见,请以许可原文为准。

@electric-sql/y-electric 的下载量有多少?

根据 npm 官方下载统计,@electric-sql/y-electric 在 2026/10/14 – 2026/11/12 这 30 天内被下载 2,527 次,日均约 84 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@electric-sql/y-electric 还在维护吗?

最新版本 0.1.54 发布于 2026/09/09(2 个月前);至今共发布 55 个版本,其中过去 12 个月发布了 38 个;首次发布于 2025/05/13。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。