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

@ninjalib/sql

v0.3.0

Published

SQL ORM

Downloads

2

Readme

Ninja SQL library

A lightweight MySQL ORM

Note: Requires node v19.6.0 for testing

Features

  • Easy way to connect
  • Prepared statement support
  • Transaction support
  • Plenty of helper functions
  • Easily type return values with generics

Usage

First, create a new instance and connect

import sql from '@ninjalib/sql';
const db = new sql.MySQL({ user: 'root', host: 'localhost', database: 'application', password: 'the_password', port: 3306 });
await db.connect();

Then, you can get straight into it and use the methods given by the library:

const user = await db.query('SELECT * FROM users WHERE id = ?', [5]);

There is also transaction support:

await db.transaction(async () => {
    await db.query(`
        INSERT INTO users (name, email)
        VALUES ('Bob', '[email protected]')
    `);
});

Docs

Helpers

getRows

Returns rows found from the given table and conditions

Parameters:

  • table: name of the table to query from
  • where?: object of conditions for the select (defaults to no conditions)
const users = await db.getRows<User>('auth.users', { archived: null });

getRow

Returns first row found from the given table and conditions

Parameters:

  • table: name of the table to query from
  • where?: object of conditions for the select (defaults to no conditions)
  • defaultValue?: the fallback value to use if no row was found (defaults to null)
const user = await db.getRow<User>('auth.users', { first_name: 'mike', last_name: 'tyson', archived: null });

// Can easily type fallback value
const notFoundUser = await db.getRow<User, 'not_found'>('auth.users', { first_name: 'not', last_name: 'defined' }, 'not_found');

insertOne

Inserts one row into the given table and returns the created rows' id

Parameters:

  • table: name of the table to insert into
  • data: object of data that maps to the field and value that will be inserted
const id = await db.insertOne<number>('auth.users', { first_name: 'mike', last_name: 'tyson' });

insertMany

Inserts multiple rows into the given table and returns the first created rows' id

Parameters:

  • table: name of the table to insert into
  • data: array of data that maps to the field and value that will be inserted per object
const data = [
    { first_name: 'new', last_name: 'guy', email: '[email protected]' },
    { first_name: 'new2', last_name: 'guy', email: '[email protected]' },
    { first_name: 'new3', last_name: 'guy', email: '[email protected]' },
    { first_name: 'new4', last_name: 'guy', email: '[email protected]' },
    { first_name: 'new5', last_name: 'guy', email: '[email protected]' },
];

const firstId = await db.insertMany<number>('auth.users', data);

Testing

After installing dependencies with npm i, you should be able to run npm test in the package root.

NOTE: when adding new tests: node's test runner (which npm test uses under the hood), runs files in parallel. Be careful with this, as querying the same table that two files use can fail since one file might have cleaned up already (dropped the table) whilst the other is trying to use it (as an example, but you get the picture).

We could pass --test-concurrency=1 into the test runner, but it will run slower. The solution is to give any tables being tested within a file some unique prefix that no other file would use.