mirror of
https://github.com/shishantbiswas/bknd.git
synced 2026-03-17 12:56:05 +00:00
added format command and added trailing commas to reduce conflicts
This commit is contained in:
@@ -9,7 +9,7 @@ import {
|
||||
ManyToOneRelation,
|
||||
type MutatorResponse,
|
||||
type RepositoryResponse,
|
||||
TextField
|
||||
TextField,
|
||||
} from "../../src/data";
|
||||
import { DataController } from "../../src/data/api/DataController";
|
||||
import { dataConfigSchema } from "../../src/data/data-schema";
|
||||
@@ -35,17 +35,17 @@ describe("[data] DataController", async () => {
|
||||
meta: {
|
||||
total: 0,
|
||||
count: 0,
|
||||
items: 0
|
||||
}
|
||||
items: 0,
|
||||
},
|
||||
});
|
||||
|
||||
expect(res).toEqual({
|
||||
meta: {
|
||||
total: 0,
|
||||
count: 0,
|
||||
items: 0
|
||||
items: 0,
|
||||
},
|
||||
data: []
|
||||
data: [],
|
||||
});
|
||||
});
|
||||
|
||||
@@ -59,22 +59,22 @@ describe("[data] DataController", async () => {
|
||||
data: [] as any,
|
||||
sql: "",
|
||||
parameters: [] as any,
|
||||
result: [] as any
|
||||
result: [] as any,
|
||||
});
|
||||
|
||||
expect(res).toEqual({
|
||||
data: []
|
||||
data: [],
|
||||
});
|
||||
});
|
||||
|
||||
describe("getController", async () => {
|
||||
const users = new Entity("users", [
|
||||
new TextField("name", { required: true }),
|
||||
new TextField("bio")
|
||||
new TextField("bio"),
|
||||
]);
|
||||
const posts = new Entity("posts", [new TextField("content")]);
|
||||
const em = new EntityManager([users, posts], dummyConnection, [
|
||||
new ManyToOneRelation(posts, users)
|
||||
new ManyToOneRelation(posts, users),
|
||||
]);
|
||||
|
||||
await em.schema().sync({ force: true });
|
||||
@@ -83,12 +83,12 @@ describe("[data] DataController", async () => {
|
||||
users: [
|
||||
{ name: "foo", bio: "bar" },
|
||||
{ name: "bar", bio: null },
|
||||
{ name: "baz", bio: "!!!" }
|
||||
{ name: "baz", bio: "!!!" },
|
||||
],
|
||||
posts: [
|
||||
{ content: "post 1", users_id: 1 },
|
||||
{ content: "post 2", users_id: 2 }
|
||||
]
|
||||
{ content: "post 2", users_id: 2 },
|
||||
],
|
||||
};
|
||||
|
||||
const ctx: any = { em, guard: new Guard() };
|
||||
@@ -118,7 +118,7 @@ describe("[data] DataController", async () => {
|
||||
for await (const _user of fixtures.users) {
|
||||
const res = await app.request("/entity/users", {
|
||||
method: "POST",
|
||||
body: JSON.stringify(_user)
|
||||
body: JSON.stringify(_user),
|
||||
});
|
||||
//console.log("res", { _user }, res);
|
||||
const result = (await res.json()) as MutatorResponse;
|
||||
@@ -133,7 +133,7 @@ describe("[data] DataController", async () => {
|
||||
for await (const _post of fixtures.posts) {
|
||||
const res = await app.request("/entity/posts", {
|
||||
method: "POST",
|
||||
body: JSON.stringify(_post)
|
||||
body: JSON.stringify(_post),
|
||||
});
|
||||
const result = (await res.json()) as MutatorResponse;
|
||||
const { id, ...data } = result.data as any;
|
||||
@@ -159,11 +159,11 @@ describe("[data] DataController", async () => {
|
||||
const res = await app.request("/entity/users/query", {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json"
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
body: JSON.stringify({
|
||||
where: { bio: { $isnull: 1 } }
|
||||
})
|
||||
where: { bio: { $isnull: 1 } },
|
||||
}),
|
||||
});
|
||||
const data = (await res.json()) as RepositoryResponse;
|
||||
|
||||
@@ -199,7 +199,7 @@ describe("[data] DataController", async () => {
|
||||
test("/:entity (update one)", async () => {
|
||||
const res = await app.request("/entity/users/3", {
|
||||
method: "PATCH",
|
||||
body: JSON.stringify({ name: "new name" })
|
||||
body: JSON.stringify({ name: "new name" }),
|
||||
});
|
||||
const { data } = (await res.json()) as MutatorResponse;
|
||||
|
||||
@@ -221,7 +221,7 @@ describe("[data] DataController", async () => {
|
||||
|
||||
test("/:entity/:id (delete one)", async () => {
|
||||
const res = await app.request("/entity/posts/2", {
|
||||
method: "DELETE"
|
||||
method: "DELETE",
|
||||
});
|
||||
const { data } = (await res.json()) as RepositoryResponse<EntityData>;
|
||||
expect(data).toEqual({ id: 2, ...fixtures.posts[1] });
|
||||
|
||||
@@ -30,7 +30,7 @@ describe("data-query-impl", () => {
|
||||
[{ val: { $isnull: 0 } }, '"val" is not null', []],
|
||||
[{ val: { $isnull: false } }, '"val" is not null', []],
|
||||
[{ val: { $like: "what" } }, '"val" like ?', ["what"]],
|
||||
[{ val: { $like: "w*t" } }, '"val" like ?', ["w%t"]]
|
||||
[{ val: { $like: "w*t" } }, '"val" like ?', ["w%t"]],
|
||||
];
|
||||
|
||||
for (const [query, expectedSql, expectedParams] of tests) {
|
||||
@@ -51,22 +51,22 @@ describe("data-query-impl", () => {
|
||||
[
|
||||
{ val1: { $eq: "foo" }, val2: { $eq: "bar" } },
|
||||
'("val1" = ? and "val2" = ?)',
|
||||
["foo", "bar"]
|
||||
["foo", "bar"],
|
||||
],
|
||||
[
|
||||
{ val1: { $eq: "foo" }, val2: { $eq: "bar" } },
|
||||
'("val1" = ? and "val2" = ?)',
|
||||
["foo", "bar"]
|
||||
["foo", "bar"],
|
||||
],
|
||||
|
||||
// or constructs
|
||||
[
|
||||
{ $or: { val1: { $eq: "foo" }, val2: { $eq: "bar" } } },
|
||||
'("val1" = ? or "val2" = ?)',
|
||||
["foo", "bar"]
|
||||
["foo", "bar"],
|
||||
],
|
||||
[{ val1: { $eq: 1 }, $or: { val1: { $eq: 2 } } }, '("val1" = ? or "val1" = ?)', [1, 2]],
|
||||
[{ val1: { $eq: 1 }, $or: { val1: { $eq: 2 } } }, '("val1" = ? or "val1" = ?)', [1, 2]]
|
||||
[{ val1: { $eq: 1 }, $or: { val1: { $eq: 2 } } }, '("val1" = ? or "val1" = ?)', [1, 2]],
|
||||
];
|
||||
|
||||
for (const [query, expectedSql, expectedParams] of tests) {
|
||||
@@ -86,7 +86,7 @@ describe("data-query-impl", () => {
|
||||
|
||||
// or constructs
|
||||
[{ $or: { val1: { $eq: "foo" }, val2: { $eq: "bar" } } }, ["val1", "val2"]],
|
||||
[{ val1: { $eq: 1 }, $or: { val1: { $eq: 2 } } }, ["val1"]]
|
||||
[{ val1: { $eq: 1 }, $or: { val1: { $eq: 2 } } }, ["val1"]],
|
||||
];
|
||||
|
||||
for (const [query, expectedKeys] of tests) {
|
||||
@@ -105,23 +105,23 @@ describe("data-query-impl", () => {
|
||||
posts: {
|
||||
with: {
|
||||
images: {
|
||||
select: ["id"]
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
select: ["id"],
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
with: {
|
||||
posts: {
|
||||
with: {
|
||||
images: {
|
||||
select: ["id"]
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
select: ["id"],
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
);
|
||||
|
||||
// over http
|
||||
|
||||
@@ -5,7 +5,7 @@ import {
|
||||
NumberField,
|
||||
PrimaryField,
|
||||
Repository,
|
||||
TextField
|
||||
TextField,
|
||||
} from "../../src/data";
|
||||
import { getDummyConnection } from "./helper";
|
||||
|
||||
@@ -18,14 +18,14 @@ describe("some tests", async () => {
|
||||
|
||||
const users = new Entity("users", [
|
||||
new TextField("username", { required: true, default_value: "nobody" }),
|
||||
new TextField("email", { maxLength: 3 })
|
||||
new TextField("email", { maxLength: 3 }),
|
||||
]);
|
||||
|
||||
const posts = new Entity("posts", [
|
||||
new TextField("title"),
|
||||
new TextField("content"),
|
||||
new TextField("created_at"),
|
||||
new NumberField("likes", { default_value: 0 })
|
||||
new NumberField("likes", { default_value: 0 }),
|
||||
]);
|
||||
|
||||
const em = new EntityManager([users, posts], connection);
|
||||
@@ -43,7 +43,7 @@ describe("some tests", async () => {
|
||||
});*/
|
||||
|
||||
expect(query.sql).toBe(
|
||||
'select "users"."id" as "id", "users"."username" as "username", "users"."email" as "email" from "users" where "id" = ? limit ?'
|
||||
'select "users"."id" as "id", "users"."username" as "username", "users"."email" as "email" from "users" where "id" = ? limit ?',
|
||||
);
|
||||
expect(query.parameters).toEqual([1, 1]);
|
||||
expect(query.result).toEqual([]);
|
||||
@@ -53,7 +53,7 @@ describe("some tests", async () => {
|
||||
const query = await em.repository(users).findMany();
|
||||
|
||||
expect(query.sql).toBe(
|
||||
'select "users"."id" as "id", "users"."username" as "username", "users"."email" as "email" from "users" order by "users"."id" asc limit ? offset ?'
|
||||
'select "users"."id" as "id", "users"."username" as "username", "users"."email" as "email" from "users" order by "users"."id" asc limit ? offset ?',
|
||||
);
|
||||
expect(query.parameters).toEqual([10, 0]);
|
||||
expect(query.result).toEqual([]);
|
||||
@@ -63,7 +63,7 @@ describe("some tests", async () => {
|
||||
const query = await em.repository(posts).findMany();
|
||||
|
||||
expect(query.sql).toBe(
|
||||
'select "posts"."id" as "id", "posts"."title" as "title", "posts"."content" as "content", "posts"."created_at" as "created_at", "posts"."likes" as "likes" from "posts" order by "posts"."id" asc limit ? offset ?'
|
||||
'select "posts"."id" as "id", "posts"."title" as "title", "posts"."content" as "content", "posts"."created_at" as "created_at", "posts"."likes" as "likes" from "posts" order by "posts"."id" asc limit ? offset ?',
|
||||
);
|
||||
expect(query.parameters).toEqual([10, 0]);
|
||||
expect(query.result).toEqual([]);
|
||||
@@ -74,7 +74,7 @@ describe("some tests", async () => {
|
||||
new Entity("users", [
|
||||
new TextField("username"),
|
||||
new TextField("email"),
|
||||
new TextField("email") // not throwing, it's just being ignored
|
||||
new TextField("email"), // not throwing, it's just being ignored
|
||||
]);
|
||||
}).toBeDefined();
|
||||
|
||||
@@ -83,7 +83,7 @@ describe("some tests", async () => {
|
||||
new TextField("username"),
|
||||
new TextField("email"),
|
||||
// field config differs, will throw
|
||||
new TextField("email", { required: true })
|
||||
new TextField("email", { required: true }),
|
||||
]);
|
||||
}).toThrow();
|
||||
|
||||
@@ -91,7 +91,7 @@ describe("some tests", async () => {
|
||||
new Entity("users", [
|
||||
new PrimaryField(),
|
||||
new TextField("username"),
|
||||
new TextField("email")
|
||||
new TextField("email"),
|
||||
]);
|
||||
}).toBeDefined();
|
||||
});
|
||||
|
||||
@@ -16,7 +16,7 @@ export function getDummyDatabase(memory: boolean = true): {
|
||||
afterAllCleanup: async () => {
|
||||
if (!memory) await unlink(DB_NAME);
|
||||
return true;
|
||||
}
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
@@ -26,7 +26,7 @@ export function getDummyConnection(memory: boolean = true) {
|
||||
|
||||
return {
|
||||
dummyConnection,
|
||||
afterAllCleanup
|
||||
afterAllCleanup,
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -6,7 +6,7 @@ import {
|
||||
ManyToOneRelation,
|
||||
NumberField,
|
||||
SchemaManager,
|
||||
TextField
|
||||
TextField,
|
||||
} from "../../src/data";
|
||||
import { getDummyConnection } from "./helper";
|
||||
|
||||
@@ -21,7 +21,7 @@ describe("Mutator relation", async () => {
|
||||
const posts = new Entity("posts", [
|
||||
new TextField("title"),
|
||||
new TextField("content", { default_value: "..." }),
|
||||
new NumberField("count", { default_value: 0 })
|
||||
new NumberField("count", { default_value: 0 }),
|
||||
]);
|
||||
|
||||
const users = new Entity("users", [new TextField("username")]);
|
||||
@@ -44,7 +44,7 @@ describe("Mutator relation", async () => {
|
||||
expect(em.mutator(posts).insertOne({ title: "post2", users_id: 10 })).rejects.toThrow();
|
||||
|
||||
expect(
|
||||
em.mutator(posts).insertOne({ title: "post2", users_id: data.id })
|
||||
em.mutator(posts).insertOne({ title: "post2", users_id: data.id }),
|
||||
).resolves.toBeDefined();
|
||||
});
|
||||
});
|
||||
|
||||
@@ -14,7 +14,7 @@ describe("Mutator simple", async () => {
|
||||
|
||||
const items = new Entity("items", [
|
||||
new TextField("label", { required: true, minLength: 1 }),
|
||||
new NumberField("count", { default_value: 0 })
|
||||
new NumberField("count", { default_value: 0 }),
|
||||
]);
|
||||
const em = new EntityManager<any>([items], connection);
|
||||
|
||||
@@ -29,11 +29,11 @@ describe("Mutator simple", async () => {
|
||||
test("insert single row", async () => {
|
||||
const mutation = await em.mutator(items).insertOne({
|
||||
label: "test",
|
||||
count: 1
|
||||
count: 1,
|
||||
});
|
||||
|
||||
expect(mutation.sql).toBe(
|
||||
'insert into "items" ("count", "label") values (?, ?) returning "id", "label", "count"'
|
||||
'insert into "items" ("count", "label") values (?, ?) returning "id", "label", "count"',
|
||||
);
|
||||
expect(mutation.data).toEqual({ id: 1, label: "test", count: 1 });
|
||||
|
||||
@@ -41,8 +41,8 @@ describe("Mutator simple", async () => {
|
||||
limit: 1,
|
||||
sort: {
|
||||
by: "id",
|
||||
dir: "desc"
|
||||
}
|
||||
dir: "desc",
|
||||
},
|
||||
});
|
||||
|
||||
expect(query.result).toEqual([{ id: 1, label: "test", count: 1 }]);
|
||||
@@ -53,18 +53,18 @@ describe("Mutator simple", async () => {
|
||||
limit: 1,
|
||||
sort: {
|
||||
by: "id",
|
||||
dir: "desc"
|
||||
}
|
||||
dir: "desc",
|
||||
},
|
||||
});
|
||||
const id = query.data![0].id as number;
|
||||
|
||||
const mutation = await em.mutator(items).updateOne(id, {
|
||||
label: "new label",
|
||||
count: 100
|
||||
count: 100,
|
||||
});
|
||||
|
||||
expect(mutation.sql).toBe(
|
||||
'update "items" set "label" = ?, "count" = ? where "id" = ? returning "id", "label", "count"'
|
||||
'update "items" set "label" = ?, "count" = ? where "id" = ? returning "id", "label", "count"',
|
||||
);
|
||||
expect(mutation.data).toEqual({ id, label: "new label", count: 100 });
|
||||
});
|
||||
@@ -74,15 +74,15 @@ describe("Mutator simple", async () => {
|
||||
limit: 1,
|
||||
sort: {
|
||||
by: "id",
|
||||
dir: "desc"
|
||||
}
|
||||
dir: "desc",
|
||||
},
|
||||
});
|
||||
|
||||
const id = query.data![0].id as number;
|
||||
const mutation = await em.mutator(items).deleteOne(id);
|
||||
|
||||
expect(mutation.sql).toBe(
|
||||
'delete from "items" where "id" = ? returning "id", "label", "count"'
|
||||
'delete from "items" where "id" = ? returning "id", "label", "count"',
|
||||
);
|
||||
expect(mutation.data).toEqual({ id, label: "new label", count: 100 });
|
||||
|
||||
@@ -94,7 +94,7 @@ describe("Mutator simple", async () => {
|
||||
const incompleteCreate = async () =>
|
||||
await em.mutator(items).insertOne({
|
||||
//label: "test",
|
||||
count: 1
|
||||
count: 1,
|
||||
});
|
||||
|
||||
expect(incompleteCreate()).rejects.toThrow();
|
||||
@@ -104,7 +104,7 @@ describe("Mutator simple", async () => {
|
||||
const invalidCreate1 = async () =>
|
||||
await em.mutator(items).insertOne({
|
||||
label: 111, // this should work
|
||||
count: "1" // this should fail
|
||||
count: "1", // this should fail
|
||||
});
|
||||
|
||||
expect(invalidCreate1()).rejects.toThrow(TransformPersistFailedException);
|
||||
@@ -112,7 +112,7 @@ describe("Mutator simple", async () => {
|
||||
const invalidCreate2 = async () =>
|
||||
await em.mutator(items).insertOne({
|
||||
label: "", // this should fail
|
||||
count: 1
|
||||
count: 1,
|
||||
});
|
||||
|
||||
expect(invalidCreate2()).rejects.toThrow(TransformPersistFailedException);
|
||||
@@ -152,27 +152,27 @@ describe("Mutator simple", async () => {
|
||||
await em.mutator(items).updateWhere(
|
||||
{ count: 2 },
|
||||
{
|
||||
count: 10
|
||||
}
|
||||
count: 10,
|
||||
},
|
||||
);
|
||||
expect((await em.repository(items).findMany()).data).toEqual([
|
||||
{ id: 6, label: "update", count: 1 },
|
||||
{ id: 7, label: "update too", count: 1 },
|
||||
{ id: 8, label: "keep", count: 0 }
|
||||
{ id: 8, label: "keep", count: 0 },
|
||||
]);
|
||||
|
||||
// expect 2 to be updated
|
||||
await em.mutator(items).updateWhere(
|
||||
{ count: 2 },
|
||||
{
|
||||
count: 1
|
||||
}
|
||||
count: 1,
|
||||
},
|
||||
);
|
||||
|
||||
expect((await em.repository(items).findMany()).data).toEqual([
|
||||
{ id: 6, label: "update", count: 2 },
|
||||
{ id: 7, label: "update too", count: 2 },
|
||||
{ id: 8, label: "keep", count: 0 }
|
||||
{ id: 8, label: "keep", count: 0 },
|
||||
]);
|
||||
});
|
||||
|
||||
|
||||
@@ -23,23 +23,23 @@ describe("Polymorphic", async () => {
|
||||
source: "categories",
|
||||
target: "media",
|
||||
config: {
|
||||
mappedBy: "image"
|
||||
}
|
||||
mappedBy: "image",
|
||||
},
|
||||
});
|
||||
// media should not see categories
|
||||
expect(em.relationsOf(media.name).map((r) => r.toJSON())).toEqual([]);
|
||||
|
||||
// it's important that media cannot access categories
|
||||
expect(em.relations.targetRelationsOf(categories).map((r) => r.source.entity.name)).toEqual(
|
||||
[]
|
||||
[],
|
||||
);
|
||||
expect(em.relations.targetRelationsOf(media).map((r) => r.source.entity.name)).toEqual([]);
|
||||
|
||||
expect(em.relations.sourceRelationsOf(categories).map((r) => r.target.entity.name)).toEqual([
|
||||
"media"
|
||||
"media",
|
||||
]);
|
||||
expect(em.relations.sourceRelationsOf(categories).map((r) => r.target.reference)).toEqual([
|
||||
"image"
|
||||
"image",
|
||||
]);
|
||||
expect(em.relations.sourceRelationsOf(media).map((r) => r.target.entity.name)).toEqual([]);
|
||||
|
||||
@@ -48,7 +48,7 @@ describe("Polymorphic", async () => {
|
||||
"id",
|
||||
"path",
|
||||
"reference",
|
||||
"entity_id"
|
||||
"entity_id",
|
||||
]);
|
||||
expect(media.getSelect()).toEqual(["id", "path"]);
|
||||
});
|
||||
@@ -60,7 +60,7 @@ describe("Polymorphic", async () => {
|
||||
const entities = [media, categories];
|
||||
const single = new PolymorphicRelation(categories, media, {
|
||||
mappedBy: "single",
|
||||
targetCardinality: 1
|
||||
targetCardinality: 1,
|
||||
});
|
||||
const multiple = new PolymorphicRelation(categories, media, { mappedBy: "multiple" });
|
||||
|
||||
@@ -71,17 +71,17 @@ describe("Polymorphic", async () => {
|
||||
|
||||
// it's important that media cannot access categories
|
||||
expect(em.relations.targetRelationsOf(categories).map((r) => r.source.entity.name)).toEqual(
|
||||
[]
|
||||
[],
|
||||
);
|
||||
expect(em.relations.targetRelationsOf(media).map((r) => r.source.entity.name)).toEqual([]);
|
||||
|
||||
expect(em.relations.sourceRelationsOf(categories).map((r) => r.target.entity.name)).toEqual([
|
||||
"media",
|
||||
"media"
|
||||
"media",
|
||||
]);
|
||||
expect(em.relations.sourceRelationsOf(categories).map((r) => r.target.reference)).toEqual([
|
||||
"single",
|
||||
"multiple"
|
||||
"multiple",
|
||||
]);
|
||||
expect(em.relations.sourceRelationsOf(media).map((r) => r.target.entity.name)).toEqual([]);
|
||||
|
||||
@@ -90,7 +90,7 @@ describe("Polymorphic", async () => {
|
||||
"id",
|
||||
"path",
|
||||
"reference",
|
||||
"entity_id"
|
||||
"entity_id",
|
||||
]);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -12,7 +12,7 @@ import {
|
||||
NumberField,
|
||||
OneToOneRelation,
|
||||
PolymorphicRelation,
|
||||
TextField
|
||||
TextField,
|
||||
} from "../../src/data";
|
||||
import { DummyConnection } from "../../src/data/connection/DummyConnection";
|
||||
import {
|
||||
@@ -31,7 +31,7 @@ import {
|
||||
medium,
|
||||
number,
|
||||
relation,
|
||||
text
|
||||
text,
|
||||
} from "../../src/data/prototype";
|
||||
import { MediaField } from "../../src/media/MediaField";
|
||||
|
||||
@@ -54,7 +54,7 @@ describe("prototype", () => {
|
||||
name: text(),
|
||||
bio: text(),
|
||||
age: number(),
|
||||
some: number()
|
||||
some: number(),
|
||||
});
|
||||
type db = {
|
||||
users: Schema<typeof users>;
|
||||
@@ -70,7 +70,7 @@ describe("prototype", () => {
|
||||
name: text({ default_value: "hello" }).required(),
|
||||
bio: text(),
|
||||
age: number(),
|
||||
some: number().required()
|
||||
some: number().required(),
|
||||
});
|
||||
|
||||
const obj: InsertSchema<typeof user> = { name: "yo", some: 1 };
|
||||
@@ -83,12 +83,12 @@ describe("prototype", () => {
|
||||
new TextField("title", { required: true }),
|
||||
new TextField("content"),
|
||||
new DateField("created_at", {
|
||||
type: "datetime"
|
||||
type: "datetime",
|
||||
}),
|
||||
// @ts-ignore
|
||||
new MediaField("images", { entity: "posts" }),
|
||||
// @ts-ignore
|
||||
new MediaField("cover", { entity: "posts", max_items: 1 })
|
||||
new MediaField("cover", { entity: "posts", max_items: 1 }),
|
||||
]);
|
||||
|
||||
const posts2 = entity("posts", {
|
||||
@@ -96,7 +96,7 @@ describe("prototype", () => {
|
||||
content: text(),
|
||||
created_at: datetime(),
|
||||
images: media(),
|
||||
cover: medium()
|
||||
cover: medium(),
|
||||
});
|
||||
|
||||
type Posts = Schema<typeof posts2>;
|
||||
@@ -117,11 +117,11 @@ describe("prototype", () => {
|
||||
type: "objects",
|
||||
values: [
|
||||
{ value: "active", label: "Active" },
|
||||
{ value: "inactive", label: "Not active" }
|
||||
]
|
||||
}
|
||||
{ value: "inactive", label: "Not active" },
|
||||
],
|
||||
},
|
||||
}),
|
||||
new JsonField("json")
|
||||
new JsonField("json"),
|
||||
]);
|
||||
|
||||
const test2 = entity("test", {
|
||||
@@ -134,10 +134,10 @@ describe("prototype", () => {
|
||||
status: enumm<"active" | "inactive">({
|
||||
enum: [
|
||||
{ value: "active", label: "Active" },
|
||||
{ value: "inactive", label: "Not active" }
|
||||
]
|
||||
{ value: "inactive", label: "Not active" },
|
||||
],
|
||||
}),
|
||||
json: json<{ some: number }>()
|
||||
json: json<{ some: number }>(),
|
||||
});
|
||||
|
||||
expect(test.toJSON()).toEqual(test2.toJSON());
|
||||
@@ -161,12 +161,12 @@ describe("prototype", () => {
|
||||
// category has single image
|
||||
new PolymorphicRelation(categories, _media, {
|
||||
mappedBy: "image",
|
||||
targetCardinality: 1
|
||||
targetCardinality: 1,
|
||||
}),
|
||||
|
||||
// post has multiple images
|
||||
new PolymorphicRelation(posts, _media, { mappedBy: "images" }),
|
||||
new PolymorphicRelation(posts, _media, { mappedBy: "cover", targetCardinality: 1 })
|
||||
new PolymorphicRelation(posts, _media, { mappedBy: "cover", targetCardinality: 1 }),
|
||||
];
|
||||
|
||||
const relations2 = [
|
||||
@@ -180,7 +180,7 @@ describe("prototype", () => {
|
||||
relation(categories).polyToOne(_media, { mappedBy: "image" }),
|
||||
|
||||
relation(posts).polyToMany(_media, { mappedBy: "images" }),
|
||||
relation(posts).polyToOne(_media, { mappedBy: "cover" })
|
||||
relation(posts).polyToOne(_media, { mappedBy: "cover" }),
|
||||
];
|
||||
|
||||
expect(relations.map((r) => r.toJSON())).toEqual(relations2.map((r) => r.toJSON()));
|
||||
@@ -194,21 +194,21 @@ describe("prototype", () => {
|
||||
posts,
|
||||
categories,
|
||||
{
|
||||
connectionTableMappedName: "custom"
|
||||
connectionTableMappedName: "custom",
|
||||
},
|
||||
[new TextField("description")]
|
||||
[new TextField("description")],
|
||||
);
|
||||
|
||||
const fields = {
|
||||
description: text()
|
||||
description: text(),
|
||||
};
|
||||
let o: FieldSchema<typeof fields>;
|
||||
const rel2 = relation(posts).manyToMany(
|
||||
categories,
|
||||
{
|
||||
connectionTableMappedName: "custom"
|
||||
connectionTableMappedName: "custom",
|
||||
},
|
||||
fields
|
||||
fields,
|
||||
);
|
||||
|
||||
expect(rel.toJSON()).toEqual(rel2.toJSON());
|
||||
@@ -216,11 +216,11 @@ describe("prototype", () => {
|
||||
|
||||
test("devexample", async () => {
|
||||
const users = entity("users", {
|
||||
username: text()
|
||||
username: text(),
|
||||
});
|
||||
|
||||
const comments = entity("comments", {
|
||||
content: text()
|
||||
content: text(),
|
||||
});
|
||||
|
||||
const posts = entity("posts", {
|
||||
@@ -228,17 +228,17 @@ describe("prototype", () => {
|
||||
content: text(),
|
||||
created_at: datetime(),
|
||||
images: media(),
|
||||
cover: medium()
|
||||
cover: medium(),
|
||||
});
|
||||
|
||||
const categories = entity("categories", {
|
||||
name: text(),
|
||||
description: text(),
|
||||
image: medium()
|
||||
image: medium(),
|
||||
});
|
||||
|
||||
const settings = entity("settings", {
|
||||
theme: text()
|
||||
theme: text(),
|
||||
});
|
||||
|
||||
const test = entity("test", {
|
||||
@@ -251,10 +251,10 @@ describe("prototype", () => {
|
||||
status: enumm<"active" | "inactive">({
|
||||
enum: [
|
||||
{ value: "active", label: "Active" },
|
||||
{ value: "inactive", label: "Not active" }
|
||||
]
|
||||
{ value: "inactive", label: "Not active" },
|
||||
],
|
||||
}),
|
||||
json: json<{ some: number }>()
|
||||
json: json<{ some: number }>(),
|
||||
});
|
||||
|
||||
const _media = entity("media", {});
|
||||
@@ -270,7 +270,7 @@ describe("prototype", () => {
|
||||
relation(users).oneToOne(settings),
|
||||
|
||||
relation(comments).manyToOne(users, { required: true }),
|
||||
relation(comments).manyToOne(posts, { required: true })
|
||||
relation(comments).manyToOne(posts, { required: true }),
|
||||
];
|
||||
|
||||
const obj: Schema<typeof test> = {} as any;
|
||||
@@ -281,12 +281,12 @@ describe("prototype", () => {
|
||||
{
|
||||
posts: entity("posts", { name: text(), slug: text().required() }),
|
||||
comments: entity("comments", { some: text() }),
|
||||
users: entity("users", { email: text() })
|
||||
users: entity("users", { email: text() }),
|
||||
},
|
||||
({ relation, index }, { posts, comments, users }) => {
|
||||
relation(posts).manyToOne(comments).manyToOne(users);
|
||||
index(posts).on(["name"]).on(["slug"], true);
|
||||
}
|
||||
},
|
||||
);
|
||||
|
||||
type LocalDb = (typeof _em)["DB"];
|
||||
@@ -294,7 +294,7 @@ describe("prototype", () => {
|
||||
const es = [
|
||||
new Entity("posts", [new TextField("name"), new TextField("slug", { required: true })]),
|
||||
new Entity("comments", [new TextField("some")]),
|
||||
new Entity("users", [new TextField("email")])
|
||||
new Entity("users", [new TextField("email")]),
|
||||
];
|
||||
const _em2 = new EntityManager(
|
||||
es,
|
||||
@@ -302,8 +302,8 @@ describe("prototype", () => {
|
||||
[new ManyToOneRelation(es[0], es[1]), new ManyToOneRelation(es[0], es[2])],
|
||||
[
|
||||
new EntityIndex(es[0], [es[0].field("name")!]),
|
||||
new EntityIndex(es[0], [es[0].field("slug")!], true)
|
||||
]
|
||||
new EntityIndex(es[0], [es[0].field("slug")!], true),
|
||||
],
|
||||
);
|
||||
|
||||
// @ts-ignore
|
||||
|
||||
@@ -6,7 +6,7 @@ import {
|
||||
ManyToOneRelation,
|
||||
OneToOneRelation,
|
||||
PolymorphicRelation,
|
||||
RelationField
|
||||
RelationField,
|
||||
} from "../../src/data/relations";
|
||||
import { getDummyConnection } from "./helper";
|
||||
|
||||
@@ -22,7 +22,7 @@ describe("Relations", async () => {
|
||||
const r1 = new RelationField("users_id", {
|
||||
reference: "users",
|
||||
target: "users",
|
||||
target_field: "id"
|
||||
target_field: "id",
|
||||
});
|
||||
|
||||
const sql1 = schema
|
||||
@@ -31,14 +31,14 @@ describe("Relations", async () => {
|
||||
.compile().sql;
|
||||
|
||||
expect(sql1).toBe(
|
||||
'create table "posts" ("users_id" integer references "users" ("id") on delete set null)'
|
||||
'create table "posts" ("users_id" integer references "users" ("id") on delete set null)',
|
||||
);
|
||||
|
||||
//const r2 = new RelationField(new Entity("users"), "author");
|
||||
const r2 = new RelationField("author_id", {
|
||||
reference: "author",
|
||||
target: "users",
|
||||
target_field: "id"
|
||||
target_field: "id",
|
||||
});
|
||||
|
||||
const sql2 = schema
|
||||
@@ -47,7 +47,7 @@ describe("Relations", async () => {
|
||||
.compile().sql;
|
||||
|
||||
expect(sql2).toBe(
|
||||
'create table "posts" ("author_id" integer references "users" ("id") on delete set null)'
|
||||
'create table "posts" ("author_id" integer references "users" ("id") on delete set null)',
|
||||
);
|
||||
});
|
||||
|
||||
@@ -57,7 +57,7 @@ describe("Relations", async () => {
|
||||
reference: "users",
|
||||
target: "users",
|
||||
target_field: "id",
|
||||
required: true
|
||||
required: true,
|
||||
});
|
||||
expect(r1.isRequired()).toBeTrue();
|
||||
});
|
||||
@@ -66,8 +66,8 @@ describe("Relations", async () => {
|
||||
const users = new Entity("users", [new TextField("username")]);
|
||||
const posts = new Entity("posts", [
|
||||
new TextField("title", {
|
||||
maxLength: 2
|
||||
})
|
||||
maxLength: 2,
|
||||
}),
|
||||
]);
|
||||
|
||||
const entities = [users, posts];
|
||||
@@ -122,7 +122,7 @@ describe("Relations", async () => {
|
||||
.selectFrom(users.name)
|
||||
.select((eb) => postAuthorRel.buildWith(users, "posts")(eb).as("posts"));
|
||||
expect(selectPostsFromUsers.compile().sql).toBe(
|
||||
'select (select from "posts" as "posts" where "posts"."author_id" = "users"."id") as "posts" from "users"'
|
||||
'select (select from "posts" as "posts" where "posts"."author_id" = "users"."id") as "posts" from "users"',
|
||||
);
|
||||
expect(postAuthorRel!.getField()).toBeInstanceOf(RelationField);
|
||||
const userObj = { id: 1, username: "test" };
|
||||
@@ -142,7 +142,7 @@ describe("Relations", async () => {
|
||||
.select((eb) => postAuthorRel.buildWith(posts, "author")(eb).as("author"));
|
||||
|
||||
expect(selectUsersFromPosts.compile().sql).toBe(
|
||||
'select (select from "users" as "author" where "author"."id" = "posts"."author_id" limit ?) as "author" from "posts"'
|
||||
'select (select from "users" as "author" where "author"."id" = "posts"."author_id" limit ?) as "author" from "posts"',
|
||||
);
|
||||
expect(postAuthorRel.getField()).toBeInstanceOf(RelationField);
|
||||
const postObj = { id: 1, title: "test" };
|
||||
@@ -158,7 +158,7 @@ describe("Relations", async () => {
|
||||
$detach: false,
|
||||
primary: undefined,
|
||||
cardinality: undefined,
|
||||
relation_type: "n:1"
|
||||
relation_type: "n:1",
|
||||
});
|
||||
|
||||
expect(postAuthorRel!.helper(posts.name)!.getMutationInfo()).toEqual({
|
||||
@@ -170,7 +170,7 @@ describe("Relations", async () => {
|
||||
$detach: false,
|
||||
primary: "id",
|
||||
cardinality: 1,
|
||||
relation_type: "n:1"
|
||||
relation_type: "n:1",
|
||||
});
|
||||
|
||||
/*console.log("ManyToOne (source=posts, target=users)");
|
||||
@@ -225,7 +225,7 @@ describe("Relations", async () => {
|
||||
$detach: false,
|
||||
primary: "id",
|
||||
cardinality: 1,
|
||||
relation_type: "1:1"
|
||||
relation_type: "1:1",
|
||||
});
|
||||
expect(userSettingRel!.helper(settings.name)!.getMutationInfo()).toEqual({
|
||||
reference: "users",
|
||||
@@ -236,7 +236,7 @@ describe("Relations", async () => {
|
||||
$detach: false,
|
||||
primary: undefined,
|
||||
cardinality: 1,
|
||||
relation_type: "1:1"
|
||||
relation_type: "1:1",
|
||||
});
|
||||
|
||||
/*console.log("");
|
||||
@@ -312,14 +312,14 @@ describe("Relations", async () => {
|
||||
.selectFrom(posts.name)
|
||||
.select((eb) => postCategoriesRel.buildWith(posts)(eb).select("id").as("categories"));
|
||||
expect(selectCategoriesFromPosts.compile().sql).toBe(
|
||||
'select (select "id" 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"'
|
||||
'select (select "id" 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 = kysely
|
||||
.selectFrom(categories.name)
|
||||
.select((eb) => postCategoriesRel.buildWith(categories)(eb).select("id").as("posts"));
|
||||
expect(selectPostsFromCategories.compile().sql).toBe(
|
||||
'select (select "id" 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"'
|
||||
'select (select "id" 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"',
|
||||
);
|
||||
|
||||
// mutation info
|
||||
@@ -332,7 +332,7 @@ describe("Relations", async () => {
|
||||
$detach: true,
|
||||
primary: "id",
|
||||
cardinality: undefined,
|
||||
relation_type: "m:n"
|
||||
relation_type: "m:n",
|
||||
});
|
||||
expect(relations[0].helper(categories.name)!.getMutationInfo()).toEqual({
|
||||
reference: "posts",
|
||||
@@ -343,7 +343,7 @@ describe("Relations", async () => {
|
||||
$detach: false,
|
||||
primary: undefined,
|
||||
cardinality: undefined,
|
||||
relation_type: "m:n"
|
||||
relation_type: "m:n",
|
||||
});
|
||||
|
||||
/*console.log("");
|
||||
|
||||
@@ -6,7 +6,7 @@ describe("[data] Entity", async () => {
|
||||
new TextField("name", { required: true }),
|
||||
new TextField("description"),
|
||||
new NumberField("age", { fillable: false, default_value: 18 }),
|
||||
new TextField("hidden", { hidden: true, default_value: "secret" })
|
||||
new TextField("hidden", { hidden: true, default_value: "secret" }),
|
||||
]);
|
||||
|
||||
test("getSelect", async () => {
|
||||
@@ -17,7 +17,7 @@ describe("[data] Entity", async () => {
|
||||
expect(entity.getFillableFields().map((f) => f.name)).toEqual([
|
||||
"name",
|
||||
"description",
|
||||
"hidden"
|
||||
"hidden",
|
||||
]);
|
||||
});
|
||||
|
||||
@@ -28,7 +28,7 @@ describe("[data] Entity", async () => {
|
||||
test("getDefaultObject", async () => {
|
||||
expect(entity.getDefaultObject()).toEqual({
|
||||
age: 18,
|
||||
hidden: "secret"
|
||||
hidden: "secret",
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
@@ -4,7 +4,7 @@ import {
|
||||
EntityManager,
|
||||
ManyToManyRelation,
|
||||
ManyToOneRelation,
|
||||
SchemaManager
|
||||
SchemaManager,
|
||||
} from "../../../src/data";
|
||||
import { UnableToConnectException } from "../../../src/data/errors";
|
||||
import { getDummyConnection } from "../helper";
|
||||
@@ -25,7 +25,7 @@ describe("[data] EntityManager", async () => {
|
||||
expect(await em.ping()).toBe(true);
|
||||
expect(() => em.entity("...")).toThrow();
|
||||
expect(() =>
|
||||
em.addRelation(new ManyToOneRelation(new Entity("1"), new Entity("2")))
|
||||
em.addRelation(new ManyToOneRelation(new Entity("1"), new Entity("2"))),
|
||||
).toThrow();
|
||||
expect(em.schema()).toBeInstanceOf(SchemaManager);
|
||||
|
||||
@@ -98,7 +98,7 @@ describe("[data] EntityManager", async () => {
|
||||
expect(userTargetRel.map((r) => r.other(users).entity.name)).toEqual(["posts", "comments"]);
|
||||
expect(postTargetRel.map((r) => r.other(posts).entity.name)).toEqual([
|
||||
"comments",
|
||||
"categories"
|
||||
"categories",
|
||||
]);
|
||||
expect(commentTargetRel.map((r) => r.other(comments).entity.name)).toEqual([]);
|
||||
expect(categoriesTargetRel.map((r) => r.other(categories).entity.name)).toEqual(["posts"]);
|
||||
|
||||
@@ -12,7 +12,7 @@ describe("[data] JoinBuilder", async () => {
|
||||
const em = new EntityManager([users], dummyConnection);
|
||||
|
||||
expect(() =>
|
||||
JoinBuilder.addClause(em, em.connection.kysely.selectFrom("users"), users, ["posts"])
|
||||
JoinBuilder.addClause(em, em.connection.kysely.selectFrom("users"), users, ["posts"]),
|
||||
).toThrow('Relation "posts" not found');
|
||||
});
|
||||
|
||||
@@ -23,7 +23,7 @@ describe("[data] JoinBuilder", async () => {
|
||||
const em = new EntityManager([users, posts], dummyConnection, relations);
|
||||
|
||||
const qb = JoinBuilder.addClause(em, em.connection.kysely.selectFrom("users"), users, [
|
||||
"posts"
|
||||
"posts",
|
||||
]);
|
||||
|
||||
const res = qb.compile();
|
||||
@@ -34,7 +34,7 @@ describe("[data] JoinBuilder", async () => {
|
||||
);*/
|
||||
|
||||
const qb2 = JoinBuilder.addClause(em, em.connection.kysely.selectFrom("posts"), posts, [
|
||||
"author"
|
||||
"author",
|
||||
]);
|
||||
|
||||
const res2 = qb2.compile();
|
||||
|
||||
@@ -9,7 +9,7 @@ import {
|
||||
OneToOneRelation,
|
||||
type RelationField,
|
||||
RelationMutator,
|
||||
TextField
|
||||
TextField,
|
||||
} from "../../../src/data";
|
||||
import * as proto from "../../../src/data/prototype";
|
||||
import { getDummyConnection } from "../helper";
|
||||
@@ -22,7 +22,7 @@ describe("[data] Mutator (base)", async () => {
|
||||
new TextField("label", { required: true }),
|
||||
new NumberField("count"),
|
||||
new TextField("hidden", { hidden: true }),
|
||||
new TextField("not_fillable", { fillable: false })
|
||||
new TextField("not_fillable", { fillable: false }),
|
||||
]);
|
||||
const em = new EntityManager<any>([entity], dummyConnection);
|
||||
await em.schema().sync({ force: true });
|
||||
@@ -44,7 +44,7 @@ describe("[data] Mutator (base)", async () => {
|
||||
test("updateOne", async () => {
|
||||
const { data } = await em.mutator(entity).insertOne(payload);
|
||||
const updated = await em.mutator(entity).updateOne(data.id, {
|
||||
count: 2
|
||||
count: 2,
|
||||
});
|
||||
|
||||
expect(updated.parameters).toEqual([2, data.id]);
|
||||
@@ -77,7 +77,7 @@ describe("[data] Mutator (ManyToOne)", async () => {
|
||||
|
||||
// persisting relational field should just return key value to be added
|
||||
expect(
|
||||
postRelMutator.persistRelationField(postRelField, "users_id", userData.data.id)
|
||||
postRelMutator.persistRelationField(postRelField, "users_id", userData.data.id),
|
||||
).resolves.toEqual(["users_id", userData.data.id]);
|
||||
|
||||
// persisting invalid value should throw
|
||||
@@ -86,8 +86,8 @@ describe("[data] Mutator (ManyToOne)", async () => {
|
||||
// persisting reference should ...
|
||||
expect(
|
||||
postRelMutator.persistReference(relations[0]!, "users", {
|
||||
$set: { id: userData.data.id }
|
||||
})
|
||||
$set: { id: userData.data.id },
|
||||
}),
|
||||
).resolves.toEqual(["users_id", userData.data.id]);
|
||||
// @todo: add what methods are allowed to relation, like $create should not be allowed for post<>users
|
||||
|
||||
@@ -99,8 +99,8 @@ describe("[data] Mutator (ManyToOne)", async () => {
|
||||
expect(
|
||||
em.mutator(posts).insertOne({
|
||||
title: "post1",
|
||||
users_id: 100 // user does not exist yet
|
||||
})
|
||||
users_id: 100, // user does not exist yet
|
||||
}),
|
||||
).rejects.toThrow();
|
||||
});
|
||||
|
||||
@@ -111,7 +111,7 @@ describe("[data] Mutator (ManyToOne)", async () => {
|
||||
const em = new EntityManager([items, cats], dummyConnection, relations);
|
||||
|
||||
expect(em.mutator(items).insertOne({ label: "test" })).rejects.toThrow(
|
||||
'Field "cats_id" is required'
|
||||
'Field "cats_id" is required',
|
||||
);
|
||||
});
|
||||
|
||||
@@ -119,14 +119,14 @@ describe("[data] Mutator (ManyToOne)", async () => {
|
||||
const { data } = await em.mutator(users).insertOne({ username: "user1" });
|
||||
const res = await em.mutator(posts).insertOne({
|
||||
title: "post1",
|
||||
users_id: data.id
|
||||
users_id: data.id,
|
||||
});
|
||||
expect(res.data.users_id).toBe(data.id);
|
||||
|
||||
// setting "null" should be allowed
|
||||
const res2 = await em.mutator(posts).insertOne({
|
||||
title: "post1",
|
||||
users_id: null
|
||||
users_id: null,
|
||||
});
|
||||
expect(res2.data.users_id).toBe(null);
|
||||
});
|
||||
@@ -135,14 +135,14 @@ describe("[data] Mutator (ManyToOne)", async () => {
|
||||
const { data } = await em.mutator(users).insertOne({ username: "user1" });
|
||||
const res = await em.mutator(posts).insertOne({
|
||||
title: "post1",
|
||||
users: { $set: { id: data.id } }
|
||||
users: { $set: { id: data.id } },
|
||||
});
|
||||
expect(res.data.users_id).toBe(data.id);
|
||||
|
||||
// setting "null" should be allowed
|
||||
const res2 = await em.mutator(posts).insertOne({
|
||||
title: "post1",
|
||||
users: { $set: { id: null } }
|
||||
users: { $set: { id: null } },
|
||||
});
|
||||
expect(res2.data.users_id).toBe(null);
|
||||
});
|
||||
@@ -151,8 +151,8 @@ describe("[data] Mutator (ManyToOne)", async () => {
|
||||
expect(
|
||||
em.mutator(posts).insertOne({
|
||||
title: "test",
|
||||
users: { $create: { username: "test" } }
|
||||
})
|
||||
users: { $create: { username: "test" } },
|
||||
}),
|
||||
).rejects.toThrow();
|
||||
});
|
||||
|
||||
@@ -162,27 +162,27 @@ describe("[data] Mutator (ManyToOne)", async () => {
|
||||
const res2 = await em.mutator(posts).insertOne({ title: "post1" });
|
||||
|
||||
const up1 = await em.mutator(posts).updateOne(res2.data.id, {
|
||||
users: { $set: { id: res1.data.id } }
|
||||
users: { $set: { id: res1.data.id } },
|
||||
});
|
||||
expect(up1.data.users_id).toBe(res1.data.id);
|
||||
|
||||
const up2 = await em.mutator(posts).updateOne(res2.data.id, {
|
||||
users: { $set: { id: res1_1.data.id } }
|
||||
users: { $set: { id: res1_1.data.id } },
|
||||
});
|
||||
expect(up2.data.users_id).toBe(res1_1.data.id);
|
||||
|
||||
const up3_1 = await em.mutator(posts).updateOne(res2.data.id, {
|
||||
users_id: res1.data.id
|
||||
users_id: res1.data.id,
|
||||
});
|
||||
expect(up3_1.data.users_id).toBe(res1.data.id);
|
||||
|
||||
const up3_2 = await em.mutator(posts).updateOne(res2.data.id, {
|
||||
users_id: res1_1.data.id
|
||||
users_id: res1_1.data.id,
|
||||
});
|
||||
expect(up3_2.data.users_id).toBe(res1_1.data.id);
|
||||
|
||||
const up4 = await em.mutator(posts).updateOne(res2.data.id, {
|
||||
users_id: null
|
||||
users_id: null,
|
||||
});
|
||||
expect(up4.data.users_id).toBe(null);
|
||||
});
|
||||
@@ -199,8 +199,8 @@ describe("[data] Mutator (OneToOne)", async () => {
|
||||
expect(
|
||||
em.mutator(users).insertOne({
|
||||
username: "test",
|
||||
settings_id: 1 // todo: throws because it doesn't exist, but it shouldn't be allowed
|
||||
})
|
||||
settings_id: 1, // todo: throws because it doesn't exist, but it shouldn't be allowed
|
||||
}),
|
||||
).rejects.toThrow();
|
||||
});
|
||||
|
||||
@@ -210,15 +210,15 @@ describe("[data] Mutator (OneToOne)", async () => {
|
||||
expect(
|
||||
em.mutator(users).insertOne({
|
||||
username: "test",
|
||||
settings: { $set: { id: data.id } }
|
||||
})
|
||||
settings: { $set: { id: data.id } },
|
||||
}),
|
||||
).rejects.toThrow();
|
||||
});
|
||||
|
||||
test("insertOne: using $create", async () => {
|
||||
const res = await em.mutator(users).insertOne({
|
||||
username: "test",
|
||||
settings: { $create: { theme: "dark" } }
|
||||
settings: { $create: { theme: "dark" } },
|
||||
});
|
||||
expect(res.data.settings_id).toBeDefined();
|
||||
});
|
||||
@@ -303,7 +303,7 @@ describe("[data] Mutator (Events)", async () => {
|
||||
test("insertOne event return is respected", async () => {
|
||||
const posts = proto.entity("posts", {
|
||||
title: proto.text(),
|
||||
views: proto.number()
|
||||
views: proto.number(),
|
||||
});
|
||||
|
||||
const conn = getDummyConnection();
|
||||
@@ -318,10 +318,10 @@ describe("[data] Mutator (Events)", async () => {
|
||||
async (event) => {
|
||||
return {
|
||||
...event.params.data,
|
||||
views: 2
|
||||
views: 2,
|
||||
};
|
||||
},
|
||||
"sync"
|
||||
"sync",
|
||||
);
|
||||
|
||||
const mutator = em.mutator("posts");
|
||||
@@ -329,14 +329,14 @@ describe("[data] Mutator (Events)", async () => {
|
||||
expect(result.data).toEqual({
|
||||
id: 1,
|
||||
title: "test",
|
||||
views: 2
|
||||
views: 2,
|
||||
});
|
||||
});
|
||||
|
||||
test("updateOne event return is respected", async () => {
|
||||
const posts = proto.entity("posts", {
|
||||
title: proto.text(),
|
||||
views: proto.number()
|
||||
views: proto.number(),
|
||||
});
|
||||
|
||||
const conn = getDummyConnection();
|
||||
@@ -351,10 +351,10 @@ describe("[data] Mutator (Events)", async () => {
|
||||
async (event) => {
|
||||
return {
|
||||
...event.params.data,
|
||||
views: event.params.data.views + 1
|
||||
views: event.params.data.views + 1,
|
||||
};
|
||||
},
|
||||
"sync"
|
||||
"sync",
|
||||
);
|
||||
|
||||
const mutator = em.mutator("posts");
|
||||
@@ -363,7 +363,7 @@ describe("[data] Mutator (Events)", async () => {
|
||||
expect(result.data).toEqual({
|
||||
id: 1,
|
||||
title: "test",
|
||||
views: 3
|
||||
views: 3,
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -7,7 +7,7 @@ import {
|
||||
LibsqlConnection,
|
||||
ManyToOneRelation,
|
||||
RepositoryEvents,
|
||||
TextField
|
||||
TextField,
|
||||
} from "../../../src/data";
|
||||
import { getDummyConnection } from "../helper";
|
||||
|
||||
@@ -70,13 +70,13 @@ describe("[Repository]", async () => {
|
||||
const q1 = selectQ(conn).compile();
|
||||
const res = await client.execute({
|
||||
sql: q1.sql,
|
||||
args: q1.parameters as any
|
||||
args: q1.parameters as any,
|
||||
});
|
||||
|
||||
const q2 = countQ(conn).compile();
|
||||
const count = await client.execute({
|
||||
sql: q2.sql,
|
||||
args: q2.parameters as any
|
||||
args: q2.parameters as any,
|
||||
});
|
||||
return [res, count];
|
||||
}
|
||||
@@ -93,7 +93,7 @@ describe("[Repository]", async () => {
|
||||
const exec = async (
|
||||
name: string,
|
||||
fn: (em: EntityManager<any>) => Promise<any>,
|
||||
em: EntityManager<any>
|
||||
em: EntityManager<any>,
|
||||
) => {
|
||||
const res = await Perf.execute(() => fn(em), times);
|
||||
await sleep(1000);
|
||||
@@ -102,7 +102,7 @@ describe("[Repository]", async () => {
|
||||
total: res.total.toFixed(2),
|
||||
avg: (res.total / times).toFixed(2),
|
||||
first: res.marks[0].time.toFixed(2),
|
||||
last: res.marks[res.marks.length - 1].time.toFixed(2)
|
||||
last: res.marks[res.marks.length - 1].time.toFixed(2),
|
||||
};
|
||||
console.log(info.name, info, res.marks);
|
||||
return info;
|
||||
@@ -183,7 +183,7 @@ describe("[data] Repository (Events)", async () => {
|
||||
const items = new Entity("items", [new TextField("label")]);
|
||||
const categories = new Entity("categories", [new TextField("label")]);
|
||||
const em = new EntityManager([items, categories], dummyConnection, [
|
||||
new ManyToOneRelation(categories, items)
|
||||
new ManyToOneRelation(categories, items),
|
||||
]);
|
||||
await em.schema().sync({ force: true });
|
||||
const events = new Map<string, any>();
|
||||
|
||||
@@ -26,7 +26,7 @@ describe("SchemaManager tests", async () => {
|
||||
isNullable: true,
|
||||
isAutoIncrementing: true,
|
||||
hasDefaultValue: false,
|
||||
comment: undefined
|
||||
comment: undefined,
|
||||
},
|
||||
{
|
||||
name: "username",
|
||||
@@ -34,7 +34,7 @@ describe("SchemaManager tests", async () => {
|
||||
isNullable: true,
|
||||
isAutoIncrementing: false,
|
||||
hasDefaultValue: false,
|
||||
comment: undefined
|
||||
comment: undefined,
|
||||
},
|
||||
{
|
||||
name: "email",
|
||||
@@ -42,7 +42,7 @@ describe("SchemaManager tests", async () => {
|
||||
isNullable: true,
|
||||
isAutoIncrementing: false,
|
||||
hasDefaultValue: false,
|
||||
comment: undefined
|
||||
comment: undefined,
|
||||
},
|
||||
{
|
||||
name: "bio",
|
||||
@@ -50,8 +50,8 @@ describe("SchemaManager tests", async () => {
|
||||
isNullable: true,
|
||||
isAutoIncrementing: false,
|
||||
hasDefaultValue: false,
|
||||
comment: undefined
|
||||
}
|
||||
comment: undefined,
|
||||
},
|
||||
],
|
||||
indices: [
|
||||
{
|
||||
@@ -61,11 +61,11 @@ describe("SchemaManager tests", async () => {
|
||||
columns: [
|
||||
{
|
||||
name: "email",
|
||||
order: 0
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
order: 0,
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
});
|
||||
});
|
||||
|
||||
@@ -77,10 +77,10 @@ describe("SchemaManager tests", async () => {
|
||||
new Entity(table, [
|
||||
new TextField("username"),
|
||||
new TextField("email"),
|
||||
new TextField("bio")
|
||||
])
|
||||
new TextField("bio"),
|
||||
]),
|
||||
],
|
||||
dummyConnection
|
||||
dummyConnection,
|
||||
);
|
||||
const kysely = em.connection.kysely;
|
||||
|
||||
@@ -101,8 +101,8 @@ describe("SchemaManager tests", async () => {
|
||||
name: table,
|
||||
isNew: false,
|
||||
columns: { add: ["bio"], drop: [], change: [] },
|
||||
indices: { add: [], drop: [index] }
|
||||
}
|
||||
indices: { add: [], drop: [index] },
|
||||
},
|
||||
]);
|
||||
|
||||
// now sync
|
||||
@@ -119,7 +119,7 @@ describe("SchemaManager tests", async () => {
|
||||
const table = "drop_column";
|
||||
const em = new EntityManager(
|
||||
[new Entity(table, [new TextField("username")])],
|
||||
dummyConnection
|
||||
dummyConnection,
|
||||
);
|
||||
const kysely = em.connection.kysely;
|
||||
|
||||
@@ -141,10 +141,10 @@ describe("SchemaManager tests", async () => {
|
||||
columns: {
|
||||
add: [],
|
||||
drop: ["email"],
|
||||
change: []
|
||||
change: [],
|
||||
},
|
||||
indices: { add: [], drop: [] }
|
||||
}
|
||||
indices: { add: [], drop: [] },
|
||||
},
|
||||
]);
|
||||
|
||||
// now sync
|
||||
@@ -165,15 +165,15 @@ describe("SchemaManager tests", async () => {
|
||||
new Entity(usersTable, [
|
||||
new TextField("username"),
|
||||
new TextField("email"),
|
||||
new TextField("bio")
|
||||
new TextField("bio"),
|
||||
]),
|
||||
new Entity(postsTable, [
|
||||
new TextField("title"),
|
||||
new TextField("content"),
|
||||
new TextField("created_at")
|
||||
])
|
||||
new TextField("created_at"),
|
||||
]),
|
||||
],
|
||||
dummyConnection
|
||||
dummyConnection,
|
||||
);
|
||||
const kysely = em.connection.kysely;
|
||||
|
||||
@@ -192,7 +192,7 @@ describe("SchemaManager tests", async () => {
|
||||
name: usersTable,
|
||||
isNew: false,
|
||||
columns: { add: ["bio"], drop: [], change: [] },
|
||||
indices: { add: [], drop: [] }
|
||||
indices: { add: [], drop: [] },
|
||||
},
|
||||
{
|
||||
name: postsTable,
|
||||
@@ -200,10 +200,10 @@ describe("SchemaManager tests", async () => {
|
||||
columns: {
|
||||
add: ["id", "title", "content", "created_at"],
|
||||
drop: [],
|
||||
change: []
|
||||
change: [],
|
||||
},
|
||||
indices: { add: [], drop: [] }
|
||||
}
|
||||
indices: { add: [], drop: [] },
|
||||
},
|
||||
]);
|
||||
|
||||
// now sync
|
||||
@@ -228,8 +228,8 @@ describe("SchemaManager tests", async () => {
|
||||
name: entity.name,
|
||||
isNew: true,
|
||||
columns: { add: ["id", "email"], drop: [], change: [] },
|
||||
indices: { add: [index.name!], drop: [] }
|
||||
}
|
||||
indices: { add: [index.name!], drop: [] },
|
||||
},
|
||||
]);
|
||||
|
||||
// sync and then check again
|
||||
@@ -256,8 +256,8 @@ describe("SchemaManager tests", async () => {
|
||||
name: entity.name,
|
||||
isNew: false,
|
||||
columns: { add: [], drop: [], change: [] },
|
||||
indices: { add: [index.name!], drop: [] }
|
||||
}
|
||||
indices: { add: [index.name!], drop: [] },
|
||||
},
|
||||
]);
|
||||
|
||||
// sync and then check again
|
||||
|
||||
@@ -7,7 +7,7 @@ import {
|
||||
ManyToOneRelation,
|
||||
PolymorphicRelation,
|
||||
TextField,
|
||||
WithBuilder
|
||||
WithBuilder,
|
||||
} from "../../../src/data";
|
||||
import * as proto from "../../../src/data/prototype";
|
||||
import { compileQb, prettyPrintQb, schemaToEm } from "../../helper";
|
||||
@@ -21,12 +21,12 @@ describe("[data] WithBuilder", async () => {
|
||||
{
|
||||
posts: proto.entity("posts", {}),
|
||||
users: proto.entity("users", {}),
|
||||
media: proto.entity("media", {})
|
||||
media: proto.entity("media", {}),
|
||||
},
|
||||
({ relation }, { posts, users, media }) => {
|
||||
relation(posts).manyToOne(users);
|
||||
relation(users).polyToOne(media, { mappedBy: "avatar" });
|
||||
}
|
||||
},
|
||||
);
|
||||
const em = schemaToEm(schema);
|
||||
|
||||
@@ -36,17 +36,17 @@ describe("[data] WithBuilder", async () => {
|
||||
expect(
|
||||
WithBuilder.validateWiths(em, "posts", {
|
||||
users: {
|
||||
with: { avatar: {} }
|
||||
}
|
||||
})
|
||||
with: { avatar: {} },
|
||||
},
|
||||
}),
|
||||
).toBe(2);
|
||||
expect(() => WithBuilder.validateWiths(em, "posts", { author: {} })).toThrow();
|
||||
expect(() =>
|
||||
WithBuilder.validateWiths(em, "posts", {
|
||||
users: {
|
||||
with: { glibberish: {} }
|
||||
}
|
||||
})
|
||||
with: { glibberish: {} },
|
||||
},
|
||||
}),
|
||||
).toThrow();
|
||||
});
|
||||
|
||||
@@ -56,8 +56,8 @@ describe("[data] WithBuilder", async () => {
|
||||
|
||||
expect(() =>
|
||||
WithBuilder.addClause(em, em.connection.kysely.selectFrom("users"), users, {
|
||||
posts: {}
|
||||
})
|
||||
posts: {},
|
||||
}),
|
||||
).toThrow('Relation "users<>posts" not found');
|
||||
});
|
||||
|
||||
@@ -68,13 +68,13 @@ describe("[data] WithBuilder", async () => {
|
||||
const em = new EntityManager([users, posts], dummyConnection, relations);
|
||||
|
||||
const qb = WithBuilder.addClause(em, em.connection.kysely.selectFrom("users"), users, {
|
||||
posts: {}
|
||||
posts: {},
|
||||
});
|
||||
|
||||
const res = qb.compile();
|
||||
|
||||
expect(res.sql).toBe(
|
||||
'select (select coalesce(json_group_array(json_object(\'id\', "agg"."id", \'content\', "agg"."content", \'author_id\', "agg"."author_id")), \'[]\') from (select "posts"."id" as "id", "posts"."content" as "content", "posts"."author_id" as "author_id" from "posts" as "posts" where "posts"."author_id" = "users"."id" order by "posts"."id" asc limit ? offset ?) as agg) as "posts" from "users"'
|
||||
'select (select coalesce(json_group_array(json_object(\'id\', "agg"."id", \'content\', "agg"."content", \'author_id\', "agg"."author_id")), \'[]\') from (select "posts"."id" as "id", "posts"."content" as "content", "posts"."author_id" as "author_id" from "posts" as "posts" where "posts"."author_id" = "users"."id" order by "posts"."id" asc limit ? offset ?) as agg) as "posts" from "users"',
|
||||
);
|
||||
expect(res.parameters).toEqual([10, 0]);
|
||||
|
||||
@@ -83,14 +83,14 @@ describe("[data] WithBuilder", async () => {
|
||||
em.connection.kysely.selectFrom("posts"),
|
||||
posts, // @todo: try with "users", it gives output!
|
||||
{
|
||||
author: {}
|
||||
}
|
||||
author: {},
|
||||
},
|
||||
);
|
||||
|
||||
const res2 = qb2.compile();
|
||||
|
||||
expect(res2.sql).toBe(
|
||||
'select (select json_object(\'id\', "obj"."id", \'username\', "obj"."username") from (select "users"."id" as "id", "users"."username" as "username" from "users" as "author" where "author"."id" = "posts"."author_id" order by "users"."id" asc limit ? offset ?) as obj) as "author" from "posts"'
|
||||
'select (select json_object(\'id\', "obj"."id", \'username\', "obj"."username") from (select "users"."id" as "id", "users"."username" as "username" from "users" as "author" where "author"."id" = "posts"."author_id" order by "users"."id" asc limit ? offset ?) as obj) as "author" from "posts"',
|
||||
);
|
||||
expect(res2.parameters).toEqual([1, 0]);
|
||||
});
|
||||
@@ -124,7 +124,7 @@ describe("[data] WithBuilder", async () => {
|
||||
.values([
|
||||
{ posts_id: 1, categories_id: 1 },
|
||||
{ posts_id: 2, categories_id: 2 },
|
||||
{ posts_id: 1, categories_id: 2 }
|
||||
{ posts_id: 1, categories_id: 2 },
|
||||
])
|
||||
.execute();
|
||||
|
||||
@@ -138,14 +138,14 @@ describe("[data] WithBuilder", async () => {
|
||||
title: "fashion post",
|
||||
categories: [
|
||||
{ id: 1, label: "fashion" },
|
||||
{ id: 2, label: "beauty" }
|
||||
]
|
||||
{ id: 2, label: "beauty" },
|
||||
],
|
||||
},
|
||||
{
|
||||
id: 2,
|
||||
title: "beauty post",
|
||||
categories: [{ id: 2, label: "beauty" }]
|
||||
}
|
||||
categories: [{ id: 2, label: "beauty" }],
|
||||
},
|
||||
]);
|
||||
|
||||
const res2 = await em.repository(categories).findMany({ with: { posts: {} } });
|
||||
@@ -156,21 +156,21 @@ describe("[data] WithBuilder", async () => {
|
||||
{
|
||||
id: 1,
|
||||
label: "fashion",
|
||||
posts: [{ id: 1, title: "fashion post" }]
|
||||
posts: [{ id: 1, title: "fashion post" }],
|
||||
},
|
||||
{
|
||||
id: 2,
|
||||
label: "beauty",
|
||||
posts: [
|
||||
{ id: 1, title: "fashion post" },
|
||||
{ id: 2, title: "beauty post" }
|
||||
]
|
||||
{ id: 2, title: "beauty post" },
|
||||
],
|
||||
},
|
||||
{
|
||||
id: 3,
|
||||
label: "tech",
|
||||
posts: []
|
||||
}
|
||||
posts: [],
|
||||
},
|
||||
]);
|
||||
});
|
||||
|
||||
@@ -181,7 +181,7 @@ describe("[data] WithBuilder", async () => {
|
||||
const entities = [media, categories];
|
||||
const single = new PolymorphicRelation(categories, media, {
|
||||
mappedBy: "single",
|
||||
targetCardinality: 1
|
||||
targetCardinality: 1,
|
||||
});
|
||||
const multiple = new PolymorphicRelation(categories, media, { mappedBy: "multiple" });
|
||||
|
||||
@@ -191,11 +191,11 @@ describe("[data] WithBuilder", async () => {
|
||||
em,
|
||||
em.connection.kysely.selectFrom("categories"),
|
||||
categories,
|
||||
{ single: {} }
|
||||
{ single: {} },
|
||||
);
|
||||
const res = qb.compile();
|
||||
expect(res.sql).toBe(
|
||||
'select (select json_object(\'id\', "obj"."id", \'path\', "obj"."path") from (select "media"."id" as "id", "media"."path" as "path" from "media" where "media"."reference" = ? and "categories"."id" = "media"."entity_id" order by "media"."id" asc limit ? offset ?) as obj) as "single" from "categories"'
|
||||
'select (select json_object(\'id\', "obj"."id", \'path\', "obj"."path") from (select "media"."id" as "id", "media"."path" as "path" from "media" where "media"."reference" = ? and "categories"."id" = "media"."entity_id" order by "media"."id" asc limit ? offset ?) as obj) as "single" from "categories"',
|
||||
);
|
||||
expect(res.parameters).toEqual(["categories.single", 1, 0]);
|
||||
|
||||
@@ -203,11 +203,11 @@ describe("[data] WithBuilder", async () => {
|
||||
em,
|
||||
em.connection.kysely.selectFrom("categories"),
|
||||
categories,
|
||||
{ multiple: {} }
|
||||
{ multiple: {} },
|
||||
);
|
||||
const res2 = qb2.compile();
|
||||
expect(res2.sql).toBe(
|
||||
'select (select coalesce(json_group_array(json_object(\'id\', "agg"."id", \'path\', "agg"."path")), \'[]\') from (select "media"."id" as "id", "media"."path" as "path" from "media" where "media"."reference" = ? and "categories"."id" = "media"."entity_id" order by "media"."id" asc limit ? offset ?) as agg) as "multiple" from "categories"'
|
||||
'select (select coalesce(json_group_array(json_object(\'id\', "agg"."id", \'path\', "agg"."path")), \'[]\') from (select "media"."id" as "id", "media"."path" as "path" from "media" where "media"."reference" = ? and "categories"."id" = "media"."entity_id" order by "media"."id" asc limit ? offset ?) as agg) as "multiple" from "categories"',
|
||||
);
|
||||
expect(res2.parameters).toEqual(["categories.multiple", 10, 0]);
|
||||
});
|
||||
@@ -240,16 +240,16 @@ describe("[data] WithBuilder", async () => {
|
||||
{
|
||||
posts: proto.entity("posts", {}),
|
||||
users: proto.entity("users", {
|
||||
username: proto.text()
|
||||
username: proto.text(),
|
||||
}),
|
||||
media: proto.entity("media", {
|
||||
path: proto.text()
|
||||
})
|
||||
path: proto.text(),
|
||||
}),
|
||||
},
|
||||
({ relation }, { posts, users, media }) => {
|
||||
relation(posts).manyToOne(users);
|
||||
relation(users).polyToOne(media, { mappedBy: "avatar" });
|
||||
}
|
||||
},
|
||||
);
|
||||
const em = schemaToEm(schema);
|
||||
|
||||
@@ -265,16 +265,16 @@ describe("[data] WithBuilder", async () => {
|
||||
with: {
|
||||
avatar: {
|
||||
select: ["id", "path"],
|
||||
limit: 2 // ignored
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
limit: 2, // ignored
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
);
|
||||
|
||||
//prettyPrintQb(qb);
|
||||
expect(qb.compile().sql).toBe(
|
||||
'select (select json_object(\'id\', "obj"."id", \'username\', "obj"."username", \'avatar\', "obj"."avatar") from (select "users"."id" as "id", "users"."username" as "username", (select json_object(\'id\', "obj"."id", \'path\', "obj"."path") from (select "media"."id" as "id", "media"."path" as "path" from "media" where "media"."reference" = ? and "users"."id" = "media"."entity_id" order by "media"."id" asc limit ? offset ?) as obj) as "avatar" from "users" as "users" where "users"."id" = "posts"."users_id" order by "users"."username" asc limit ? offset ?) as obj) as "users" from "posts"'
|
||||
'select (select json_object(\'id\', "obj"."id", \'username\', "obj"."username", \'avatar\', "obj"."avatar") from (select "users"."id" as "id", "users"."username" as "username", (select json_object(\'id\', "obj"."id", \'path\', "obj"."path") from (select "media"."id" as "id", "media"."path" as "path" from "media" where "media"."reference" = ? and "users"."id" = "media"."entity_id" order by "media"."id" asc limit ? offset ?) as obj) as "avatar" from "users" as "users" where "users"."id" = "posts"."users_id" order by "users"."username" asc limit ? offset ?) as obj) as "users" from "posts"',
|
||||
);
|
||||
expect(qb.compile().parameters).toEqual(["users.avatar", 1, 0, 1, 0]);
|
||||
});
|
||||
@@ -285,17 +285,17 @@ describe("[data] WithBuilder", async () => {
|
||||
posts: proto.entity("posts", {}),
|
||||
comments: proto.entity("comments", {}),
|
||||
users: proto.entity("users", {
|
||||
username: proto.text()
|
||||
username: proto.text(),
|
||||
}),
|
||||
media: proto.entity("media", {
|
||||
path: proto.text()
|
||||
})
|
||||
path: proto.text(),
|
||||
}),
|
||||
},
|
||||
({ relation }, { posts, comments, users, media }) => {
|
||||
relation(posts).manyToOne(users).polyToOne(media, { mappedBy: "images" });
|
||||
relation(users).polyToOne(media, { mappedBy: "avatar" });
|
||||
relation(comments).manyToOne(posts).manyToOne(users);
|
||||
}
|
||||
},
|
||||
);
|
||||
const em = schemaToEm(schema);
|
||||
|
||||
@@ -308,15 +308,15 @@ describe("[data] WithBuilder", async () => {
|
||||
limit: 12,
|
||||
with: {
|
||||
users: {
|
||||
select: ["username"]
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
select: ["username"],
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
);
|
||||
|
||||
expect(qb.compile().sql).toBe(
|
||||
'select (select coalesce(json_group_array(json_object(\'id\', "agg"."id", \'posts_id\', "agg"."posts_id", \'users_id\', "agg"."users_id", \'users\', "agg"."users")), \'[]\') from (select "comments"."id" as "id", "comments"."posts_id" as "posts_id", "comments"."users_id" as "users_id", (select json_object(\'username\', "obj"."username") from (select "users"."username" as "username" from "users" as "users" where "users"."id" = "comments"."users_id" order by "users"."id" asc limit ? offset ?) as obj) as "users" from "comments" as "comments" where "comments"."posts_id" = "posts"."id" order by "comments"."id" asc limit ? offset ?) as agg) as "comments" from "posts"'
|
||||
'select (select coalesce(json_group_array(json_object(\'id\', "agg"."id", \'posts_id\', "agg"."posts_id", \'users_id\', "agg"."users_id", \'users\', "agg"."users")), \'[]\') from (select "comments"."id" as "id", "comments"."posts_id" as "posts_id", "comments"."users_id" as "users_id", (select json_object(\'username\', "obj"."username") from (select "users"."username" as "username" from "users" as "users" where "users"."id" = "comments"."users_id" order by "users"."id" asc limit ? offset ?) as obj) as "users" from "comments" as "comments" where "comments"."posts_id" = "posts"."id" order by "comments"."id" asc limit ? offset ?) as agg) as "comments" from "posts"',
|
||||
);
|
||||
expect(qb.compile().parameters).toEqual([1, 0, 12, 0]);
|
||||
});
|
||||
@@ -325,23 +325,23 @@ describe("[data] WithBuilder", async () => {
|
||||
const schema = proto.em(
|
||||
{
|
||||
posts: proto.entity("posts", {
|
||||
title: proto.text()
|
||||
title: proto.text(),
|
||||
}),
|
||||
comments: proto.entity("comments", {
|
||||
content: proto.text()
|
||||
content: proto.text(),
|
||||
}),
|
||||
users: proto.entity("users", {
|
||||
username: proto.text()
|
||||
username: proto.text(),
|
||||
}),
|
||||
media: proto.entity("media", {
|
||||
path: proto.text()
|
||||
})
|
||||
path: proto.text(),
|
||||
}),
|
||||
},
|
||||
({ relation }, { posts, comments, users, media }) => {
|
||||
relation(posts).manyToOne(users).polyToOne(media, { mappedBy: "images" });
|
||||
relation(users).polyToOne(media, { mappedBy: "avatar" });
|
||||
relation(comments).manyToOne(posts).manyToOne(users);
|
||||
}
|
||||
},
|
||||
);
|
||||
const em = schemaToEm(schema);
|
||||
await em.schema().sync({ force: true });
|
||||
@@ -351,7 +351,7 @@ describe("[data] WithBuilder", async () => {
|
||||
await em.mutator("posts").insertMany([
|
||||
{ title: "post1", users_id: 1 },
|
||||
{ title: "post2", users_id: 1 },
|
||||
{ title: "post3", users_id: 2 }
|
||||
{ title: "post3", users_id: 2 },
|
||||
]);
|
||||
await em.mutator("comments").insertMany([
|
||||
{ content: "comment1", posts_id: 1, users_id: 1 },
|
||||
@@ -360,7 +360,7 @@ describe("[data] WithBuilder", async () => {
|
||||
{ content: "comment3", posts_id: 2, users_id: 1 },
|
||||
{ content: "comment4", posts_id: 2, users_id: 2 },
|
||||
{ content: "comment5", posts_id: 3, users_id: 1 },
|
||||
{ content: "comment6", posts_id: 3, users_id: 2 }
|
||||
{ content: "comment6", posts_id: 3, users_id: 2 },
|
||||
]);
|
||||
|
||||
const result = await em.repo("posts").findMany({
|
||||
@@ -371,11 +371,11 @@ describe("[data] WithBuilder", async () => {
|
||||
select: ["content"],
|
||||
with: {
|
||||
users: {
|
||||
select: ["username"]
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
select: ["username"],
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
expect(result.data).toEqual([
|
||||
@@ -385,16 +385,16 @@ describe("[data] WithBuilder", async () => {
|
||||
{
|
||||
content: "comment1",
|
||||
users: {
|
||||
username: "user1"
|
||||
}
|
||||
username: "user1",
|
||||
},
|
||||
},
|
||||
{
|
||||
content: "comment1-1",
|
||||
users: {
|
||||
username: "user1"
|
||||
}
|
||||
}
|
||||
]
|
||||
username: "user1",
|
||||
},
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
title: "post2",
|
||||
@@ -402,16 +402,16 @@ describe("[data] WithBuilder", async () => {
|
||||
{
|
||||
content: "comment3",
|
||||
users: {
|
||||
username: "user1"
|
||||
}
|
||||
username: "user1",
|
||||
},
|
||||
},
|
||||
{
|
||||
content: "comment4",
|
||||
users: {
|
||||
username: "user2"
|
||||
}
|
||||
}
|
||||
]
|
||||
username: "user2",
|
||||
},
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
title: "post3",
|
||||
@@ -419,17 +419,17 @@ describe("[data] WithBuilder", async () => {
|
||||
{
|
||||
content: "comment5",
|
||||
users: {
|
||||
username: "user1"
|
||||
}
|
||||
username: "user1",
|
||||
},
|
||||
},
|
||||
{
|
||||
content: "comment6",
|
||||
users: {
|
||||
username: "user2"
|
||||
}
|
||||
}
|
||||
]
|
||||
}
|
||||
username: "user2",
|
||||
},
|
||||
},
|
||||
],
|
||||
},
|
||||
]);
|
||||
//console.log(_jsonp(result.data));
|
||||
});
|
||||
|
||||
@@ -22,10 +22,10 @@ describe("Connection", async () => {
|
||||
columns: [
|
||||
{
|
||||
name: "name",
|
||||
order: 0
|
||||
}
|
||||
]
|
||||
}
|
||||
order: 0,
|
||||
},
|
||||
],
|
||||
},
|
||||
]);
|
||||
});
|
||||
|
||||
@@ -54,14 +54,14 @@ describe("Connection", async () => {
|
||||
columns: [
|
||||
{
|
||||
name: "name",
|
||||
order: 0
|
||||
order: 0,
|
||||
},
|
||||
{
|
||||
name: "desc",
|
||||
order: 1
|
||||
}
|
||||
]
|
||||
}
|
||||
order: 1,
|
||||
},
|
||||
],
|
||||
},
|
||||
]);
|
||||
});
|
||||
|
||||
@@ -83,10 +83,10 @@ describe("Connection", async () => {
|
||||
columns: [
|
||||
{
|
||||
name: "name",
|
||||
order: 0
|
||||
}
|
||||
]
|
||||
}
|
||||
order: 0,
|
||||
},
|
||||
],
|
||||
},
|
||||
]);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -10,12 +10,12 @@ describe("[data] EnumField", async () => {
|
||||
runBaseFieldTests(
|
||||
EnumField,
|
||||
{ defaultValue: "a", schemaType: "text" },
|
||||
{ options: options(["a", "b", "c"]) }
|
||||
{ options: options(["a", "b", "c"]) },
|
||||
);
|
||||
|
||||
test("yields if default value is not a valid option", async () => {
|
||||
expect(
|
||||
() => new EnumField("test", { options: options(["a", "b"]), default_value: "c" })
|
||||
() => new EnumField("test", { options: options(["a", "b"]), default_value: "c" }),
|
||||
).toThrow();
|
||||
});
|
||||
|
||||
@@ -31,7 +31,7 @@ describe("[data] EnumField", async () => {
|
||||
const field = new EnumField("test", {
|
||||
options: options(["a", "b", "c"]),
|
||||
default_value: "a",
|
||||
required: true
|
||||
required: true,
|
||||
});
|
||||
|
||||
expect(field.transformRetrieve(null)).toBe("a");
|
||||
|
||||
@@ -24,20 +24,20 @@ describe("[data] Field", async () => {
|
||||
const required = new FieldSpec("test", { required: true });
|
||||
const requiredDefault = new FieldSpec("test", {
|
||||
required: true,
|
||||
default_value: "test"
|
||||
default_value: "test",
|
||||
});
|
||||
|
||||
expect(required.transformPersist(null, undefined as any, undefined as any)).rejects.toThrow();
|
||||
expect(
|
||||
required.transformPersist(undefined, undefined as any, undefined as any)
|
||||
required.transformPersist(undefined, undefined as any, undefined as any),
|
||||
).rejects.toThrow();
|
||||
|
||||
// works because it has a default value
|
||||
expect(
|
||||
requiredDefault.transformPersist(null, undefined as any, undefined as any)
|
||||
requiredDefault.transformPersist(null, undefined as any, undefined as any),
|
||||
).resolves.toBeDefined();
|
||||
expect(
|
||||
requiredDefault.transformPersist(undefined, undefined as any, undefined as any)
|
||||
requiredDefault.transformPersist(undefined, undefined as any, undefined as any),
|
||||
).resolves.toBeDefined();
|
||||
});
|
||||
});
|
||||
|
||||
@@ -5,7 +5,7 @@ import {
|
||||
EntityIndex,
|
||||
type EntityManager,
|
||||
Field,
|
||||
type SchemaResponse
|
||||
type SchemaResponse,
|
||||
} from "../../../../src/data";
|
||||
|
||||
class TestField extends Field {
|
||||
|
||||
@@ -7,7 +7,7 @@ describe("[data] JsonField", async () => {
|
||||
runBaseFieldTests(JsonField, {
|
||||
defaultValue: { a: 1 },
|
||||
sampleValues: ["string", { test: 1 }, 1],
|
||||
schemaType: "text"
|
||||
schemaType: "text",
|
||||
});
|
||||
|
||||
test("transformPersist (no config)", async () => {
|
||||
|
||||
@@ -18,7 +18,7 @@ export function transformPersist(field: Field, value: any, context?: TActionCont
|
||||
export function runBaseFieldTests(
|
||||
fieldClass: ConstructableField,
|
||||
config: FieldTestConfig,
|
||||
_requiredConfig: any = {}
|
||||
_requiredConfig: any = {},
|
||||
) {
|
||||
const noConfigField = new fieldClass("no_config", _requiredConfig);
|
||||
const fillable = new fieldClass("fillable", { ..._requiredConfig, fillable: true });
|
||||
@@ -29,7 +29,7 @@ export function runBaseFieldTests(
|
||||
..._requiredConfig,
|
||||
fillable: true,
|
||||
required: true,
|
||||
default_value: config.defaultValue
|
||||
default_value: config.defaultValue,
|
||||
});
|
||||
|
||||
test("schema", () => {
|
||||
@@ -37,7 +37,7 @@ export function runBaseFieldTests(
|
||||
expect(noConfigField.schema(null as any)).toEqual([
|
||||
"no_config",
|
||||
config.schemaType,
|
||||
expect.any(Function)
|
||||
expect.any(Function),
|
||||
]);
|
||||
});
|
||||
|
||||
@@ -96,7 +96,7 @@ export function runBaseFieldTests(
|
||||
//order: 1,
|
||||
fillable: true,
|
||||
required: false,
|
||||
hidden: false
|
||||
hidden: false,
|
||||
//virtual: false,
|
||||
//default_value: undefined
|
||||
};
|
||||
@@ -105,20 +105,20 @@ export function runBaseFieldTests(
|
||||
const json = field.toJSON();
|
||||
return {
|
||||
...json,
|
||||
config: omit(json.config, ["html"])
|
||||
config: omit(json.config, ["html"]),
|
||||
};
|
||||
}
|
||||
|
||||
expect(fieldJson(noConfigField)).toEqual({
|
||||
//name: "no_config",
|
||||
type: noConfigField.type,
|
||||
config: _config
|
||||
config: _config,
|
||||
});
|
||||
|
||||
expect(fieldJson(fillable)).toEqual({
|
||||
//name: "fillable",
|
||||
type: noConfigField.type,
|
||||
config: _config
|
||||
config: _config,
|
||||
});
|
||||
|
||||
expect(fieldJson(required)).toEqual({
|
||||
@@ -126,8 +126,8 @@ export function runBaseFieldTests(
|
||||
type: required.type,
|
||||
config: {
|
||||
..._config,
|
||||
required: true
|
||||
}
|
||||
required: true,
|
||||
},
|
||||
});
|
||||
|
||||
expect(fieldJson(hidden)).toEqual({
|
||||
@@ -135,8 +135,8 @@ export function runBaseFieldTests(
|
||||
type: required.type,
|
||||
config: {
|
||||
..._config,
|
||||
hidden: true
|
||||
}
|
||||
hidden: true,
|
||||
},
|
||||
});
|
||||
|
||||
expect(fieldJson(dflt)).toEqual({
|
||||
@@ -144,8 +144,8 @@ export function runBaseFieldTests(
|
||||
type: dflt.type,
|
||||
config: {
|
||||
..._config,
|
||||
default_value: config.defaultValue
|
||||
}
|
||||
default_value: config.defaultValue,
|
||||
},
|
||||
});
|
||||
|
||||
expect(fieldJson(requiredAndDefault)).toEqual({
|
||||
@@ -155,8 +155,8 @@ export function runBaseFieldTests(
|
||||
..._config,
|
||||
fillable: true,
|
||||
required: true,
|
||||
default_value: config.defaultValue
|
||||
}
|
||||
default_value: config.defaultValue,
|
||||
},
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
@@ -4,7 +4,7 @@ import {
|
||||
type BaseRelationConfig,
|
||||
EntityRelation,
|
||||
EntityRelationAnchor,
|
||||
RelationTypes
|
||||
RelationTypes,
|
||||
} from "../../../../src/data/relations";
|
||||
|
||||
class TestEntityRelation extends EntityRelation {
|
||||
@@ -12,7 +12,7 @@ class TestEntityRelation extends EntityRelation {
|
||||
super(
|
||||
new EntityRelationAnchor(new Entity("source"), "source"),
|
||||
new EntityRelationAnchor(new Entity("target"), "target"),
|
||||
config
|
||||
config,
|
||||
);
|
||||
}
|
||||
initialize(em: EntityManager<any>) {}
|
||||
|
||||
Reference in New Issue
Block a user