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

nodatabase

v1.1.1

Published

A simple persistent in-memory database.

Downloads

16

Readme

nodatabase

A collection of persistent in-memory database-like utilities.

Useful for prototyping or hobby projects when you need persistent storage but don't want to rely on an external database. As the name suggests, this is not an actual database and you should always use one in anything more than a hobby project.

How It Works

All your data is kept in memory and continuously persisted as a single JSON file. This limits the types you can store to string, number, boolean and null, as well as objects and arrays containing these. Depending on your configuration, an additional file is used as a transaction journal to improve performance.

Install

Using npm:

npm install nodatabase

Using Yarn:

yarn add nodatabase

Usage

SingleValue

import { SingleValue } from 'nodatabase'

const message = new SingleValue({
  dirPath: './data',
  fileName: 'message',
  defaultValue: '',
})

await message.set('Hello, World!')

console.log(message.value)
// => Hello, World!

KeyValueDatabase

import { KeyValueDatabase } from 'nodatabase'

const users = new KeyValueDatabase({
  dirPath: './data',
  fileName: 'users',
})

await users.set('john', { name: 'John', age: 31 })

await users.update('john', { age: 32 })

console.log(users.get('john'))
// => { name: 'John', age: 32 }

DocumentDatabase

import { DocumentDatabase } from 'nodatabase'

const messages = new DocumentDatabase({
  dirPath: './data',
  fileName: 'messages',
})

await messages.insert({ user: 'alice', text: 'hello' })
await messages.insert({ user: 'bob', text: 'hi' })
await messages.insert({ user: 'bob', text: 'how are you?' })

await messages.update({ user: 'bob' }, { text: null })

console.log(users.findMany({ user: 'bob' }))
// => [{ user: 'bob', text: null }, { user: 'bob', text: null }]

API

SingleValue

Options

type SingleValueOptions<T> = {
  /**
   * The default value to use if no stored value is found.
   */
  defaultValue: T
  /**
   * Directory to store data in.
   */
  dirPath: string
  /**
   * Optional name for the data file. If left undefined, a default name will be
   * used. You must specify this when using more than one database in a
   * directory.
   */
  fileName?: string
}

.value

A getter that returns the stored value.

.set(value)

Replaces the stored value with a new value.

.update(update)

Updates the stored value by deeply merging it with the given update. If the value stored is not an object this has the same effect as calling set.

KeyValueDatabase

Options

type KeyValueDatabaseOptions = {
  /**
   * Directory to store data in.
   */
  dirPath: string
  /**
   * Optional name for the data file. If left undefined, a default name will be
   * used. You must specify this when using more than one database in a
   * directory.
   */
  fileName?: string
  /**
   * Whether to use journaling. You can also set a custom maximum length of the
   * journal file. Defaults to `true`.
   */
  journal?: boolean | { maxLines: number }
  /**
   * Maximum number of items that are allowed to be stored.
   */
  maxItems?: number
}

.size

A getter that returns the number of stored values.

.keys

A getter than returns an array of all existing keys.

.values

A getter that returns an array of all stored values.

.entries

A getter that returns an array of all stored key-value pairs.

.has(key)

Returns true if the given key exists in the database, false otherwise.

.get(key)

Returns the value stored at the given key. Returns undefined if the key does not hold a value.

.forEach(callbackfn)

Performs the specified action for each key-value pair in the database.

.set(key, value)

Stores the given value at the given key. If there already exists a value at this key it is overwritten.

.update(key, update)

Updates the value at the given key by deeply merging it with the given update. If the value stored is not an object this has the same effect as calling set.

.delete(key)

Deletes the value at the given key.

.clear()

Deletes all values in the database.

DocumentDatabase

Options

type DocumentDatabaseOptions<T extends Document> = {
  /**
   * Directory to store data in.
   */
  dirPath: string
  /**
   * Optional name for the data file. If left undefined, a default name will be
   * used. You must specify this when using more than one database in a
   * directory.
   */
  fileName?: string
  /**
   * Whether to use journaling. You can also set a custom maximum length of the
   * journal file. Defaults to `true`.
   */
  journal?: boolean | { maxLines: number }
  /**
   * Maximum number of documents that are allowed to be stored.
   */
  maxDocuments?: number
  /**
   * An array of fields that should be indexed for fast searching.
   */
  indexedFields?: Array<keyof T>
}

.size

A getter that returns the number of stored documents.

.findOne(query)

Finds and returns the first document that matches the query. Returns undefined if no documents match.

.findMany(query)

Returns an array of documents that match the query.

.insert(document)

Inserts a new document into the database.

.update(query, update)

Updates all database documents that match the query.

.delete(query)

Deletes all database documents that match the query.

.clear()

Deletes all documents in the database.