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

fast-mysql

v1.0.6

Published

装饰器风格的Sql语句查询框架

Downloads

11

Readme

fast-mysql

装饰器风格的Sql语句查询框架

安装

npm i fast-mysql

使用

index.ts

import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import initMysql from 'fast-mysql';

async function bootstrap() {
  initMysql({
    connectionLimit: 10,
    host: 'localhost',
    user: 'root',
    password: 123456,
    database: 'xuan_admin',
  });

  const app = await NestFactory.create(AppModule);
  await app.listen(1234);
}
bootstrap();

userMapping.ts

import { Injectable } from '@nestjs/common';
import { Sql } from 'fast-mysql';

@Injectable()
export class LoginMapping {
  @Sql('select * from user where id = #{id}')
  findUser(id: number): any {}
}

useController.ts

import { Controller, Get, Post } from '@nestjs/common';
import { LoginMapping } from './login.mapping';

@Controller('user')
export class LoginController {
  constructor(private readonly loginMapping: LoginMapping) {}

  @Get()
  async userlogin() {
    const user = await this.loginMapping.findUser(1);
    return user;
  }
}

理论上fast-mysql能搭配任意node框架使用,例如Koaexpress 等等,这里选择nest示例是因为nest框架更符合装饰器风格

装饰器

@Sql

@Sql("Sql语句")

被Sql标识的函数接收的参数填入Sql语句占位符内,并将查询出来的数据当做函数被调用时的返回值返回

示例:

@Sql('select * from user where id = #{id}')
findUser(id: number): any {}

findUser(777)
// 解析的Sql为 select * from user where id = '777'

上述的#{}将传入的数据都当成一个字符串,会对自动传入的数据加一个双引号

示例:

@Sql('select * from user where id = ${id}')
findUser(id: number): any {}

findUser(777)
// 解析的Sql为 select * from user where id = 777

上述的${}将传入的数据直接显示生成在sql中,$方式无法防止Sql注入,应当谨慎使用!

示例:

@Sql('select * from user where id = ${id}')
findUser(user: User): any {}

findUser({ name:'我是用户',id:777 })
// 解析的Sql为 select * from user where id = 777

接收的参数可以是对象

@ResultType

@ResultType(实体类)

被ResultType标识的函数返回的参数将处理为指定的实体类进行返回

示例:

class User {
    constructor(
    readonly user_id: number,
     readonly user_name: string,
     readonly user_password: string,
    ) {}
}


@ResultType(User)
@Sql('select * from user where id = #{id}')
findUser(id: number): any {}

findUser(777)

上述的findUser返回值为User实体类字段,例如数据库字段有 用户id、用户名、用户密码、用户邮箱,但使用ResultType约束后将只返回 用户id、用户名、用户密码

疑问交流

暂无