Files
profilarr/src/lib/server/pcd/queries/customFormats/create.ts
Sam Chau 8deef25c9e feat: add create and delete custom format functionality
- Implemented `create.ts` for creating custom formats with associated tags.
- Added `delete.ts` for deleting custom formats with cascading deletes for related entities.
- Updated `index.ts` to export new create and delete functions.
- Enhanced the server-side logic in `+page.server.ts` for handling new custom format creation.
- Created a new Svelte component `GeneralForm.svelte` for managing custom format details.
- Updated the UI in `+page.svelte` for creating new custom formats and handling form submissions.
- Integrated dirty state management for form inputs in `TestForm.svelte` and `GeneralForm.svelte`.
- Added delete functionality in the UI for custom formats with confirmation modals.
2026-01-03 04:07:08 +10:30

79 lines
1.8 KiB
TypeScript

/**
* Create a custom format operation
*/
import type { PCDCache } from '../../cache.ts';
import { writeOperation, type OperationLayer } from '../../writer.ts';
export interface CreateCustomFormatInput {
name: string;
description: string | null;
includeInRename: boolean;
tags: string[];
}
export interface CreateCustomFormatOptions {
databaseId: number;
cache: PCDCache;
layer: OperationLayer;
input: CreateCustomFormatInput;
}
/**
* Create a custom format by writing an operation to the specified layer
*/
export async function create(options: CreateCustomFormatOptions) {
const { databaseId, cache, layer, input } = options;
const db = cache.kb;
const queries = [];
// 1. Insert the custom format
const insertFormat = db
.insertInto('custom_formats')
.values({
name: input.name,
description: input.description,
include_in_rename: input.includeInRename ? 1 : 0
})
.compile();
queries.push(insertFormat);
// 2. Insert tags (create if not exist, then link)
for (const tagName of input.tags) {
// Insert tag if not exists
const insertTag = db
.insertInto('tags')
.values({ name: tagName })
.onConflict((oc) => oc.column('name').doNothing())
.compile();
queries.push(insertTag);
// Link tag to custom format
const linkTag = {
sql: `INSERT INTO custom_format_tags (custom_format_id, tag_id) VALUES ((SELECT id FROM custom_formats WHERE name = '${input.name.replace(/'/g, "''")}'), tag('${tagName.replace(/'/g, "''")}'))`,
parameters: [],
query: {} as never
};
queries.push(linkTag);
}
// Write the operation
const result = await writeOperation({
databaseId,
layer,
description: `create-custom-format-${input.name}`,
queries,
metadata: {
operation: 'create',
entity: 'custom_format',
name: input.name
}
});
return result;
}