added new em() shorthand for prototyping, added insertMany mutator function for seeding

This commit is contained in:
dswbx
2024-12-19 07:38:40 +01:00
parent 602235b372
commit 386c0d3ff0
6 changed files with 145 additions and 17 deletions

View File

@@ -16,7 +16,7 @@ describe("Mutator simple", async () => {
new TextField("label", { required: true, minLength: 1 }),
new NumberField("count", { default_value: 0 })
]);
const em = new EntityManager([items], connection);
const em = new EntityManager<any>([items], connection);
await em.connection.kysely.schema
.createTable("items")
@@ -175,4 +175,18 @@ describe("Mutator simple", async () => {
{ id: 8, label: "keep", count: 0 }
]);
});
test("insertMany", async () => {
const oldCount = (await em.repo(items).count()).count;
const inserts = [{ label: "insert 1" }, { label: "insert 2" }];
const { data } = await em.mutator(items).insertMany(inserts);
expect(data.length).toBe(2);
expect(data.map((d) => ({ label: d.label }))).toEqual(inserts);
const newCount = (await em.repo(items).count()).count;
expect(newCount).toBe(oldCount + inserts.length);
const { data: data2 } = await em.repo(items).findMany();
expect(data2).toEqual(data);
});
});

View File

@@ -13,6 +13,7 @@ import {
PolymorphicRelation,
TextField
} from "../../src/data";
import { DummyConnection } from "../../src/data/connection/DummyConnection";
import {
FieldPrototype,
type FieldSchema,
@@ -21,6 +22,7 @@ import {
boolean,
date,
datetime,
em,
entity,
enumm,
json,
@@ -272,4 +274,29 @@ describe("prototype", () => {
const obj: Schema<typeof test> = {} as any;
});
test("schema", async () => {
const _em = em(
{
posts: entity("posts", { name: text() }),
comments: entity("comments", { some: text() })
},
(relation, { posts, comments }) => {
relation(posts).manyToOne(comments);
}
);
type LocalDb = (typeof _em)["DB"];
const es = [
new Entity("posts", [new TextField("name")]),
new Entity("comments", [new TextField("some")])
];
const _em2 = new EntityManager(es, new DummyConnection(), [
new ManyToOneRelation(es[0], es[1])
]);
// @ts-ignore
expect(_em2.toJSON()).toEqual(_em.toJSON());
});
});

View File

@@ -22,7 +22,7 @@ describe("[data] Mutator (base)", async () => {
new TextField("hidden", { hidden: true }),
new TextField("not_fillable", { fillable: false })
]);
const em = new EntityManager([entity], dummyConnection);
const em = new EntityManager<any>([entity], dummyConnection);
await em.schema().sync({ force: true });
const payload = { label: "item 1", count: 1 };
@@ -61,7 +61,7 @@ describe("[data] Mutator (ManyToOne)", async () => {
const posts = new Entity("posts", [new TextField("title")]);
const users = new Entity("users", [new TextField("username")]);
const relations = [new ManyToOneRelation(posts, users)];
const em = new EntityManager([posts, users], dummyConnection, relations);
const em = new EntityManager<any>([posts, users], dummyConnection, relations);
await em.schema().sync({ force: true });
test("RelationMutator", async () => {
@@ -192,7 +192,7 @@ describe("[data] Mutator (OneToOne)", async () => {
const users = new Entity("users", [new TextField("username")]);
const settings = new Entity("settings", [new TextField("theme")]);
const relations = [new OneToOneRelation(users, settings)];
const em = new EntityManager([users, settings], dummyConnection, relations);
const em = new EntityManager<any>([users, settings], dummyConnection, relations);
await em.schema().sync({ force: true });
test("insertOne: missing ref", async () => {
@@ -276,7 +276,7 @@ describe("[data] Mutator (ManyToMany)", async () => {
describe("[data] Mutator (Events)", async () => {
const entity = new Entity("test", [new TextField("label")]);
const em = new EntityManager([entity], dummyConnection);
const em = new EntityManager<any>([entity], dummyConnection);
await em.schema().sync({ force: true });
const events = new Map<string, any>();