refactor auth/media entities to separate files, suppress node:sqlite warning

This commit is contained in:
dswbx
2025-07-02 16:36:06 +02:00
parent 80034b9b0a
commit 45138c25f0
8 changed files with 104 additions and 89 deletions

View File

@@ -1,6 +1,7 @@
import type { Entity, EntityManager, EntityRelation, TEntityType } from "data";
import { autoFormatString } from "core/utils";
import { AppAuth, AppMedia } from "modules";
import { usersFields } from "auth/auth-entities";
import { mediaFields } from "media/media-entities";
export type TEntityTSType = {
name: string;
@@ -32,8 +33,8 @@ export type EntityTypescriptOptions = {
// keep a local copy here until properties have a type
const systemEntities = {
users: AppAuth.usersFields,
media: AppMedia.mediaFields,
users: usersFields,
media: mediaFields,
};
export class EntityTypescript {

View File

@@ -3,16 +3,13 @@ import { EntityManager } from "data/entities/EntityManager";
import type { Generated } from "kysely";
import { MediaField, type MediaFieldConfig, type MediaItem } from "media/MediaField";
import type { ModuleConfigs } from "modules";
import {
BooleanField,
type BooleanFieldConfig,
type Connection,
DateField,
type DateFieldConfig,
Entity,
type EntityConfig,
EntityIndex,
type EntityRelation,
EnumField,
type EnumFieldConfig,
type Field,
@@ -20,20 +17,27 @@ import {
type JsonFieldConfig,
JsonSchemaField,
type JsonSchemaFieldConfig,
NumberField,
type NumberFieldConfig,
TextField,
type TextFieldConfig,
} from "data/fields";
import { Entity, type EntityConfig, type TEntityType } from "data/entities";
import type { Connection } from "data/connection";
import {
type EntityRelation,
ManyToManyRelation,
type ManyToManyRelationConfig,
ManyToOneRelation,
type ManyToOneRelationConfig,
NumberField,
type NumberFieldConfig,
OneToOneRelation,
type OneToOneRelationConfig,
PolymorphicRelation,
type PolymorphicRelationConfig,
type TEntityType,
TextField,
type TextFieldConfig,
} from "../index";
} from "data/relations";
type Options<Config = any> = {
entity: { name: string; fields: Record<string, Field<any, any, any>> };
@@ -61,6 +65,46 @@ const FieldMap = {
} as const;
type TFieldType = keyof typeof FieldMap;
export class FieldPrototype {
constructor(
public type: TFieldType,
public config: any,
public is_required: boolean,
) {}
required() {
this.is_required = true;
return this;
}
getField(o: Options): Field {
if (!FieldMap[this.type]) {
throw new Error(`Unknown field type: ${this.type}`);
}
try {
return FieldMap[this.type](o) as unknown as Field;
} catch (e) {
throw new Error(`Faild to construct field "${this.type}": ${e}`);
}
}
make(field_name: string): Field {
if (!FieldMap[this.type]) {
throw new Error(`Unknown field type: ${this.type}`);
}
try {
return FieldMap[this.type]({
entity: { name: "unknown", fields: {} },
field_name,
config: this.config,
is_required: this.is_required,
}) as unknown as Field;
} catch (e) {
throw new Error(`Faild to construct field "${this.type}": ${e}`);
}
}
}
export function text(
config?: Omit<TextFieldConfig, "required">,
): TextField<false> & { required: () => TextField<true> } {
@@ -132,46 +176,6 @@ export function make<Actual extends Field<any, any>>(name: string, field: Actual
throw new Error("Invalid field");
}
export class FieldPrototype {
constructor(
public type: TFieldType,
public config: any,
public is_required: boolean,
) {}
required() {
this.is_required = true;
return this;
}
getField(o: Options): Field {
if (!FieldMap[this.type]) {
throw new Error(`Unknown field type: ${this.type}`);
}
try {
return FieldMap[this.type](o) as unknown as Field;
} catch (e) {
throw new Error(`Faild to construct field "${this.type}": ${e}`);
}
}
make(field_name: string): Field {
if (!FieldMap[this.type]) {
throw new Error(`Unknown field type: ${this.type}`);
}
try {
return FieldMap[this.type]({
entity: { name: "unknown", fields: {} },
field_name,
config: this.config,
is_required: this.is_required,
}) as unknown as Field;
} catch (e) {
throw new Error(`Faild to construct field "${this.type}": ${e}`);
}
}
}
export function entity<
EntityName extends string,
Fields extends Record<string, Field<any, any, any>>,