import { update, type Pool, type PoolClient } from '@evershop/postgres-query-builder'; import { createMetafieldDefinition, validateMetafield, validateMetafields, type CreateDefinitionInput, type MetaData } from '../../../../lib/metafield/index.js'; import { buildMetaDataUpdate } from '../../../../lib/metafield/metaDataSql.js'; import { pool } from '../../../../lib/postgres/connection.js'; const OWNER = 'collection'; /** Create a collection metafield definition (the core service with the owner baked in). */ export function addCollectionMetafieldDefinition( input: Omit ) { return createMetafieldDefinition({ ...input, ownerType: OWNER }); } /** * Set the full metafield value set for a collection (the form-save path). * Validates against the collection definitions and writes `meta_data` wholesale. */ export async function setCollectionMetafields( collectionId: number, values: MetaData, connection: Pool | PoolClient = pool ): Promise { const metaData = await validateMetafields(OWNER, values); await update('collection') .given({ meta_data: metaData }) .where('collection_id', '=', collectionId) .execute(connection); } /** * Set a single collection metafield without touching the others (out-of-band * path). Targeted `jsonb_set` merge that creates the * namespace object if missing; a blank value removes the key instead of * storing a JSON null. */ export async function setCollectionMetafield( collectionId: number, namespace: string, key: string, value: unknown, connection: Pool | PoolClient = pool ): Promise { const validated = await validateMetafield(OWNER, namespace, key, value); const { sql, params } = buildMetaDataUpdate({ table: 'collection', whereClause: 'collection_id = $1', idParams: [collectionId], namespace, key, validated }); await connection.query(sql, params); }