{"id":13598822,"url":"https://github.com/notadd/magnus","last_synced_at":"2025-04-10T10:30:27.266Z","repository":{"id":57130627,"uuid":"200968056","full_name":"notadd/magnus","owner":"notadd","description":"magnus","archived":true,"fork":false,"pushed_at":"2020-10-20T02:13:11.000Z","size":4843,"stargazers_count":11,"open_issues_count":23,"forks_count":2,"subscribers_count":4,"default_branch":"master","last_synced_at":"2024-05-22T05:40:52.465Z","etag":null,"topics":["graphql","grpc","orm","protobuf","typeorm"],"latest_commit_sha":null,"homepage":null,"language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/notadd.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-08-07T03:51:25.000Z","updated_at":"2023-02-18T06:30:45.000Z","dependencies_parsed_at":"2022-09-01T00:12:19.374Z","dependency_job_id":null,"html_url":"https://github.com/notadd/magnus","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/notadd%2Fmagnus","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/notadd%2Fmagnus/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/notadd%2Fmagnus/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/notadd%2Fmagnus/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/notadd","download_url":"https://codeload.github.com/notadd/magnus/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":212475300,"owners_count":15446812,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["graphql","grpc","orm","protobuf","typeorm"],"created_at":"2024-08-01T17:00:57.131Z","updated_at":"2024-08-01T17:05:13.810Z","avatar_url":"https://github.com/notadd.png","language":"TypeScript","funding_links":[],"categories":["资源"],"sub_categories":["集成"],"readme":"\u003cp align=\"center\"\u003e\n    \u003cimg style=\"margin: 0 auto;\" width=\"220px\" src=\"./logo.jpg\"/\u003e\n\u003c/p\u003e\n\n\n### 功能特点\n\n- 可生成@nestjs/graphql相应接口的`.graphql`文件，无缝对接\n- 可生成@nestjs/grpc相应的`.proto`文件，无缝对接\n- 编译生成，打包更小，速度更快\n- magnus去掉服务端，速度更快，更灵活!\n- 程序自动维护graphql和proto文件，无缝对接nestjs\n- 根据 Typescript 自动生成`.graphql`和`.proto`配置文件（灵活）\n- 生成自动支持按条件搜索、分页、排序的增删改查\n- 自动更新数据库结构\n- 局域网内项目 proto 同步及接口文件自动生成，方便团队协作，可在不同电脑启动不同微服务\n- 全面支持微服务架构\n\n\u003cp align=\"center\"\u003e\n\u003cimg style=\"margin: 0 auto;\" width=\"650px\" src=\"static/Functional-structure-CN.svg\"/\u003e\n\u003c/p\u003e\n\n\u003cp align=\"center\"\u003e\n\u003cimg style=\"margin: 0 auto;\" width=\"900px\" src=\"static/Functional-diciaion-CN.svg\"/\u003e\n\u003c/p\u003e\n\n\u003cp align=\"center\"\u003e\n\u003cimg style=\"margin: 0 auto;\" width=\"450px\" src=\"static/sync-CN.svg\"/\u003e\n\u003c/p\u003e\n\n\u003cp align=\"center\"\u003e\n\u003cimg style=\"margin: 0 auto;\" width=\"900px\" src=\"static/magnus-graphql-CN.png\"/\u003e\n\u003c/p\u003e\n\n\n## install\n\n```\nnpm install -g @notadd/magnus\n```\n\n## add magnus.json\n```json\n{\n  \"prefix\": \"\", // 前缀\n  \"inputs\": [// 输入\n    \"demo/src/**/*.ts\"\n  ],\n  \"output\": \"demo/config\",// 输出\n  \"assets\": \"demo/assets\",// 静态资源\n  \"debug\": false,// 调试\n  \"name\": \"demo\",// 名称\n  \"client\": [//客户端\n    \"demo/src/**/*.graphql\"\n  ],\n  \"hasGrpc\": true,// 是否提供grpc\n  \"runner\": {// magnus runner目录\n    \"name\": \"runner\",\n    \"path\": \"../runner\"\n  },// 定义文件\n  \"types\": \"../../config/demo/magnus.server\",\n  \"def\": \"demo/assets/demo/magnus.server.graphql\"// 定义graphql文件\n}\n```\n\n## run magnus\n\n```ts\nmagnus --watch 监听文件变化\nmagnus 单次运行\n```\n\n### orm\n\n```ts\nimport { Magnus, MagnusBase, Mutation, Query, Where, Order } from '@notadd/magnus-core';\nimport { getRepository, SelectQueryBuilder, FindOperator, FindConditions } from 'typeorm';\nimport {\n    Department,\n    Role,\n    RoleGroup,\n    SafetyScoreLog,\n    SafetyScoreRule,\n    Station,\n    System,\n    SystemEvent,\n    SystemRight,\n    ToDoItem,\n    User,\n    UserLoginLog\n} from '../entities';\nimport { PageLimit } from './types';\n@Magnus({\n    entities: [\n        User,\n        System,\n        SystemEvent,\n        SystemRight,\n        Department,\n        Role,\n        Station,\n        ToDoItem,\n        RoleGroup,\n        UserLoginLog,\n        SafetyScoreLog,\n        SafetyScoreRule\n    ]\n})\nexport class Controller\u003cT\u003e extends MagnusBase\u003cT\u003e {\n    get repository() {\n        return getRepository\u003cT\u003e(this.entity);\n    }\n\t/**\n\t * 没有就插入，有就更新\n\t * @param entity  要保存的数据\n\t */\n    @Mutation()\n    async save(entity: T): Promise\u003cT\u003e {\n        const item = this.repository.create(entity);\n        return await this.repository.save(item);\n    }\n\n\t/**\n\t * 插入数据\n\t * @param entity 要插入的数据\n\t */\n    @Mutation()\n    async insert(entity: T): Promise\u003cT\u003e {\n        const item = this.repository.create(entity);\n        const res = await this.repository.insert(item);\n        return item;\n    }\n\t/**\n\t * 通过指定条件获取一条数据\n\t * @param entity 条件\n\t */\n    @Query()\n    async get(entity: Where\u003cT\u003e): Promise\u003cT | undefined\u003e {\n        const selectAndRelations = this.createSelectAndRelations();\n        const res = await this.repository.findOne({\n            where: entity,\n            select: selectAndRelations.select,\n            relations: selectAndRelations.relations\n        });\n        return res;\n    }\n\n    /**\n     * 通过制定条件获取一组数据\n     * @param entity 条件\n     */\n    @Query()\n    async find(entity: Where\u003cT\u003e, order: Order\u003cT\u003e, limit: PageLimit = {\n        page: 1,\n        psize: 20\n    }): Promise\u003cT[]\u003e {\n        const selectAndRelations = this.createSelectAndRelations();\n        const where = createWhere(entity);\n        const res = await this.repository.find({\n            select: selectAndRelations.select,\n            where,\n            relations: selectAndRelations.relations,\n            skip: (limit.page - 1) * limit.psize,\n            take: limit.psize,\n            order: order\n        });\n        return res;\n    }\n\n    /**\n     * 删除\n     * @param id 根据id删除\n     */\n    @Query()\n    async delete(id: number): Promise\u003cboolean\u003e {\n        const res = await this.repository.delete(id);\n        if (res) return true;\n        return false;\n    }\n\n}\n```\n\n### 覆盖orm\n```ts\n@Magnus()\nexport class Controller2 extends MagnusBase\u003cUser\u003e {\n    tablename: string = 'User';\n    /**\n     * 覆盖Controller中的getUser，单独处理\n     **/\n    @Query()\n    getUser(): User {\n        return {\n            id: 1,\n        };\n    }\n}\n```\n\n## 使用 编写client.graphql 自动生成对应client.ts文件函数\n```graphql\nquery getUser($user: UserInput!){\n    getUser(entity: $user){\n        id,\n        nickname\n    }\n}\n```\n\n```ts\nimport { getUser } from './client.ts'\ngetUser({id: 1}).then(res=\u003eres.getUser).then(res=\u003e({\n    id: res.id,\n    nickname: res.nickname\n}))\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnotadd%2Fmagnus","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fnotadd%2Fmagnus","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnotadd%2Fmagnus/lists"}