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

darksdb

v2.1.3

Published

Lightweight API to use in conjunction with MySQL

Downloads

14

Readme

Latest Version: 2.1.3

Dev Package

Main Package

What's New?

  • 2.1.3:
    • Update TS type declarations

DarksDB

DarksDB is a simple database using node.js and mysql (v1)/promise-mysql(v2+) that aims to make writing SQL statements easier by providing simple methods like get() and update()

Installation

 $ npm i darksdb

Using DarksDB

// Get the DarkDB class
const { DarkDB } = require("./darkdb.js");
// Create a new database
const db = new DarkDB({
  host: "localhost",
  port: 3306,
  user: "example",
  password: "example", // Preferably have this in something like a .env file (for example process.env.db_pass)
  database: "example",
  table: "example",
});

await db.connect(); // You must call this otherwise an error will be thrown!

Important: You should use promises while using DarksDB (Async/Await)

Methods

  • db.setTable(): Set a new table
    • Takes in 1 argument, table
  • db.set(): Create a new record in the table with predefined values.
    • Takes in 2 arguments, keys[], and values[]
  • db.get(): Fetch the keys from all the records
    • Takes in 1 arguments, keys[]
  • db.getWhere(): Fetch the keys from all records that meet the where clauses
    • Takes in 2 arguments, keys[], and where[]
  • db.getAll(): Fetch all of the records and fields
    • Takes in 0 arguments
  • db.getAllWhere(): Fetch all of the records and fields that meet the where clauses
    • Takes in 1 argument, where[]
  • db.update(): Update all the records in that table with the new value
    • Takes in 2 arguments, keys[] and values[]
  • db.updateWhere(): Update all the records that meet the where clauses
    • Takes in 3 arguments, keys[], values[], and where[]
  • db.delete(): Delete all the records that meet the where clauses
    • Takes in 1 argument, where[]
  • db.deleteAll(): Delete ALL the records in a table;
    • Takes in 0 arguments
  • db.runStatement(): Run any SQL statement
    • Takes in 1 argument, statement
  • db.has(): Checks how many records there are with the key and value
    • Takes in 2 arguments, key and value

How to use where

To use where, each "where" of yours needs its own object following this data structure:

{ name: 'WHERENAME', value: 'WHEREVALUE' }

See Examples for more information.

Examples

const { DarkDB } = require('darksdb')
const db = new DarkDB({
    host    : 'localhost'
    port    : 3306
    user    : 'example'
    password: 'example'
    database: 'example'
    table   : 'example'
});

await db.connect();

await db.set([`one`, `two`, `three`], [1, 2, 3]);
// Add more data to the db
await db.set([`one`, `two`, `three`], [10, 20, 30]);
await db.set([`one`, `two`, `three`], [100, 200, 300]);
// Creates a simple database and makes this database
// one     two    three
//   1       2       3
//  10      20      30
// 100     200     300

Say you needed to get values: You can use any one of get(), getWhere(), getAll(), or getAllWhere()

await db.get([`one`]) // => [1, 10, 100];
await db.getWhere([`two`], [{ name: `one`, value: 10 }]) // => [20]
await db.getAll() // => entire database
await db.getAllWhere([{name: `three`, value: 300 }]) => // => [100, 200, 300]

Now what if you changed a value, and it needs to go back into the database. We can use update() or updateWhere()

await db.update([`one`], [10]); // =>
// one     two    three
//  10       2       3
//  10      20      30
//  10     200     300

await db.updateWhere([`two`], [2000], [{ name: `three`, value: 3 }]); // =>
// one     two    three
//   1    2000       3
//  10      20      30
// 100     200     300

Next you decide you need to delete some data. We can use delete() or deleteAll()

await db.delete([{ name: `two`, value: 20 }]); // =>
// one     two    three
//  10       2       3
//  10     200     300

await db.deleteAll(); // =>
// one     two    three
//      (No Data)

You can run an arbitrary SQL statement, such as SELECT COUNT(*) FROM numbers WHERE 'one' = 10 by using runStatement()

await db.runStatement(`SELECT COUNT(*) FROM numbers WHERE 'one' = 10`); // => 1

There is also db.has(), which checks if a record with the specific key and value exists

await db.has(`three`, 30); // => true

Keep in mind, optimally you should use your primary key in has(), however it is not required

Changelog:

  • 2.1.3:

    • Update TS type declarations
  • 2.1.2:

    • Add a check to see if a connection exists
  • 2.1.1:

    • Condensing where for loop into a sepeerate function
    • Update all functions using where for loops
    • Fixed delete()
    • Update documentation to add changelog/what's new?
  • 2.1.0:

    • Condensing keys for loop into a seperate function
    • Update all functions using keys for loops
    • Update documentation to add links to the main and dev packages
  • 2.0.5:

    • Switch to Promise-MySQL
    • Rewrite a lot of the code to run better
    • Fix has()
    • Add documentation
  • 1.0.0 - 1.0.3:

    • Initial release
    • Small upgrades