inversify-datastore
Version:
Some utilities for the development of Google Cloud Datastore applications using Inversify
87 lines (86 loc) • 2.93 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
var inversify_1 = require("inversify");
var constants_1 = require("./constants");
var class_validator_1 = require("class-validator");
/**
* Decorates a Google Datastore Repository
* @param entityIdentifier the entity assinged for that repository
*/
function repository(entityIdentifier) {
return function (target) {
var metadata = {
target: target,
entityIdentifier: entityIdentifier
};
Reflect.defineMetadata(constants_1.METADATA_KEY.repository, metadata, target);
inversify_1.decorate(inversify_1.injectable(), target);
return target;
};
}
exports.repository = repository;
/**
* Decorates an entity.
* @param kind the kind name of an entity.
* @param entityOptions the options for serialization
*/
function entity(kind, entityOptions) {
var metadata = {
kind: kind,
entityOptions: entityOptions
};
return Reflect.metadata(constants_1.METADATA_KEY.entity, metadata);
}
exports.entity = entity;
/**
* Identify the path to compose an entity key.
* @param validationOptions optional for customize the field validation
*/
function id(validationOptions) {
return function (target, propertyKey) {
Reflect.defineMetadata(constants_1.METADATA_KEY.entityId, propertyKey, target);
class_validator_1.registerDecorator({
name: "isValidId",
target: target.constructor,
propertyName: propertyKey,
options: validationOptions,
validator: {
validate: function (value) {
return value && value.length > 0 && !constants_1.CONTRAINTS.id.test(value);
}
}
});
};
}
exports.id = id;
/**
* The field decorated will be excluded from entity indexes.
*/
function excludeFromIndex() {
return function (target, propertyKey) {
var excludeFromIndexes = Reflect.getMetadata(constants_1.METADATA_KEY.excludeFromIndexes, target) || [];
excludeFromIndexes.push(propertyKey);
Reflect.defineMetadata(constants_1.METADATA_KEY.excludeFromIndexes, excludeFromIndexes, target);
};
}
exports.excludeFromIndex = excludeFromIndex;
/**
* The field decorated will be assigned with a new date when created.
* The property decorated with this field must be a Data.
*/
function createdAt() {
return function (target, propertyKey) {
Reflect.defineMetadata(constants_1.METADATA_KEY.createdAt, propertyKey, target);
};
}
exports.createdAt = createdAt;
/**
* The field decorated will be assigned with a new date when updated.
* The property decorated with this field must be a Data.
*/
function updatedAt() {
return function (target, propertyKey) {
Reflect.defineMetadata(constants_1.METADATA_KEY.updatedAt, propertyKey, target);
};
}
exports.updatedAt = updatedAt;
;