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

yeeql

v1.0.7

Published

A SQL-like interface around Y.js

Downloads

4

Readme

yeeql

API Documentation

yee dinosaur

yeeql (pronounced yee-quel) is a wrapper atop of Y.js's Y.Map.

It allows for creating schematized SQL-like queries on top of Y.Maps.

Features

  • select, filter, sort, groupBy, count
  • subscribe to query changes
  • React support with useQuery

Install

npm install yeeql

Example

import { UUID, Field, Table } from 'yeeql'
import * as Y from 'yjs'

const doc = new Y.Doc()
const yTable = doc.getMap('dinosaurs') as Y.Map<Y.Map<unknown>>

const dinosaursSchema = {
    id: new Field<UUID>(),
    genus: new Field<string>(),
    ageInMillionsOfYears: new Field<number>(),
    diet: new Field<'herbivore' | 'carnivore'>()
}

const dinoTable = new Table(yTable, dinosaursSchema)

dinoTable.insert({
    genus: 'Tyrannosaurus',
    ageInMillionsOfYears: 67,
    diet: 'carnivore'
})

dinoTable.insert({
    genus: 'Stegosaurus',
    ageInMillionsOfYears: 152,
    diet: 'herbivore'
})

dinoTable.insert({
    genus: 'Triceratops',
    ageInMillionsOfYears: 66,
    diet: 'herbivore'
})

const herbivoresByAge = dinoTable.query({
    select: ['genus', 'ageInMillionsOfYears'],
    filter: { diet: 'herbivore' },
    sort: (a, b) => a.ageInMillionsOfYears - b.ageInMillionsOfYears
})

herbivoresByAge.result /* [
    { genus: 'Triceratops', ageInMillionsOfYears: 66 }, 
    { genus: 'Stegosaurus', ageInMillionsOfYears: 152 }
] */

import { QueryChange } from 'yeeql'
const herbivoresByAgeObserver = (change: QueryChange<typeof herbivoresByAge>) => {
    console.log(`herbivoresByAge change ${change}`)
}

herbivoresByAge.observe(herbivorseByAgeObserver)

dinoTable.insert({
    genus: 'Brachiosaurus',
    ageInMillionsOfYears: 150,
    diet: 'herbivore'
})

/*
`herbivoresByAgeObserver` logs:
herbivorsByAge change {
    kind: 'add',
    row: { genus: 'Brachiosaurus', ageInMillionsOfYears: 150 },
    newIndex: 1, // inserts after Triceratops and before Segosaurus according to query `sort` function
    type: 'add' // Indicates that the row was newly added to the table. If the row came into the filter of this query due to an update, is 'update'
}
*/

herbivoresByAge.result /* [
    { genus: 'Triceratops', ageInMillionsOfYears: 66 },
    { genus: 'Brachiosaurus', ageInMillionsOfYears: 150 },
    { genus: 'Stegosaurus', ageInMillionsOfYears: 152 }
] */

const velociraptorId: UUID = dinoTable.insert({
    genus: 'Velociraptor',
    ageInMillionsOfYears: 72,
    diet: 'carnivore'
})

// herbivoresByAgeObserver does not log, since the Velociraptor is not a herbivore

dinoTable.update(velociraptorId, 'diet', 'herbivore')

/*
`herbivoresByAgeObserver` logs:
herbivorsByAge change {
    kind: 'add',
    row: { genus: 'Velociraptor', ageInMillionsOfYears: 72 },
    newIndex: 1, // inserts after Triceratops and before Brachiosaurus according to query `sort` function
    type: 'update' // Indicates that the row newly came into the query's filter due to an update. If the row was newly added, would be 'add'
}
*/

herbivoresByAge.result /* [
    { genus: 'Triceratops', ageInMillionsOfYears: 66 },
    { genus: 'Velociraptor', ageInMillionsOfYears: 72 },
    { genus: 'Brachiosaurus', ageInMillionsOfYears: 150 },
    { genus: 'Stegosaurus', ageInMillionsOfYears: 152 }
] */

dinoTable.update(velociraptorId, 'ageInMillionsOfYears', 160)

/*
`herbivoresByAgeObserver` logs:
herbivorsByAge change {
    kind: 'update',
    row: { genus: 'Velociraptor', ageInMillionsOfYears: 160 },
    oldIndex: 1,
    newIndex: 3, // Has moved to the end of the query results because it has the highest age,
    oldValues: { ageInMillionsOfYears: 72 },
    type: 'update' // Always 'update' for `kind: 'update'` changes
}
*/

dinoTable.delete(velociraptorId)

/*
`herbivoresByAgeObserver` logs:
herbivorsByAge change {
    kind: 'remove',
    row: { genus: 'Velociraptor', ageInMillionsOfYears: 160 },
    newIndex: 3,
    type: 'delete'
}
*/

React hook

// (Assuming we're using the dinosarus table above)

import React from 'react'
import { useQuery } from 'yeeql'

const genusSort = (a: { genus: string }, b: { genus: string }) => a.genus.localeCompare(b.genus)

function DinoListComponent({ diet }: { diet: 'herbivore' | 'carnivore' }) {
    const dinos = useQuery(() => dinoTable.query({
        select: ['id', 'genus'],
        filter: { diet }
        sort: genusSort
    }), [diet])

    const dinoNames = dinos.map(dino => (
        <p key={dino.id}>
            ${dino.genus}
        </p>
    ))
    
    return (
        <>
            <h1>
                ${diet}s
            </h1>
            {dinoNames}
        </>
    )
}

<DinoListComponent diet='carnivore'/> // Rendered somewhere

const allosaurusId = dinoTable.insert({ genus: 'Allosaurus', ageInMillionsOfYears: 145, diet: 'carnivore' })
// DinoListComponent re-renders

dinoTable.update(allosaurusId, 'ageInMillionsOfYears', 150)
// DinoListComponent DOES NOT re-render, since 'ageInMillionsOfYears' is not selected in the query

dinoTable.insert({ genus: 'Styracosaurus', ageInMillionsOfYears: 75, diet: 'herbivore' })
// DinoListComponent DOES NOT re-render, since Styracosaurus is not a carnivore

dinoTable.update(allosaurusId, 'genus', 'Allosaurus ❤️')
// DinoListComponent re-renders, since 'genus' is selected

API Documentation

Performance

Runtimes

yeeql has a O(QD) runtime when a row is inserted, updated, or deleted, where:

  • Q is the number of queries whose result is affected by the operation.
  • D is the number of fields of the row.

This is notably better than the naive runtime of O(ND) where we must check each query to see if an operation has affected its result.

Query Cleanup

Queries are only weakly referenced by the table, and once they are garbage collected, they're no longer updated (obviously). This saves memory and runtime.

Query Caching

The Table weakly caches queries, and returns the same instance for duplicate queries. This additionally saves runtime and memory.

Example

const genusSort = (a: { genus: string }, b: { genus: string }) => a.genus.localeCompare(b.genus)

const queryA = dinoTable.query({
    select: ['genus', 'diet'],
    sort: genusSort
})

const queryB = dinoTable.query({
    select: ['genus', 'diet'],
    sort: genusSort
})

console.log(queryA === queryB) // Prints `true`

Note how the genusSort function is the same instance. Prefer using common instances for each type of sort function you use. This way yeeql can more effectively re-use queries, since it knows the sort function is the same.

No Copying

Once a row is constructed from the Y.Table, it is reused across all queries. Each query maintains its result using these rows. The query result is not copied on access, so if you reference a query's result, that array will change as the query's result changes.

Future Work

  • Non-literal (Less Than, Greater Than) filter parameters. (Should be possible using a datastructure that maps segments of a domain to different values.)
  • LIMIT and OFFSET equivalents.
  • Option to validate rows and only include rows from peers with a correct schema.