Router/Entity.js

"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
var object_hash_1 = __importDefault(require("object-hash"));
var ExpressTS_1 = __importDefault(require("../app/ExpressTS"));
/**
 * This decorator is used to define an Entity.
 * Entities are **automatically** injected.
 *
 * All the Entities must be in ``src/domain/entities`` and they must have [\@Inject]{@link Inject}
 *
 * @static
 * @return {any}
 * @method Entity
 * @memberof module:Router
 * @param {!string} name The name of the entity (This will have as prefix ``Entity.``)
 * @example <caption>Example usage of [\@Entity]{@link Entity}.</caption>
 * // This will be accessed as 'Entity.Name' but most of the ORMs such as Mongoose
 * // keeps an instance of the model, e.g. mongoose.models
 * [\@Inject]{@link Inject}
 * [\@Entity]{@link Entity}('Name')
 * export default class UserEntity implements [InjectedEntity]{@link module:Injector~InjectedEntity} {
 *    [\@Retrieve]{@link Retrieve}('Mongoose')
 *    private mongoose?: MongooseClass;
 *
 *    public async [onLoad]{@link module:Injector~InjectedEntity.onLoad}(): Promise<void> {
 *      if (!this.mongoose) {
 *        return;
 *      }
 *
 *      const { ObjectId } = Schema as any;
 *
 *      this.mongoose.model(
 *        'User',
 *        new Schema({
 *          id: ObjectId,
 *          email: {
 *            type: String,
 *            min: 3,
 *            max: 255,
 *            required: true
 *          },
 *          password: {
 *            type: String,
 *            min: 8,
 *            required: true
 *          }
 *        })
 *      );
 *    }
 * }
 */
exports.default = (function (name) { return function (constructor) {
    if (constructor.name === '_injected') {
        throw new Error('The entity must be called first!');
    }
    var entityHash = (0, object_hash_1.default)(constructor);
    if (!ExpressTS_1.default.getData(entityHash, 'entities')) {
        ExpressTS_1.default.setData(entityHash, name, 'entities');
    }
    return constructor;
}; });
//# sourceMappingURL=Entity.js.map