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

badcube

v1.1.0

Published

literally the worst database

Downloads

11

Readme

badcube.js

The tiny filesystem node.js database no one asked for.

badcube.js is an ultra-minimal filesystem database that anyone can drop into a project that needs to store data but not be deployed to the web. It has a built-in ODM that allows for checking the constructor of object keys as they are being inserted into the database. The key benefits to using badcube are the ease of implementation, and the resultant data file is a JSON array which can subsequently be used in imports to other databases or re-used anywhere JSON files are useful. Additionally, the implementation is written in such a way that it is easy to refactor a codebase to use mongoose if you have been working in badcube.

It should be noted that the data in the filesystem will also exists in memory until the running application purges the memory copy or stops the application.

Installation

$ npm install badcube

Collections and Schemas Folders

When badcube is brought into your project using require, it will auto-scan your directory looking for collections and schemas. If it fails to find these folders it will create them for you. The collections folder will contain the JSON files with your data, and the schemas folder will contain your schema files. The format for each will be discussed in the Models and Schemas section below. Upon starting a project with badcube, logs will print containing information about which valid collections and schemas were imported into memory for use.

Models and Schemas

Models are the structures that serve as references to the .json files in the collections folder. They enable simple CRUD operations on the filesystem and when used in conjunction with schemas, can type check data being inserted in the database.

To create a model, simply ensure that the collections folder contains a .json file for the data you wish to save. At minimum, ensure that it contains an empty array []. badcube will automatically create a model for that file, with a capitalized name. For example,

//In /collections, there is a file called apples.json

let bc = require('badcube');
//Console will give message saying that it has foundapples.json

console.log(bc.Apples.findAll({}));
//Logs the entire JSON array for apples.json.

Operations that can be performed with the models are discussed in the API section. If you would like to create a model after the initilization, the constructor function is exposed on the require(), so you may use it. Documentation is available below

Schemas are simple objects containing constructor references that are used to check data being inserted into the database. Similarly to models, creation of a schema is automated as long as the file for it is in the schemas folder. Schemas in badcube are plainObjects (they do not support deep searching as of this version) in which the value of each key is a reference to a constructor function. For example,

// /schemas/Oranges.js
module.exports = {
    variety: String,
    size: String,
    number: Number
    }

For more advanced uses, one can use user-defined constructors in this object provided that they are available in the schema file.

API

In this section we provide documentation for the use of Models and Schemas to handle data.

Schemas

The Schema prototype is an extension to the Model prototype, and contains only one extra function, .schemaCheck(). When a schema is instantiated, you can still run all of the same methods from Model.

  • Schema.schemaCheck(obj)
//using Oranges schema from above
Oranges.schemaCheck({
    variety: "Navel",
    size: "Medium",
    number: 13
});
//returns true

Oranges.schemaCheck({
    variety: 49,
    size: "salty",
    many: "many"
});
// THROWS an error

Models

The Model prototype contains several different functions for basic CRUD operations.

  • Model.find(queryObj)

Model.find() currently allows searching for a single object by a single property.

Oranges.find({
    variety: "Navel"
});

/*
returns
{
    variety: "Navel",
    size: "Medium",
    number: 13
}
*/
  • Model.findAll(queryObj)

Similar to .find(), .findAll() will grab each instance of an object with a matching property and return them in an array.

Oranges.findAll({
    size: "Medium"
});

/*
returns
[{
    variety: "Navel",
    size: "Medium",
    number: 13
},{
    variety: "Mandarin",
    size: "Medium",
    number: 55
}]
*/

Additionally, you can grab all the objects in a collection by passing an empty object e.g. Oranges.findAll({}).

  • Model.insert(obj)

the .insert() method takes an object and inserts it into the appropriate JSON array, then rewrites the file. If the Model is a Schema, it first does a .schemaCheck() to ensure that the inserted object's values are from the right kind of constructor.

Oranges.insert({
    variety: "Mandarin",
    size: "Medium",
    number: 55
});

The .insert() method also returns the inserted object. The objet inserted into the database will contain three additional properties:

  • _id: a randomly generated identifier

  • _createdDate: UTC timestamp of time of record creation

  • _updatedDate: UTC timestamp of time of record modification

  • Model.insertMany(array of obj)

.insertMany() is analogous to .insert() but instead takes an array of objects.

  • Model.update(queryobj,toUpdateObj)

.update() takes two arguments, the object to find in the database (has same rules as find()) and then updates the object with the properties in the toUpdateObj. This uses the Object.assign() method, and thus will update properties for those that already exist on the toUpdateObj and add those that are not there yet.

  • Model.delete(queryobj)

.delete() finds a record (same rules as .find()) and removes it from the json file.

Creating Models After initialization

The Model and Schema constructor functions are exposed on the .require(),

let bc = require('bc');
bc.Model(modelName, collectionRef, collectionObj);


bc.Schema(schemaObj, schemaName, collectionRef, collectionObj)

Why build this?

More about that here

PR is welcome!