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 🙏

© 2026 – Pkg Stats / Ryan Hefner

@mlagie/sql-connector

v1.4.9

Published

Le module sql-connector permet de gérer les connexions à une base de données MySQL, de définir des schémas de tables, et d'interagir avec les données de manière simple et efficace.

Readme

sql-connector documentation

Français | English

sql-connector helps manage MySQL connections, define table schemas, sync tables automatically, and work with database models through a small API.

Import

const { Schema, connect, logout, Model, ModelInstance, client, sqlTypeMap } = require('sql-connector');

Database connection

connect(config) opens a MySQL connection using a configuration object compatible with mysql2.

const config = {
  host: 'localhost',
  port: 3306,
  user: 'root',
  password: 'password',
  database: 'mydatabase'
};

await connect(config);

logout() closes the active connection.

await logout();

Schema

Schema describes the structure of a table. Each field can use the following properties.

| Property | Type | Description | |---|---|---| | type | SqlType or { name: SqlType } | SQL type for the field | | length | number | Maximum length | | required | boolean | Not null constraint | | default | any | Default value | | unique | boolean | Unique constraint | | auto_increment | boolean | Auto increment | | foreignKey | string | Foreign key reference | | enum | string[] | Allowed values | | primary_key | boolean | Primary key flag | | customize | string | Extra SQL options |

const userSchema = new Schema({
  id: {
    type: Number,
    auto_increment: true,
    primary_key: true
  },
  email: {
    type: String,
    length: 255,
    unique: true,
    required: true
  },
  status: {
    type: String,
    enum: ['active', 'inactive', 'pending'],
    default: 'pending'
  }
});

Table synchronization

Model.syncAllTables() compares JS schemas with the database and applies only meaningful differences.

  • New columns are added automatically.
  • Removed columns are only dropped with dangerousSync: true.
  • Column renames are supported through oldName.
  • Orphan tables are backed up to a backup_*.sql file before deletion.
await Model.syncAllTables();
await Model.syncAllTables({ dangerousSync: true });

Important: do not set both primary_key: true and unique: true on the same field. A primary key is already unique and not null.

Models

Model represents a SQL table.

Main methods:

  • save(data) inserts a row
  • findOne(filter, fields) fetches a single row
  • find(filter, fields) fetches multiple rows
  • findAll(options) supports advanced queries
  • count(filter) counts rows
  • customRequest(custom) runs a custom SQL query
  • delete(filter) deletes a row
  • dropTable() drops the table
  • generate_uuid() generates a unique UUID
  • Model.createAllTables() creates tables in dependency order
const userModel = new Model('users', userSchema);

await Model.createAllTables();
await userModel.save({ email: '[email protected]', status: 'active' });

const user = await userModel.findOne({ email: '[email protected]' });
await userModel.delete({ email: '[email protected]' });

Model instances

ModelInstance represents a row already loaded from the database.

  • updateOne(model) updates the row
  • delete(model) deletes the row using a filter
  • deleteOne() deletes the instance row
  • customRequest(custom) runs a custom query
const userInstance = new ModelInstance('users', { email: '[email protected]' });

await userInstance.updateOne({ status: 'inactive' });
await userInstance.deleteOne();

SQL types

sqlTypeMap exposes the common SQL types.

console.log(sqlTypeMap.String); // "VARCHAR"

Client

client is a shared object meant to host reusable application functions.

module.exports = client => {
  client.checkServer = () => {
    if (server.isLaunch()) {
      return 1;
    }

    return 0;
  };
};

Summary

sql-connector provides a small layer to connect to MySQL, describe schemas, synchronize tables, and manipulate data with typed models.