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

@forty-boy/sql

v1.1.2

Published

A MySQL Library for Node.js

Downloads

34

Readme

@Forty-boy/SQL

A MySQL Library for Node.js

Currently creating this as a hobby project, but we'll see where it goes.

Installing the Project

  1. npm install @forty-boy/sql OR yarn add @forty-boy/sql
  2. const Forty = require('@forty-boy/sql')
    OR
    import Forty from '@forty-boy/sql'
    OR
    import { Table } from '@forty-boy/sql'
  3. Create .env file at root with values for corresponding keys in .env-example found here

Cloning the Project

  1. Clone the repository here
  2. Run npm install OR yarn install
  3. Create .env file at root with values for corresponding keys in .env-example
  4. Run npm run dev OR yarn run dev

Changelog

Version 1.1.1

Version 1.1.0

  • Completely refactored
    • Note Almost everything syntax wise has stayed the same.
    • Removed unnecessary dependencies
    • Now using a chaining convention, although you may not ever need to touch it.
    • Now uses Query as an abstraction.
      • Now use TableQuery or DatabaseQuery
    • Can now create, switch, or drop Databases
    • SqlService is now an abstraction
      • Now can use TableService or DatabaseService

Version 1.0.5

Version 1.0.4

Version 1.0.3

Version 1.0.2

Version 1.0.1

Examples

Creating a Table Schema

For the rest of these examples we'll be using this user table

class UserSchema {
  id?: number; // This is nullable for Create calls
  fullName: string;
  dateOfBirth: Date;

  constructor(id: number, fullName: string, dateOfBirth: Date) {
    this.id = id;
    this.fullName = fullName;
    this.dateOfBirth = dateOfBirth;
  }
}

type UserDateset = Array<UserSchema>;

export class UserTable extends Table<UserSchema> {
  constructor(tableName: string, users: UserDataset = []) {
    super(tableName, UserSchema, users);
  }
}

Create Table

async createProducts(): Promise<void> {
  return new Promise((resolve, reject) => {
    try {
      const tableService = new TableService('products');
      tableService.create({
        columns: [
          {
            name: 'id',
            type: 'INT',
            size: 11,
            primaryKey: true,
            autoIncrement: true,
            nullable: false,
          },
          { name: 'name', type: 'VARCHAR', size: 255, default: 'Test Product' },
          { name: 'price', type: 'INT', size: 11 },
          { name: 'createdAt', type: 'DATETIME' },
          {
            name: 'createdBy',
            type: 'INT',
            nullable: false,
            foreignKey: {
              referenceId: 'id',
              referenceTable: 'users',
            },
          },
        ]
      }).subscribe((res) => resolve());
    } catch (err) {
      return reject(err);
    }
  })
}

Add Values to Table

userTable.insert({
  fullName: 'Blaze Rowland',
  dateOfBirth: new Date(1997, 11, 14),
});

Find, Find one, or Find a specific amount of Values

userTable
  .find({
    columns: ['id', 'fullName'],
    condition: { id: 1 },
  })
  .subscribe((users) => console.log(users));
userTable
  .findOne({
    columns: ['id'],
    condition: {
      fullName: 'Blaze Rowland',
    },
  })
  .subscribe((user) => console.log(user));
userTable
  .findAmount({
    columns: ['id'],
    condition: {
      fullName: 'Blaze Rowland',
    },
  }, 3)
  .subscribe((user) => console.log(user));

Update Values

userTable
  .update({
    values: { fullName: 'Rylee Brown' },
    condition: { id: 1 },
  })
  .subscribe((res) => console.log(res));

Find and Update Values

userTable
  .findOne({
    columns: ['id'],
    condition: {
      id: 1,
    },
  })
  .subscribe({
    next: (user) =>
      userTable
        .update({
          values: { fullName: 'Forrest Rowland' },
          condition: { id: user.id },
        })
        .subscribe((res) => console.log(res)),
  });

Find and Add to Relational Table

userTable
  .findOne({
    columns: ['id'],
    condition: {
      fullName: 'Forrest Rowland',
    },
  })
  .subscribe({
    next: (user) => {
      productTable
        .insert({
          name: 'Pacifier',
          price: 5,
          createdAt: new Date(),
          createdBy: user.id,
        })
        .subscribe((res) => console.log(res));
    },
  });

Delete from Table

productTable.delete({ id: 1 });

Join Tables

  productTable
  .join({
    joinType: 'INNER JOIN',
    columnsToSelect: [
      { column: 'name' },
      { column: 'price' },
      { column: 'fullName', as: 'userName', table: userTable.tableName },
      { column: 'dateOfBirth', table: userTable.tableName },
    ],
    columnsOn: [
      {
        from: { column: 'id', table: userTable.tableName },
        to: { column: 'createdBy', table: productTable.tableName },
      },
    ],
  })
  .subscribe((res) => console.log(res));
  productTable
  .join({
    joinType: 'LEFT JOIN',
    columnsToSelect: [
      { column: 'name' },
      { column: 'price' },
      { column: 'fullName', as: 'userName', table: userTable.tableName },
      { column: 'dateOfBirth', table: userTable.tableName },
    ],
    columnsOn: [
      {
        from: { column: 'id', table: userTable.tableName },
        to: { column: 'createdBy', table: productTable.tableName },
      },
    ],
  })
  .subscribe((res) => console.log(res));
  productTable
  .join({
    joinType: 'RIGHT JOIN',
    columnsToSelect: [
      { column: 'name' },
      { column: 'price' },
      { column: 'fullName', as: 'userName', table: userTable.tableName },
      { column: 'dateOfBirth', table: userTable.tableName },
    ],
    columnsOn: [
      {
        from: { column: 'id', table: userTable.tableName },
        to: { column: 'createdBy', table: productTable.tableName },
      },
    ],
  })
  .subscribe((res) => console.log(res));

Union Tables

productTable
  .union({
    columns: ['id', 'name'],
    conditions: {
      id: '1',
    },
    all: true,
    union: {
      table: userTable.tableName,
      columns: ['id', 'fullName'],
      conditions: {
        id: '1',
      },
    },
  })
  .subscribe((res) => console.log(res));

Alter Tables

Create an instance of the SQL Service

const sqlService = new SqlService('users')

Add Columns:

userTable.alter({
  columnsToAdd: [
    {
      name: 'location',
      type: 'VARCHAR',
      size: 255,
    }
  ]
}).subscribe((res) => console.log(res))

Alter Columns:

userTable.alter({
  columnsToModify: [
    {
      name: 'firstName',
      newName: 'fullName',
      type: 'VARCHAR',
      size: 255,
    },
  ],
});

Remove Columns:

userTable.alter({
  columnsToRemove: [
    {
      name: 'lastName',
    },
  ],
});

columnsToAdd, columnsToAlter, and columnsToRemove can all be added to the alterAbleQuery like so:

userTable.alter({
  columnsToAdd: [
    {
      name: 'location',
      type: 'VARCHAR',
      size: 255,
    },
  ],
  columnsToModify: [
    {
      name: 'firstName',
      newName: 'fullName',
      type: 'VARCHAR',
      size: 255,
    },
  ],
  columnsToRemove: [
    {
      name: 'lastName',
    },
  ],
});

Drop Tables

userTable().drop();

Database

Creating an instance of the Database class

const fortyDatabase = new Database('forty');

Creating a database

fortyDatabase.create();

Switching databases

Option 1

fortyDatabase.databaseName = 'newDatabase';
fortyDatabase.switch();

Option 2

fortyDatabase.switch('newDatabase');

Dropping a database

Option 1

// This will throw an error if you haven't FIRST switched databases.

fortyDatabase.delete();

Option 2

fortyDatabase.delete('newDatabase');