From 88f71accc771e6b830abe9d8fcfc4559a7c5b891 Mon Sep 17 00:00:00 2001 From: Jeongho Nam Date: Mon, 14 Mar 2022 20:48:35 +0900 Subject: [PATCH] Close #61 again --- package.json | 2 +- src/builders/JsonSelectBuilder.ts | 25 ++++++----- .../features/test_app_join_builder_filter.ts | 27 ++++-------- .../test_json_select_builder_filter.ts | 42 +++++++++++++++++++ 4 files changed, 66 insertions(+), 30 deletions(-) create mode 100644 src/test/features/test_json_select_builder_filter.ts diff --git a/package.json b/package.json index 26073ac..f336c7f 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "safe-typeorm", - "version": "1.0.8", + "version": "1.0.9", "description": "Safe Relationship Decorators for the TypeORM", "main": "lib/index.js", "typings": "lib/index.d.ts", diff --git a/src/builders/JsonSelectBuilder.ts b/src/builders/JsonSelectBuilder.ts index e15a07d..f994c88 100644 --- a/src/builders/JsonSelectBuilder.ts +++ b/src/builders/JsonSelectBuilder.ts @@ -16,6 +16,7 @@ import { Primitive } from "../typings/Primitive"; import { toPrimitive } from "../functional/toPrimitive"; import { DEFAULT } from "../DEFAULT"; +import { SpecialFields } from "../typings/SpecialFields"; /** * JSON Select Builder. @@ -53,20 +54,22 @@ export class JsonSelectBuilder< this.mine_ = mine; this.joiner_ = new AppJoinBuilder(this.mine_); - for (const [key, value] of Object.entries(input)) + for (const [label, data] of Object.entries(input)) { + const key: SpecialFields> = label as SpecialFields>; + const filter: ((stmt: orm.SelectQueryBuilder>) => void) | null = data instanceof Array ? data[1] : null; + const value: DEFAULT | "recursive" | "join" | JsonSelectBuilder = data instanceof Array ? data[0] : data; + if (value instanceof JsonSelectBuilder) - this.joiner_.set(key as any, value.joiner_); + if (filter !== null) + this.joiner_.set(key, filter, value.joiner_); + else + this.joiner_.set(key, value.joiner_); else if (value === "join") - this.joiner_.join(key as AppJoinBuilder.Key); - else if (value instanceof Array) - { - const [builder, filter] = value; - if (builder instanceof JsonSelectBuilder) - this.joiner_.set(key as any, filter, builder as any); - else if (builder === "join") - this.joiner_.join([key, filter] as any); - } + if (filter !== null) + this.joiner_.join([key, filter]); + else + this.joiner_.join(key); } } diff --git a/src/test/features/test_app_join_builder_filter.ts b/src/test/features/test_app_join_builder_filter.ts index 1beb50a..8035cf8 100644 --- a/src/test/features/test_app_join_builder_filter.ts +++ b/src/test/features/test_app_join_builder_filter.ts @@ -5,18 +5,9 @@ import { BbsArticle } from "../models/bbs/BbsArticle"; import { BbsGroup } from "../models/bbs/BbsGroup"; import { generate_random_clean_groups } from "../internal/generators/generate_random_clean_groups"; -async function test(groupList: BbsGroup[]): Promise -{ - for (const group of groupList) - { - const articles: BbsArticle[] = await group.articles.get(); - if (articles.length !== 1) - throw new Error("Bug on AppJoinBuilder.execute(): failed to filter."); - } -} - export async function test_app_join_builder_filter(): Promise { + // FILTERING const groupList: BbsGroup[] = await generate_random_clean_groups(); const articleList: BbsArticle[] = []; @@ -30,16 +21,16 @@ export async function test_app_join_builder_filter(): Promise stmt.andWhere(...BbsArticle.getWhereArguments("id", "IN", articleList)); }; - // TEST-APP-JOIN + // APP JOIN WITH FILTER const app = new safe.AppJoinBuilder(BbsGroup); app.join(["articles" as const, filter]); await app.execute(groupList); - await test(groupList); - const reloaded: BbsGroup[] = await BbsGroup.findByIds(groupList.map(g => g.id)); - const json = safe.createJsonSelectBuilder(BbsGroup, { - articles: ["join" as const, filter] - }); - await json.join(reloaded); - await test(reloaded); + // DO TEST + for (const group of groupList) + { + const articles: BbsArticle[] = await group.articles.get(); + if (articles.length !== 1) + throw new Error("Bug on AppJoinBuilder.execute(): failed to filter."); + } } \ No newline at end of file diff --git a/src/test/features/test_json_select_builder_filter.ts b/src/test/features/test_json_select_builder_filter.ts new file mode 100644 index 0000000..5a832b5 --- /dev/null +++ b/src/test/features/test_json_select_builder_filter.ts @@ -0,0 +1,42 @@ +import * as orm from "typeorm"; +import safe from "../.."; + +import { BbsArticle } from "../models/bbs/BbsArticle"; +import { BbsGroup } from "../models/bbs/BbsGroup"; +import { generate_random_clean_groups } from "../internal/generators/generate_random_clean_groups"; + +export async function test_json_select_builder_filter(): Promise +{ + // FILTERING + const groupList: BbsGroup[] = await generate_random_clean_groups(); + const articleList: BbsArticle[] = []; + + for (const group of groupList) + { + const articles: BbsArticle[] = await group.articles.get(); + articleList.push(articles[0]); + } + const filter = (stmt: orm.SelectQueryBuilder) => + { + stmt.andWhere(...BbsArticle.getWhereArguments("id", "IN", articleList)); + }; + + // JSON WITH FILTER + const json = safe.createJsonSelectBuilder + ( + BbsGroup, + { + articles: [ + safe.createJsonSelectBuilder + ( + BbsArticle, + {} + ), + filter + ] + } + ); + for (const { articles } of await json.getMany(groupList)) + if (articles.length !== 1) + throw new Error("Bug on JsonSelectBuilder.getMany(): failed to filter."); +} \ No newline at end of file