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-object

v1.0.7

Published

a very lightweight mysql orm library

Downloads

10

Readme

mysql-object

a very lightweight mysql orm library

不同于sequelize等orm,mysql-object无需手动维护一份对象类型。my-orm着重于把update中的普通object,转成sql语句

400行左右的代码,支持特性包括:

  • 面向object的update
  • 可充分自定义的insert ignore/update
  • 自动维护的create_time update_time

usage

npm install mysql-object --save

js

let {Mysql} = require('../mmysql-object')

  let mysql = Mysql.create(JSON.parse(process.env.MYSQL))
  let r = await mysql.find('test_book')

typescript

import {Mysql, Pager} from 'mysql-object'

  let mysql = Mysql.create(JSON.parse(process.env.MYSQL))
  let r = await mysql.find('test_book')

run test

  • run data.sql to setup your data
  • MYSQL='{"host":"xxx", "user":"xxx", "password":"xxx","database":"xxx"}' npm test

detail usage

import { Mysql, Pager, setCreateUpdateField } from './index'

(async function main() {
  setCreateUpdateField('create_time', 'update_time')
  let mysql: Mysql = Mysql.create(JSON.parse(process.env.MYSQL))
  let r: any = null
  // 返回数组
  r = await mysql.query(`select * from test_book`)
  // 返回1行
  r = await mysql.queryOne(`select * from test_book`)
  // 返回1个值
  r = await mysql.queryValue(`select count(*) from test_book`)
  // 返回1列,结果为id数组
  r = await mysql.queryColumn(`select id from test_book`)

  r = await mysql.find('test_book') // 获取test_book表的所有内容
  r = await mysql.find('test_book', {
    attributes: ['id', 'name'], // 生成 select id, name
    where: {
      origin_price: 40.00, // 生成 origin_price=40.00
      description: [92.00, 36.80], // 生成 description in (92.00, 36.80)
      $like: {
        name: 'Gossie', // 生成 goods_title like '%Gossie%'
      }
    },
    pager: new Pager({
      order: '-id',  // 生成 order by id desc
      offset: 20,
      limit: 10, // 生成 limit 20, 10
    }),
  })

  // 删除id=26的记录
  r = await mysql.deleteById('test_book', 26)
  // 插入1条数据
  r = await mysql.insert(`test_book`, { id: 26, name: '123..' })
  // 重复插入,但是catch异常
  r = await mysql.insert(`test_book`, { id: 26, name: '123..' }).catch(f => f)
  // 生成 insert into ... on duplicate ignore
  r = await mysql.insert(`test_book`, { id: 26, name: '123..' }, { ignore: true })

  r = await mysql.insert(`test_book`,
    [{ id: 26, name: '123..' }, { id: 1, name: '企鹅兰登经典分级读物' }], // 支持批量插入
    { updates: ['name'] }, // 生成 insert into ... on duplicate update ... 如果updates:[],则更新对象中的所有属性
  )

  // 更新匹配的属性,要求传入的对象给出id字段
  // 如果没有给出id字段,则需要给出除id外的其他所有的非NULL字段
  r = await mysql.update(`test_book`, { id: 26, name: '123..' })

  r = await mysql.updateReturn(`test_book`, { id: 26, name: '123..' }) // 更新并返回该对象

  r = await mysql.deleteWhere('test_book', { id: 26 })

  // 表join的高级用法
  r = await mysql.find('test_book_author', {
    attributes: ['id', 'name'],
    // 包含test_book,test_book为一个对象,通过book_author表里的book_id能够找到对应的book
    include: ['test_book'],
  })
  r = await mysql.find(`test_book`, {
    where: { id: 1, 'test_book_author.name': '兰登' },
    // 包含test_book_image,为一个数组,通过test_book_image里面的book_id,能够找到一个book的所有image
    include: ['test_book_image', {
      // include中的表,只选取部分属性
      table: 'test_book_author',
      attributes: ['name', 'description'],
    }],
  })
  process.exit(0)
})()
  • more detail please see test.ts