added format command and added trailing commas to reduce conflicts

This commit is contained in:
dswbx
2025-02-26 20:06:03 +01:00
parent 88b5359f1c
commit 7743f71a11
414 changed files with 3622 additions and 3610 deletions

View File

@@ -5,9 +5,9 @@ import { Field, type TActionContext, type TRenderContext, baseFieldConfigSchema
export const booleanFieldConfigSchema = Type.Composite([
Type.Object({
default_value: Type.Optional(Type.Boolean({ default: false }))
default_value: Type.Optional(Type.Boolean({ default: false })),
}),
baseFieldConfigSchema
baseFieldConfigSchema,
]);
export type BooleanFieldConfig = Static<typeof booleanFieldConfigSchema>;
@@ -40,7 +40,7 @@ export class BooleanField<Required extends true | false = false> extends Field<
override getHtmlConfig() {
return {
...super.getHtmlConfig(),
element: "boolean"
element: "boolean",
};
}
@@ -64,7 +64,7 @@ export class BooleanField<Required extends true | false = false> extends Field<
override async transformPersist(
val: unknown,
em: EntityManager<any>,
context: TActionContext
context: TActionContext,
): Promise<boolean | undefined> {
const value = await super.transformPersist(val, em, context);
if (this.nullish(value)) {

View File

@@ -9,13 +9,13 @@ export const dateFieldConfigSchema = Type.Composite(
type: StringEnum(["date", "datetime", "week"] as const, { default: "date" }),
timezone: Type.Optional(Type.String()),
min_date: Type.Optional(Type.String()),
max_date: Type.Optional(Type.String())
max_date: Type.Optional(Type.String()),
}),
baseFieldConfigSchema
baseFieldConfigSchema,
],
{
additionalProperties: false
}
additionalProperties: false,
},
);
export type DateFieldConfig = Static<typeof dateFieldConfigSchema>;
@@ -43,8 +43,8 @@ export class DateField<Required extends true | false = false> extends Field<
...super.getHtmlConfig(),
element: "date",
props: {
type: htmlType
}
type: htmlType,
},
};
}
@@ -53,7 +53,7 @@ export class DateField<Required extends true | false = false> extends Field<
if (this.config.type === "week" && value.includes("-W")) {
const [year, week] = value.split("-W").map((n) => Number.parseInt(n, 10)) as [
number,
number
number,
];
//console.log({ year, week });
// @ts-ignore causes errors on build?
@@ -129,7 +129,7 @@ export class DateField<Required extends true | false = false> extends Field<
override async transformPersist(
_value: any,
em: EntityManager<any>,
context: TActionContext
context: TActionContext,
): Promise<string | undefined> {
const value = await super.transformPersist(_value, em, context);
if (this.nullish(value)) return value;

View File

@@ -12,9 +12,9 @@ export const enumFieldConfigSchema = Type.Composite(
Type.Object(
{
type: Const("strings"),
values: Type.Array(Type.String())
values: Type.Array(Type.String()),
},
{ title: "Strings" }
{ title: "Strings" },
),
Type.Object(
{
@@ -22,23 +22,23 @@ export const enumFieldConfigSchema = Type.Composite(
values: Type.Array(
Type.Object({
label: Type.String(),
value: Type.String()
})
)
value: Type.String(),
}),
),
},
{
title: "Objects",
additionalProperties: false
}
)
])
)
additionalProperties: false,
},
),
]),
),
}),
baseFieldConfigSchema
baseFieldConfigSchema,
],
{
additionalProperties: false
}
additionalProperties: false,
},
);
export type EnumFieldConfig = Static<typeof enumFieldConfigSchema>;
@@ -123,7 +123,7 @@ export class EnumField<Required extends true | false = false, TypeOverride = str
override async transformPersist(
_value: any,
em: EntityManager<any>,
context: TActionContext
context: TActionContext,
): Promise<string | undefined> {
const value = await super.transformPersist(_value, em, context);
if (this.nullish(value)) return value;
@@ -132,7 +132,7 @@ export class EnumField<Required extends true | false = false, TypeOverride = str
throw new TransformPersistFailedException(
`Field "${this.name}" must be one of the following values: ${this.getOptions()
.map((o) => o.value)
.join(", ")}`
.join(", ")}`,
);
}
@@ -146,8 +146,8 @@ export class EnumField<Required extends true | false = false, TypeOverride = str
[];
return this.toSchemaWrapIfRequired(
StringEnum(values, {
default: this.getDefault()
})
default: this.getDefault(),
}),
);
}
}

View File

@@ -5,7 +5,7 @@ import {
Type,
TypeInvalidError,
parse,
snakeToPascalWithSpaces
snakeToPascalWithSpaces,
} from "core/utils";
import type { ColumnBuilderCallback, ColumnDataType, ColumnDefinitionBuilder } from "kysely";
import type { HTMLInputTypeAttribute, InputHTMLAttributes } from "react";
@@ -38,32 +38,32 @@ export const baseFieldConfigSchema = Type.Object(
Type.Union(
[
Type.Boolean({ title: "Boolean", default: DEFAULT_FILLABLE }),
Type.Array(StringEnum(ActionContext), { title: "Context", uniqueItems: true })
Type.Array(StringEnum(ActionContext), { title: "Context", uniqueItems: true }),
],
{
default: DEFAULT_FILLABLE
}
)
default: DEFAULT_FILLABLE,
},
),
),
hidden: Type.Optional(
Type.Union(
[
Type.Boolean({ title: "Boolean", default: DEFAULT_HIDDEN }),
// @todo: tmp workaround
Type.Array(StringEnum(TmpContext), { title: "Context", uniqueItems: true })
Type.Array(StringEnum(TmpContext), { title: "Context", uniqueItems: true }),
],
{
default: DEFAULT_HIDDEN
}
)
default: DEFAULT_HIDDEN,
},
),
),
// if field is virtual, it will not call transformPersist & transformRetrieve
virtual: Type.Optional(Type.Boolean()),
default_value: Type.Optional(Type.Any())
default_value: Type.Optional(Type.Any()),
},
{
additionalProperties: false
}
additionalProperties: false,
},
);
export type BaseFieldConfig = Static<typeof baseFieldConfigSchema>;
@@ -72,7 +72,7 @@ export type SchemaResponse = [string, ColumnDataType, ColumnBuilderCallback] | u
export abstract class Field<
Config extends BaseFieldConfig = BaseFieldConfig,
Type = any,
Required extends true | false = false
Required extends true | false = false,
> {
_required!: Required;
_type!: Type;
@@ -108,7 +108,7 @@ export abstract class Field<
protected useSchemaHelper(
type: ColumnDataType,
builder?: (col: ColumnDefinitionBuilder) => ColumnDefinitionBuilder
builder?: (col: ColumnDefinitionBuilder) => ColumnDefinitionBuilder,
): SchemaResponse {
return [
this.name,
@@ -116,7 +116,7 @@ export abstract class Field<
(col: ColumnDefinitionBuilder) => {
if (builder) return builder(col);
return col;
}
},
];
}
@@ -189,7 +189,7 @@ export abstract class Field<
getHtmlConfig(): { element: HTMLInputTypeAttribute | string; props?: InputHTMLAttributes<any> } {
return {
element: "input",
props: { type: "text" }
props: { type: "text" },
};
}
@@ -217,7 +217,7 @@ export abstract class Field<
async transformPersist(
value: unknown,
em: EntityManager<any>,
context: TActionContext
context: TActionContext,
): Promise<any> {
if (this.nullish(value)) {
if (this.isRequired() && !this.hasDefault()) {
@@ -245,7 +245,7 @@ export abstract class Field<
return {
// @todo: current workaround because of fixed string type
type: this.type as any,
config: this.config
config: this.config,
};
}
}

View File

@@ -83,7 +83,7 @@ export class JsonField<Required extends true | false = false, TypeOverride = obj
override async transformPersist(
_value: any,
em: EntityManager<any>,
context: TActionContext
context: TActionContext,
): Promise<string | undefined> {
const value = await super.transformPersist(_value, em, context);
//console.log("value", value);
@@ -91,7 +91,7 @@ export class JsonField<Required extends true | false = false, TypeOverride = obj
if (!this.isSerializable(value)) {
throw new TransformPersistFailedException(
`Field "${this.name}" must be serializable to JSON.`
`Field "${this.name}" must be serializable to JSON.`,
);
}

View File

@@ -9,20 +9,20 @@ export const jsonSchemaFieldConfigSchema = Type.Composite(
Type.Object({
schema: Type.Object({}, { default: {} }),
ui_schema: Type.Optional(Type.Object({})),
default_from_schema: Type.Optional(Type.Boolean())
default_from_schema: Type.Optional(Type.Boolean()),
}),
baseFieldConfigSchema
baseFieldConfigSchema,
],
{
additionalProperties: false
}
additionalProperties: false,
},
);
export type JsonSchemaFieldConfig = Static<typeof jsonSchemaFieldConfigSchema>;
export class JsonSchemaField<
Required extends true | false = false,
TypeOverride = object
TypeOverride = object,
> extends Field<JsonSchemaFieldConfig, TypeOverride, Required> {
override readonly type = "jsonschema";
private validator: Validator;
@@ -107,7 +107,7 @@ export class JsonSchemaField<
override async transformPersist(
_value: any,
em: EntityManager<any>,
context: TActionContext
context: TActionContext,
): Promise<string | undefined> {
const value = await super.transformPersist(_value, em, context);
if (this.nullish(value)) return value;
@@ -130,8 +130,8 @@ export class JsonSchemaField<
return this.toSchemaWrapIfRequired(
FromSchema({
default: this.getDefault(),
...schema
})
...schema,
}),
);
}
}

View File

@@ -11,13 +11,13 @@ export const numberFieldConfigSchema = Type.Composite(
maximum: Type.Optional(Type.Number()),
exclusiveMinimum: Type.Optional(Type.Number()),
exclusiveMaximum: Type.Optional(Type.Number()),
multipleOf: Type.Optional(Type.Number())
multipleOf: Type.Optional(Type.Number()),
}),
baseFieldConfigSchema
baseFieldConfigSchema,
],
{
additionalProperties: false
}
additionalProperties: false,
},
);
export type NumberFieldConfig = Static<typeof numberFieldConfigSchema>;
@@ -39,8 +39,8 @@ export class NumberField<Required extends true | false = false> extends Field<
props: {
type: "number",
pattern: "d*",
inputMode: "numeric"
} as any // @todo: react expects "inputMode", but type dictates "inputmode"
inputMode: "numeric",
} as any, // @todo: react expects "inputMode", but type dictates "inputmode"
};
}
@@ -62,7 +62,7 @@ export class NumberField<Required extends true | false = false> extends Field<
override async transformPersist(
_value: unknown,
em: EntityManager<any>,
context: TActionContext
context: TActionContext,
): Promise<number | undefined> {
const value = await super.transformPersist(_value, em, context);
@@ -72,13 +72,13 @@ export class NumberField<Required extends true | false = false> extends Field<
if (this.config.maximum && (value as number) > this.config.maximum) {
throw new TransformPersistFailedException(
`Field "${this.name}" cannot be greater than ${this.config.maximum}`
`Field "${this.name}" cannot be greater than ${this.config.maximum}`,
);
}
if (this.config.minimum && (value as number) < this.config.minimum) {
throw new TransformPersistFailedException(
`Field "${this.name}" cannot be less than ${this.config.minimum}`
`Field "${this.name}" cannot be less than ${this.config.minimum}`,
);
}
@@ -93,8 +93,8 @@ export class NumberField<Required extends true | false = false> extends Field<
maximum: this.config?.maximum,
exclusiveMinimum: this.config?.exclusiveMinimum,
exclusiveMaximum: this.config?.exclusiveMaximum,
multipleOf: this.config?.multipleOf
})
multipleOf: this.config?.multipleOf,
}),
);
}
}

View File

@@ -5,8 +5,8 @@ import { Field, baseFieldConfigSchema } from "./Field";
export const primaryFieldConfigSchema = Type.Composite([
Type.Omit(baseFieldConfigSchema, ["required"]),
Type.Object({
required: Type.Optional(Type.Literal(false))
})
required: Type.Optional(Type.Literal(false)),
}),
]);
export type PrimaryFieldConfig = Static<typeof primaryFieldConfigSchema>;

View File

@@ -19,19 +19,19 @@ export const textFieldConfigSchema = Type.Composite(
{
additionalProperties: Type.Union([
Type.String({ title: "String" }),
Type.Number({ title: "Number" })
])
}
)
)
})
)
Type.Number({ title: "Number" }),
]),
},
),
),
}),
),
}),
baseFieldConfigSchema
baseFieldConfigSchema,
],
{
additionalProperties: false
}
additionalProperties: false,
},
);
export type TextFieldConfig = Static<typeof textFieldConfigSchema>;
@@ -81,7 +81,7 @@ export class TextField<Required extends true | false = false> extends Field<
override async transformPersist(
_value: any,
em: EntityManager<any>,
context: TActionContext
context: TActionContext,
): Promise<string | undefined> {
let value = await super.transformPersist(_value, em, context);
@@ -94,19 +94,19 @@ export class TextField<Required extends true | false = false> extends Field<
if (this.config.maxLength && value?.length > this.config.maxLength) {
throw new TransformPersistFailedException(
`Field "${this.name}" must be at most ${this.config.maxLength} character(s)`
`Field "${this.name}" must be at most ${this.config.maxLength} character(s)`,
);
}
if (this.config.minLength && value?.length < this.config.minLength) {
throw new TransformPersistFailedException(
`Field "${this.name}" must be at least ${this.config.minLength} character(s)`
`Field "${this.name}" must be at least ${this.config.minLength} character(s)`,
);
}
if (this.config.pattern && value && !new RegExp(this.config.pattern).test(value)) {
throw new TransformPersistFailedException(
`Field "${this.name}" must match the pattern ${this.config.pattern}`
`Field "${this.name}" must match the pattern ${this.config.pattern}`,
);
}
@@ -119,8 +119,8 @@ export class TextField<Required extends true | false = false> extends Field<
default: this.getDefault(),
minLength: this.config?.minLength,
maxLength: this.config?.maxLength,
pattern: this.config?.pattern
})
pattern: this.config?.pattern,
}),
);
}
}

View File

@@ -25,8 +25,8 @@ export class VirtualField extends Field<VirtualFieldConfig> {
return this.toSchemaWrapIfRequired(
Type.Any({
default: this.getDefault(),
readOnly: true
})
readOnly: true,
}),
);
}
}

View File

@@ -5,7 +5,7 @@ import { JsonField, type JsonFieldConfig, jsonFieldConfigSchema } from "./JsonFi
import {
JsonSchemaField,
type JsonSchemaFieldConfig,
jsonSchemaFieldConfigSchema
jsonSchemaFieldConfigSchema,
} from "./JsonSchemaField";
import { NumberField, type NumberFieldConfig, numberFieldConfigSchema } from "./NumberField";
import { PrimaryField, type PrimaryFieldConfig, primaryFieldConfigSchema } from "./PrimaryField";
@@ -35,7 +35,7 @@ export {
type NumberFieldConfig,
TextField,
textFieldConfigSchema,
type TextFieldConfig
type TextFieldConfig,
};
export * from "./Field";
@@ -51,5 +51,5 @@ export const FieldClassMap = {
date: { schema: dateFieldConfigSchema, field: DateField },
enum: { schema: enumFieldConfigSchema, field: EnumField },
json: { schema: jsonFieldConfigSchema, field: JsonField },
jsonschema: { schema: jsonSchemaFieldConfigSchema, field: JsonSchemaField }
jsonschema: { schema: jsonSchemaFieldConfigSchema, field: JsonSchemaField },
} as const;

View File

@@ -6,7 +6,7 @@ export class EntityIndex {
public entity: Entity,
public fields: Field[],
public unique: boolean = false,
public name?: string
public name?: string,
) {
if (fields.length === 0) {
throw new Error("Indices must contain at least one field");
@@ -21,7 +21,7 @@ export class EntityIndex {
throw new Error(
`Unique indices must have first field as required: ${fields
.map((f) => f.name)
.join(", ")}`
.join(", ")}`,
);
}
}
@@ -30,7 +30,7 @@ export class EntityIndex {
this.name = [
unique ? "idx_unique" : "idx",
entity.name,
...fields.map((f) => f.name)
...fields.map((f) => f.name),
].join("_");
}
}
@@ -40,7 +40,7 @@ export class EntityIndex {
entity: this.entity.name,
fields: this.fields.map((f) => f.name),
//name: this.name,
unique: this.unique
unique: this.unique,
};
}
}