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

@tducasse/js-db

v0.2.2

Published

A very tiny js in-memory database, using native JavaScript objects

Downloads

2

Readme

js-db

weekend-project Build Status Coverage Status

A very tiny js in-memory database, using native JavaScript objects. Inspired by mongodb's syntax, even though it implements a subset of it.

Install

Using Node:

npm i @tducasse/js-db

Or in the browser:

<html>
  <head>
    <script src="https://unpkg.com/@tducasse/js-db/dist/umd/js-db.js"></script>
    <script>
      // the script is exported as 'jsdb'
      jsdb.register("users")
      jsdb.db.users.insert({name: "name"});
      const user = jsdb.db.users.findOne();
      console.log(user.name)
      // "name"
    </script>
  </head>
</html>

Docs

We basically export one object, called db, to which we attach collections using the register() function.

import { db, register } from "@tducasse/js-db";

// register the "users" collection
register("users");

// now you can call `db.users`
db.users.insert({ name: "John" });

const user = db.users.findOne({ name: "John" });
console.log(user);
// { name: "John" }

register()

Registers a collection on the db object.

register("users");

// now you can access `users` on `db`
db.users.find();
// []

seed()

Import initial data into the database. Also calls register() under the hood.

const items = {
  users: [{ name: "John" }, { name: "Joe" }],
  cities: [{ name: "Paris" }],
};

seed(items);

const users = db.users.find();
// [{ name: "John" }, { name: "Joe" }]
const cities = db.cities.find();
// [{ name: "Paris" }]

reset()

Just a nice helper to reset the database. I mostly use it in tests though!

register("users");

// now you can access `users` on `db`
db.users.find();
// []

reset();
// db.users is now undefined

collection.find()

Finds all the elements that match the query. Calling it without a query argument will just return everything.

register("users");
db.users.insert([{ name: "John" }, { name: "Joe" }]);

const users = db.users.find({ name: "John" });
// [{ name: "John" }]

collection.count()

Like find(), except it just returns the number of elements that match the query. Again, calling it without any arguments will count everything.

register("users");
db.users.insert([{ name: "John" }, { name: "Joe" }]);

const count = db.users.find({ name: "John" });
// 1

collection.findOne()

Finds the first (not sure about the order) element that matches the query. Calling it without a query argument will just return the first item (again, not really sure which).

register("users");
db.users.insert([{ name: "John" }, { name: "Joe" }]);

const users = db.users.findOne({ name: "John" });
// { name: "John" }

collection.insert()

You've seen me use it already, but it does what it looks like: inserts the item(s) in the collection. Can be either an array or a single item.

register("users");
// works with an array
db.users.insert([{ name: "John" }, { name: "Joe" }]);
// or with a single element
db.users.insert({ name: "Mark" });

collection.remove()

Removes all the elements that match the query.

register("users");
db.users.insert([{ name: "John" }, { name: "Joe" }]);

db.users.remove({ name: "John" });
const users = db.users.find();
// [{ name: "Joe" }]

collection.update()

Updates every element that matches the query. Use $set for single value fields, and $push for array fields. $push and $set don't need the key to exist, they will create it if it doesn't.

register("users");
db.users.insert([{ name: "John" }, { name: "Joe", cities: [] }]);

// use $set for single value fields
db.users.update({ name: "John" }, { $set: { name: "Mark" } });
const users = db.users.find();
// [{ name: "Mark"}, { name: "Joe" }]

// and $push for array fields
db.users.update({ name: "Joe" }, { $push: { cities: "Melbourne" } });
const users = db.users.find();
// [{ name: "Mark"}, { name: "Joe", cities: ["Melbourne"] }]

Note on the query/update object

You can access nested keys using the dot-notation syntax.

register("users");
db.users.insert({ name: "John" });

db.users.update({ name: "John" }, { $set: { "address.city": "Melbourne" } });
const users = db.users.find();
// [{ name: "John", address: { city: "Melbourne" }}]

const user = db.users.findOne({ "address.city": "Melbourne" });
// [{ name: "John", address: { city: "Melbourne" }}]

Creating a shell

You can access the database in your code, importing the db object, but you can also create a separate npm script to run it through a Node repl.

// start a socket on 1337
net
  .createServer((socket) => {
    const r = repl.start({
      prompt: "js-db>",
      input: socket,
      output: socket,
      terminal: true,
      preview: false,
    });
    // share the `db` object with the socket
    r.context.db = db;
  })
  .listen(1337);
// in cli.js
import net from "net";

const sock = net.connect(1337);

process.stdin.pipe(sock);
sock.pipe(process.stdout);

process.stdin.on("data", (b) => {
  if (b.length === 1 && b[0] === 4) {
    process.stdin.emit("end");
  }
});
// in package.json
{
  "scripts:
    // I use esm, but you get the idea
    "cli": "node -r esm cli.js"
}

Now, when you run npm run cli, you get a node interactive shell (with autocompletion, etc), in which you can access db.