Refactor entity handling to preserve config while overriding type

Reworked `ensureEntity` to replace entities while maintaining their configuration and allowing type adjustments. Updated tests to verify type persistence and synchronization of entity properties.
This commit is contained in:
dswbx
2025-01-10 15:51:47 +01:00
parent 1d5f14fae0
commit e94e8d8bd1
10 changed files with 63 additions and 41 deletions

View File

@@ -121,15 +121,10 @@ describe("AppAuth", () => {
await app.build();
const userfields = app.modules.em.entity("users").fields.map((f) => f.name);
expect(userfields).toContain("additional");
expect(userfields).toEqual([
"id",
"additional",
"email",
"strategy",
"strategy_value",
"role"
]);
const e = app.modules.em.entity("users");
const fields = e.fields.map((f) => f.name);
expect(e.type).toBe("system");
expect(fields).toContain("additional");
expect(fields).toEqual(["id", "email", "strategy", "strategy_value", "role", "additional"]);
});
});

View File

@@ -33,11 +33,12 @@ describe("AppMedia", () => {
await app.build();
const fields = app.modules.em.entity("media").fields.map((f) => f.name);
const e = app.modules.em.entity("media");
const fields = e.fields.map((f) => f.name);
expect(e.type).toBe("system");
expect(fields).toContain("additional");
expect(fields).toEqual([
"id",
"additional",
"path",
"folder",
"mime_type",
@@ -47,7 +48,8 @@ describe("AppMedia", () => {
"modified_at",
"reference",
"entity_id",
"metadata"
"metadata",
"additional"
]);
});
});

View File

@@ -68,7 +68,8 @@ describe("Module", async () => {
return {
entities: _em.entities.map((e) => ({
name: e.name,
fields: e.fields.map((f) => f.name)
fields: e.fields.map((f) => f.name),
type: e.type
})),
indices: _em.indices.map((i) => ({
name: i.name,
@@ -105,7 +106,8 @@ describe("Module", async () => {
entities: [
{
name: "u",
fields: ["id", "name"]
fields: ["id", "name"],
type: "regular"
}
],
indices: []
@@ -124,7 +126,8 @@ describe("Module", async () => {
entities: [
{
name: "u",
fields: ["id", "name"]
fields: ["id", "name"],
type: "regular"
}
],
indices: []
@@ -139,9 +142,14 @@ describe("Module", async () => {
// this should only add the field "important"
m.prt.ensureEntity(
entity("u", {
important: text()
})
entity(
"u",
{
important: text()
},
undefined,
"system"
)
);
expect(m.ctx.flags.sync_required).toBe(true);
@@ -149,11 +157,15 @@ describe("Module", async () => {
entities: [
{
name: "u",
fields: ["id", "name", "important"]
// ensured properties must come first
fields: ["id", "important", "name"],
// ensured type must be present
type: "system"
},
{
name: "p",
fields: ["id", "title"]
fields: ["id", "title"],
type: "regular"
}
],
indices: []
@@ -177,7 +189,8 @@ describe("Module", async () => {
entities: [
{
name: "u",
fields: ["id", "name", "title"]
fields: ["id", "name", "title"],
type: "regular"
}
],
indices: [

View File

@@ -5,7 +5,7 @@ import { Guard } from "../../src/auth";
import { EventManager } from "../../src/core/events";
import { Default, stripMark } from "../../src/core/utils";
import { EntityManager } from "../../src/data";
import type { Module, ModuleBuildContext } from "../../src/modules/Module";
import { Module, type ModuleBuildContext } from "../../src/modules/Module";
import { getDummyConnection } from "../helper";
export function makeCtx(overrides?: Partial<ModuleBuildContext>): ModuleBuildContext {
@@ -16,6 +16,7 @@ export function makeCtx(overrides?: Partial<ModuleBuildContext>): ModuleBuildCon
em: new EntityManager([], dummyConnection),
emgr: new EventManager(),
guard: new Guard(),
flags: Module.ctx_flags,
...overrides
};
}