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

gg-mysql-connector

v1.0.37

Published

**`gg-mysql-connector`** is an intuitive MySQL model creator designed to simplify database management and operations by synchronizing TypeScript models with MySQL tables. This package allows you to declare and manage your MySQL database structure programm

Downloads

16

Readme

GG-Mysql-Connector

gg-mysql-connector is an intuitive MySQL model creator designed to simplify database management and operations by synchronizing TypeScript models with MySQL tables. This package allows you to declare and manage your MySQL database structure programmatically, with strong type enforcement.

Features

  • Declare Database Structure: Define your database tables and columns as TypeScript variables.
  • Sync Database Structure: Automatically synchronize your declared models with the actual MySQL database.
  • Automated View Management: Automatically create or update SQL views based on your declarations.
  • Generate TypeScript Models: Generate interfaces that mirror your MySQL structure, ensuring type-safe operations.
  • Type-Safe MySQL Connector: Provides type-enforced MySQL operations like CRUD (Select, Update, Delete).

Installation

npm install gg-mysql-connector

1. Declare Your Model

Start by declaring your database tables and columns as TypeScript variables:

import { MyModel } from "gg-mysql-connector"

const model: MyModel[] = [
  {
    tableName: "user",
    columns: [
      { COLUMN_NAME: "id", DATA_TYPE: "int", AUTO_INCREMENT: true },
      { COLUMN_NAME: "name", DATA_TYPE: "varchar(512)" },
    ],
  },
  {
    tableName: "item",
    columns: [
      { COLUMN_NAME: "id", DATA_TYPE: "int", AUTO_INCREMENT: true },
      { COLUMN_NAME: "name", DATA_TYPE: "varchar(512)" },
      { COLUMN_NAME: "price", DATA_TYPE: "float" },
      {
        COLUMN_NAME: "status",
        DATA_TYPE: "varchar(64)",
        POSSIBLE_VALUE: ["ACTIVE", "DISABLE"],
      },
      { COLUMN_NAME: "description", DATA_TYPE: "varchar(512)" },
    ],
  },
]

2. Sync Model and Views with MySQL & Generate Model Interface

Use the provided functions to sync your model with MySQL and generate TypeScript interfaces.

import { ModelGenerator } from "gg-mysql-connector"

const migrator = new ModelGenerator(
  {
    host: "your-host",
    user: "your-user",
    password: "your-password",
    database: "test_ggdb_connector",
  },
  model
)

await migrator.init()
await migrator.pushModelToDB()
await migrator.pushViewToDB("./views")
await migrator.generateModelInterface({
  appName: "app",
  model: model,
  outputDirectory: ["./"],
})

process.exit()

3. CRUD Operations with Type Enforcement

Once you've generated your interfaces, you can perform type-enforced MySQL operations.

import GGMySQLConnector from "gg-mysql-connector"
import app_INF from "./your-output-directory/app_INF" // Generated in step 2

const db = new GGMySQLConnector<app_INF>({
  host: "your-host",
  user: "your-user",
  password: "your-password",
  database: "test_ggdb_connector",
})

await db.init()

// Select operations
const result_1 = await db.select("item")
const result_2 = await db.selectByID("user", 2)
const result_3 = await db.selectByMatchParams("item", { id: 5 })
const result_4 = await db.selectByMatchParams("item", { name: "pen", price: 3 })

// Update operations
const result_5 = await db.update("user", { id: 3, name: "Tony" })
const result_6 = await db.update("item", { id: 3, price: 4, name: "Ruler" })
const result_7 = await db.updateOnlyID("item", { oldID: 7, newID: 4 })

// Delete operations
const result_8 = await db.deleteByID("item", 5)
const result_9 = await db.deleteByMatchParams("item", { name: "pen", price: 5 })

// raw query
const result_10 = await db.query("SELECT * FROM item where id = ?", [5])

License

MIT License