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

memory-database

v1.0.2

Published

As the name suggests this is an in memory database which is equipped with a indexes (B-Trees), a cache (Least Recently Used) and a spinnunng lock to handle cncurrency.

Downloads

18

Readme

In Memory Database

As the name suggests this is an in memory database which is equipped with indexes (B-Trees), a cache (Least Recently Used) and a spinning lock to handle concurrency.

Table of Contents

Installation

npm install memory-database

Usage

To use the database first import the class, then create a db instance. With the instance, you can preform CRUD actions.

const InMemoryDB = require("memory-database");

const db = new InMemoryDB();

const x = await db.create({ name: "John", age: 19 });
const y = await db.findById(x.id);

console.log(y);

API Reference

Creating a record

'db.create()' accepts an object value of any kind which it generates a unique id with 'uuid.v4' and returns the object with its id.

await db.create({ name: "John", age: 19 });

Finding a record by ID

'db.findById() accepts the id of a value and first checks if it is available in the cache, then any indexes and finally in the data store hash map. If no value is found, it returns an error message.

const x = await db.create({ name: "Joe", age: 19 });
await db.findById(x.id);

Deleting a record

'db.deleteById()' removes the value from the data store, indexes and if it was present in the cache.

await db.deleteById(x.id);

Updating a record

'db.updateById()' functions similar to deleteById by updating in the data store, indexes and cache.

await db.updateById(x.id, { name: "Joseph", age: 23 });

Finding all records

'db.findAll()' accepts two object parameters 'where' and 'indexes'. However, with no parameters it simply returns all the data in the store.

await db.findAll();

Finding records with a condition

For more specific queries, using the where object offers two forms. Using a key-value pair to locate specific values and a function for a more dynamic approach.

await db.findAll({ where: { key: "age", value: 35 } });

// Or

await db.findAll({where: (data) => data.age === 35});

Finding records within a range

'db.findInRange()' accepts a range object consisting of the key that the range should be checked, along with the lower and upper bounds of the range.

await db.findInRange({ range: { key: "age", low: 20, high: 30 } });

Using Indexes

'db.createIndex()' creates an index for the key passed and populates it with the data in the store. We can also pass specific instructions about indexes to the 'db.findAll' such as which key it is checking and if we should create a new index for it if one does not already exist.

await db.createIndex('age');

await db.findAll({
    where: (data) => data.age === 21,
    index: { key: "age", create: true },
});

Areas for Improvement

  • Error handling: Provide more specific errors for different failures in any of the CRUD operations.
  • Types: Classes or Types for parameters passed as Objects for easier understanding and usage.