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

jfauna

v1.0.1

Published

jQuery-like chainable methods to make FaunaDB more accessible

Downloads

4

Readme

jFauna

jQuery-like chainable methods to make FaunaDB more accessible

Installation

npm i jfauna

Usage

const jFauna = require('jfauna');
const serverClient = require('./faunadb-serverclient');

const $ = new jFauna(serverClient);

// get first post
$('posts').get(1).now().then((posts) => console.log(posts.length)); // 1

// get all posts where title is 'My first post'
$('posts').get().where('title').is('My first post').then((posts) => posts[0].title); // 'My first post'

API

It starts by creating a new instance of jFauna and passing in your server client instance as the argument. This allows the instance to make calls to your database with the correct permissions.

const $ = new jFauna(serverClient);

From here, you can make simple queries to your database. One of the easiest things to do is to create/reference a collection.

await $('posts'); // Either create or reference a collection called 'posts'

This will either create the collection if it doesn't exist or start the query using this collection.

CRUD

From the collection, you can query using the following CRUD commands:

  • .create(data) - Creates documents in the collection
  • .get(amount) - Gets the specified number of documents (requires longer chain).
  • .update(data) - Updates the documents in the collection (requires longer chain).
  • .remove(amount) - Deletes the specified number documents (requires longer chain).

Chaining

All the CRUD methods (except .create()) have the following chains possible. Each returns a promise.

  • .now() - Executes the query now on the collection. If no amount is set prior, the default is 64 (FaunaDB default).
  • .where(field).is(value) - Executes a search on the collection for where a given field equals the given value.
  • .where(field).isnt(value) - Executes a search on the collection for where a given field does not equal the given value.

Examples

// Create a user
$('users').create({ name: 'Jane Doe', email: '[email protected]' });

// Update email address for user(s)
$('users').update({ email: '[email protected]' }).where('email').is('[email protected]');

// Get the first user named Jane Doe
$('users').get(1).where('name').is('Jane Doe');

// Deleting all users (collection still exists)
$('users').delete().now();

Future ideas

  • Case-insensitive searching
  • Customized methods (change .is() to .equals())
  • .includes(value) - partial search
  • .between(lower).and(upper) - range search
  • .lt(number), .gt(number), .lte(number), .gte(number) - numbers / dates

Purpose

I really loved the idea of FaunaDB, easy database of JSON blobs that you could query in static site hosting platforms. It's conceptually good for small projects where you need to store data but the language of constructing queries is super ugly.

This is what you need to do to get all entries (documents) in a table (collection), assuming you have the database and client:

CreateIndex({
  name: 'all-posts',
  source: Collection('Posts'),
})

Map(
  Paginate(
    Match(
      Index('all-posts')
    )
  ),
  Lambda(
    'x',
    Get(
      Var('x')
    )
  )
)

This, doesn't look fun to construct at all. Imagine trying to get entries for a specific kind of value. Probably much worse!

So, I dove into the language and began trying to make an abstraction layer for the language that helps do common operations without the need to know what's happening under-the-hood. As a user just looking to store data, I don't want to need to construct an index everytime I make a query. It should just make the query and be smart enough to know the index.

I also made this to have chainable methods that look alot like jQuery or testing assertion libraries. This gets the composition looking closer to the description of the request in easily readable terms. I've also begun to build this with jQuery's no-failure principle. The application shouldn't stop because you've made a mistake, but we should still log if something goes wrong. Not everything is prepared to sliently fail this way yet.