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

fastsqli

v1.0.2

Published

A library to dynamically manage database schemas and fetch data.

Downloads

75

Readme


fastsqli CLI Tool Documentation


Overview

The fastsqli library simplifies the management of database schemas and data extraction for MySQL databases. It helps users dynamically create tables, fetch data, and update schema configurations, all within a convenient CLI tool. fastsqli facilitates easier handling of SQL databases alongside MongoDB-like model definitions, providing an efficient approach to schema migrations, data extraction, and JSON exports.


Project Structure

project/
|-- fastsqli/
    |-- createmodel/     # Directory for MongoDB model files that create new SQL tables
    |-- data/            # Holds data tables in JSON format
    |-- schema/          # Contains schema configuration files and models

Directory Breakdown

fastsqli/createmodel/

This folder contains MongoDB model files, which define the structure of new models. These models are used to automatically generate corresponding tables in the SQL database.

Example MongoDB model:

// test.js
const mongoose = require('mongoose');
const testSchema = new mongoose.Schema(
    {
        id: { type: Number, required: true },
        name: { type: String },
        role: { type: String },
    },
    { 
        timestamps: true // MongoDB automatically handles createdAt and updatedAt
    }
);

module.exports = mongoose.model('Test', testSchema);

fastsqli/data/

This directory holds data tables in JSON format. These JSON files represent data extracted from SQL tables and can be used for exporting or manipulating data in a portable format.

Example data file:

// test.json
[
    {
        "id": 1,
        "name": "Mawuli Stephen",
        "role": "Admin"
    }
]

fastsqli/schema/

Contains schema configuration files that define the structure and relationships of database tables.

Example schema file (schema.json):

{
    "test": {
        "columns": [
            {
                "column_name": "id",
                "column_type": "int",
                "is_nullable": "NO"
            },
            {
                "column_name": "name",
                "column_type": "varchar",
                "is_nullable": "YES"
            },
            {
                "column_name": "category",
                "column_type": "varchar",
                "is_nullable": "YES"
            },
            {
                "column_name": "createdAt",
                "column_type": "datetime",
                "is_nullable": "YES"
            },
            {
                "column_name": "updatedAt",
                "column_type": "datetime",
                "is_nullable": "YES"
            }
        ],
        "relationships": []
    }
}

This file represents a table with its columns, types, nullable attributes, and any relationships (foreign keys).


fastsqli/schema/models/

Contains MongoDB-like models for each table, helping structure the schema and assisting in the table creation process.

Example model for test table:

// test.js
const mongoose = require('mongoose');

const testSchema = new mongoose.Schema({
    id: { type: 'int', required: true },
    name: { type: 'varchar' },
    category: { type: 'varchar' },
    createdAt: { type: 'datetime' },
    updatedAt: { type: 'datetime' }
}, { timestamps: true });

module.exports = mongoose.model('test', testSchema);

Table Structure Example

For the test table, the structure is as follows:

| Column Name | Type | Attributes | |-----------------|--------------|---------------------------------------| | id | int | PRIMARY KEY, AUTO_INCREMENT | | name | varchar | | | category | varchar | | | createdAt | datetime | | | updatedAt | datetime | |


Features of fastsqli

  1. Dynamic Table Creation (via Mongoose Models)
    The createmodel directory contains MongoDB-like model definitions that, when executed, will automatically create the corresponding SQL tables in your database.

  2. Schema Representation with schema.json
    The schema.json file in the schema folder represents your database schema, which includes tables, columns, data types, and relationships. This provides a blueprint for creating or updating your schema in the SQL database.

  3. Data Extraction in JSON Format
    You can extract data from your SQL tables and save it in JSON format using the scripts in the data directory. This functionality is useful for exporting data or generating datasets for further processing.


Usage

Installation

To install fastsqli, run the following npm command:

npm install -g fastsqli

Alternatively, you can install it locally within your project:

npm install --save-dev fastsqli

Configuration

Before using the CLI tool, set up your environment with the necessary database credentials. You can define these credentials using environment variables or specify them directly in the CLI commands.

Create a .env file in the root directory of your project to store the database credentials:

HOST=localhost
USER=root
PASSWORD=my-secret-password
DATABASE=my_database

These values can also be passed as flags in the CLI commands.

CLI Commands

The CLI tool offers three main commands:

  1. Migrate Command
    This command generates or updates the database schema based on the provided configuration files.

    Usage:

    fastsqli migrate --baseDir <directory> --host <db_host> --user <db_user> --password <db_password> --database <db_name>

    Options:

    • --baseDir or -b: Directory for storing schema and data files (default is fastsqli).
    • --host or -h: Database host (default is localhost).
    • --user or -u: Database username.
    • --password or -p: Database password.
    • --database or -d: Database name.

    Example:

    fastsqli migrate --baseDir ./fastsqli --host localhost --user root --password mysecretpassword --database mydatabase

    This command will start the schema migration process and generate the necessary schema files.

  2. Fetch Data Command
    This command fetches data from the SQL tables and saves it as JSON files in the specified directory.

    Usage:

    fastsqli fetch --baseDir <directory> --host <db_host> --user <db_user> --password <db_password> --database <db_name>

    Options:

    • --baseDir or -b: Directory for storing schema and data files (default is fastsqli).
    • --host or -h: Database host (default is localhost).
    • --user or -u: Database username.
    • --password or -p: Database password.
    • --database or -d: Database name.

    Example:

    fastsqli fetch --baseDir ./fastsqli --host localhost --user root --password mysecretpassword --database mydatabase

    This will fetch the data from all tables in the specified database and save it as JSON files in the given directory.

  3. Push Data Command
    This command pushes data from JSON files to the database tables.

    Usage:

    fastsqli push --table <table_name> --baseDir <directory> --host <db_host> --user <db_user> --password <db_password> --database <db_name>

    Options:

    • --table or -t: The table to push data to (use "all" for all tables).
    • --baseDir or -b: Directory for storing schema and data files (default is fastsqli).
    • --host or -h: Database host (default is localhost).
    • --user or -u: Database username.
    • --password or -p: Database password.
    • --database or -d: Database name.

    Example:

    fastsqli push --table test --baseDir ./fastsqli --host localhost --user root --password mysecretpassword --database mydatabase

    This will push the data from the specified JSON file to the given table in the database.


Version and Help

To check the version or get help on the available commands, use the following commands:

fastsqli --version
fastsqli --help