3.6.5 • Published 4 years ago

@notadd/magnus-core v3.6.5

Weekly downloads
113
License
ISC
Repository
github
Last release
4 years ago

功能特点

  • 可生成@nestjs/graphql相应接口的.graphql文件,无缝对接
  • 可生成@nestjs/grpc相应的.proto文件,无缝对接
  • 编译生成,打包更小,速度更快
  • magnus去掉服务端,速度更快,更灵活!
  • 程序自动维护graphql和proto文件,无缝对接nestjs
  • 根据 Typescript 自动生成.graphql.proto配置文件(灵活)
  • 生成自动支持按条件搜索、分页、排序的增删改查
  • 自动更新数据库结构
  • 局域网内项目 proto 同步及接口文件自动生成,方便团队协作,可在不同电脑启动不同微服务
  • 全面支持微服务架构

install

npm install -g @notadd/magnus

add magnus.json

{
  "prefix": "", // 前缀
  "inputs": [// 输入
    "demo/src/**/*.ts"
  ],
  "output": "demo/config",// 输出
  "assets": "demo/assets",// 静态资源
  "debug": false,// 调试
  "name": "demo",// 名称
  "client": [//客户端
    "demo/src/**/*.graphql"
  ],
  "hasGrpc": true,// 是否提供grpc
  "runner": {// magnus runner目录
    "name": "runner",
    "path": "../runner"
  },// 定义文件
  "types": "../../config/demo/magnus.server",
  "def": "demo/assets/demo/magnus.server.graphql"// 定义graphql文件
}

run magnus

magnus --watch 监听文件变化
magnus 单次运行

orm

import { Magnus, MagnusBase, Mutation, Query, Where, Order } from '@notadd/magnus-core';
import { getRepository, SelectQueryBuilder, FindOperator, FindConditions } from 'typeorm';
import {
    Department,
    Role,
    RoleGroup,
    SafetyScoreLog,
    SafetyScoreRule,
    Station,
    System,
    SystemEvent,
    SystemRight,
    ToDoItem,
    User,
    UserLoginLog
} from '../entities';
import { PageLimit } from './types';
@Magnus({
    entities: [
        User,
        System,
        SystemEvent,
        SystemRight,
        Department,
        Role,
        Station,
        ToDoItem,
        RoleGroup,
        UserLoginLog,
        SafetyScoreLog,
        SafetyScoreRule
    ]
})
export class Controller<T> extends MagnusBase<T> {
    get repository() {
        return getRepository<T>(this.entity);
    }
	/**
	 * 没有就插入,有就更新
	 * @param entity  要保存的数据
	 */
    @Mutation()
    async save(entity: T): Promise<T> {
        const item = this.repository.create(entity);
        return await this.repository.save(item);
    }

	/**
	 * 插入数据
	 * @param entity 要插入的数据
	 */
    @Mutation()
    async insert(entity: T): Promise<T> {
        const item = this.repository.create(entity);
        const res = await this.repository.insert(item);
        return item;
    }
	/**
	 * 通过指定条件获取一条数据
	 * @param entity 条件
	 */
    @Query()
    async get(entity: Where<T>): Promise<T | undefined> {
        const selectAndRelations = this.createSelectAndRelations();
        const res = await this.repository.findOne({
            where: entity,
            select: selectAndRelations.select,
            relations: selectAndRelations.relations
        });
        return res;
    }

    /**
     * 通过制定条件获取一组数据
     * @param entity 条件
     */
    @Query()
    async find(entity: Where<T>, order: Order<T>, limit: PageLimit = {
        page: 1,
        psize: 20
    }): Promise<T[]> {
        const selectAndRelations = this.createSelectAndRelations();
        const where = createWhere(entity);
        const res = await this.repository.find({
            select: selectAndRelations.select,
            where,
            relations: selectAndRelations.relations,
            skip: (limit.page - 1) * limit.psize,
            take: limit.psize,
            order: order
        });
        return res;
    }

    /**
     * 删除
     * @param id 根据id删除
     */
    @Query()
    async delete(id: number): Promise<boolean> {
        const res = await this.repository.delete(id);
        if (res) return true;
        return false;
    }

}

覆盖orm

@Magnus()
export class Controller2 extends MagnusBase<User> {
    tablename: string = 'User';
    /**
     * 覆盖Controller中的getUser,单独处理
     **/
    @Query()
    getUser(): User {
        return {
            id: 1,
        };
    }
}

使用 编写client.graphql 自动生成对应client.ts文件函数

query getUser($user: UserInput!){
    getUser(entity: $user){
        id,
        nickname
    }
}
import { getUser } from './client.ts'
getUser({id: 1}).then(res=>res.getUser).then(res=>({
    id: res.id,
    nickname: res.nickname
}))
3.6.5

4 years ago

3.6.4

4 years ago

3.6.3

4 years ago

3.6.2

4 years ago

3.6.1

5 years ago

3.5.17

5 years ago

3.5.16

5 years ago

3.5.14

5 years ago

3.5.12

5 years ago

3.5.8

5 years ago

3.4.17

5 years ago

3.4.16

5 years ago

3.4.8

5 years ago

3.4.4

5 years ago

3.3.30

5 years ago

3.3.29

5 years ago

3.3.28

5 years ago

3.3.27

5 years ago

3.3.23

5 years ago

3.3.18

5 years ago

3.3.17

5 years ago

3.3.16

5 years ago

3.3.12

5 years ago

3.3.11

5 years ago

3.3.7

5 years ago

3.3.4

5 years ago

3.2.7

5 years ago

3.2.6

5 years ago

3.2.5

5 years ago

3.2.0

5 years ago

3.1.28

5 years ago

3.1.27

5 years ago

3.1.26

5 years ago

3.1.13

5 years ago

3.1.12

5 years ago

3.1.11

5 years ago

3.1.10

5 years ago

3.1.5

5 years ago

3.0.10

5 years ago

3.0.9

5 years ago

3.0.7

5 years ago

3.0.1

5 years ago

2.9.5

5 years ago

2.9.4

5 years ago

2.9.3

5 years ago

2.9.0

5 years ago

2.8.1

5 years ago

2.7.14

5 years ago

2.7.13

5 years ago

2.7.4

5 years ago

2.6.17

5 years ago

2.6.16

5 years ago

2.6.15

5 years ago

2.6.13

5 years ago

2.6.6

5 years ago

2.6.1

5 years ago

2.6.0

5 years ago

2.5.4

5 years ago

2.3.11

5 years ago

2.3.7

5 years ago

2.3.6

5 years ago

2.3.2

5 years ago

2.2.7

5 years ago

2.2.4

5 years ago

2.2.3

5 years ago

2.2.1

5 years ago

2.2.0

5 years ago

2.1.2

5 years ago

2.0.15

5 years ago

2.0.14

5 years ago

2.0.13

5 years ago

2.0.12

5 years ago

2.0.11

5 years ago

2.0.10

5 years ago

2.0.9

5 years ago

2.0.8

5 years ago

2.0.3

5 years ago

2.0.0

5 years ago

1.6.16

5 years ago

1.6.8

5 years ago

1.6.5

5 years ago

1.6.2

5 years ago

1.5.1

5 years ago

1.5.0

5 years ago

1.4.9

5 years ago

1.4.4

5 years ago

1.3.4

5 years ago

1.3.0

5 years ago

1.2.2

5 years ago

1.1.24

5 years ago

1.1.12

5 years ago

1.1.8

5 years ago

1.1.6

5 years ago

1.1.5

5 years ago

1.1.0

5 years ago

1.0.24

5 years ago

1.0.22

5 years ago

1.0.21

5 years ago

1.0.20

5 years ago

1.0.19

5 years ago

1.0.17

5 years ago

1.0.16

5 years ago

1.0.15

5 years ago

1.0.14

5 years ago

1.0.13

5 years ago

1.0.12

5 years ago

1.0.5

5 years ago

1.0.1

5 years ago

0.2.7-alpha.0

5 years ago

0.2.5-alpha.0

5 years ago

0.2.2-alpha.0

5 years ago

0.2.1-alpha.0

5 years ago