Metrics
0
Watch 7 Star 9 Fork 0

Notadd / magnusTypeScript

Sign up for free
Explore and code with more than 2 million developers,Free private repositories !:)
Sign up
根据ts自动生成`.graphql`和`.proto`配置文件 spread retract

https://magnus.notadd.com/

  • TypeScript 40.3%
  • JavaScript 36.0%
  • HTML 23.1%
  • CSS 0.6%
Clone or download
Loading...
README.md

根据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();

Comments ( 2 )

You need to Sign in for post a comment

Help Search

183227_9af5e6a8_1826025 111910_4d91f001_1826025