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

lazy-sync

v2.0.1

Published

A Vue library for querying data from a Node.js backend running Sequelize ORM, and keeping the data in sync.

Downloads

12

Readme

Lazy Sync

A Vue library for querying data from a Node.js backend running Sequelize ORM, and keeping the data in sync.

It enables you to write realtime applications with code like this in a decently efficient manner:

<template>
  <div>
    <div v-for="post in posts.list" :key="post.id">
      <h2>{{ post.name }}</h2>
      <i>{{ post.author }}</i>
      <p>{{ post.text }}</p>
      <hr>
    </div>
  </div>
</template>

<script>
export default {
  name: 'Blog',
  props: {
    author: {}
  },
  data () {
    const where = { AuthorId: this.author.id }
    const options = { limit: 10, order: ['createdAt', 'DESC'] }
    return {
      // Fetches the latest 10 posts by the author from the backend,
      // and keeps the list of them in sync when posts are modified, added or removed.
      posts: Z.Posts.find(where, options)
    }
  }
}
</script>

What's happening below the surface is:

  1. Fetch a list of post IDs by making a query to the backend (or cache if in sync). Cache the list of IDs.
  2. Fetch the content of any missing posts and cache them.
  3. When a post gets added or removed, the library gets notified via websocket, and marks the lists as out of sync.
  4. The next time the list gets rendered, goto 1.
  5. If a post gets modified, the post gets updated directly. Only lists that queries fields that have been modified will be invalidated.

Implementation of communication, access control and limiting queries made by untrusted users is left as an exercise. (for now...)

Installation

npm i lazy-sync

Frontend example

import { Z } from 'lazy-sync/LazySync'

// these are server functions called by lazySync
const clientApi = {
  access: {
    async create ({ model, objValues }) {},
    async addMember ({ model, entryId, memberModel, memberEntryId }) {},
    async removeMember ({ model, entryId, memberModel, memberEntryId }) {},
    async bulkCount ({ model, searches = [] }) {},
    async bulkFind ({ model, searches = [], limit = undefined, offset = 0, order = [] }) {},
    async get ({ model, entryIds = [] }) {},
    async order ({ model, entryIds = [], order = [] }) {},
    async update ({ model, entryId, objValues }) {},
    async delete ({ model, entryId }) {},
    async restore ({ model, entryId }) {}
  }
}

// replace them with a socket.io event call to 'api'
Object.keys(clientApi.access).forEach(method => {
  clientApi.access[method] = args => {
    const api = 'access'
    return new Promise((resolve, reject) => {
      socket.emit('api', { api, method, args }, function callback (error, result) {
        error ? reject(new Error(error)) : resolve(result)
      })
    })
  }
})

Z.init(['Posts', 'Users', 'News'], clientApi.access)

export const socket = io(import.meta.env.VITE_BACKEND_URL)
socket
  .on('connect', () => {
    console.log('connected to websocket')
    Object.keys(Z).forEach(model => {
      if (typeof Z[model].ready === 'boolean') {
        // set ready to true when ready
        Z[model].ready = true
        Z[model].invalidate()
      }
    })
  })
  .on('z', data => {
    Z.onChange(data)
  })

Backend example

TODO