mirror of
https://github.com/shishantbiswas/bknd.git
synced 2026-03-19 13:56:04 +00:00
feat: improved abilities of plugins, moved schema fns to ctx
This commit is contained in:
@@ -2,19 +2,10 @@ import type { Guard } from "auth";
|
||||
import { type DebugLogger, SchemaObject } from "core";
|
||||
import type { EventManager } from "core/events";
|
||||
import type { Static, TSchema } from "core/utils";
|
||||
import {
|
||||
type Connection,
|
||||
type EntityIndex,
|
||||
type EntityManager,
|
||||
type Field,
|
||||
FieldPrototype,
|
||||
make,
|
||||
type em as prototypeEm,
|
||||
} from "data";
|
||||
import { Entity } from "data";
|
||||
import type { Connection, EntityManager } from "data";
|
||||
import type { Hono } from "hono";
|
||||
import { isEqual } from "lodash-es";
|
||||
import type { ServerEnv } from "modules/Controller";
|
||||
import type { ModuleHelper } from "./ModuleHelper";
|
||||
|
||||
export type ModuleBuildContext = {
|
||||
connection: Connection;
|
||||
@@ -24,6 +15,7 @@ export type ModuleBuildContext = {
|
||||
guard: Guard;
|
||||
logger: DebugLogger;
|
||||
flags: (typeof Module)["ctx_flags"];
|
||||
helper: ModuleHelper;
|
||||
};
|
||||
|
||||
export abstract class Module<Schema extends TSchema = TSchema, ConfigSchema = Static<Schema>> {
|
||||
@@ -141,80 +133,4 @@ export abstract class Module<Schema extends TSchema = TSchema, ConfigSchema = St
|
||||
toJSON(secrets?: boolean): Static<ReturnType<(typeof this)["getSchema"]>> {
|
||||
return this.config;
|
||||
}
|
||||
|
||||
protected ensureEntity(entity: Entity) {
|
||||
const instance = this.ctx.em.entity(entity.name, true);
|
||||
|
||||
// check fields
|
||||
if (!instance) {
|
||||
this.ctx.em.addEntity(entity);
|
||||
this.ctx.flags.sync_required = true;
|
||||
return;
|
||||
}
|
||||
|
||||
// if exists, check all fields required are there
|
||||
// @todo: check if the field also equal
|
||||
for (const field of entity.fields) {
|
||||
const instanceField = instance.field(field.name);
|
||||
if (!instanceField) {
|
||||
instance.addField(field);
|
||||
this.ctx.flags.sync_required = true;
|
||||
} else {
|
||||
const changes = this.setEntityFieldConfigs(field, instanceField);
|
||||
if (changes > 0) {
|
||||
this.ctx.flags.sync_required = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// replace entity (mainly to keep the ensured type)
|
||||
this.ctx.em.__replaceEntity(
|
||||
new Entity(instance.name, instance.fields, instance.config, entity.type),
|
||||
);
|
||||
}
|
||||
|
||||
protected ensureIndex(index: EntityIndex) {
|
||||
if (!this.ctx.em.hasIndex(index)) {
|
||||
this.ctx.em.addIndex(index);
|
||||
this.ctx.flags.sync_required = true;
|
||||
}
|
||||
}
|
||||
|
||||
protected ensureSchema<Schema extends ReturnType<typeof prototypeEm>>(schema: Schema): Schema {
|
||||
Object.values(schema.entities ?? {}).forEach(this.ensureEntity.bind(this));
|
||||
schema.indices?.forEach(this.ensureIndex.bind(this));
|
||||
|
||||
return schema;
|
||||
}
|
||||
|
||||
protected setEntityFieldConfigs(
|
||||
parent: Field,
|
||||
child: Field,
|
||||
props: string[] = ["hidden", "fillable", "required"],
|
||||
) {
|
||||
let changes = 0;
|
||||
for (const prop of props) {
|
||||
if (!isEqual(child.config[prop], parent.config[prop])) {
|
||||
child.config[prop] = parent.config[prop];
|
||||
changes++;
|
||||
}
|
||||
}
|
||||
return changes;
|
||||
}
|
||||
|
||||
protected replaceEntityField(
|
||||
_entity: string | Entity,
|
||||
field: Field | string,
|
||||
_newField: Field | FieldPrototype,
|
||||
) {
|
||||
const entity = this.ctx.em.entity(_entity);
|
||||
const name = typeof field === "string" ? field : field.name;
|
||||
const newField =
|
||||
_newField instanceof FieldPrototype ? make(name, _newField as any) : _newField;
|
||||
|
||||
// ensure keeping vital config
|
||||
this.setEntityFieldConfigs(entity.field(name)!, newField);
|
||||
|
||||
entity.__replaceField(name, newField);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user