9 Star 11 Fork 0

Notadd / magnus

Create your Gitee Account
Explore and code with more than 5 million developers,Free private repositories !:)
Sign up
This repository doesn't specify license. Without author's permission, this code is only for learning and cannot be used for other purposes.
Clone or download
README.md 2.13 KB
Copy Edit Web IDE Raw Blame History
meepobrother authored 2019-07-16 07:23 . docs: ✏️ update docs

根据ts自动生成`.graphql`和`.proto`配置文件

install

npm install -g @notadd/magnus

add magnus.json

{
	"inputs": [ "demo/src/app.resolver.ts" ],
	"output": "demo/src/magnus",
	"temp": "demo/src/magnus/temp",
    "debug": true,
    "copyTo": []
}

run magnus

// 开发模式
magnus
// 生产模式 自动复制生成的配置文件到指定的文件夹
magnus --prod

orm

@Magnus({
    entities: [
        User,Post
    ],
})
export class Controller<T> extends MagnusBase<T> {
    // 自动生成 getUser,getPost
    get repository() {
        return getRepository<T>(this.tablename);
    }

    @Query()
    async get(entity: T): Promise<T> {
        return this.repository.findOne({
            where: entity,
        });
    }
    // 自动生成 addUser,addPost
    @Mutation()
    async add(entity: Partial<T>): Promise<T> {
        return {
            id: 1,
        } as any;
    }

    // 自动生成 insertUser,insertPost
    @Mutation()
    async insert(entity: T): Promise<InsertResult<T>> {
        return {
            id: 1,
        } as any;
    }
}

覆盖orm

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

使用

import { createRunner } from '@notadd/magnus-core';
import { Controller, Controller2 } from './index';
import { join } from 'path';
import { Query } from '../magnus/magnus'
import { parse } from '@notadd/magnus-graphql';
const map = require(join(__dirname, '..', 'magnus/magnus.metadata.json'));
const context = require(join(__dirname, '..', 'magnus/magnus.server.json'));
const run = createRunner(map, context, {
    Controller,
    Controller2,
});
const query: Query = run.query;
async function run2() {
    const res1 = await run(`{
        getUser{
            id
        }
    }`);
    const res = await query.getUser(`{
        id
    }`);
}
run2();

Comment ( 0 )

Sign in for post a comment

TypeScript
1
https://gitee.com/notadd/magnus.git
git@gitee.com:notadd/magnus.git
notadd
magnus
magnus
master

Search