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

database-sempai

v2.7.0

Published

Local database

Downloads

56

Readme

Database-Sempai

Database-Sempai is a TypeScript class that provides a simple key-value storage mechanism using JSON files. It allows you to create and manage multiple tables and perform various operations on them.

Installation

The CreateStorage class can be used in a TypeScript project by importing it as follows:

import { CreateStorage } from "database-sempai";

Constructor

The CreateStorage class accepts an optional configuration object in its constructor. The available configuration options are:

  • path: The directory path where the database files will be stored. Default is "database".
  • table: An array of table names to be created. Default is ["main"].
  • extname: The file extension for the database files. Default is ".sql".
  • key: A secret key used for data encryption (optional).

Methods

The CreateStorage class provides the following methods to interact with the storage:

Add, Update, and Delete Operations:

  • set: Add or update a key-value pair in the specified table.
  • delete: Remove a key-value pair from the specified table.
  • clear: Clear all data from the specified table.

Retrieval Operations:

  • get: Get the value associated with a specific key in the specified table.
  • all: Get all key-value pairs from the specified table.
  • findByValue: Find key-value pairs in the specified table based on a specific value.

Query Operations:

  • filter: Filter key-value pairs in the specified table based on a custom filtering function.
  • at: Access a value in the specified table by its index or key.
  • randomAt: Get a random value from the specified table.
  • endKey: Get the last key from the specified table.
  • endValue: Get the last value from the specified table.
  • firtsKey: Get the first key from the specified table.
  • firtsValue: Get the first value from the specified table.
  • includes: Check if a specific value exists in the specified table.
  • keys: Get all keys from the specified table.
  • values: Get all values from the specified table.

Miscellaneous:

  • length: Get the number of key-value pairs in the specified table.
  • getPath: Get the current database directory path.
  • hasTable: Check if a table with the given name exists.
  • getTables: Get an array of all table names.
  • isTable: Check if a specific table exists.
  • connect: Emit a "ready" event when the database is ready to be used.

Error Handling

The CreateStorage class throws custom error types for various scenarios, such as invalid table names, missing extensions, encryption key not specified, etc.

Asynchronous File Operations

The class uses asynchronous file operations (fs.promises.readFile and fs.promises.writeFile) for reading and writing data to the database files. This helps to prevent blocking the main thread and enhances performance.

Usage Example

Here is an example of how to use the CreateStorage class:

// Import the class
import { CreateStorage } from "database-sempai";

// Create a new instance of CreateStorage
const db = new CreateStorage<number, string>({
  path: "myDatabase",
  table: ["users"],
  extname: ".json",
});

// Set data in the "users" table
await db.set("users", 1, "John Doe");
await db.set("users", 2, "Jane Smith");

// Get a value from the "users" table
const user = await db.get("users", 1);
console.log(user); // Output: "John Doe"

// Get all key-value pairs from the "users" table
const allUsers = await db.all("users");
console.log(allUsers); // Output: { "1": "John Doe", "2": "Jane Smith" }

// Filter key-value pairs in the "users" table
const filteredUsers = await db.filter("users", ({ value }) =>
  value.startsWith("John")
);
console.log(filteredUsers); // Output: [[1, "John Doe"]]

// Delete a value from the "users" table
await db.delete("users", 2);

// Clear all data from the "users" table
await db.clear("users");

Please note that the example above assumes the use of numeric keys (number) and string values (string). You can customize the types of keys and values by providing different generic types when creating an instance of CreateStorage.

Contributing

Contributions to this project are welcome! If you encounter any issues or have ideas for improvements, feel free to open an issue or submit a pull request. Happy coding!