import { sql } from "kysely";
async function purgeOrphans(db, options) {
  const { entityType, tenantId, partitionIndex, partitionCount, startedAt } = options;
  let q = db.deleteFrom("entity_indexes").where("entity_type", "=", entityType);
  if (tenantId !== void 0) {
    q = q.where(sql`tenant_id is not distinct from ${tenantId ?? null}`);
  }
  if (options.organizationId !== void 0) {
    q = q.where(sql`organization_id is not distinct from ${options.organizationId ?? null}`);
  }
  if (partitionIndex != null && partitionCount != null) {
    q = q.where(sql`mod(abs(hashtext(entity_id::text)), ${partitionCount}) = ${partitionIndex}`);
  }
  q = q.where("updated_at", "<", startedAt);
  await q.execute();
}
export {
  purgeOrphans
};
//# sourceMappingURL=stale.js.map
