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

collie-db

v0.0.3

Published

A Simple JSON DB

Downloads

9

Readme

Collie DataBase

Collie-DB is a simple JSON database package that provides basic database functionalities. It allows you to create, read, update, and delete data in JSON format. This package is designed to be easy to use and understand, making it suitable for small projects and applications.

How to Use

Installation

You can install the package using npm:

npm install collie-db

Importing the Database

First, import the database class into your project:

import DataBase from "collie-db";
const db = new DataBase({
  tables: [{ name: "users", mod: "users.json" }],
});

Configuration

| option | description | type | default | | ------------- | ------------------------------------------------- | --------- | -------------------------------------- | | timeoutsTable | Table name for timeouts. | string | "timeouts" | | autoSave | Indicates whether it will be saved automatically. | boolean | true | | tables | List of tables in the database. | Table[] | [{ name: "main", mod: "main.json" }] | | mod | Database directory path. | string | "./database/" |

Initializing the Database

Before using the database, you need to initialize it. You can do this asynchronously:

void (async function init() {
  await db.init();
  console.log("DataBase initialized and ready to use!");
})();

Basic Operations

1. Setting Data:

You can set data in the database using the set method:

// DataBase.set<T>(table: string | undefined, key: string | string[], value: T, emit?: boolean): T
db.set("users", "JonhDoe", { name: "John Doe", age: 30 });

2. Getting Data:

To retrieve data, use the get method:

// DataBase.get<T extends unknown, K extends string | string[] = string, R = GetFieldType<T, K>>(table: string | undefined, key: K, dvalue?: R): R | undefined | null
const user = db.get("users", "JonhDoe");
console.log(user); // Output: { name: 'John Doe', age: 30 }

3. Updating Data:

Update existing data using the set method:

db.set("users", "JonhDoe", { name: "John Doe", age: 31 });

Update existing data using the edit method:

// DataBase.edit<T>(table: string | undefined, key: string | string[], predicate: (value: T, key: string | string[]) => T, force?: boolean, emit?: boolean): boolean
db.edit("users", "JonhDoe", function (value) {
  value.job = null; // ¿Chamban't?
  return value;
});

4. Deleting Data:

Remove data from the database with the delete method:

// DataBase.delete(table: string | undefined, key: string): true
db.delete("users", "JonhDoe");

Timeouts

The database also supports timeouts. You can set a timeout for a specific piece of data:

// DataBase.timeout<T = unknown>(value: T, time: number, id?: string, table?: string): T
db.timeout(
  "This will disappear after 5 seconds",
  5000,
  "unique-id" // If you don't put something, the dabase generates a random token.
);

Events

The database emits events when data is updated, deleted, or when a timeout expires. You can subscribe to these events:

db.on("update", function (key, value, table) {
  console.log(`Data updated: ${key} in table ${table}`);
});

db.on("delete", function (key, table) {
  console.log(`Data deleted: ${key} from table ${table}`);
});

db.on("expires", function (data) {
  console.log(`Timeout expired for data: ${data}`);
});

Auto-saving

The database supports auto-saving, which means it will automatically save changes to the JSON files:

const db = new Database({ autoSave: true });

By default, the database will use the main table. You can customize table names and file paths when initializing the database.

Contributing

Feel free to contribute to this project by reporting issues or creating pull requests. Your contributions are highly appreciated!