Release 0.12 (#143)

* changed tb imports

* cleanup: replace console.log/warn with $console, remove commented-out code

Removed various commented-out code and replaced direct `console.log` and `console.warn` usage across the codebase with `$console` from "core" for standardized logging. Also adjusted linting rules in biome.json to enable warnings for `console.log` usage.

* ts: enable incremental

* fix imports in test files

reorganize imports to use "@sinclair/typebox" directly, replacing local utility references, and add missing "override" keywords in test classes.

* added media permissions (#142)

* added permissions support for media module

introduced `MediaPermissions` for fine-grained access control in the media module, updated routes to enforce these permissions, and adjusted permission registration logic.

* fix: handle token absence in getUploadHeaders and add tests for transport modes

ensure getUploadHeaders does not set Authorization header when token is missing. Add unit tests to validate behavior for different token_transport options.

* remove console.log on DropzoneContainer.tsx

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

---------

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* add bcrypt and refactored auth resolve (#147)

* reworked auth architecture with improved password handling and claims

Refactored password strategy to prepare supporting bcrypt, improving hashing/encryption flexibility. Updated authentication flow with enhanced user resolution mechanisms, safe JWT generation, and consistent profile handling. Adjusted dependencies to include bcryptjs and updated lock files accordingly.

* fix strategy forms handling, add register route and hidden fields

Refactored strategy forms to include hidden fields for type and name. Added a registration route with necessary adjustments to the admin controller and routes. Corrected field handling within relevant forms and components.

* refactored auth handling to support bcrypt, extracted user pool

* update email regex to allow '+' and '_' characters

* update test stub password for AppAuth spec

* update data exceptions to use HttpStatus constants, adjust logging level in AppUserPool

* rework strategies to extend a base class instead of interface

* added simple bcrypt test

* add validation logs and improve data validation handling (#157)

Added warning logs for invalid data during mutator validation, refined field validation logic to handle undefined values, and adjusted event validation comments for clarity. Minor improvements include exporting events from core and handling optional chaining in entity field validation.

* modify MediaApi to support custom fetch implementation, defaults to native fetch (#158)

* modify MediaApi to support custom fetch implementation, defaults to native fetch

added an optional `fetcher` parameter to allow usage of a custom fetch function in both `upload` and `fetcher` methods. Defaults to the standard `fetch` if none is provided.

* fix tests and improve api fetcher types

* update admin basepath handling and window context integration (#155)

Refactored `useBkndWindowContext` to include `admin_basepath` and updated its usage in routing. Improved type consistency with `AdminBkndWindowContext` and ensured default values are applied for window context.

* trigger `repository-find-[one|many]-[before|after]` based on `limit` (#160)

* refactor error handling in authenticator and password strategy (#161)

made `respondWithError` method public, updated login and register routes in `PasswordStrategy` to handle errors using `respondWithError` for consistency.

* add disableSubmitOnError prop to NativeForm and export getFlashMessage (#162)

Introduced a `disableSubmitOnError` prop to NativeForm to control submit button behavior when errors are present. Also exported `getFlashMessage` from the core for external usage.

* update dependencies in package.json (#156)

moved several dependencies between devDependencies and dependencies for better categorization and removed redundant entries.

* update imports to adjust nodeTestRunner path and remove unused export (#163)

updated imports in test files to reflect the correct path for nodeTestRunner. removed redundant export of nodeTestRunner from index file to clean up module structure. In some environments this could cause issues requiring to exclude `node:test`, just removing it for now.

* fix sync events not awaited (#164)

* refactor(dropzone): extract DropzoneInner and unify state management with zustand (#165)

Simplified Dropzone implementation by extracting inner logic to a new component, `DropzoneInner`. Replaced local dropzone state logic with centralized state management using zustand. Adjusted API exports and props accordingly for consistency and maintainability.

* replace LiquidJs rendering with simplified renderer (#167)

* replace LiquidJs rendering with simplified renderer

Removed dependency on LiquidJS and replaced it with a custom templating solution using lodash `get`. Updated corresponding components, editors, and tests to align with the new rendering approach. Removed unused filters and tags.

* remove liquid js from package json

* feat/cli-generate-types (#166)

* init types generation

* update type generation for entities and fields

Refactored `EntityTypescript` to support improved field types and relations. Added `toType` method overrides for various fields to define accurate TypeScript types. Enhanced CLI `types` command with new options for output style and file handling. Removed redundant test files.

* update type generation code and CLI option description

removed unused imports definition, adjusted formatting in EntityTypescript, and clarified the CLI style option description.

* fix json schema field type generation

* reworked system entities to prevent recursive types

* reworked system entities to prevent recursive types

* remove unused object function

* types: use number instead of Generated

* update data hooks and api types

* update data hooks and api types

* update data hooks and api types

* update data hooks and api types

---------

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
This commit is contained in:
dswbx
2025-05-01 10:12:18 +02:00
committed by GitHub
parent d6f94a2ce1
commit 372f94d22a
186 changed files with 2617 additions and 1997 deletions

View File

@@ -1,13 +1,14 @@
import { config } from "core";
import { $console, config } from "core";
import {
type Static,
StringEnum,
Type,
parse,
snakeToPascalWithSpaces,
transformObject,
} from "core/utils";
import { type Field, PrimaryField, type TActionContext, type TRenderContext } from "../fields";
import * as tbbox from "@sinclair/typebox";
const { Type } = tbbox;
// @todo: entity must be migrated to typebox
export const entityConfigSchema = Type.Object(
@@ -183,9 +184,9 @@ export class Entity<
if (existing) {
// @todo: for now adding a graceful method
if (JSON.stringify(existing) === JSON.stringify(field)) {
/*console.warn(
$console.warn(
`Field "${field.name}" already exists on entity "${this.name}", but it's the same, so skipping.`,
);*/
);
return;
}
@@ -231,8 +232,14 @@ export class Entity<
}
for (const field of fields) {
if (!field.isValid(data[field.name], context)) {
console.log("Entity.isValidData:invalid", context, field.name, data[field.name]);
if (!field.isValid(data?.[field.name], context)) {
$console.warn(
"invalid data given for",
this.name,
context,
field.name,
data[field.name],
);
if (options?.explain) {
throw new Error(`Field "${field.name}" has invalid data: "${data[field.name]}"`);
}
@@ -258,7 +265,6 @@ export class Entity<
const _fields = Object.fromEntries(fields.map((field) => [field.name, field]));
const schema = Type.Object(
transformObject(_fields, (field) => {
//const hidden = field.isHidden(options?.context);
const fillable = field.isFillable(options?.context);
return {
title: field.config.label,
@@ -274,11 +280,18 @@ export class Entity<
return options?.clean ? JSON.parse(JSON.stringify(schema)) : schema;
}
toTypes() {
return {
name: this.name,
type: this.type,
comment: this.config.description,
fields: Object.fromEntries(this.getFields().map((field) => [field.name, field.toType()])),
};
}
toJSON() {
return {
//name: this.name,
type: this.type,
//fields: transformObject(this.fields, (field) => field.toJSON()),
fields: Object.fromEntries(this.fields.map((field) => [field.name, field.toJSON()])),
config: this.config,
};

View File

@@ -1,4 +1,4 @@
import type { DB as DefaultDB } from "core";
import { $console, type DB as DefaultDB } from "core";
import { EventManager } from "core/events";
import { sql } from "kysely";
import { Connection } from "../connection/Connection";
@@ -55,7 +55,6 @@ export class EntityManager<TBD extends object = DefaultDB> {
this.connection = connection;
this.emgr = emgr ?? new EventManager();
//console.log("registering events", EntityManager.Events);
this.emgr.registerEvents(EntityManager.Events);
}
@@ -90,7 +89,9 @@ export class EntityManager<TBD extends object = DefaultDB> {
if (existing) {
// @todo: for now adding a graceful method
if (JSON.stringify(existing) === JSON.stringify(entity)) {
//console.warn(`Entity "${entity.name}" already exists, but it's the same, so skipping.`);
$console.warn(
`Entity "${entity.name}" already exists, but it's the same, skipping adding it.`,
);
return;
}
@@ -108,7 +109,6 @@ export class EntityManager<TBD extends object = DefaultDB> {
}
this._entities[entityIndex] = entity;
// caused issues because this.entity() was using a reference (for when initial config was given)
}
@@ -295,7 +295,6 @@ export class EntityManager<TBD extends object = DefaultDB> {
return {
entities: Object.fromEntries(this.entities.map((e) => [e.name, e.toJSON()])),
relations: Object.fromEntries(this.relations.all.map((r) => [r.getName(), r.toJSON()])),
//relations: this.relations.all.map((r) => r.toJSON()),
indices: Object.fromEntries(this.indices.map((i) => [i.name, i.toJSON()])),
};
}

View File

@@ -0,0 +1,228 @@
import type { Entity, EntityManager, EntityRelation, TEntityType } from "data";
import { autoFormatString } from "core/utils";
import { AppAuth, AppMedia } from "modules";
export type TEntityTSType = {
name: string;
type: TEntityType;
comment?: string;
fields: Record<string, TFieldTSType>;
};
// [select, insert, update]
type TFieldContextType = boolean | [boolean, boolean, boolean];
export type TFieldTSType = {
required?: TFieldContextType;
fillable?: TFieldContextType;
type: "PrimaryFieldType" | string;
comment?: string;
import?: {
package: string;
name: string;
}[];
};
export type EntityTypescriptOptions = {
indentWidth?: number;
indentChar?: string;
entityCommentMultiline?: boolean;
fieldCommentMultiline?: boolean;
};
// keep a local copy here until properties have a type
const systemEntities = {
users: AppAuth.usersFields,
media: AppMedia.mediaFields,
};
export class EntityTypescript {
constructor(
protected em: EntityManager,
protected _options: EntityTypescriptOptions = {},
) {}
get options() {
return {
...this._options,
indentWidth: 2,
indentChar: " ",
entityCommentMultiline: true,
fieldCommentMultiline: false,
};
}
toTypes() {
return this.em.entities.map((e) => e.toTypes());
}
protected getTab(count = 1) {
return this.options.indentChar.repeat(this.options.indentWidth).repeat(count);
}
collectImports(
type: TEntityTSType,
imports: Record<string, string[]> = {},
): Record<string, string[]> {
for (const [, entity_type] of Object.entries(type.fields)) {
for (const imp of entity_type.import ?? []) {
const name = imp.name;
const pkg = imp.package;
if (!imports[pkg]) {
imports[pkg] = [];
}
if (!imports[pkg].includes(name)) {
imports[pkg].push(name);
}
}
}
return imports;
}
typeName(name: string) {
return autoFormatString(name);
}
fieldTypesToString(type: TEntityTSType, opts?: { ignore_fields?: string[]; indent?: number }) {
let string = "";
const coment_multiline = this.options.fieldCommentMultiline;
const indent = opts?.indent ?? 1;
for (const [field_name, field_type] of Object.entries(type.fields)) {
if (opts?.ignore_fields?.includes(field_name)) continue;
let f = "";
f += this.commentString(field_type.comment, indent, coment_multiline);
f += `${this.getTab(indent)}${field_name}${field_type.required ? "" : "?"}: `;
f += field_type.type + ";";
f += "\n";
string += f;
}
return string;
}
relationToFieldType(relation: EntityRelation, entity: Entity) {
const other = relation.other(entity);
const listable = relation.isListableFor(entity);
const name = this.typeName(other.entity.name);
let type = name;
if (other.entity.type === "system") {
type = `DB["${other.entity.name}"]`;
}
return {
fields: {
[other.reference]: {
required: false,
type: `${type}${listable ? "[]" : ""}`,
},
},
};
}
importsToString(imports: Record<string, string[]>) {
const strings: string[] = [];
for (const [pkg, names] of Object.entries(imports)) {
strings.push(`import type { ${names.join(", ")} } from "${pkg}";`);
}
return strings;
}
commentString(comment?: string, indents = 0, multiline = true) {
if (!comment) return "";
const indent = this.getTab(indents);
if (!multiline) return `${indent}// ${comment}\n`;
return `${indent}/**\n${indent} * ${comment}\n${indent} */\n`;
}
entityToTypeString(
entity: Entity,
opts?: { ignore_fields?: string[]; indent?: number; export?: boolean },
) {
const type = entity.toTypes();
const name = this.typeName(type.name);
const indent = opts?.indent ?? 1;
const min_indent = Math.max(0, indent - 1);
let s = this.commentString(type.comment, min_indent, this.options.entityCommentMultiline);
s += `${opts?.export ? "export " : ""}interface ${name} {\n`;
s += this.fieldTypesToString(type, opts);
// add listable relations
const relations = this.em.relations.relationsOf(entity);
const rel_types = relations.map((r) =>
this.relationToFieldType(r, entity),
) as TEntityTSType[];
for (const rel_type of rel_types) {
s += this.fieldTypesToString(rel_type, {
indent,
});
}
s += `${this.getTab(min_indent)}}`;
return s;
}
toString() {
const strings: string[] = [];
const tables: Record<string, string> = {};
const imports: Record<string, string[]> = {
"bknd/core": ["DB"],
kysely: ["Insertable", "Selectable", "Updateable", "Generated"],
};
// add global types
let g = "declare global {\n";
g += `${this.getTab(1)}type BkndEntity<T extends keyof DB> = Selectable<DB[T]>;\n`;
g += `${this.getTab(1)}type BkndEntityCreate<T extends keyof DB> = Insertable<DB[T]>;\n`;
g += `${this.getTab(1)}type BkndEntityUpdate<T extends keyof DB> = Updateable<DB[T]>;\n`;
g += "}";
strings.push(g);
const system_entities = this.em.entities.filter((e) => e.type === "system");
for (const entity of this.em.entities) {
// skip system entities, declare addtional props in the DB interface
if (system_entities.includes(entity)) continue;
const type = entity.toTypes();
if (!type) continue;
this.collectImports(type, imports);
tables[type.name] = this.typeName(type.name);
const s = this.entityToTypeString(entity, {
export: true,
});
strings.push(s);
}
// write tables
let tables_string = "interface Database {\n";
for (const [name, type] of Object.entries(tables)) {
tables_string += `${this.getTab(1)}${name}: ${type};\n`;
}
tables_string += "}";
strings.push(tables_string);
// merge
let merge = `declare module "bknd/core" {\n`;
for (const systemEntity of system_entities) {
const system_fields = Object.keys(systemEntities[systemEntity.name]);
const additional_fields = systemEntity.fields
.filter((f) => !system_fields.includes(f.name) && f.type !== "primary")
.map((f) => f.name);
if (additional_fields.length === 0) continue;
merge += `${this.getTab(1)}${this.entityToTypeString(systemEntity, {
ignore_fields: ["id", ...system_fields],
indent: 2,
})}\n\n`;
}
merge += `${this.getTab(1)}interface DB extends Database {}\n}`;
strings.push(merge);
const final = [this.importsToString(imports).join("\n"), strings.join("\n\n")];
return final.join("\n\n");
}
}

View File

@@ -1,4 +1,4 @@
import type { DB as DefaultDB, PrimaryFieldType } from "core";
import { $console, type DB as DefaultDB, type PrimaryFieldType } from "core";
import { type EmitsEvents, EventManager } from "core/events";
import type { DeleteQueryBuilder, InsertQueryBuilder, UpdateQueryBuilder } from "kysely";
import { type TActionContext, WhereBuilder } from "..";
@@ -72,7 +72,6 @@ export class Mutator<
// if relation field (include key and value in validatedData)
if (Array.isArray(result)) {
//console.log("--- (instructions)", result);
const [relation_key, relation_value] = result;
validatedData[relation_key] = relation_value;
}
@@ -122,7 +121,7 @@ export class Mutator<
};
} catch (e) {
// @todo: redact
console.log("[Error in query]", sql);
$console.error("[Error in query]", sql);
throw e;
}
}

View File

@@ -302,24 +302,38 @@ export class Repository<TBD extends object = DefaultDB, TB extends keyof TBD = a
}
}
private async triggerFindBefore(entity: Entity, options: RepoQuery): Promise<void> {
const event =
options.limit === 1
? Repository.Events.RepositoryFindOneBefore
: Repository.Events.RepositoryFindManyBefore;
await this.emgr.emit(new event({ entity, options }));
}
private async triggerFindAfter(
entity: Entity,
options: RepoQuery,
data: EntityData[],
): Promise<void> {
if (options.limit === 1) {
await this.emgr.emit(
new Repository.Events.RepositoryFindOneAfter({ entity, options, data: data[0]! }),
);
} else {
await this.emgr.emit(
new Repository.Events.RepositoryFindManyAfter({ entity, options, data }),
);
}
}
protected async single(
qb: RepositoryQB,
options: RepoQuery,
): Promise<RepositoryResponse<EntityData>> {
await this.emgr.emit(
new Repository.Events.RepositoryFindOneBefore({ entity: this.entity, options }),
);
await this.triggerFindBefore(this.entity, options);
const { data, ...response } = await this.performQuery(qb);
await this.emgr.emit(
new Repository.Events.RepositoryFindOneAfter({
entity: this.entity,
options,
data: data[0]!,
}),
);
await this.triggerFindAfter(this.entity, options, data);
return { ...response, data: data[0]! };
}
@@ -420,26 +434,16 @@ export class Repository<TBD extends object = DefaultDB, TB extends keyof TBD = a
limit: 1,
});
return this.single(qb, options) as any;
return (await this.single(qb, options)) as any;
}
async findMany(_options?: Partial<RepoQuery>): Promise<RepositoryResponse<TBD[TB][]>> {
const { qb, options } = this.buildQuery(_options);
await this.emgr.emit(
new Repository.Events.RepositoryFindManyBefore({ entity: this.entity, options }),
);
await this.triggerFindBefore(this.entity, options);
const res = await this.performQuery(qb);
await this.emgr.emit(
new Repository.Events.RepositoryFindManyAfter({
entity: this.entity,
options,
data: res.data,
}),
);
await this.triggerFindAfter(this.entity, options, res.data);
return res as any;
}