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

mysql-noschema

v1.0.2

Published

> Use mysql2/promise

Downloads

14

Readme

mysql-noschema

Use mysql2/promise

  • 给 mysql 添加 NoSQL 的风格,根据 insert 语句 自动创建表和字段
  • 内置一系列钩子,可以根据条件自定义类型和忽略某些情况的 NoSQL
  • 自动类型推导

Install

$ yarn add mysql-noschema

Use

import MysqlNoSchema from 'mysql-noschema'


async function start(){
  // db 为 mysql2 连机器默认对象
  const db = await MysqlNoSchema({
    host: "localhost",
    user: "root",
    password: "...",
    database: "...",
  });

  // 默认 query 若未创建表,或者个别字段未有,下面这句会抛出错误
  await db.query('INSERT INTO user (name, age) VALUES ("dog", 20)');

  // 使用 insert 方法执行 `INSERT INTO` 语句,会自动创建表或字段
  await db.inset('INSERT INTO user (name, age) VALUES ("dog", 20)');
}

start();

自动类型推导

根据 VALUES ("dog", 20, 1.3, "2020-12-20") 等内容,默认情况做如下推导:

  • string 类型: 根据插入的长度 * 4 判定在 255 - 65500 的某个2的平方区间
  • boolean 类型: TINYINT
  • number 类型,若有小数点: FLOAT
  • number 类型,若无小数点: INT
  • 时间字符串, 若有小数点: TIMESTAMP
  • 时间字符串, 若无小数点: DATETIME

钩子

  const db = await MysqlNoSchema({
    host: "localhost",
    user: "root",
    password: "...",
    database: "...",
    noSchema: {
      /** 判断哪些情况忽略 NoSchema, 线上稳定之后,可以改为默认返回 true */
      ignoreNoSchema?: (checker) => any;
      /** 仅在开发时使用, 当 Table 小于一定的长度,若插入的内容和表结构冲突,自动重新创建表,清空表数据 */
      resetTableLimit?: { [tableName: string]: number };
      /** 主键名称,默认为 id */
      primaryKey?: string;
      /** 忽略默认字段 id 的 tableNames */
      ignoreId?: string[];
      /** 忽略默认字段 create_at 的 tableNames */
      ignoreCreateAt?: string[];
      /** 忽略默认字段 update_at 的 tableNames */
      ignoreUpdateAt?: string[];
      /** 带小数点的 number 自动识别为 Double 的 tableNames,默认为 Float  */
      focusDoubleType?: string[];
      /** string 类型默认创建 varchar 最小值, 默认为 255 */
      varcharMinLength?: number;
      /** 根据首次插入的长度 * varcharRate 来判定 varchar 区间, 默认为 4 倍, 最后会和 varcharMinLength 之间取最大值 */
      varcharRate?: number;
      // 每个表自动添加时,都会执行此方法,可用于覆盖 `CREATE TABLE` 的 SQL
      replaceCreateTable: ({ table, sql, columns }) => string,
      // 每个字段自动添加时,都会执行此方法,可用于覆盖 `ALTER TABLE` 的 SQL
      replaceAlterColumn: ({ table, column, sql }) => string,
    }
  });