@plumier/generic-controller
Advanced tools
Comparing version 1.0.0-canary.kd6wewq6.0 to 1.0.0-canary.kdf0lwtn.0
@@ -13,11 +13,2 @@ "use strict"; | ||
// --------------------------------------------------------------------- // | ||
function getIdType(type) { | ||
const meta = tinspector_1.default(type); | ||
for (const prop of meta.properties) { | ||
const decorator = prop.decorators.find((x) => x.kind === "GenericDecoratorId"); | ||
if (decorator) | ||
return prop.type; | ||
} | ||
return String; | ||
} | ||
function copyDecorators(decorators, controller) { | ||
@@ -71,3 +62,3 @@ const result = []; | ||
// get type of ID column on entity | ||
const idType = getIdType(entity); | ||
const idType = core_1.entityHelper.getIdType(entity); | ||
// create controller type dynamically | ||
@@ -84,23 +75,21 @@ const Controller = tinspector_1.generic.create({ parent: controller, name: controller.name }, entity, idType); | ||
} | ||
function createOneToManyController(rootPath, dec, controller, nameConversion) { | ||
const decType = tinspector_1.metadata.isCallback(dec.type) ? dec.type({}) : dec.type; | ||
const realType = Array.isArray(decType) ? decType[0] : decType; | ||
function createOneToManyController(rootPath, entity, relation, relationProperty, controller, nameConversion) { | ||
// get type of ID column on parent entity | ||
const parentIdType = getIdType(dec.parentType); | ||
const parentIdType = core_1.entityHelper.getIdType(entity); | ||
// get type of ID column on entity | ||
const idType = getIdType(realType); | ||
const idType = core_1.entityHelper.getIdType(relation); | ||
// create controller | ||
const Controller = tinspector_1.generic.create({ parent: controller, name: controller.name }, dec.parentType, realType, parentIdType, idType); | ||
const Controller = tinspector_1.generic.create({ parent: controller, name: controller.name }, entity, relation, parentIdType, idType); | ||
// add root decorator | ||
const name = nameConversion(dec.parentType.name); | ||
const path = core_1.appendRoute(rootPath, `${name}/:pid/${dec.propertyName}`); | ||
const name = nameConversion(entity.name); | ||
const path = core_1.appendRoute(rootPath, `${name}/:pid/${relationProperty}`); | ||
// copy @route.ignore() on entity to the controller to control route generation | ||
const meta = tinspector_1.default(dec.parentType); | ||
const decorators = meta.properties.find(x => x.name === dec.propertyName).decorators; | ||
const meta = tinspector_1.default(entity); | ||
const decorators = meta.properties.find(x => x.name === relationProperty).decorators; | ||
decorateController(Controller, copyDecorators(decorators, controller)); | ||
Reflect.decorate([ | ||
core_1.route.root(path), | ||
core_1.api.tag(dec.parentType.name), | ||
core_1.api.tag(entity.name), | ||
// re-assign oneToMany decorator which will be used on OneToManyController constructor | ||
tinspector_1.decorateClass(dec), | ||
tinspector_1.decorateClass({ kind: "plumier-meta:relation-prop-name", name: relationProperty }), | ||
], Controller); | ||
@@ -124,5 +113,5 @@ Reflect.decorate([], Controller); | ||
for (const prop of meta.properties) { | ||
const oneToMany = prop.decorators.find((x) => x.kind === "GenericDecoratorOneToMany"); | ||
if (oneToMany) { | ||
controllers.push(createOneToManyController(opt.rootPath, Object.assign(Object.assign({}, oneToMany), { propertyName: prop.name }), opt.oneToManyController, opt.nameConversion)); | ||
const relation = prop.decorators.find((x) => x.kind === "plumier-meta:relation"); | ||
if (relation && prop.typeClassification === "Array") { | ||
controllers.push(createOneToManyController(opt.rootPath, entity, prop.type[0], prop.name, opt.oneToManyController, opt.nameConversion)); | ||
} | ||
@@ -129,0 +118,0 @@ } |
export * from "./controller"; | ||
export * from "./decorator"; | ||
export * from "./facility"; | ||
export * from "./types"; |
@@ -5,4 +5,3 @@ "use strict"; | ||
tslib_1.__exportStar(require("./controller"), exports); | ||
tslib_1.__exportStar(require("./decorator"), exports); | ||
tslib_1.__exportStar(require("./facility"), exports); | ||
tslib_1.__exportStar(require("./types"), exports); |
import { Class } from "@plumier/core"; | ||
export interface OneToManyDecorator { | ||
kind: "GenericDecoratorOneToMany"; | ||
propertyName: string; | ||
type: Class | Class[] | ((x: any) => Class | Class[]); | ||
parentType: Class; | ||
} | ||
export interface IdentifierDecorator { | ||
kind: "GenericDecoratorId"; | ||
} | ||
export interface InversePropertyDecorator { | ||
kind: "GenericInverseProperty"; | ||
} | ||
export interface GenericControllerDecorator { | ||
kind: "GenericController"; | ||
} | ||
export interface Repository<T> { | ||
@@ -54,2 +39,6 @@ find(offset: number, limit: number, query: Partial<T>): Promise<T[]>; | ||
} | ||
export interface RelationPropertyDecorator { | ||
kind: "plumier-meta:relation-prop-name"; | ||
name: string; | ||
} | ||
export declare abstract class OneToManyControllerGeneric<P, T, PID, TID> extends BaseControllerGeneric { | ||
@@ -56,0 +45,0 @@ protected readonly entityType: Class<T>; |
@@ -39,6 +39,6 @@ "use strict"; | ||
this.entityType = types[1]; | ||
const oneToMany = meta.decorators.find((x) => x.kind === "GenericDecoratorOneToMany"); | ||
this.relation = oneToMany.propertyName; | ||
const oneToMany = meta.decorators.find((x) => x.kind === "plumier-meta:relation-prop-name"); | ||
this.relation = oneToMany.name; | ||
} | ||
} | ||
exports.OneToManyControllerGeneric = OneToManyControllerGeneric; |
{ | ||
"name": "@plumier/generic-controller", | ||
"version": "1.0.0-canary.kd6wewq6.0+3251487", | ||
"version": "1.0.0-canary.kdf0lwtn.0+b159de7", | ||
"description": "Plumier generic controller implementation", | ||
@@ -18,3 +18,3 @@ "main": "lib/index.js", | ||
"dependencies": { | ||
"@plumier/core": "1.0.0-canary.kd6wewq6.0+3251487", | ||
"@plumier/core": "1.0.0-canary.kdf0lwtn.0+b159de7", | ||
"@types/pluralize": "^0.0.29", | ||
@@ -33,3 +33,3 @@ "pluralize": "^8.0.0" | ||
}, | ||
"gitHead": "3251487851196165e3ab62144161d3329811b4de" | ||
"gitHead": "b159de7becd3e5b54dd97b796be4cd7715f9d3e7" | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Manifest confusion
Supply chain riskThis package has inconsistent metadata. This could be malicious or caused by an error when publishing the package.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Manifest confusion
Supply chain riskThis package has inconsistent metadata. This could be malicious or caused by an error when publishing the package.
Found 1 instance in 1 package
38980
11
552