node.js typeorm stati c маршруты - PullRequest
0 голосов
/ 24 апреля 2020

Я пытаюсь использовать typeorm и mongodb. У меня есть stati c маршруты, вот пример:

export default class UserController {

    private userRepository = getRepository(User);

    constructor() {
        const test = this.userRepository.find();
        console.log(test);
    }

    @request("get", "/users")
    @summary("Find all users")
    public static async getUsers(ctx: BaseContext): Promise<void> {
        const all_users = getRepository(User).find();
        console.log(all_users);
        // const loadedPosts = await getMongoManager().connection.getRepository().find(User);
        // console.log("Loaded posts from the database: ", loadedPosts);

        // get a user repository to perform operations with user
        const userRepository = getMongoManager().getRepository(User);

        // load all users
        const users: User[] = await userRepository.find();

        // return OK status code and loaded users array
        ctx.status = 200;
        ctx.body = users;
    }

Проблема в том, что я пытаюсь получить ссылку на основное соединение, которое я создал, в моем файле index.ts, например так:

createConnection({
    type: 'mongodb',
    url: config.databaseUrl,
    database: 'apidb',
    synchronize: true,
    logging: true,
    entities: [User],
}).then(async connection => {

    const app = new Koa();

    // Provides important security headers to make your app more secure
    app.use(helmet());

    // Enable cors with default options
    app.use(cors());

    // Logger middleware -> use winston as logger (logging.ts with config)
    app.use(logger(winston));

    // Enable bodyParser with default options
    app.use(bodyParser());

    // these routes are NOT protected by the JWT middleware, also include middleware to respond with "Method Not Allowed - 405".
    app.use(unprotectedRouter.routes()).use(unprotectedRouter.allowedMethods());

и я не могу найти способ. Должен ли я изменить свои маршруты, чтобы не быть стати c? если так, я думаю, что у меня будут проблемы с другой из моих библиотек koa-swagger-decorator, которая требует, чтобы мои маршруты были stati c.

Кто-нибудь знает, как получить ссылку на typeORM? Я предполагаю, что это не выполнимо. Любые идеи о том, как я могу изменить это, чтобы работать?

Спасибо!

...