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

@@ -250,13 +250,11 @@ export class AppAuth extends Module<typeof authConfigSchema> {
};
registerEntities() {
const name = this.config.entity_name as "users";
const {
entities: { users }
} = this.ensureSchema(
const users = this.getUsersEntity(true);
this.ensureSchema(
em(
{
[name]: entity(name, AppAuth.usersFields)
[users.name as "users"]: users
},
({ index }, { users }) => {
index(users).on(["email"], true).on(["strategy"]).on(["strategy_value"]);
@@ -267,13 +265,13 @@ export class AppAuth extends Module<typeof authConfigSchema> {
try {
const roles = Object.keys(this.config.roles ?? {});
const field = make("role", enumm({ enum: roles }));
users.__experimental_replaceField("role", field);
users.__replaceField("role", field);
} catch (e) {}
try {
const strategies = Object.keys(this.config.strategies ?? {});
const field = make("strategy", enumm({ enum: strategies }));
users.__experimental_replaceField("strategy", field);
users.__replaceField("strategy", field);
} catch (e) {}
}

View File

@@ -140,7 +140,7 @@ export class Entity<
return this.fields.find((field) => field.name === name);
}
__experimental_replaceField(name: string, field: Field) {
__replaceField(name: string, field: Field) {
const index = this.fields.findIndex((f) => f.name === name);
if (index === -1) {
throw new Error(`Field "${name}" not found on entity "${this.name}"`);

View File

@@ -99,6 +99,16 @@ export class EntityManager<TBD extends object = DefaultDB> {
this.entities.push(entity);
}
__replaceEntity(entity: Entity, name: string | undefined = entity.name) {
const entityIndex = this._entities.findIndex((e) => e.name === name);
if (entityIndex === -1) {
throw new Error(`Entity "${name}" not found and cannot be replaced`);
}
this._entities[entityIndex] = entity;
}
entity(e: Entity | keyof TBD | string): Entity {
let entity: Entity | undefined;
if (typeof e === "string") {

View File

@@ -48,10 +48,9 @@ export class AppMedia extends Module<typeof mediaConfigSchema> {
this.setupListeners();
this.ctx.server.route(this.basepath, new MediaController(this).getController());
const mediaEntity = this.getMediaEntity(true);
const name = mediaEntity.name as "media";
const media = this.getMediaEntity(true);
this.ensureSchema(
em({ [name]: mediaEntity }, ({ index }, { media }) => {
em({ [media.name as "media"]: media }, ({ index }, { media }) => {
index(media).on(["path"], true).on(["reference"]);
})
);

View File

@@ -3,7 +3,8 @@ import type { Guard } from "auth";
import { SchemaObject } from "core";
import type { EventManager } from "core/events";
import type { Static, TSchema } from "core/utils";
import type { Connection, Entity, EntityIndex, EntityManager, em as prototypeEm } from "data";
import type { Connection, EntityIndex, EntityManager, em as prototypeEm } from "data";
import { Entity } from "data";
import type { Hono } from "hono";
export type ServerEnv = {
@@ -138,8 +139,6 @@ export abstract class Module<Schema extends TSchema = TSchema, ConfigSchema = St
return this.config;
}
// @todo: add a method to signal the requirement of database sync!!!
protected ensureEntity(entity: Entity) {
// check fields
if (!this.ctx.em.hasEntity(entity.name)) {
@@ -152,13 +151,18 @@ export abstract class Module<Schema extends TSchema = TSchema, ConfigSchema = St
// if exists, check all fields required are there
// @todo: check if the field also equal
for (const field of entity.fields) {
const _field = instance.field(field.name);
for (const field of instance.fields) {
const _field = entity.field(field.name);
if (!_field) {
instance.addField(field);
entity.addField(field);
this.ctx.flags.sync_required = true;
}
}
// replace entity (mainly to keep the ensured type)
this.ctx.em.__replaceEntity(
new Entity(entity.name, entity.fields, instance.config, entity.type)
);
}
protected ensureIndex(index: EntityIndex) {