feat: adding initial uuid support

This commit is contained in:
dswbx
2025-06-06 20:46:01 +02:00
parent abbd372ddf
commit 9a18e354cd
22 changed files with 184 additions and 52 deletions

View File

@@ -6,7 +6,13 @@ import {
snakeToPascalWithSpaces,
transformObject,
} from "core/utils";
import { type Field, PrimaryField, type TActionContext, type TRenderContext } from "../fields";
import {
type Field,
PrimaryField,
primaryFieldTypes,
type TActionContext,
type TRenderContext,
} from "../fields";
import * as tbbox from "@sinclair/typebox";
const { Type } = tbbox;
@@ -18,6 +24,7 @@ export const entityConfigSchema = Type.Object(
description: Type.Optional(Type.String()),
sort_field: Type.Optional(Type.String({ default: config.data.default_primary_field })),
sort_dir: Type.Optional(StringEnum(["asc", "desc"], { default: "asc" })),
primary_format: Type.Optional(StringEnum(primaryFieldTypes)),
},
{
additionalProperties: false,
@@ -68,7 +75,14 @@ export class Entity<
if (primary_count > 1) {
throw new Error(`Entity "${name}" has more than one primary field`);
}
this.fields = primary_count === 1 ? [] : [new PrimaryField()];
this.fields =
primary_count === 1
? []
: [
new PrimaryField(undefined, {
format: this.config.primary_format,
}),
];
if (fields) {
fields.forEach((field) => this.addField(field));

View File

@@ -143,7 +143,7 @@ export class Mutator<
// if listener returned, take what's returned
const _data = result.returned ? result.params.data : data;
const validatedData = {
let validatedData = {
...entity.getDefaultObject(),
...(await this.getValidatedData(_data, "create")),
};
@@ -159,6 +159,16 @@ export class Mutator<
}
}
// primary
const primary = entity.getPrimaryField();
const primary_value = primary.getNewValue();
if (primary_value) {
validatedData = {
[primary.name]: primary_value,
...validatedData,
};
}
const query = this.conn
.insertInto(entity.name)
.values(validatedData)
@@ -175,7 +185,7 @@ export class Mutator<
async updateOne(id: PrimaryFieldType, data: Partial<Input>): Promise<MutatorResponse<Output>> {
const entity = this.entity;
if (!Number.isInteger(id)) {
if (!id) {
throw new Error("ID must be provided for update");
}
@@ -212,7 +222,7 @@ export class Mutator<
async deleteOne(id: PrimaryFieldType): Promise<MutatorResponse<Output>> {
const entity = this.entity;
if (!Number.isInteger(id)) {
if (!id) {
throw new Error("ID must be provided for deletion");
}