Ошибка Inversify, TypeError: Reflect.hasOwnMetadata не является функцией - PullRequest
0 голосов
/ 23 марта 2019

Я использую inversify.js в проекте, и я решил отправить свои классы доступа к данным как отдельный пакет NPM.

Пакет экспортирует класс

import { injectable, unmanaged } from 'inversify';
import { Document, Query, Model, model } from 'mongoose';
import { merge } from 'lodash';
import { BaseMongoSchema } from './schema';

export interface QueryOptions {
    conditions: any;
    projections?: any;
    populate?: [{
        path: string;
        select: string;
    }];
    archived?: boolean;
    select?: any;
    sort?: any;
    per_page?: number;
    page?: number;
}

export interface PaginationOptions {
    total: number;
    per_page: number;
    page: number;
    pages?: number;
}

export interface PaginatedResult<T> {
    pagination: {
        total: number;
        per_page: number;
        pages: number;
        page: number;
    };
    result: T[];
}

export interface IMongoRepository<T> {
    all(options: QueryOptions): Promise<PaginatedResult<T> | T[]>;
    one(id: string): Promise<T|any>;
    oneByField(field: string, value: any): Promise<T>;
    create(attributes: any): Promise<any | T>;
    delete(id: string): Promise<boolean>;
    destroy(id: string): Promise<boolean>;
    update(id: string, attributes: any): Promise<T>;
    restore(id: string): Promise<T>;
}

export class ModelNotFoundError extends Error {
    constructor(message: string) {
        super(message);
    }
}

@injectable()
export abstract class MongoRepository<T extends Document> implements IMongoRepository<T> {
    private name: string;
    private model: Model<T>;
    private schema: BaseMongoSchema;

    constructor(
        @unmanaged() name: string,
        @unmanaged() schema: BaseMongoSchema
    ) {
        this.name = name;
        this.schema = schema;
        this.model = model<T>(this.name, schema);
    }
    ...methods
}

Использование классав моих основных проектах вызывают ошибку с reflect-metadata

import { injectable, unmanaged } from 'inversify';
import { MongoRepository } from '@scope/data';
import { UserSchema, IUser } from '../schema/user';

@injectable()
class UserRepository extends MongoRepository<IUser> {
    constructor() {
        super(
            'UserAccount',
            UserAccountSchema
        );
    }
}

ошибка

if (Reflect.hasOwnMetadata(metadataKey, annotationTarget)) {
                ^
TypeError: Reflect.hasOwnMetadata is not a function

Не уверен, что вызывает ошибку, все кажется в порядке.Любая помощь будет принята с благодарностью.

1 Ответ

0 голосов
/ 29 марта 2019

Нашел способ обойти это, сделал класс репозитория неабстрактным и удалил все инвертируемые аннотации.

В пакете / сервисе, который нуждался в классе репозитория, у меня есть помощник

import { Container } from 'inversify';
import { BaseSchema, MongoRepository } from 'some package';
import { Document } from 'mongoose';

export namespace DIHelper {
    export function registerRepository<T extends Document>(container: Container, symbol: any, name: string, schema: BaseMongoSchema) {
        container.bind(symbol)
            .toDynamicValue((_ctx) => {
                return new MongoRepository<T>(name, schema);
            })
            .inSingletonScope();
    }
}

хранилище привязано к контейнеру как

import 'reflect-metadata';
import { Container } from 'inversify';
import { IUserAccount, UserAccountSchema } from '../../data/schemas/user-account';
import { IRole, RoleSchema } from '../../data/schemas/role';
import { IPolicy, PolicySchema } from '../../data/schemas/policy';
// Controllers
import '../controllers';
import { DIHelper } from '../helpers/di';
// Container
const DIContainer = new Container();
// Repository bindings
DIHelper.registerRepository<IUserAccount>(DIContainer, 'UserAccounts', 'UserAccount', UserAccountSchema);
DIHelper.registerRepository<IRole>(DIContainer, 'Roles', 'Role', RoleSchema);
DIHelper.registerRepository<IPolicy>(DIContainer, 'Policies', 'Policy', PolicySchema);
export default DIContainer;
...