mirror of
https://github.com/shishantbiswas/bknd.git
synced 2026-03-16 04:27:21 +00:00
updated repo query schema, repo and withbuilder validation, and reworked relation with building
This commit is contained in:
@@ -1,8 +1,14 @@
|
||||
import { describe, expect, test } from "bun:test";
|
||||
import { Value } from "../../src/core/utils";
|
||||
import { WhereBuilder, type WhereQuery, querySchema } from "../../src/data";
|
||||
import { Value, _jsonp } from "../../src/core/utils";
|
||||
import { type RepoQuery, WhereBuilder, type WhereQuery, querySchema } from "../../src/data";
|
||||
import type { RepoQueryIn } from "../../src/data/server/data-query-impl";
|
||||
import { getDummyConnection } from "./helper";
|
||||
|
||||
const decode = (input: RepoQueryIn, expected: RepoQuery) => {
|
||||
const result = Value.Decode(querySchema, input);
|
||||
expect(result).toEqual(expected);
|
||||
};
|
||||
|
||||
describe("data-query-impl", () => {
|
||||
function qb() {
|
||||
const c = getDummyConnection();
|
||||
@@ -88,21 +94,47 @@ describe("data-query-impl", () => {
|
||||
expect(keys).toEqual(expectedKeys);
|
||||
}
|
||||
});
|
||||
|
||||
test("with", () => {
|
||||
decode({ with: ["posts"] }, { with: { posts: {} } });
|
||||
decode({ with: { posts: {} } }, { with: { posts: {} } });
|
||||
decode({ with: { posts: { limit: "1" } } }, { with: { posts: { limit: 1 } } });
|
||||
decode(
|
||||
{
|
||||
with: {
|
||||
posts: {
|
||||
with: {
|
||||
images: {
|
||||
select: "id"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
with: {
|
||||
posts: {
|
||||
with: {
|
||||
images: {
|
||||
select: ["id"]
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe("data-query-impl: Typebox", () => {
|
||||
test("sort", async () => {
|
||||
const decode = (input: any, expected: any) => {
|
||||
const result = Value.Decode(querySchema, input);
|
||||
expect(result.sort).toEqual(expected);
|
||||
};
|
||||
const _dflt = { by: "id", dir: "asc" };
|
||||
const _dflt = { sort: { by: "id", dir: "asc" } };
|
||||
|
||||
decode({ sort: "" }, _dflt);
|
||||
decode({ sort: "name" }, { by: "name", dir: "asc" });
|
||||
decode({ sort: "-name" }, { by: "name", dir: "desc" });
|
||||
decode({ sort: "-posts.name" }, { by: "posts.name", dir: "desc" });
|
||||
decode({ sort: "name" }, { sort: { by: "name", dir: "asc" } });
|
||||
decode({ sort: "-name" }, { sort: { by: "name", dir: "desc" } });
|
||||
decode({ sort: "-posts.name" }, { sort: { by: "posts.name", dir: "desc" } });
|
||||
decode({ sort: "-1name" }, _dflt);
|
||||
decode({ sort: { by: "name", dir: "desc" } }, { by: "name", dir: "desc" });
|
||||
decode({ sort: { by: "name", dir: "desc" } }, { sort: { by: "name", dir: "desc" } });
|
||||
});
|
||||
});
|
||||
|
||||
@@ -119,12 +119,9 @@ describe("Relations", async () => {
|
||||
- select: users.*
|
||||
- cardinality: 1
|
||||
*/
|
||||
const selectPostsFromUsers = postAuthorRel.buildWith(
|
||||
users,
|
||||
kysely.selectFrom(users.name),
|
||||
jsonFrom,
|
||||
"posts"
|
||||
);
|
||||
const selectPostsFromUsers = kysely
|
||||
.selectFrom(users.name)
|
||||
.select((eb) => postAuthorRel.buildWith(users, "posts")(eb).as("posts"));
|
||||
expect(selectPostsFromUsers.compile().sql).toBe(
|
||||
'select (select "posts"."id" as "id", "posts"."title" as "title", "posts"."author_id" as "author_id" from "posts" as "posts" where "posts"."author_id" = "users"."id" limit ?) as "posts" from "users"'
|
||||
);
|
||||
@@ -141,12 +138,9 @@ describe("Relations", async () => {
|
||||
- select: posts.*
|
||||
- cardinality:
|
||||
*/
|
||||
const selectUsersFromPosts = postAuthorRel.buildWith(
|
||||
posts,
|
||||
kysely.selectFrom(posts.name),
|
||||
jsonFrom,
|
||||
"author"
|
||||
);
|
||||
const selectUsersFromPosts = kysely
|
||||
.selectFrom(posts.name)
|
||||
.select((eb) => postAuthorRel.buildWith(posts, "author")(eb).as("author"));
|
||||
|
||||
expect(selectUsersFromPosts.compile().sql).toBe(
|
||||
'select (select "author"."id" as "id", "author"."username" as "username" from "users" as "author" where "author"."id" = "posts"."author_id" limit ?) as "author" from "posts"'
|
||||
@@ -315,20 +309,16 @@ describe("Relations", async () => {
|
||||
- select: users.*
|
||||
- cardinality: 1
|
||||
*/
|
||||
const selectCategoriesFromPosts = postCategoriesRel.buildWith(
|
||||
posts,
|
||||
kysely.selectFrom(posts.name),
|
||||
jsonFrom
|
||||
);
|
||||
const selectCategoriesFromPosts = kysely
|
||||
.selectFrom(posts.name)
|
||||
.select((eb) => postCategoriesRel.buildWith(posts)(eb).as("categories"));
|
||||
expect(selectCategoriesFromPosts.compile().sql).toBe(
|
||||
'select (select "categories"."id" as "id", "categories"."label" as "label" from "categories" inner join "posts_categories" on "categories"."id" = "posts_categories"."categories_id" where "posts"."id" = "posts_categories"."posts_id" limit ?) as "categories" from "posts"'
|
||||
);
|
||||
|
||||
const selectPostsFromCategories = postCategoriesRel.buildWith(
|
||||
categories,
|
||||
kysely.selectFrom(categories.name),
|
||||
jsonFrom
|
||||
);
|
||||
const selectPostsFromCategories = kysely
|
||||
.selectFrom(categories.name)
|
||||
.select((eb) => postCategoriesRel.buildWith(categories)(eb).as("posts"));
|
||||
expect(selectPostsFromCategories.compile().sql).toBe(
|
||||
'select (select "posts"."id" as "id", "posts"."title" as "title" from "posts" inner join "posts_categories" on "posts"."id" = "posts_categories"."posts_id" where "categories"."id" = "posts_categories"."categories_id" limit ?) as "posts" from "categories"'
|
||||
);
|
||||
|
||||
@@ -8,19 +8,60 @@ import {
|
||||
TextField,
|
||||
WithBuilder
|
||||
} from "../../../src/data";
|
||||
import * as proto from "../../../src/data/prototype";
|
||||
import { getDummyConnection } from "../helper";
|
||||
|
||||
const { dummyConnection, afterAllCleanup } = getDummyConnection();
|
||||
afterAll(afterAllCleanup);
|
||||
|
||||
function schemaToEm(s: ReturnType<(typeof proto)["em"]>): EntityManager<any> {
|
||||
return new EntityManager(Object.values(s.entities), dummyConnection, s.relations, s.indices);
|
||||
}
|
||||
|
||||
describe("[data] WithBuilder", async () => {
|
||||
test("validate withs", async () => {
|
||||
const schema = proto.em(
|
||||
{
|
||||
posts: proto.entity("posts", {}),
|
||||
users: proto.entity("users", {}),
|
||||
media: proto.entity("media", {})
|
||||
},
|
||||
({ relation }, { posts, users, media }) => {
|
||||
relation(posts).manyToOne(users);
|
||||
relation(users).polyToOne(media, { mappedBy: "avatar" });
|
||||
}
|
||||
);
|
||||
const em = schemaToEm(schema);
|
||||
|
||||
expect(WithBuilder.validateWiths(em, "posts", undefined)).toBe(0);
|
||||
expect(WithBuilder.validateWiths(em, "posts", {})).toBe(0);
|
||||
expect(WithBuilder.validateWiths(em, "posts", { users: {} })).toBe(1);
|
||||
expect(
|
||||
WithBuilder.validateWiths(em, "posts", {
|
||||
users: {
|
||||
with: { avatar: {} }
|
||||
}
|
||||
})
|
||||
).toBe(2);
|
||||
expect(() => WithBuilder.validateWiths(em, "posts", { author: {} })).toThrow();
|
||||
expect(() =>
|
||||
WithBuilder.validateWiths(em, "posts", {
|
||||
users: {
|
||||
with: { glibberish: {} }
|
||||
}
|
||||
})
|
||||
).toThrow();
|
||||
});
|
||||
|
||||
test("missing relation", async () => {
|
||||
const users = new Entity("users", [new TextField("username")]);
|
||||
const em = new EntityManager([users], dummyConnection);
|
||||
|
||||
expect(() =>
|
||||
WithBuilder.addClause(em, em.connection.kysely.selectFrom("users"), users, ["posts"])
|
||||
).toThrow('Relation "posts" not found');
|
||||
WithBuilder.addClause(em, em.connection.kysely.selectFrom("users"), users, {
|
||||
posts: {}
|
||||
})
|
||||
).toThrow('Relation "users<>posts" not found');
|
||||
});
|
||||
|
||||
test("addClause: ManyToOne", async () => {
|
||||
@@ -29,9 +70,9 @@ describe("[data] WithBuilder", async () => {
|
||||
const relations = [new ManyToOneRelation(posts, users, { mappedBy: "author" })];
|
||||
const em = new EntityManager([users, posts], dummyConnection, relations);
|
||||
|
||||
const qb = WithBuilder.addClause(em, em.connection.kysely.selectFrom("users"), users, [
|
||||
"posts"
|
||||
]);
|
||||
const qb = WithBuilder.addClause(em, em.connection.kysely.selectFrom("users"), users, {
|
||||
posts: {}
|
||||
});
|
||||
|
||||
const res = qb.compile();
|
||||
|
||||
@@ -44,7 +85,9 @@ describe("[data] WithBuilder", async () => {
|
||||
em,
|
||||
em.connection.kysely.selectFrom("posts"),
|
||||
posts, // @todo: try with "users", it gives output!
|
||||
["author"]
|
||||
{
|
||||
author: {}
|
||||
}
|
||||
);
|
||||
|
||||
const res2 = qb2.compile();
|
||||
@@ -56,9 +99,10 @@ describe("[data] WithBuilder", async () => {
|
||||
});
|
||||
|
||||
test("test with empty join", async () => {
|
||||
const em = new EntityManager([], dummyConnection);
|
||||
const qb = { qb: 1 } as any;
|
||||
|
||||
expect(WithBuilder.addClause(null as any, qb, null as any, [])).toBe(qb);
|
||||
expect(WithBuilder.addClause(em, qb, null as any, {})).toBe(qb);
|
||||
});
|
||||
|
||||
test("test manytomany", async () => {
|
||||
@@ -89,7 +133,7 @@ describe("[data] WithBuilder", async () => {
|
||||
|
||||
//console.log((await em.repository().findMany("posts_categories")).result);
|
||||
|
||||
const res = await em.repository(posts).findMany({ with: ["categories"] });
|
||||
const res = await em.repository(posts).findMany({ with: { categories: {} } });
|
||||
|
||||
expect(res.data).toEqual([
|
||||
{
|
||||
@@ -107,7 +151,7 @@ describe("[data] WithBuilder", async () => {
|
||||
}
|
||||
]);
|
||||
|
||||
const res2 = await em.repository(categories).findMany({ with: ["posts"] });
|
||||
const res2 = await em.repository(categories).findMany({ with: { posts: {} } });
|
||||
|
||||
//console.log(res2.sql, res2.data);
|
||||
|
||||
@@ -150,7 +194,7 @@ describe("[data] WithBuilder", async () => {
|
||||
em,
|
||||
em.connection.kysely.selectFrom("categories"),
|
||||
categories,
|
||||
["single"]
|
||||
{ single: {} }
|
||||
);
|
||||
const res = qb.compile();
|
||||
expect(res.sql).toBe(
|
||||
@@ -162,7 +206,7 @@ describe("[data] WithBuilder", async () => {
|
||||
em,
|
||||
em.connection.kysely.selectFrom("categories"),
|
||||
categories,
|
||||
["multiple"]
|
||||
{ multiple: {} }
|
||||
);
|
||||
const res2 = qb2.compile();
|
||||
expect(res2.sql).toBe(
|
||||
|
||||
Reference in New Issue
Block a user