Skip to content

Commit

Permalink
revert: Revert relation property readonly/writeonly on first class en…
Browse files Browse the repository at this point in the history
…tity (#966)
  • Loading branch information
ktutnik committed Jun 10, 2021
1 parent 5ac4867 commit bac445e
Show file tree
Hide file tree
Showing 12 changed files with 430 additions and 329 deletions.
33 changes: 3 additions & 30 deletions packages/mongoose/src/facility.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,20 @@
import { Class, DefaultFacility, findClassRecursive, PlumierApplication, RelationDecorator } from "@plumier/core"
import { DefaultFacility, PlumierApplication, RelationDecorator } from "@plumier/core"
import { RequestHookMiddleware } from "@plumier/generic-controller"
import {
FilterQueryAuthorizeMiddleware,
OrderQueryAuthorizeMiddleware,
SelectQueryAuthorizeMiddleware,
} from "@plumier/query-parser"
import reflect from "@plumier/reflect"
import { Result, ResultMessages, VisitorInvocation } from "@plumier/validator"
import Mongoose from "mongoose"
import pluralize from "pluralize"

import { getModels, model as globalModel, MongooseHelper, proxy as globalProxy } from "./generator"
import { MongooseControllerGeneric, MongooseNestedControllerGeneric } from "./generic-controller"
import { normalizeEntity } from "./helper"
import { filterConverter, orderConverter, selectConverter } from "./query-parser"
import { ClassOptionDecorator } from "./types"


interface MongooseFacilityOption { uri?: string, helper?: MongooseHelper, entity?: Class | Class[] | string | string[] }
interface MongooseFacilityOption { uri?: string, helper?: MongooseHelper }

function convertValue(value: any, path: string): Result {
if (Array.isArray(value)) {
Expand All @@ -44,35 +41,11 @@ function relationConverter(i: VisitorInvocation): Result {
return i.proceed()
}

async function loadEntities(entity: Class | Class[] | string | string[], opt: { rootDir: string }): Promise<Class[]> {
const classes = await findClassRecursive(entity, { rootDir: opt.rootDir })
const result = []
for (const cls of classes) {
const meta = reflect(cls.type)
if(!!meta.decorators.find((x: ClassOptionDecorator) => x.name === "ClassOption"))
result.push(cls.type)
}
return result
}

export class MongooseFacility extends DefaultFacility {
option: MongooseFacilityOption
constructor(opts?: MongooseFacilityOption) {
super()
this.option = {
entity: [
require.main!.filename,
"./**/*controller.+(ts|js)",
"./**/*entity.+(ts|js)"
], ...opts
}
}

async preInitialize(app: Readonly<PlumierApplication>) {
const entities = await loadEntities(this.option.entity!, app.config)
for (const entity of entities) {
normalizeEntity(entity)
}
this.option = { ...opts }
}

setup(app: Readonly<PlumierApplication>) {
Expand Down
14 changes: 1 addition & 13 deletions packages/mongoose/src/generic-controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import {
import reflect, { generic } from "@plumier/reflect"
import { Context } from "koa"
import pluralize from "pluralize"
import { normalizeEntity } from "./helper"

import { MongooseNestedRepository, MongooseRepository } from "./repository"

Expand Down Expand Up @@ -77,18 +76,7 @@ function createGenericControllerMongoose(controllers?: GenericControllers) {
createGenericController(type, {
controllers: controllers ?? [MongooseControllerGeneric, MongooseNestedControllerGeneric],
nameConversion: pluralize,
config, normalize: type => {
if (Array.isArray(type)) {
const [parentEntity, relation] = type
normalizeEntity(parentEntity)
const meta = reflect(parentEntity)
const prop = meta.properties.find(x => x.name === relation)!
const entity: Class = Array.isArray(prop.type) ? prop.type[0] : prop.type
normalizeEntity(entity)
}
else
normalizeEntity(type)
}
config, normalize: type => {}
})
}

Expand Down
24 changes: 0 additions & 24 deletions packages/mongoose/src/helper.ts

This file was deleted.

1 change: 0 additions & 1 deletion packages/mongoose/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,4 @@ export { MongooseControllerGeneric, MongooseNestedControllerGeneric, GenericCont
export { MongooseRepository, MongooseNestedRepository } from "./repository"
export * from "./types"
export * from "./query-parser"
export * from "./helper"
export default model;
4 changes: 1 addition & 3 deletions packages/typeorm/src/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,7 @@ function normalizeEntityNoCache(type: Class) {
const inverseProperty = typeof inverse === "string" ? inverse : inverseSideParser(inverse)
const decorators = [
reflect.type(x => [rawType]),
entity.relation({ inverseProperty }),
authorize.readonly(),
authorize.writeonly()
entity.relation({ inverseProperty })
]
Reflect.decorate(decorators, target.prototype, col.propertyName, void 0)
}
Expand Down
1 change: 0 additions & 1 deletion tests/behavior/export/__snapshots__/export.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,6 @@ Object {
"getModels": bound getModels,
"model": bound model,
"models": Map {},
"normalizeEntity": ,
"orderConverter": ,
"proxy": bound proxy,
"selectConverter": ,
Expand Down
215 changes: 208 additions & 7 deletions tests/behavior/mongoose/__snapshots__/mongoose-generic.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -719,6 +719,214 @@ Object {
}
`;

exports[`CRUD Nested CRUD One to Many Function Should allow nested array relation accessed from parent 1`] = `
Object {
"animals": Array [
Object {
"id": "MONGODB UNIQUE ID",
"name": "Mimi",
},
Object {
"id": "MONGODB UNIQUE ID",
"name": "Mimi",
},
Object {
"id": "MONGODB UNIQUE ID",
"name": "Mimi",
},
Object {
"id": "MONGODB UNIQUE ID",
"name": "Mimi",
},
Object {
"id": "MONGODB UNIQUE ID",
"name": "Mimi",
},
Object {
"id": "MONGODB UNIQUE ID",
"name": "Mimi",
},
Object {
"id": "MONGODB UNIQUE ID",
"name": "Mimi",
},
Object {
"id": "MONGODB UNIQUE ID",
"name": "Mimi",
},
Object {
"id": "MONGODB UNIQUE ID",
"name": "Mimi",
},
Object {
"id": "MONGODB UNIQUE ID",
"name": "Mimi",
},
Object {
"id": "MONGODB UNIQUE ID",
"name": "Mimi",
},
Object {
"id": "MONGODB UNIQUE ID",
"name": "Mimi",
},
Object {
"id": "MONGODB UNIQUE ID",
"name": "Mimi",
},
Object {
"id": "MONGODB UNIQUE ID",
"name": "Mimi",
},
Object {
"id": "MONGODB UNIQUE ID",
"name": "Mimi",
},
Object {
"id": "MONGODB UNIQUE ID",
"name": "Mimi",
},
Object {
"id": "MONGODB UNIQUE ID",
"name": "Mimi",
},
Object {
"id": "MONGODB UNIQUE ID",
"name": "Mimi",
},
Object {
"id": "MONGODB UNIQUE ID",
"name": "Mimi",
},
Object {
"id": "MONGODB UNIQUE ID",
"name": "Mimi",
},
Object {
"id": "MONGODB UNIQUE ID",
"name": "Mimi",
},
Object {
"id": "MONGODB UNIQUE ID",
"name": "Mimi",
},
Object {
"id": "MONGODB UNIQUE ID",
"name": "Mimi",
},
Object {
"id": "MONGODB UNIQUE ID",
"name": "Mimi",
},
Object {
"id": "MONGODB UNIQUE ID",
"name": "Mimi",
},
Object {
"id": "MONGODB UNIQUE ID",
"name": "Mimi",
},
Object {
"id": "MONGODB UNIQUE ID",
"name": "Mimi",
},
Object {
"id": "MONGODB UNIQUE ID",
"name": "Mimi",
},
Object {
"id": "MONGODB UNIQUE ID",
"name": "Mimi",
},
Object {
"id": "MONGODB UNIQUE ID",
"name": "Mimi",
},
Object {
"id": "MONGODB UNIQUE ID",
"name": "Mimi",
},
Object {
"id": "MONGODB UNIQUE ID",
"name": "Mimi",
},
Object {
"id": "MONGODB UNIQUE ID",
"name": "Mimi",
},
Object {
"id": "MONGODB UNIQUE ID",
"name": "Mimi",
},
Object {
"id": "MONGODB UNIQUE ID",
"name": "Mimi",
},
Object {
"id": "MONGODB UNIQUE ID",
"name": "Mimi",
},
Object {
"id": "MONGODB UNIQUE ID",
"name": "Mimi",
},
Object {
"id": "MONGODB UNIQUE ID",
"name": "Mimi",
},
Object {
"id": "MONGODB UNIQUE ID",
"name": "Mimi",
},
Object {
"id": "MONGODB UNIQUE ID",
"name": "Mimi",
},
Object {
"id": "MONGODB UNIQUE ID",
"name": "Mimi",
},
Object {
"id": "MONGODB UNIQUE ID",
"name": "Mimi",
},
Object {
"id": "MONGODB UNIQUE ID",
"name": "Mimi",
},
Object {
"id": "MONGODB UNIQUE ID",
"name": "Mimi",
},
Object {
"id": "MONGODB UNIQUE ID",
"name": "Mimi",
},
Object {
"id": "MONGODB UNIQUE ID",
"name": "Mimi",
},
Object {
"id": "MONGODB UNIQUE ID",
"name": "Mimi",
},
Object {
"id": "MONGODB UNIQUE ID",
"name": "Mimi",
},
Object {
"id": "MONGODB UNIQUE ID",
"name": "Mimi",
},
Object {
"id": "MONGODB UNIQUE ID",
"name": "Mimi",
},
],
"id": "MONGODB UNIQUE ID",
}
`;

exports[`CRUD Nested CRUD One to Many Function Should filter with exact value GET /users/:parentId/animals?filter 1`] = `
Array [
Object {
Expand Down Expand Up @@ -746,13 +954,6 @@ Array [
]
`;

exports[`CRUD Nested CRUD One to Many Function Should not allow nested array relation accessed from parent 1`] = `
Object {
"message": "Unauthorized to access select properties animals",
"status": 403,
}
`;

exports[`CRUD Nested CRUD One to Many Function Should not confused on reverse properties with the same type POST /users/:parentId/animals 1`] = `
Object {
"createdBy": Object {
Expand Down

0 comments on commit bac445e

Please sign in to comment.