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

midway3-sequelize

v1.0.5

Published

midway squelize

Downloads

2

Readme

midway3-sequelize

###只适用于midway3.x版本

使用方法

###安装

// yarn
yarn add midway3-sequelize

// npm
npm install midway3-sequelize
// 使用mysql  注意mysql版本不能低于5.7 
yarn add mysql2 

configuration.ts 文件引入组件

import * as sequelize from 'midway3-sequelize';
@Configuration({
  imports: [
    koa,
    sequelize,
  ],
  importConfigs: [join(__dirname, './config')],
})

添加配置项 config.local.ts

// 和sequelize参数一样一样的,自行修改
export const sequelize = {
  options: {
    dialect: 'mysql',
    host: '127.0.0.1',
    port: '3306',
    database: 'fang_test',
    username: 'root',
    password: 'root',
    timezone: '+08:00',
    define: {
      timestamps: true,
      paranoid: true,
      charset: 'utf8',
      underscored: true,
    },
    dialectOptions: {
      dateStrings: true,
      typeCast: (field: any, next: () => void) => {
        if (field.type === 'DATETIME') {
          return field.string();
        }
        return next();
      },
    },
  },
  sync: false,
};

创建model文件 参考下就行了 🤣

后面会更新下model文件生成工具 适用于此版本(写代码,能少些就不多写,写的越多bug越多🤓)

import { Model, Column } from 'sequelize-typescript';
import { BaseTable, STRING } from 'midway3-sequelize';

// #region enum
// #endregion

@BaseTable({
  tableName: 'user',
})
export class UserModel extends Model {
  /**
   * 密码
   */
  @Column({ comment: '密码', type: STRING })
  password: string;

  /**
   * 用户名
   */
  @Column({ comment: '用户名', type: STRING })
  username: string;
}

// 常量生成
export class ConstUser { 
  /**
   * password
   */
  static readonly PASSWORD: string = 'password';
  /**
   * username
   */
  static readonly USERNAME: string = 'username';
}

service 使用 值得你参考😉

import { Inject, Provide } from '@midwayjs/decorator';
import { BaseService } from '../core/base/base.service';
import { GetUser } from '../core/dto/user';
import { UserModel, ConsUser } from '../core/models/user.model';
import { DBContext, Op, literal, Transaction } from 'midway3-sequelize'

@Provide()
export class UserService extends BaseService {
  @Inject()
  dBContext: DBContext; // 获取 sequelize 实例化对象
  async getUser(options: GetUser) {
    // 事物 隔离级别自己设置就完了
    const t: Transaction = await this.dBContext.sequelize.transaction(); 
    try {
      await UserModel.create({[ConstUser.USERNAME]: 'lisi'})
      await UserModel.update(
        {[ConstUser.USERNAME]: 'zhangsan'}, 
        {where: {[ConstSysUser.USERNAME]: 'lisi'}, transaction: t})
      await t.commit()
    } catch (error) {
      console.log(error)
      await t.rollback()
    }
    
    const res = await UserModel.findOne({
      where: { [ConstUser.ID]: options.uid },
    });
    return { res }
  }
}