import {
  BaseRepository
} from "./chunk-J6KZS54B.js";

// runtime/base-classes/metadata-entity-repository.ts
import { eq, and, desc } from "drizzle-orm";
var MetadataEntityRepository = class extends BaseRepository {
  /**
   * Bulk upsert with a caller-specified conflict target.
   * Uses Drizzle's onConflictDoUpdate to merge records.
   */
  async upsertMany(inputs, tx, options) {
    if (inputs.length === 0) return [];
    const conflictTarget = options?.conflictTarget;
    if (!conflictTarget) {
      return super.upsertMany(inputs, tx);
    }
    const data = inputs.map(
      (input) => this.withTimestamps(input, "create")
    );
    const rows = await this.runner(tx).insert(this.table).values(data).onConflictDoUpdate({
      target: this.table[conflictTarget],
      set: data[0]
      // eslint-disable-line @typescript-eslint/no-explicit-any
    }).returning();
    return rows;
  }
  /**
   * Find metadata by entity ID and entity type (compound lookup).
   */
  async findByEntityIdAndType(entityId, entityType) {
    const rows = await this.baseQuery().where(
      and(
        eq(this.table["entityId"], entityId),
        eq(this.table["entityType"], entityType)
      )
    );
    return rows;
  }
  /**
   * List all metadata records for an entity.
   */
  async listByEntityId(entityId) {
    const rows = await this.baseQuery().where(eq(this.table["entityId"], entityId));
    return rows;
  }
  /**
   * List metadata history for an entity, ordered by validFrom descending.
   */
  async listHistoryByEntityId(entityId) {
    const rows = await this.baseQuery().where(eq(this.table["entityId"], entityId)).orderBy(desc(this.table["validFrom"]));
    return rows;
  }
};

export {
  MetadataEntityRepository
};
//# sourceMappingURL=chunk-7KOW6PU6.js.map