Skip to content
This repository has been archived by the owner on May 18, 2024. It is now read-only.

Commit

Permalink
Merge branch 'v1.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
samchon committed Mar 14, 2022
2 parents 53476ac + 88f71ac commit 0403e1f
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 30 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
25 changes: 14 additions & 11 deletions src/builders/JsonSelectBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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<Mine, Relationship<any>> = label as SpecialFields<Mine, Relationship<any>>;
const filter: ((stmt: orm.SelectQueryBuilder<Relationship.TargetType<Mine, any>>) => void) | null = data instanceof Array ? data[1] : null;
const value: DEFAULT | "recursive" | "join" | JsonSelectBuilder<any, any, any> = 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<Mine>);
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);
}
}

Expand Down
27 changes: 9 additions & 18 deletions src/test/features/test_app_join_builder_filter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<void>
{
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<void>
{
// FILTERING
const groupList: BbsGroup[] = await generate_random_clean_groups();
const articleList: BbsArticle[] = [];

Expand All @@ -30,16 +21,16 @@ export async function test_app_join_builder_filter(): Promise<void>
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.");
}
}
42 changes: 42 additions & 0 deletions src/test/features/test_json_select_builder_filter.ts
Original file line number Diff line number Diff line change
@@ -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<void>
{
// 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<BbsArticle>) =>
{
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.");
}

0 comments on commit 0403e1f

Please sign in to comment.