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

es-batis

v0.1.4

Published

mybatis dynamic SQL for nodejs14+

Downloads

8

Readme

Node.js PackageCoverage Status


mybatis in nodejs,also support Single Table ORM


使用例子(也可参考测试代码)sample

import sqlTemplate, {
  AuroraEntity,
  DBColumn,
  initDaoContext,
  MySqlDaoContext,
} from 'es-batis'
//更详细的参数请看参数类型申明,现在只支持非连接池的方式(因为用于serverless)
//more info for parameter,please refer to the source,only support none connection pool
const dao = new MySqlDaoContext({
  charset: 'utf8',
  host: 'mysqlhost',
  user: 'user',
  password: 'password',
})
await dao.initialize()
dao = initDaoContext({ dao })

const sql = sqlTemplate`<?xml version="1.0" encoding="UTF-8"?>
  <!DOCTYPE mapper PUBLIC
  "-//mybatis.org//DTD Mapper 3.0//EN"
  "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  <mapper
  namespace="esbatis.test">

  <select id="selectOne"   >
      SELECT
          t1.bank_name,
          t1.id
      FROM
          bank AS t1
      <where>
        <if test="id != null">
          and t1.id = #{id}
        </if>
      </where>
      ORDER BY
          t1.updated
  </select>
  </mapper>
  `

let sqlResults = await sql.selectOne('esbatis.test.selectOne', { id: 1 })
//example returns:
//sqlResults = {
//   bank_name: 'bank1',
//   id: 1,
// }

es-batis ORM example

table:

CREATE TABLE SYSTEM_MST(
	SYSTEM_ID VARCHAR(5) NOT NULL,
	SYSTEM_NAME VARCHAR(128) NOT NULL,
	UNIQUE KEY  SYSTEM_ID_UNIQUE(SYSTEM_ID ASC)
)

import sqlTemplate, {
  AuroraEntity,
  DBColumn,
  initDaoContext,
  MySqlDaoContext,
} from 'es-batis'

//tsconfig.json:
//"experimentalDecorators": true,
//"emitDecoratorMetadata": true,
class SystemMst extends AuroraEntity {
  @DBColumn({ idType: 'uniqueIndex' })
  public systemId!: string | null
  public systemName: string | null = null
}
const dao = new MySqlDaoContext({
  charset: 'utf8',
  host: 'mysqlhost',
  user: 'user',
  password: 'password',
})
await dao.initialize()
//additional parameter for ORM supporting in dynamic SQL,see the next sample
dao = initDaoContext({ dao, types: { SystemMst } })

let systemMst = new SystemMst()
systemMst.systemId = '0001'
const deletecnt = await systemMst.delete()
//OR
//const deletecnt = await systemMst.delete('0001')
console.log(deletecnt)
let resultSystemMst = await systemMst.load('efg')
if (resultSystemMst) {
  console.log(resultSystemMst.systemName)
}

es-batis Dynamic SQL and ORM

import sqlTemplate, {
  AuroraEntity,
  DBColumn,
  initDaoContext,
  MySqlDaoContext,
} from 'es-batis'

class SystemMst extends AuroraEntity {
  @DBColumn({ idType: 'uniqueIndex' })
  public systemId!: string | null
  public systemName: string | null = null
}
const dao = new MySqlDaoContext({
  charset: 'utf8',
  host: 'mysqlhost',
  user: 'user',
  password: 'password',
})
await dao.initialize()
dao = initDaoContext({ dao, types: { SystemMst } })

//initialization at the same way as the previous sample
const sql = sqlTemplate`<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC
    "-//mybatis.org//DTD Mapper 3.0//EN"
    "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper
    namespace="esbatis.test">

    <select id="nameLike"   resultType='SystemMst'>
        SELECT
            t1.system_id as systemId,
            t1.system_name as systemName
        FROM
            system_mst AS t1
        <where>
          <if test="name != null">
          <bind name="nameLike" value="escapeMYSQLLike(name)">
          <![CDATA[ and t1.system_name LIKE concat('%',#{nameLike},'%') ]]>
          </if>
        </where>
    </select>
</mapper>
    `
try {
  await dao.beginTransaction()
  const systems = await sql.selectList('esbatis.test.nameLike', {
    name: '%_"\'',
  })
  for (const system of systems) {
    system.systemName = '吼吼吼'
    await system.save()
  }
  await dao.commit()
} catch (err) {
  console.error(`error happend:${err}`)
  await dao.rollback()
}