npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@trokster/svelte-electric-through-db

v0.0.4

Published

A robust, offline-first synchronization library for SvelteKit using ElectricSQL, PGlite, and Yjs. This library implements the "Through the Database" pattern, where Yjs updates are persisted to a local PGlite database and synced via ElectricSQL.

Readme

Svelte Electric Through DB

A robust, offline-first synchronization library for SvelteKit using ElectricSQL, PGlite, and Yjs. This library implements the "Through the Database" pattern, where Yjs updates are persisted to a local PGlite database and synced via ElectricSQL.

Features

  • Offline-First: All writes go to the local PGlite database immediately.
  • CRDT Synchronization: Uses Yjs for automatic conflict resolution.
  • Bring Your Own Auth (BYOA): Plug in any authentication provider.
  • Full History: Preserves document history with automatic compaction.
  • SvelteKit Ready: Designed for seamless integration with SvelteKit.

Installation

npm install @trokster/svelte-electric-through-db

Usage

1. Client-Side Setup

Initialize the ElectricClient in your Svelte component or store.

import { ElectricClient } from '@trokster/svelte-electric-through-db';

const client = new ElectricClient({
    dbPath: 'idb://my-app-db',
    electricService: import.meta.env.VITE_ELECTRIC_SERVICE,
    docId: 'my-document-id',
    ownerId: 'current-user-id',
    authProvider: async () => {
        // Fetch your auth token here
        const res = await fetch('/api/auth/token');
        const { token } = await res.json();
        return token;
    }
});

await client.init();

// Use Yjs doc
const ytext = client.doc.getText('content');

2. Server-Side Setup

This library provides helpers to set up the necessary database schema and endpoints.

Database Schema (src/lib/server/db/init.ts):

import { initializeSchema } from '@trokster/svelte-electric-through-db/server';

// Run this on server startup or migration
await initializeSchema(pool, 'my_custom_schema'); // Optional: defaults to 'public'

Auth Token Helper (src/routes/api/auth/token/+server.ts):

import { createElectricToken } from '@trokster/svelte-electric-through-db/server';
import { json } from '@sveltejs/kit';

export async function GET({ locals }) {
    const token = createElectricToken(locals.user.id);
    return json({ token });
}

Database Selection

  • Server-Side: The database is determined by the pg.Pool or Client you pass to initializeSchema and handlePushRequest. Configure your connection string to point to the desired database (e.g., postgres://user:pass@host:5432/my_database).
  • Client-Side: The local database name is set via the dbPath option in ElectricClient (e.g., idb://my-local-db).

Environment Variables

The library uses the following environment variables if not provided explicitly:

  • ELECTRIC_TOKEN_SECRET: Used by createElectricToken to sign tokens.
  • AUTH_JWT_KEY: Used by verifyAuthToken to verify session tokens (if using the helper).

Ensure these are set in your server environment (e.g., .env file). 2. Sync: The SyncManager pushes these updates to your API and pulls changes from ElectricSQL. 3. Conflict Resolution: Yjs handles merging updates from different clients.