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 🙏

© 2024 – Pkg Stats / Ryan Hefner

electric-query

v0.0.14

Published

Library for integrating ElectricSQL queries with your (React) routes

Downloads

20

Readme

electric-query

Library for deeply integrating ElectricSQL partial syncing and queries with your React app routes.

Install

npm install electric-query

Why

This library makes it easy to sync and query the exact data that's needed for each route.

The simplest way to build an ElectricSQL app is to sync upfront all data. But this gets slow as for larger apps. So just like code splitting, you can split data syncing along route boundaries so the user waits for only the minimal amount of data to be synced.

ElectricSQL has this concept of “Shapes” — which let you declare the shape of data you want synced to construct a particular route’s UI. It’s basically the declarative equivalent of making an API call (an imperative operation). Instead of saying “fetch this shape of data”, you say “sync this shape of data”. You get the same initial load but ElectricSQL also ensures any updates across the system continue to get synced to you in real-time.

Usage

The library exposes an initElectric function which takes care of initializing Electric.

import { initElectric, setLoggedOut } from "electric-query"
import { Electric, schema } from "./generated/client"
import sqliteWasm from "wa-sqlite/dist/wa-sqlite-async.wasm?asset"

if (loggedIn) {
  const electric = await initElectric({
    appName: `my-app`,
    schema,
    sqliteWasmPath: sqliteWasm,
    config: {
      auth: {
        token,
      },
      debug: false, //DEBUG_MODE,
      url: electricUrl,
    },
  })
} else {
  setLoggedOut()
}

In the loader (or equivalent) function for each route, you define the sync shapes and queries for each route. Electric Query ensures both are finished before calling your route component. This means the new route can immediately render without any blinking.

// In routes
const routes = [
  ...otherRoutes,
  {
    path: `/type/:id`,
    element: <Type />,
    loader: async (props) => {
      const url = new URL(props.request.url)
      const key = url.pathname + url.search
      await electricSqlLoader<Electric>({
        key,
        shapes: ({ db }) => [
          {
            shape: db.youtube_videos.sync(),
            // Check that at least one video is synced.
            // Eventually Electric will probably have metadata on synced status
            // we can check.
            isReady: async () => !!(await db.youtube_videos.findFirst()),
          },
        ],
        queries: ({ db }) => Video.queries({ db, id: props.params.videoId }),
      })

      return null
    },
  },
]

Each route component then uses an useElectricData hook to get the results of the queries.

For easy reading, we suggest you write component queries alongside the UI code.

// In route components
import { useElectricData } from "electric-query"
import { useLocation } from "react-router-dom"
import { Electric, schema } from "../generated/client"

const queries = ({ db }: { db: Electric[`db`] }) => {
  return {
    foo: db.my_table.liveMany(),
  }
}

export default function Component() {
  const location = useLocation()
  const { foo } = useElectricData(location.pathname + location.search)

  return JSON.stringify(foo, null, 4)
}

Component.queries = queries

For a full example of using this library, see this starter https://github.com/KyleAMathews/vite-react-router-electric-sql-starter