-
-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Fix array relation should not accessible (read/write) on first c…
…lass entity (#956)
- Loading branch information
Showing
17 changed files
with
434 additions
and
93 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { authorize } from "@plumier/core" | ||
import reflect, { Class, useCache } from "@plumier/reflect" | ||
|
||
import { RefDecorator } from "./types" | ||
|
||
function normalizeEntityNoCache(type: Class) { | ||
const meta = reflect(type) | ||
reflect.flush(type) | ||
for (const prop of meta.properties) { | ||
const ref = prop.decorators.find((x: RefDecorator) => x.name === "MongooseRef") | ||
if (ref && prop.typeClassification === "Array") { | ||
const decorators = [ | ||
authorize.readonly(), | ||
authorize.writeonly() | ||
] | ||
Reflect.decorate(decorators, type.prototype, prop.name, void 0) | ||
} | ||
} | ||
return { success: true } | ||
} | ||
|
||
const normalizeEntityCache = new Map<Class, any>() | ||
|
||
export const normalizeEntity = useCache(normalizeEntityCache, normalizeEntityNoCache, x => x) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import { authorize, Class, entity } from "@plumier/core" | ||
import reflect, { noop, useCache } from "@plumier/reflect" | ||
import { getMetadataArgsStorage } from "typeorm" | ||
import { parse } from "acorn" | ||
|
||
function inverseSideParser(expr: ((t: any) => any)) { | ||
const node = parse(expr.toString(), { ecmaVersion: 2020 }) | ||
return getMemberExpression(node) | ||
} | ||
|
||
function getContent(node: any): any { | ||
switch (node.type) { | ||
case "Program": | ||
case "BlockStatement": | ||
return node.body[node.body.length - 1] | ||
case "ArrowFunctionExpression": | ||
return node.body | ||
case "ExpressionStatement": | ||
return node.expression | ||
case "ReturnStatement": | ||
return node.argument | ||
} | ||
} | ||
|
||
function getMemberExpression(node: any): string { | ||
const content = getContent(node) | ||
if (content.type === "MemberExpression") | ||
return content.property.name | ||
else | ||
return getMemberExpression(content) | ||
} | ||
|
||
|
||
function normalizeEntityNoCache(type: Class) { | ||
const parent: Class = Object.getPrototypeOf(type) | ||
// loop through parent entities | ||
if (!!parent.prototype) normalizeEntity(parent) | ||
const storage = getMetadataArgsStorage(); | ||
const columns = storage.filterColumns(type) | ||
for (const col of columns) { | ||
Reflect.decorate([noop()], (col.target as Function).prototype, col.propertyName, void 0) | ||
if (col.options.primary) | ||
Reflect.decorate([entity.primaryId(), authorize.readonly()], (col.target as Function).prototype, col.propertyName, void 0) | ||
} | ||
const relations = storage.filterRelations(type) | ||
for (const col of relations) { | ||
const rawType: Class = (col as any).type() | ||
if (col.relationType === "many-to-many" || col.relationType === "one-to-many") { | ||
const inverseProperty = inverseSideParser(col.inverseSideProperty as any) | ||
const decorators = [ | ||
reflect.type(x => [rawType]), | ||
entity.relation({ inverseProperty }), | ||
authorize.readonly(), | ||
authorize.writeonly() | ||
] | ||
Reflect.decorate(decorators, (col.target as Function).prototype, col.propertyName, void 0) | ||
} | ||
else { | ||
Reflect.decorate([reflect.type(x => rawType), entity.relation()], (col.target as Function).prototype, col.propertyName, void 0) | ||
} | ||
} | ||
return { success: true } | ||
} | ||
|
||
const normalizeEntityCache = new Map<Class, any>() | ||
|
||
export const normalizeEntity = useCache(normalizeEntityCache, normalizeEntityNoCache, x => x) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.