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

@synvox/offline

v0.1.8

Published

An offline sync database.

Downloads

5

Readme

@synvox/offline

An offline sync database.

Define a storage engine

Offline does not come bundled with a storage engine, but you can implement it on top of any key-value store. Here's an example using localStorage:

import {StorageEngine, MemoryStorage} from '@synvox/offline'

class LocalStorageEngine implements StorageEngine {
  async getItem<T>(key: string){
    if (!(key in localStorage)) return undefined
    return JSON.parse(localStorage[key]) as T
  }
  async setItem<T>(key: string, value: T){
    const json = JSON.stringify(value)
    localStorage[key] = json
  }
  async removeItem(key: string) {
    delete localStorage[key]
  }
  async getAllKeys() {
    return localStorage.keys()
  };
  async clear() {
    localStorage.length=0
  };
  async transaction(fn: (trx: MemoryStorage) => Promise<void>){
    const trx = new MemoryStorage(this);
    try {
      await fn(trx);
      await trx.commit();
    } catch (e) {
      trx.clear();
      throw e;
    }
  };
}

Define tables you want to sync

const storage = new LocalStorageEngine();
const db = new Database(storage);

db.table({
  key: 'pages',
  async getSince(since: Date) {
    return getAllArticlesUpdatedAfter(date);
  },
});

db.sync();

Query the Database

await db.query('pages', {
  bookId: '123',
});

This will scan through each item in the pages table for pages that have bookId = 123.

Define Indexes

You can optionally specify indexes for offline to use instead of scanning through each item. Define indexes with {[indexName:string]: columnName}.

db.table({
  key: 'pages',
  indexes: {
    pageIdIndex: 'pageId',
  },
  async getSince(since: Date) {
    return getAllArticlesUpdatedAfter(date);
  },
});

await db.query('pages', {
  bookId: '123',
});

Removing Items

You can define a function which all changed items will go through to be checked if they were deleted. If the function returns true, the item will be removed from the storage engine and indexes updated accordingly.

db.table({
  key: 'pages',
  indexes: {
    pageIdIndex: 'pageId',
  },
  async getSince(since: Date) {
    return getAllArticlesUpdatedAfter(date);
  },
  isItemDeleted(item) {
    return item._deleted;
  },
});

await db.query('pages', {
  bookId: '123',
});