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

@ckpack/midway-plugin-sequelize

v0.0.5

Published

Midway plugin for Sequelize V7

Downloads

7

Readme

@ckpack/midway-plugin-sequelize

Midway plugin for Sequelize V7

安装依赖

npm i @ckpack/midway-plugin-sequelize @sequelize/core --save

启用组件

import { join } from 'node:path';
import { Configuration, ILifeCycle } from '@midwayjs/core';
import * as sequelize from '@midwayjs/sequelize';

@Configuration({
  imports: [
    // ...
    sequelize,
  ],
  importConfigs: [join(__dirname, './config')],
})
export class MainConfiguration implements ILifeCycle {
  // ...
}

模型定义

详细参考https://sequelize.org/docs/v7/models/defining-models/

import { DataTypes, InferAttributes, InferCreationAttributes, Model } from '@sequelize/core';
import { Attribute, NotNull } from '@sequelize/core/decorators-legacy';

class User extends Model<InferAttributes<User>, InferCreationAttributes<User>> {
  @Attribute(DataTypes.STRING)
  @NotNull
  declare firstName: string;

  @Attribute(DataTypes.STRING)
  declare lastName: string | null;
}

数据源配置

import { Person } from '../entity/person';

export default {
  // ...
  sequelize: {
    dataSource: {
      // 第一个数据源,数据源的名字可以完全自定义, 具体参数与new Sequelize()构造函数参数一致
      default: {
        dialect: 'postgres',
        database: 'test4',
        username: 'root',
        models: [Person], // 注意此处与@midwayjs/sequelize不同
      },
      // 第二个数据源
      default2: {
        // ...
      },
    },
  },
};

使用方法

详细参考https://sequelize.org/docs/v7/category/querying/

import { Provide } from '@midwayjs/core';
import { Person } from '../entity/person';

@Provide()
export class PersonService {
  async createPerson() {
    const person = new Person({ name: 'bob', age: 99 });
    await person.save();
  }
}

获取数据源

数据源即创建出的 sequelize 对象,我们可以通过注入内置的数据源管理器来获取。

import { InjectDataSource } from '@ckpack/midway-plugin-sequelize';
import { Sequelize } from '@sequelize/core';
import { Person } from '../entity/person';

export class UserService {
  // 注入默认数据源
  @InjectDataSource()
  defaultDataSource: Sequelize;

  // 注入自定义数据源
  @InjectDataSource('default2')
  customDataSource: Sequelize;

  async getUser() {
    console.log(await this.defaultDataSource.models.Person.findAll());
  }
}