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

@aletso/atom-query

v0.1.5

Published

Effect atom query layer with SWR, optimistic updates, and SSR preloading

Downloads

367

Readme

@aletso/atom-query

Query and mutation primitives for Effect based apps that use route contracts from @aletso/effect-route.

This package is commonly used in layered data modules where:

  • Route and RouteGroup define API contracts
  • client data modules build query atoms and mutation triggers
  • server data modules build SSR preloaders
  • route loaders call .ensure(...) or .prefetch(...) on the right side (server or client)

Install

pnpm add @aletso/atom-query @aletso/effect-route @effect-atom/atom @effect/rpc effect

1) Define contracts once with @aletso/effect-route

import * as Route from "@aletso/effect-route/Route";
import * as RouteGroup from "@aletso/effect-route/RouteGroup";
import * as Schema from "effect/Schema";

export const SurveyListItem = Schema.Struct({
  id: Schema.String,
  title: Schema.String,
});

export class SurveyNotFound extends Schema.TaggedError<SurveyNotFound>()(
  "SurveyNotFound",
  {
    id: Schema.String,
  },
) {}

export class Group extends RouteGroup.make(
  Route.make("find_many", {
    success: Schema.Array(SurveyListItem),
  }),
  Route.make("find_by_id", {
    success: SurveyListItem,
    error: SurveyNotFound,
    payload: Schema.Struct({ id: Schema.String }),
  }),
).prefix("survey_") {}

2) Client data module

Example client data module.

import * as Atom from "@effect-atom/atom/Atom";
import * as Mutation from "@aletso/atom-query/Mutation";
import * as Query from "@aletso/atom-query/Query";
import * as Survey from "../domain/api/Survey";
import { SurveyApi } from "../client/api/survey";
import * as Effect from "effect/Effect";

const runtime = Atom.runtime(SurveyApi.Default);

export const surveyFindMany = Query.fromRoute(Survey.Group.find_many, {
  effect: Effect.gen(function* () {
    const api = yield* SurveyApi;
    return yield* api.findMany();
  }),
  runtime,
});

export const surveyFindById = Query.familyFromRoute(Survey.Group.find_by_id, {
  effect: (payload) =>
    Effect.gen(function* () {
      const api = yield* SurveyApi;
      return yield* api.findById(payload.id);
    }),
  runtime,
});

export const surveyDelete = Mutation.fromRoute(Survey.Group.delete, {
  effect: Effect.fnUntraced(function* (payload) {
    const api = yield* SurveyApi;
    return yield* api.delete(payload.id);
  }),
  runtime,
});

3) Server data module for SSR preload

Example server data module for SSR preloading.

import * as Query from "@aletso/atom-query/Query";
import * as Survey from "../domain/api/Survey";
import { SurveyService } from "../server/public/survey/service";
import * as Effect from "effect/Effect";

export const surveyFindManyServer = Query.serverFromRoute(
  Survey.Group.find_many,
  {
    effect: Effect.flatMap(SurveyService, (s) => s.findMany),
  },
);

export const surveyFindByIdServer = Query.serverFamilyFromRoute(
  Survey.Group.find_by_id,
  {
    effect: (payload) =>
      Effect.flatMap(SurveyService, (s) => s.findById(payload.id)),
  },
);

4) Isomorphic route loaders with .ensure(...) and .prefetch(...)

Example isomorphic loader usage.

import { createIsomorphicFn } from "@tanstack/react-start";
import {
  surveyFindById,
  surveyFindByIdServer,
  surveyFindManyServer,
} from "../data/survey";
import { serverRuntime } from "../routes/api/rpc/$";

export const ensureSurvey = createIsomorphicFn()
  .server((context, id: string) =>
    surveyFindByIdServer.ensure(context.registry, serverRuntime, { id }),
  )
  .client((context, id: string) =>
    surveyFindById.ensure(context.registry, { id }),
  );

export const prefetchSurveyList = createIsomorphicFn().server((context) =>
  surveyFindManyServer.prefetch(context.registry, serverRuntime),
);

5) SWR and optimistic cache updates

Example optimistic cache update flow.

import { Atom } from "@effect-atom/atom-react";
import * as Query from "@aletso/atom-query/Query";
import * as Arr from "effect/Array";
import * as Data from "effect/Data";
import * as Option from "effect/Option";

type Survey = { id: string; title: string };

export type SurveyCacheUpdate = Data.TaggedEnum<{
  Delete: { readonly id: string };
  Upsert: { readonly item: Survey };
}>;

const remoteAtom = surveyFindMany.pipe(Atom.setIdleTTL(0));

export const surveyList = Query.lazyOptimistic(
  remoteAtom,
  (current, update: SurveyCacheUpdate) => {
    switch (update._tag) {
      case "Delete":
        return Arr.filter(current, (s) => s.id !== update.id);
      case "Upsert": {
        const index = Arr.findFirstIndex(
          current,
          (s) => s.id === update.item.id,
        );
        return Option.match(index, {
          onNone: () => Arr.prepend(current, update.item),
          onSome: (i) => Arr.replace(current, i, update.item),
        });
      }
    }
  },
).pipe(Atom.setIdleTTL(300_000), Query.swr({ staleTime: 60_000 }));

// Force next read to revalidate
surveyList.invalidate();

6) Request header propagation for middleware aware queries

CurrentHeaders lets server queries read headers from effect context when explicit headers are not passed.

import { CurrentHeaders } from "@aletso/atom-query/RequestHeaders";
import * as Effect from "effect/Effect";
import * as ManagedRuntime from "effect/ManagedRuntime";
import { getRequestHeaders } from "@tanstack/react-start/server";

const makeServerRuntime = (layer: any, memoMap: any) => {
  const raw = ManagedRuntime.make(layer, memoMap);
  return new Proxy(raw, {
    get(target, prop) {
      const value = Reflect.get(target, prop, target);
      if (
        (prop === "runPromise" || prop === "runPromiseExit") &&
        typeof value === "function"
      ) {
        return (
          effect: Effect.Effect<any, any, any>,
          ...rest: ReadonlyArray<any>
        ) => {
          let headers: Record<string, string> = {};
          try {
            headers = getRequestHeaders() ?? {};
          } catch {
            headers = {};
          }
          return value.call(
            target,
            Effect.provideService(effect, CurrentHeaders, headers),
            ...rest,
          );
        };
      }
      return typeof value === "function" ? value.bind(target) : value;
    },
  });
};

API surface

  • Query.fromRoute, Query.familyFromRoute
  • Query.serverFromRoute, Query.serverFamilyFromRoute
  • Query.swr, Query.lazyOptimistic
  • Mutation.fromRoute
  • RequestHeaders.CurrentHeaders