koatty_typeorm
TypeScript icon, indicating that this package has built-in type declarations

1.3.2 • Public • Published

koatty_typeorm

TypeORM plugin for Koatty.

安装


npm i koatty_typeorm

使用


Koatty

1、项目中增加plugin

koatty plugin Typeorm;

2、修改 plugin/TypeormPlugin.ts:

import { Koatty, Plugin, IPlugin } from "koatty";
import typeorm from 'koatty_typeorm';

@Plugin()
export class TypeormPlugin implements IPlugin {
  run(options: any, app: Koatty) {
    return typeorm(options, app);
  }
}

3、项目plugin配置 config/plugin.ts:

// src/config/plugin.ts
export default {
  list: ['TypeormPlugin'], // 加载的插件列表,执行顺序按照数组元素顺序
  config: { // 插件配置
    TypeormPlugin: {
        //默认配置项
        type: "mysql", //mysql, mariadb, postgres, sqlite, mssql, oracle, mongodb, cordova
        host: "127.0.0.1",
        port: 3306,
        username: "test",
        password: "test",
        database: "test",

        synchronize: false, //true 每次运行应用程序时实体都将与数据库同步
        logging: true,
        entities: [`${process.env.APP_PATH}/model/*`],
        entityPrefix: "", //表前缀
    }
  },
};

4、定义model

import {BaseEntity, Entity, PrimaryGeneratedColumn, Column} from "typeorm";

@Entity()
export class User extends BaseEntity {

    @PrimaryGeneratedColumn()
    id: number;

    @Column()
    firstName: string;

    @Column()
    lastName: string;

    @Column()
    isActive: boolean;

    static findByName(firstName: string, lastName: string) {
        return this.createQueryBuilder("user")
            .where("user.firstName = :firstName", { firstName })
            .andWhere("user.lastName = :lastName", { lastName })
            .getMany();
    }

}

5、CURD

// example how to save AR entity
const user = new User();
user.firstName = "Timber";
user.lastName = "Saw";
user.isActive = true;
await user.save();

// example how to remove AR entity
await user.remove();

// example how to load AR entities
const users = await User.findBy({ skip: 2, take: 5 });
const newUsers = await User.findBy({ isActive: true });
const timber = await User.findOneBy({ firstName: "Timber", lastName: "Saw" });

Package Sidebar

Install

npm i koatty_typeorm

Weekly Downloads

1

Version

1.3.2

License

BSD-3-Clause

Unpacked Size

17.9 kB

Total Files

12

Last publish

Collaborators

  • richenlin