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

gun-flint

v0.0.28

Published

Micro-framework for building Gun adapters

Downloads

20

Readme

Gun-Flint: Easy Gun Adapters

Gun-Flint is a package that makes it easy to write adapters to connect your Gun database to various services (like databases, Pusher, etc.). Gun-Flint is not itself an adapter.

Installation

npm install gun-flint

or

yarn add gun-flint

A Key Decision: Node, Key:Value, Delta

Since Gun is a graph database, its data structure requires some special consideration. Gun-Flint attempts to provide as much flexibility for the adapter developer to store data in a format that makes sense for the storage system you are using.

Building your Gun-Flint gives you an advantage of allowing Flint to ensure that the data coming from Gun reaches your adapter in a consistent format and returns data to Gun in a format that it recognizes.

Node Storage

get request expect to read an entire Gun node; put requests write an entire node.

Pros:

  • Easiest to implement
  • Flint handles merging of existing nodes with a delta on write so little chance of data corruption during conflict resolution.

Cons:

  • Slower performance (requires a read > merge for every write); this is especially problematic if your data requires large nodes (e.g., a users node with millions of users)
  • Large nodes could overwhelm memory and cause crashes; if you anticipate this, consider Key:Value storage with streaming.

When to Use: Most/all nodes are small; nodes are frequently created; optimal for document-based databases (e.g., MongoDB; Postgres)

Example: gun-mongo

Key:Value

get request returns an array/list of nodes properties; put requests write batch updates to specific node's key:value pairs.

Pros:

  • Easy to implement
  • Faster than full-node storage
  • Doesn't require any additional concerns for conflict resolution

Cons:

  • Every key:value pair (node property) would presumably require a separate record in storage (although not necessarily)
  • Read requests require retrieval of multiple records

When to Use: Nodes are small/medium in size; some large nodes; nodes are frequently updated

Example: gun-mongo-key

Delta Storage

get request return an entire node, formatted in a way Gun recognizes; put requests receive a delta (diff) of node properties as well as conflict-resolution state indicators.

Pros:

  • Most flexibility for how you store a delta
  • High performance possibility, depending on how you implement storage.

Cons:

  • Most difficult to implement, and incorrect implementation can lead to data corruption during conflict resolution.

When to Use: You need total control of storage format and one of the above formats is not sufficient.

Usage

Whichever storage method you decide, your adapter needs to only implement three methods: opt, get, put. See the documentation for each storage method as the exact API depends on the method selected.

Stripped down, the API looks like this:


const {Flint, NodeAdapter} = require('gun-flint');

const myGunAdapter = new NodeAdapter({
    opt: function(context, options) {
        // etc
    },
    get: function(key, field, done) {
        // handle read
    },
    put: function(node, done) {
        // handle write
    }
});

Flint.register(myGunAdapter);

Using Your Adapter

In the end, the good users of your adapter should install it like this:


var Gun = require('gun');

// Adapter must come after requiring Gun but before `new Gun`
require('your-awesome-adapter');

// Adapters all set up? Instantiate Gun.
var gun = new Gun({
    myAwesomeAdapter: {
        key: "This gets passed into the `opt` call when gun is initialized. Useful for allowing those who use your adapter to pass in DB drivers of the like."
    }
});

Testing Your Adapter

Flint comes packaged with an integration testing suite. See full documentation here.

Performance Profiling

Flint comes packaged with an performance suite that will run against your adapter. See full documentation here.

Troubleshooting

If your adapter's opt function is never called, or when it is called, it doesn't have options that you passed to the constructor, here are some steps:

  1. Do NOT list Gun in your list of dependencies. You can list it in peerDependencies or devDependencies, especially the later if you need it for testing your adapter.
  2. Make sure Gun is not installed globally (run npm list -g --depth=0 to check), and npm uninstall -g gun if it is.
  3. Delete your node_modules and install a fresh set (preferably from a lockfile)