working primary key in code mode
This commit is contained in:
@@ -24,7 +24,7 @@ describe("[data] PrimaryField", async () => {
|
||||
});
|
||||
|
||||
test("isFillable", async () => {
|
||||
expect(field.isFillable()).toBe(false);
|
||||
expect(field.isFillable()).toBe(true);
|
||||
});
|
||||
|
||||
test("isHidden", async () => {
|
||||
@@ -36,7 +36,7 @@ describe("[data] PrimaryField", async () => {
|
||||
});
|
||||
|
||||
test("transformPersist/Retrieve", async () => {
|
||||
expect(field.transformPersist(1)).rejects.toThrow();
|
||||
expect(field.transformPersist(1)).resolves.toBe(1);
|
||||
expect(field.transformRetrieve(1)).toBe(1);
|
||||
});
|
||||
|
||||
|
||||
@@ -24,7 +24,11 @@ export class PrimaryField<Required extends true | false = false> extends Field<
|
||||
override readonly type = "primary";
|
||||
|
||||
constructor(name: string = config.data.default_primary_field, cfg?: PrimaryFieldConfig) {
|
||||
super(name, { ...cfg, fillable: false, required: false });
|
||||
super(name, {
|
||||
...cfg,
|
||||
fillable: cfg?.fillable ?? name !== config.data.default_primary_field,
|
||||
required: false,
|
||||
});
|
||||
}
|
||||
|
||||
override isRequired(): boolean {
|
||||
@@ -60,7 +64,13 @@ export class PrimaryField<Required extends true | false = false> extends Field<
|
||||
return undefined;
|
||||
}
|
||||
|
||||
override async transformPersist(value: any): Promise<number> {
|
||||
override async transformPersist(value: any): Promise<any> {
|
||||
if (this.isFillable()) {
|
||||
if (value == null) {
|
||||
return this.getNewValue();
|
||||
}
|
||||
return value;
|
||||
}
|
||||
throw new Error("PrimaryField: This function should not be called");
|
||||
}
|
||||
|
||||
|
||||
@@ -21,6 +21,8 @@ import {
|
||||
type NumberFieldConfig,
|
||||
TextField,
|
||||
type TextFieldConfig,
|
||||
PrimaryField,
|
||||
type PrimaryFieldConfig,
|
||||
} from "data/fields";
|
||||
|
||||
import { Entity, type EntityConfig, type TEntityType } from "data/entities";
|
||||
@@ -51,6 +53,8 @@ type Options<Config = any> = {
|
||||
};
|
||||
|
||||
const FieldMap = {
|
||||
primary: (o: Omit<Options, "required">) =>
|
||||
new PrimaryField(o.field_name, { ...o.config, required: true }),
|
||||
text: (o: Options) => new TextField(o.field_name, { ...o.config, required: o.is_required }),
|
||||
number: (o: Options) => new NumberField(o.field_name, { ...o.config, required: o.is_required }),
|
||||
date: (o: Options) => new DateField(o.field_name, { ...o.config, required: o.is_required }),
|
||||
@@ -73,7 +77,7 @@ export class FieldPrototype {
|
||||
public type: TFieldType,
|
||||
public config: any,
|
||||
public is_required: boolean,
|
||||
) {}
|
||||
) { }
|
||||
|
||||
required() {
|
||||
this.is_required = true;
|
||||
@@ -108,6 +112,11 @@ export class FieldPrototype {
|
||||
}
|
||||
}
|
||||
|
||||
export function primary(
|
||||
config?: Omit<PrimaryFieldConfig, "required">,
|
||||
): PrimaryField<false> & { required: () => PrimaryField<true> } {
|
||||
return new FieldPrototype("primary", config, false) as any;
|
||||
}
|
||||
export function text(
|
||||
config?: Omit<TextFieldConfig, "required">,
|
||||
): TextField<false> & { required: () => TextField<true> } {
|
||||
@@ -410,5 +419,5 @@ export type Schemas<T extends Record<string, Entity>> = {
|
||||
};
|
||||
|
||||
export type InsertSchema<T> = Simplify<OptionalUndefined<InferEntityFields<T>>>;
|
||||
export type Schema<T> = Simplify<{ id: Generated<number> } & InsertSchema<T>>;
|
||||
export type Schema<T> = Simplify<{ id: Generated<number> | string } & InsertSchema<T>>;
|
||||
export type FieldSchema<T> = Simplify<OptionalUndefined<InferFields<T>>>;
|
||||
|
||||
@@ -173,6 +173,7 @@ export {
|
||||
|
||||
// data prototype
|
||||
export {
|
||||
primary,
|
||||
text,
|
||||
number,
|
||||
date,
|
||||
|
||||
Reference in New Issue
Block a user