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 🙏

© 2025 – Pkg Stats / Ryan Hefner

@lightningdb/server

v0.1.18

Published

LightningDB Server

Readme

LightningDB Server

Getting Started

LightningDB is made up of a server and client library.

Typically LightningDB would be installed in a monorepo, with a folder for the server and a folder for the client. However you are free to disregard this.

See a working demo at https://github.com/lightningdatabase/lightning-demo.

Setup Server

We start by setting up the server. This includes the database schema, which is used to automatically generate the interface and client.

Setup Zenstack

LightningDB is built on top of Zenstack, which is built on top of Prisma.

The easiest way to install ZenStack is to use the zenstack init command. In an existing TypeScript project folder, run the following command:

npx zenstack@latest init

The "init" command does the following things for you:

  1. Install Prisma if it's not already installed.
  2. Install the zenstack CLI package as a dev dependency.
  3. Install the @zenstackhq/runtime package - used for enhancing PrismaClient at the runtime.
  4. Copy the prisma/schema.prisma file to schema.zmodel if it exists; otherwise, create a new template schema.zmodel file.

You can always manually complete the steps above if you have a special project setup that the "init" command doesn't work with.

After the initialization, please remember that you should edit the schema.zmodel moving forward. The prisma/schema.prisma file will be automatically regenerated when you run zenstack generate.

LightningDB uses Postgres, to switch Prisma change the datasource to:

datasource db {
  provider = "postgresql"
  url      = env("DATABASE_URL")
}

Add the plugin

Install the required packages

npm install @lightningdb/server @lightningdb/plugin

Edit the schema.zmodel file and add the plugin section

//schema.zmodel
...
plugin lightning {
  provider = '@lightningdb/plugin'
}
...

You can now run the plugin to generate the server code

npx zenstack generate

Setup the server

LightningDB sends updates over a websocket.

See below for a sample express implementation with the websocket handler.

//index.ts
import dotenv from "dotenv";

dotenv.config();

import express from "express";
import { WebSocketServer } from "ws";
import http from "http";
import "@lightningdb/server/replication";
import handler from "@lightningdb/server";

const app = express();

app.use(express.json());

const server = http.createServer(app);
const wss = new WebSocketServer({ server });

wss.on("connection", handler);

server.listen(3000, () =>
  console.log(`🚀 Server ready at: http://localhost:3000`)
);

LightningDB connects to the Postgres database and uses replication to catch any changes and update the frontend. This is achieved with the import "@lightningdb/server/src/replication" import.

Setup replication slot in Postgres

Run the following sql to setup a replication slot:

SELECT * FROM pg_create_logical_replication_slot('lightningdb_slot', 'wal2json');

The default name is "lightningdb_slot", however you can set the name with the REPLICATION_SLOT environment variable.

Setup Client

Install client library

In the frontend project run:

npm install @lightningdb/client

Update plugin

Edit the schema.zmodel file and update the plugin section to add the path to the frontend project

//schema.zmodel
...
plugin lightning {
  provider = '@lightningdb/plugin'
  clientPath = '../client'
}
...

Run npx zenstack generate to generate the frontend code.

Add provider

Render a <DBProvider> around your applciation:

import { StrictMode } from "react"
import { createRoot } from "react-dom/client"
import App from "./App.tsx"
import { DBProvider } from "../lightningdb"

createRoot(document.getElementById("root")!).render(
  <StrictMode>
    <DBProvider url="ws://localhost:3000">
      <App />
    </DBProvider>
  </StrictMode>,
)

Create your first query

It is easy to query the data and LightningDB keeps the frontend up to date automatically.

To query data in a component add:

const { data, loading, error } = useQuery({
  users: {},
});

return <pre>{JSON.stringify(data, null, 2)}</pre>;